diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 8700ab98..2cf02e59 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -580,21 +580,42 @@ extension ComposeViewController: DraftsTableViewControllerDelegate { updatePlaceholder() updateCharactersRemaining() + let result = PHAsset.fetchAssets(withLocalIdentifiers: draft.attachments.map { $0.assetIdentifier }, options: nil) - self.selectedAssets = [] - var i = 0 - while i < result.count { - selectedAssets.append(result[i]) - i += 1 + var assets = [String: (asset: PHAsset, description: String)]() + var addedAssets = 0 + while addedAssets < result.count { + let asset = result[addedAssets] + let attachment = draft.attachments.first(where: { $0.assetIdentifier == asset.localIdentifier })! + assets[asset.localIdentifier] = (asset, attachment.description) + addedAssets += 1 } + + self.selectedAssets = assets.values.map { $0.asset } updateAttachmentViews() - var j = 0 - for subview in attachmentsStackView.arrangedSubviews { - guard let mediaView = subview as? ComposeMediaView else { continue } - mediaView.descriptionTextView.text = draft.attachments[j].description + for case let mediaView as ComposeMediaView in attachmentsStackView.arrangedSubviews { + let attachment = draft.attachments.first(where: { $0.assetIdentifier == mediaView.assetIdentifier })! + mediaView.descriptionTextView.text = attachment.description + + // call the delegate method manually, since setting the text property doesn't call it mediaView.textViewDidChange(mediaView.descriptionTextView) - j += 1 + } + } + + func draftSelectionCompleted() { + // check that all the assets from the draft have been added + if let currentDraft = currentDraft, selectedAssets.count < currentDraft.attachments.count { + // some of the assets in the draft weren't loaded, so notify the user + + let difference = currentDraft.attachments.count - selectedAssets.count + // todo: localize me + let suffix = difference == 1 ? "" : "s" + let verb = difference == 1 ? "was" : "were" + let alertController = UIAlertController(title: "Missing Attachments", message: "\(difference) attachment\(suffix) \(verb) removed from the Photos Library and could not be loaded.", preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) + + present(alertController, animated: true) } } } diff --git a/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift b/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift index cbde5ee5..90a34370 100644 --- a/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift +++ b/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift @@ -11,6 +11,7 @@ import UIKit protocol DraftsTableViewControllerDelegate { func draftSelectionCanceled() func draftSelected(_ draft: DraftsManager.Draft) + func draftSelectionCompleted() } class DraftsTableViewController: UITableViewController { @@ -61,7 +62,9 @@ class DraftsTableViewController: UITableViewController { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { delegate?.draftSelected(draft(for: indexPath)) - dismiss(animated: true) + dismiss(animated: true) { + self.delegate?.draftSelectionCompleted() + } } override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle { diff --git a/Tusker/Views/Compose Media/ComposeMediaView.swift b/Tusker/Views/Compose Media/ComposeMediaView.swift index d3f05329..bd76335f 100644 --- a/Tusker/Views/Compose Media/ComposeMediaView.swift +++ b/Tusker/Views/Compose Media/ComposeMediaView.swift @@ -21,6 +21,8 @@ class ComposeMediaView: UIView { @IBOutlet weak var descriptionTextView: UITextView! @IBOutlet weak var placeholderLabel: UILabel! + var assetIdentifier: String? + static func create() -> ComposeMediaView { return UINib(nibName: "ComposeMediaView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! ComposeMediaView } @@ -35,7 +37,8 @@ class ComposeMediaView: UIView { } func update(asset: PHAsset) { -// let size = imageView.frame.size // is this initialized yet? + self.assetIdentifier = asset.localIdentifier + let size = CGSize(width: 80, height: 80) PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in self.imageView.image = image