Merge branch 'swipe-actions'

This commit is contained in:
Shadowfacts 2018-09-15 13:18:23 -04:00
commit fb47708508
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
12 changed files with 217 additions and 25 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

@ -17,22 +17,25 @@ public enum Timeline {
} }
extension Timeline { extension Timeline {
func request(range: RequestRange) -> Request<[Status]> { var endpoint: String {
var request: Request<[Status]>
switch self { switch self {
case .home: case .home:
request = Request(method: .get, path: "/api/v1/timelines/home") return "/api/v1/timelines/home"
case let .public(local): case .public:
request = Request(method: .get, path: "/api/v1/timelines/public") return "/api/v1/timelines/public"
if local {
request.queryParameters.append("local" => true)
}
case let .tag(hashtag): case let .tag(hashtag):
request = Request(method: .get, path: "/api/v1/timeliens/tag/\(hashtag)") return "/api/v1/timelines/tag/\(hashtag)"
case let .list(id): case let .list(id):
request = Request(method: .get, path: "/api/v1/timelines/list/\(id)") return "/api/v1/timelines/list/\(id)"
case .direct: case .direct:
request = Request(method: .get, path: "/api/v1/timelines/direct") return "/api/v1/timelines/direct"
}
}
func request(range: RequestRange) -> Request<[Status]> {
var request = Request<[Status]>(method: .get, path: endpoint)
if case .public(true) = self {
request.queryParameters.append("local" => true)
} }
request.range = range request.range = range
return request return request

View File

@ -74,7 +74,8 @@ extension Array where Element == Parameter {
var queryItems: [URLQueryItem] { var queryItems: [URLQueryItem] {
return compactMap { return compactMap {
URLQueryItem(name: $0.name, value: $0.value) guard let value = $0.value else { return nil }
return URLQueryItem(name: $0.name, value: value)
} }
} }
} }

View File

@ -40,17 +40,26 @@ extension Request {
} }
set { set {
let rangeParams = newValue.queryParameters let rangeParams = newValue.queryParameters
let max = rangeParams.first { $0.name == "max_id" } if let max = rangeParams.first(where: { $0.name == "max_id" }) {
let since = rangeParams.first { $0.name == "since_id" } if let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) {
let count = rangeParams.first { $0.name == "count" }
if let max = max, let i = queryParameters.firstIndex(where: { $0.name == "max_id" }) {
queryParameters[i] = max queryParameters[i] = max
} else {
queryParameters.append(max)
} }
if let since = since, let i = queryParameters.firstIndex(where: { $0.name == "since_id" }) { }
if let since = rangeParams.first(where: { $0.name == "since_id" }) {
if let i = queryParameters.firstIndex(where: { $0.name == "since_id" }) {
queryParameters[i] = since queryParameters[i] = since
} else {
queryParameters.append(since)
} }
if let count = count, let i = queryParameters.firstIndex(where: { $0.name == "count" }) { }
if let count = rangeParams.first(where: { $0.name == "count" }) {
if let i = queryParameters.firstIndex(where: { $0.name == "count" }) {
queryParameters[i] = count queryParameters[i] = count
} else {
queryParameters.append(count)
}
} }
} }
} }

View File

@ -30,9 +30,9 @@ extension Pagination {
var range: RequestRange { var range: RequestRange {
switch kind { switch kind {
case .next: case .next:
return .after(id: id, count: limit)
case .prev:
return .before(id: id, count: limit) return .before(id: id, count: limit)
case .prev:
return .after(id: id, count: limit)
} }
} }

View File

@ -67,6 +67,7 @@
D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AAC2128D88B005A6F37 /* LocalData.swift */; }; D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AAC2128D88B005A6F37 /* LocalData.swift */; };
D64D0AAF2128D954005A6F37 /* Onboarding.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */; }; D64D0AAF2128D954005A6F37 /* Onboarding.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */; };
D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */; }; D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */; };
D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */; };
D65A37F321472F300087646E /* SwiftSoup.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; }; D65A37F321472F300087646E /* SwiftSoup.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; };
D663625D2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */; }; D663625D2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */; };
D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */; }; D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */; };
@ -221,6 +222,7 @@
D64D0AAC2128D88B005A6F37 /* LocalData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalData.swift; sourceTree = "<group>"; }; D64D0AAC2128D88B005A6F37 /* LocalData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalData.swift; sourceTree = "<group>"; };
D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Onboarding.storyboard; sourceTree = "<group>"; }; D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Onboarding.storyboard; sourceTree = "<group>"; };
D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = "<group>"; }; D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = "<group>"; };
D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = "<group>"; };
D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConversationMainStatusTableViewCell.xib; sourceTree = "<group>"; }; D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConversationMainStatusTableViewCell.xib; sourceTree = "<group>"; };
D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = "<group>"; }; D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = "<group>"; };
D663626121360B1900C9CBA2 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; }; D663626121360B1900C9CBA2 /* Preferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Preferences.swift; sourceTree = "<group>"; };
@ -571,6 +573,7 @@
D6333B762138D94E00CE884A /* ComposeMediaView.swift */, D6333B762138D94E00CE884A /* ComposeMediaView.swift */,
D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */, D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */,
04ED00B021481ED800567C53 /* SteppedProgressView.swift */, 04ED00B021481ED800567C53 /* SteppedProgressView.swift */,
D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */,
D641C78A213DD926004B4513 /* Status */, D641C78A213DD926004B4513 /* Status */,
D641C78B213DD92F004B4513 /* Profile Header */, D641C78B213DD92F004B4513 /* Profile Header */,
D641C78C213DD937004B4513 /* Notifications */, D641C78C213DD937004B4513 /* Notifications */,
@ -926,6 +929,7 @@
D6333B772138D94E00CE884A /* ComposeMediaView.swift in Sources */, D6333B772138D94E00CE884A /* ComposeMediaView.swift in Sources */,
04ED00B121481ED800567C53 /* SteppedProgressView.swift in Sources */, 04ED00B121481ED800567C53 /* SteppedProgressView.swift in Sources */,
D663626421360D2300C9CBA2 /* AvatarStyle.swift in Sources */, D663626421360D2300C9CBA2 /* AvatarStyle.swift in Sources */,
D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */,
D6BED174212667E900F02DA0 /* StatusTableViewCell.swift in Sources */, D6BED174212667E900F02DA0 /* StatusTableViewCell.swift in Sources */,
D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */, D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */,
D6C94D892139E6EC00CB5196 /* AttachmentView.swift in Sources */, D6C94D892139E6EC00CB5196 /* AttachmentView.swift in Sources */,

View File

@ -118,6 +118,18 @@ class ConversationViewController: UIViewController, UITableViewDataSource, UITab
return status == mainStatus ? nil : indexPath return status == mainStatus ? nil : indexPath
} }
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
}
} }
extension ConversationViewController: StatusTableViewCellDelegate {} extension ConversationViewController: StatusTableViewCellDelegate {}

View File

@ -118,6 +118,18 @@ class NotificationsTableViewController: UITableViewController {
} }
} }
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
}
@IBAction func refreshNotifications(_ sender: Any) { @IBAction func refreshNotifications(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }

View File

@ -132,6 +132,18 @@ class ProfileTableViewController: UITableViewController, PreferencesAdaptive {
} }
} }
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
}
@IBAction func refreshStatuses(_ sender: Any) { @IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }

View File

@ -35,6 +35,19 @@ 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))
}
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!
var statuses: [Status] = [] { var statuses: [Status] = [] {
@ -120,6 +133,18 @@ class TimelineTableViewController: UITableViewController {
} }
} }
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
}
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
}
@IBAction func refreshStatuses(_ sender: Any) { @IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } guard let newer = newer else { return }

View File

@ -215,8 +215,8 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
let realStatus: Status = status.reblog ?? status let realStatus: Status = status.reblog ?? status
(favorited ? realStatus.favourite : realStatus.unfavourite)() { response in (favorited ? realStatus.favourite : realStatus.unfavourite)() { response in
self.favorited = realStatus.favourited ?? false
DispatchQueue.main.async { DispatchQueue.main.async {
self.favorited = realStatus.favourited ?? false
if case .success = response { if case .success = response {
UIImpactFeedbackGenerator(style: .light).impactOccurred() UIImpactFeedbackGenerator(style: .light).impactOccurred()
} else { } else {
@ -235,8 +235,8 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
let realStatus: Status = status.reblog ?? status let realStatus: Status = status.reblog ?? status
(reblogged ? realStatus.reblog : realStatus.unreblog)() { response in (reblogged ? realStatus.reblog : realStatus.unreblog)() { response in
self.reblogged = realStatus.reblogged ?? false
DispatchQueue.main.async { DispatchQueue.main.async {
self.reblogged = realStatus.reblogged ?? false
if case .success = response { if case .success = response {
UIImpactFeedbackGenerator(style: .light).impactOccurred() UIImpactFeedbackGenerator(style: .light).impactOccurred()
} else { } else {
@ -254,6 +254,99 @@ class StatusTableViewCell: UITableViewCell, PreferencesAdaptive {
} }
extension StatusTableViewCell: TableViewSwipeActionProvider {
static 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))
}
static 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))
}
static 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))
}
static 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))
}
func leadingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {
let favoriteTitle: String
let favoriteAction: (@escaping Client.Callback<Status>) -> Void
let favoriteColor: UIColor
if status.favourited ?? false {
favoriteTitle = "Unfavorite"
favoriteAction = status.unfavourite
favoriteColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
} else {
favoriteTitle = "Favorite"
favoriteAction = status.favourite
favoriteColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1)
}
let favorite = UIContextualAction(style: .normal, title: favoriteTitle) { (action, view, completion) in
favoriteAction { response in
DispatchQueue.main.async {
if case .success = response {
completion(true)
self.updateUI(for: self.status)
} else {
completion(false)
}
}
}
}
favorite.image = StatusTableViewCell.favoriteActionImage
favorite.backgroundColor = favoriteColor
let reblogTitle: String
let reblogAction: (@escaping Client.Callback<Status>) -> Void
let reblogColor: UIColor
if status.reblogged ?? false {
reblogTitle = "Unreblog"
reblogAction = status.unreblog
reblogColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
} else {
reblogTitle = "Reblog"
reblogAction = status.reblog
reblogColor = tintColor
}
let reblog = UIContextualAction(style: .normal, title: reblogTitle) { (action, view, completion) in
reblogAction { response in
DispatchQueue.main.async {
if case .success = response {
completion(true)
self.updateUI(for: self.status)
} else {
completion(false)
}
}
}
}
reblog.image = StatusTableViewCell.reblogActionImage
reblog.backgroundColor = reblogColor
return UISwipeActionsConfiguration(actions: [favorite, reblog])
}
func trailingSwipeActionsConfiguration() -> UISwipeActionsConfiguration? {
let reply = UIContextualAction(style: .normal, title: "Reply") { (action, view, completion) in
completion(true)
self.delegate?.reply(to: self.status)
}
reply.image = StatusTableViewCell.replyActionImage
reply.backgroundColor = tintColor
let more = UIContextualAction(style: .normal, title: "More") { (action, view, completion) in
completion(true)
self.delegate?.showMoreOptions(status: self.status)
}
more.image = StatusTableViewCell.moreActionImage
more.backgroundColor = .gray
return UISwipeActionsConfiguration(actions: [reply, more])
}
}
extension StatusTableViewCell: HTMLContentLabelDelegate { extension StatusTableViewCell: HTMLContentLabelDelegate {
func selected(mention: Mention) { func selected(mention: Mention) {

View File

@ -0,0 +1,17 @@
//
// TableViewSwipeActionProvider.swift
// Tusker
//
// Created by Shadowfacts on 9/15/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
protocol TableViewSwipeActionProvider {
func leadingSwipeActionsConfiguration() -> UISwipeActionsConfiguration?
func trailingSwipeActionsConfiguration() -> UISwipeActionsConfiguration?
}