From 4c9be9116246633464138127f16c2ad8cc18e9d1 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 15 Sep 2018 11:37:20 -0400 Subject: [PATCH] More swipe actions --- .../TimelineTableViewController.swift | 112 ++++++++++++++---- 1 file changed, 90 insertions(+), 22 deletions(-) diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 65963578..59a5dfdb 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -41,6 +41,12 @@ class TimelineTableViewController: UITableViewController { 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)) } + lazy var replyActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 205/151, height: 30)).image { _ in + UIImage(named: "Reply")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 205/151, height: 30)) + } + lazy var moreActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 2/1, height: 30)).image { _ in + UIImage(named: "More")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 2/1, height: 30)) + } var timeline: Timeline! @@ -130,36 +136,74 @@ 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) + let favorite: UIContextualAction + if status.favourited ?? false { + favorite = UIContextualAction(style: .normal, title: "Unfavorite", handler: { (action, view, completion) in + status.unfavourite(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.backgroundColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1) + } else { + 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.backgroundColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1) } 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) + + let reblog: UIContextualAction + if status.reblogged ?? false { + reblog = UIContextualAction(style: .normal, title: "Unreblog", handler: { (action, view, completion) in + status.unreblog(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.backgroundColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1) + } else { + 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.backgroundColor = view.tintColor } reblog.image = reblogActionImage - reblog.backgroundColor = view.tintColor + let actions = [ favorite, @@ -168,6 +212,30 @@ class TimelineTableViewController: UITableViewController { return UISwipeActionsConfiguration(actions: actions) } + override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { + let status = statuses[indexPath.row] + + let reply = UIContextualAction(style: .normal, title: "Reply") { (action, view, completion) in + completion(true) + self.reply(to: status) + } + reply.image = replyActionImage + reply.backgroundColor = view.tintColor + + let more = UIContextualAction(style: .normal, title: "More") { (action, view, completion) in + completion(true) + self.showMoreOptions(status: status) + } + more.image = moreActionImage + more.backgroundColor = .gray + + let actions = [ + reply, + more + ] + return UISwipeActionsConfiguration(actions: actions) + } + @IBAction func refreshStatuses(_ sender: Any) { guard let newer = newer else { return }