More swipe actions

This commit is contained in:
Shadowfacts 2018-09-15 11:37:20 -04:00
parent 22dfec0483
commit 4c9be91162
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
1 changed files with 90 additions and 22 deletions

View File

@ -41,6 +41,12 @@ class TimelineTableViewController: UITableViewController {
lazy var reblogActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 927/558, height: 30)).image { _ in 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)) 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! var timeline: Timeline!
@ -130,7 +136,24 @@ class TimelineTableViewController: UITableViewController {
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let status = statuses[indexPath.row] let status = statuses[indexPath.row]
let favorite = UIContextualAction(style: .normal, title: "Favorite") { (action, view, completion) in 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 status.favourite(completion: { response in
DispatchQueue.main.async { DispatchQueue.main.async {
if case .success = response { if case .success = response {
@ -143,9 +166,28 @@ class TimelineTableViewController: UITableViewController {
} }
}) })
} }
favorite.backgroundColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1)
}
favorite.image = favoriteActionImage 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 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 status.reblog(completion: { response in
DispatchQueue.main.async { DispatchQueue.main.async {
if case .success = response { if case .success = response {
@ -158,8 +200,10 @@ class TimelineTableViewController: UITableViewController {
} }
}) })
} }
reblog.image = reblogActionImage
reblog.backgroundColor = view.tintColor reblog.backgroundColor = view.tintColor
}
reblog.image = reblogActionImage
let actions = [ let actions = [
favorite, favorite,
@ -168,6 +212,30 @@ class TimelineTableViewController: UITableViewController {
return UISwipeActionsConfiguration(actions: actions) 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) { @IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }