Add favorite status XCB action

This commit is contained in:
Shadowfacts 2018-09-23 22:16:25 -04:00
parent 024f55a7a8
commit c6008bc7c0
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 87 additions and 2 deletions

View File

@ -9,7 +9,10 @@
import Foundation
enum XCBActionType: String {
// Statuses
case postStatus
case favoriteStatus
// Accounts
case getCurrentUser
var path: String {

View File

@ -7,10 +7,22 @@
//
import UIKit
import Pachyderm
struct XCBActions {
// MARK: - Posts
// MARK: - Utils
static func presentModally(_ vc: UIViewController, animated: Bool, completion: (() -> Void)? = nil) {
UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: animated, completion: completion)
}
static func presentNav(_ vc: UIViewController, animated: Bool) {
let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController
let navController = tabBarController.selectedViewController as! UINavigationController
navController.pushViewController(vc, animated: animated)
}
// MARK: - Statuses
static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let mentioning = url.arguments["mentioning"]
let text = url.arguments["text"]
@ -34,7 +46,76 @@ struct XCBActions {
}
} else {
let vc = ComposeViewController.create(for: session, mentioning: mentioning, text: text)
UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: true)
presentModally(vc, animated: true)
}
}
static func favoriteStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
func performAction(status: Status, completion: ((Status) -> Void)?) {
let request = Status.favourite(status)
MastodonController.shared.client.run(request) { (response) in
if case let .success(status, _) = response {
MastodonCache.add(status: status)
completion?(status)
session.complete(with: .success, additionalData: [
"statusURL": status.url?.absoluteString,
"statusURI": status.uri
])
} else if case let .failure(error) = response {
session.complete(with: .error, additionalData: [
"error": error.localizedDescription
])
}
}
}
func favorite(_ status: Status) {
if silent ?? false {
performAction(status: status, completion: nil)
} else {
let vc = ConversationViewController.create(for: status.id)
DispatchQueue.main.async {
presentNav(vc, animated: true)
}
let alertController = UIAlertController(title: "Favorite status?", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (_) in
performAction(status: status, completion: { (status) in
DispatchQueue.main.async {
vc.tableView.reloadData()
}
})
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
session.complete(with: .cancel)
}))
DispatchQueue.main.async {
presentModally(alertController, animated: true)
}
}
}
if let id = url.arguments["statusID"] {
MastodonCache.status(for: id) { (status) in
if let status = status {
favorite(status)
}
}
} else if let searchQuery = url.arguments["statusURL"] ?? url.arguments["statusURI"] {
let request = MastodonController.shared.client.search(query: searchQuery)
MastodonController.shared.client.run(request) { (response) in
if case let .success(results, _) = response,
let status = results.statuses.first {
favorite(status)
} else {
session.complete(with: .error, additionalData: [
"error": "Could not find status by searching '\(searchQuery)'"
])
}
}
} else {
session.complete(with: .error, additionalData: [
"error": "No status provided. Specify either instance-local statusID or remote statusURL/statusURI"
])
}
}

View File

@ -13,6 +13,7 @@ class XCBManager {
static var specs: [XCallbackURLSpec] = [
// Statuses
XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus),
XCallbackURLSpec(type: .favoriteStatus, arguments: ["statusID": true, "statusURL": true, "statusURI": true], canRunSilently: true, action: XCBActions.favoriteStatus),
// Accounts
XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser)
]