Start work on swipe actions

This commit is contained in:
Shadowfacts 2018-09-15 10:56:27 -04:00
parent d46b61595e
commit 22dfec0483
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 52 additions and 0 deletions

View File

@ -111,8 +111,10 @@ public class Status: Decodable, ClientModel {
client.run(request) { response in client.run(request) { response in
if case .success = response { if case .success = response {
self.favourited = true self.favourited = true
self.reblog?.favourited = true
} else { } else {
self.favourited = oldValue self.favourited = oldValue
self.reblog?.favourited = oldValue
} }
completion(response) completion(response)
} }
@ -124,8 +126,10 @@ public class Status: Decodable, ClientModel {
client.run(request) { response in client.run(request) { response in
if case .success = response { if case .success = response {
self.favourited = false self.favourited = false
self.reblog?.favourited = false
} else { } else {
self.favourited = oldValue self.favourited = oldValue
self.reblog?.favourited = oldValue
} }
completion(response) completion(response)
} }

View File

@ -35,6 +35,13 @@ class TimelineTableViewController: UITableViewController {
return navigationController 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 timeline: Timeline!
var statuses: [Status] = [] { 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) { @IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }