Cleanup draft attachment loading and handle missing attachments

#4
This commit is contained in:
Shadowfacts 2019-09-06 18:50:18 -04:00
parent 49380692f3
commit 818c0afec6
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 39 additions and 12 deletions

View File

@ -580,21 +580,42 @@ extension ComposeViewController: DraftsTableViewControllerDelegate {
updatePlaceholder() updatePlaceholder()
updateCharactersRemaining() updateCharactersRemaining()
let result = PHAsset.fetchAssets(withLocalIdentifiers: draft.attachments.map { $0.assetIdentifier }, options: nil) let result = PHAsset.fetchAssets(withLocalIdentifiers: draft.attachments.map { $0.assetIdentifier }, options: nil)
self.selectedAssets = [] var assets = [String: (asset: PHAsset, description: String)]()
var i = 0 var addedAssets = 0
while i < result.count { while addedAssets < result.count {
selectedAssets.append(result[i]) let asset = result[addedAssets]
i += 1 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() updateAttachmentViews()
var j = 0 for case let mediaView as ComposeMediaView in attachmentsStackView.arrangedSubviews {
for subview in attachmentsStackView.arrangedSubviews { let attachment = draft.attachments.first(where: { $0.assetIdentifier == mediaView.assetIdentifier })!
guard let mediaView = subview as? ComposeMediaView else { continue } mediaView.descriptionTextView.text = attachment.description
mediaView.descriptionTextView.text = draft.attachments[j].description
// call the delegate method manually, since setting the text property doesn't call it
mediaView.textViewDidChange(mediaView.descriptionTextView) 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)
} }
} }
} }

View File

@ -11,6 +11,7 @@ import UIKit
protocol DraftsTableViewControllerDelegate { protocol DraftsTableViewControllerDelegate {
func draftSelectionCanceled() func draftSelectionCanceled()
func draftSelected(_ draft: DraftsManager.Draft) func draftSelected(_ draft: DraftsManager.Draft)
func draftSelectionCompleted()
} }
class DraftsTableViewController: UITableViewController { class DraftsTableViewController: UITableViewController {
@ -61,7 +62,9 @@ class DraftsTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.draftSelected(draft(for: 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 { override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {

View File

@ -21,6 +21,8 @@ class ComposeMediaView: UIView {
@IBOutlet weak var descriptionTextView: UITextView! @IBOutlet weak var descriptionTextView: UITextView!
@IBOutlet weak var placeholderLabel: UILabel! @IBOutlet weak var placeholderLabel: UILabel!
var assetIdentifier: String?
static func create() -> ComposeMediaView { static func create() -> ComposeMediaView {
return UINib(nibName: "ComposeMediaView", bundle: nil).instantiate(withOwner: nil, options: nil).first as! 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) { func update(asset: PHAsset) {
// let size = imageView.frame.size // is this initialized yet? self.assetIdentifier = asset.localIdentifier
let size = CGSize(width: 80, height: 80) let size = CGSize(width: 80, height: 80)
PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in
self.imageView.image = image self.imageView.image = image