From 5d3686a038e6db0c6cf14de536bda6226e09a604 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 15 Sep 2018 10:56:27 -0400 Subject: [PATCH] Start work on swipe actions --- Pachyderm/Model/Status.swift | 4 ++ .../TimelineTableViewController.swift | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/Pachyderm/Model/Status.swift b/Pachyderm/Model/Status.swift index 28b5bc3a..ca65c03f 100644 --- a/Pachyderm/Model/Status.swift +++ b/Pachyderm/Model/Status.swift @@ -111,8 +111,10 @@ public class Status: Decodable, ClientModel { client.run(request) { response in if case .success = response { self.favourited = true + self.reblog?.favourited = true } else { self.favourited = oldValue + self.reblog?.favourited = oldValue } completion(response) } @@ -124,8 +126,10 @@ public class Status: Decodable, ClientModel { client.run(request) { response in if case .success = response { self.favourited = false + self.reblog?.favourited = false } else { self.favourited = oldValue + self.reblog?.favourited = oldValue } completion(response) } diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 2a6a7aa5..65963578 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -35,6 +35,13 @@ class TimelineTableViewController: UITableViewController { return navigationController } + lazy var favoriteActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 137/131, height: 30)).image { _ in + UIImage(named: "Favorite")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 137/131, height: 30)) + } + lazy var reblogActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 927/558, height: 30)).image { _ in + UIImage(named: "Reblog")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 927/558, height: 30)) + } + var timeline: Timeline! var statuses: [Status] = [] { @@ -120,6 +127,47 @@ class TimelineTableViewController: UITableViewController { } } + override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + let status = statuses[indexPath.row] + + let favorite = UIContextualAction(style: .normal, title: "Favorite") { (action, view, completion) in + status.favourite(completion: { response in + DispatchQueue.main.async { + if case .success = response { + completion(true) + guard let cell = tableView.cellForRow(at: indexPath) as? StatusTableViewCell else { return } + cell.updateUI(for: cell.status) + } else { + completion(false) + } + } + }) + } + favorite.image = favoriteActionImage + favorite.backgroundColor = UIColor(displayP3Red: 1, green: 0.8, blue: 0, alpha: 1) + let reblog = UIContextualAction(style: .normal, title: "Reblog") { (action, view, completion) in + status.reblog(completion: { response in + DispatchQueue.main.async { + if case .success = response { + completion(true) + guard let cell = tableView.cellForRow(at: indexPath) as? StatusTableViewCell else { return } + cell.updateUI(for: cell.status) + } else { + completion(false) + } + } + }) + } + reblog.image = reblogActionImage + reblog.backgroundColor = view.tintColor + + let actions = [ + favorite, + reblog + ] + return UISwipeActionsConfiguration(actions: actions) + } + @IBAction func refreshStatuses(_ sender: Any) { guard let newer = newer else { return }