diff --git a/Tusker/DraftsManager.swift b/Tusker/DraftsManager.swift index 4c8f7050..c3258854 100644 --- a/Tusker/DraftsManager.swift +++ b/Tusker/DraftsManager.swift @@ -37,8 +37,8 @@ class DraftsManager: Codable { return drafts.sorted(by: { $0.lastModified > $1.lastModified }) } - func create(text: String) { - drafts.append(Draft(text: text, lastModified: Date())) + func create(text: String, attachments: [DraftAttachment]) { + drafts.append(Draft(text: text, lastModified: Date(), attachments: attachments)) } func remove(_ draft: Draft) { @@ -53,20 +53,29 @@ extension DraftsManager { let id: UUID private(set) var text: String private(set) var lastModified: Date + private(set) var attachments: [DraftAttachment] - init(text: String, lastModified: Date) { + init(text: String, lastModified: Date, attachments: [DraftAttachment]) { self.id = UUID() self.text = text self.lastModified = lastModified + self.attachments = attachments } - func update(text: String) { + func update(text: String, attachments: [DraftAttachment]) { self.text = text self.lastModified = Date() + self.attachments = attachments } static func ==(lhs: Draft, rhs: Draft) -> Bool { return lhs.id == rhs.id } } + + struct DraftAttachment: Codable { + /// The local identifier of the PHAsset for this attachment + let assetIdentifier: String + let description: String + } } diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index aae8a250..783adace 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -272,12 +272,19 @@ class ComposeViewController: UIViewController { } func saveDraft() { - // TODO: save attachmenst to drafts // TODO: save CW to draft - if let currentDraft = currentDraft { - currentDraft.update(text: statusTextView.text) + var attachments = [DraftsManager.DraftAttachment]() + for asset in selectedAssets { + let index = attachments.count + let mediaView = attachmentsStackView.arrangedSubviews[index] as! ComposeMediaView + let description = mediaView.descriptionTextView.text! + + attachments.append(DraftsManager.DraftAttachment(assetIdentifier: asset.localIdentifier, description: description)) + } + if let currentDraft = self.currentDraft { + currentDraft.update(text: self.statusTextView.text, attachments: attachments) } else { - DraftsManager.shared.create(text: statusTextView.text) + DraftsManager.shared.create(text: self.statusTextView.text, attachments: attachments) } } @@ -520,8 +527,26 @@ extension ComposeViewController: DraftsTableViewControllerDelegate { func draftSelected(_ draft: DraftsManager.Draft) { self.currentDraft = draft + statusTextView.text = draft.text 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 + } + updateAttachmentViews() + + var j = 0 + for subview in attachmentsStackView.arrangedSubviews { + guard let mediaView = subview as? ComposeMediaView else { continue } + mediaView.descriptionTextView.text = draft.attachments[j].description + mediaView.textViewDidChange(mediaView.descriptionTextView) + j += 1 + } } } diff --git a/Tusker/Screens/Compose/Drafts/DraftTableViewCell.swift b/Tusker/Screens/Compose/Drafts/DraftTableViewCell.swift index 130fa386..c8b4f85e 100644 --- a/Tusker/Screens/Compose/Drafts/DraftTableViewCell.swift +++ b/Tusker/Screens/Compose/Drafts/DraftTableViewCell.swift @@ -7,15 +7,43 @@ // import UIKit +import Photos class DraftTableViewCell: UITableViewCell { @IBOutlet weak var contentLabel: UILabel! @IBOutlet weak var lastModifiedLabel: UILabel! + @IBOutlet weak var attachmentsStackViewContainer: UIView! + @IBOutlet weak var attachmentsStackView: UIStackView! func updateUI(for draft: DraftsManager.Draft) { contentLabel.text = draft.text lastModifiedLabel.text = draft.lastModified.timeAgoString() + + attachmentsStackViewContainer.isHidden = draft.attachments.count == 0 + + let result = PHAsset.fetchAssets(withLocalIdentifiers: draft.attachments.map { $0.assetIdentifier }, options: nil) + var i = 0 + while i < result.count { + let asset = result[i] + let size = CGSize(width: 50, height: 50) + + let imageView = UIImageView(frame: CGRect(origin: .zero, size: size)) + imageView.layer.masksToBounds = true + imageView.layer.cornerRadius = 5 + attachmentsStackView.addArrangedSubview(imageView) + + PHImageManager.default().requestImage(for: asset, targetSize: size, contentMode: .aspectFill, options: nil) { (image, _) in + imageView.image = image + } + i += 1 + } + } + + override func prepareForReuse() { + super.prepareForReuse() + + attachmentsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } } } diff --git a/Tusker/Screens/Compose/Drafts/DraftTableViewCell.xib b/Tusker/Screens/Compose/Drafts/DraftTableViewCell.xib index 1f7cfe1f..906ed45d 100644 --- a/Tusker/Screens/Compose/Drafts/DraftTableViewCell.xib +++ b/Tusker/Screens/Compose/Drafts/DraftTableViewCell.xib @@ -10,40 +10,76 @@ - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - + + + + + + - + diff --git a/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift b/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift index fde1dee3..cbde5ee5 100644 --- a/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift +++ b/Tusker/Screens/Compose/Drafts/DraftsTableViewController.swift @@ -31,6 +31,9 @@ class DraftsTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() + tableView.rowHeight = UITableView.automaticDimension + tableView.estimatedRowHeight = 140 + tableView.register(UINib(nibName: "DraftTableViewCell", bundle: nil), forCellReuseIdentifier: "draftCell") }