Add getCurrentUser XCB action, refactor XCB support some more

This commit is contained in:
Shadowfacts 2018-09-23 21:35:33 -04:00
parent a8d8ac783b
commit c18ef5f56f
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
7 changed files with 49 additions and 68 deletions

View File

@ -1,58 +1,3 @@
import UIKit
class Client {
func test<A>(_ thing: A) {
if var thing = thing as? ClientModel {
thing.client = self
} else if var arr = thing as? [ClientModel] {
arr.client = self
}
// } else if let arr = thing as? Array<Any> {
// for el in arr {
// if var el = el as? ClientModel {
// el.client = self
// }
// }
// }
}
}
protocol ClientModel {
var client: Client! { get set }
}
class Something: ClientModel {
var client: Client!
}
extension Array: ClientModel where Element: ClientModel {
var client: Client! {
get {
return first?.client
}
set {
for var el in self {
el.client = newValue
}
}
}
}
//extension Array: ClientModel where Element == ClientModel {
// var client: Client! {
// get {
// return first?.client
// }
// set {
// for var el in self {
// el.client = newValue
// }
// }
// }
//}
var array = [Something(), Something()]
let client = Client()
client.test(array)
array[0].client
array[1].client

View File

@ -68,6 +68,7 @@
D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AAC2128D88B005A6F37 /* LocalData.swift */; };
D64D0AAF2128D954005A6F37 /* Onboarding.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */; };
D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */; };
D64F80E2215875CC00BEF393 /* XCBActionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64F80E1215875CC00BEF393 /* XCBActionType.swift */; };
D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */; };
D65A37F321472F300087646E /* SwiftSoup.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; };
D663625D2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */; };
@ -228,6 +229,7 @@
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>"; };
D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = "<group>"; };
D64F80E1215875CC00BEF393 /* XCBActionType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBActionType.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>"; };
D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = "<group>"; };
@ -578,8 +580,9 @@
isa = PBXGroup;
children = (
D6757A7B2157E01900721E32 /* XCBManager.swift */,
D6757A812157E8FA00721E32 /* XCBSession.swift */,
D6757A7D2157E02600721E32 /* XCallbackURL.swift */,
D6757A812157E8FA00721E32 /* XCBSession.swift */,
D64F80E1215875CC00BEF393 /* XCBActionType.swift */,
D679C09E215850EF00DA27FE /* XCBActions.swift */,
);
path = XCallbackURL;
@ -971,6 +974,7 @@
D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */,
D663626821360E2C00C9CBA2 /* PreferencesTableViewController.swift in Sources */,
D66362732136FFC600C9CBA2 /* UITextView+Placeholder.swift in Sources */,
D64F80E2215875CC00BEF393 /* XCBActionType.swift in Sources */,
D66362752137068A00C9CBA2 /* Visibility+Helpers.swift in Sources */,
D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */,
D6F953EC212519E700CF0F2B /* TimelineTableViewController.swift in Sources */,

View File

@ -0,0 +1,18 @@
//
// XCBActionType.swift
// Tusker
//
// Created by Shadowfacts on 9/23/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
enum XCBActionType: String {
case postStatus
case getCurrentUser
var path: String {
return "/\(rawValue)"
}
}

View File

@ -9,8 +9,8 @@
import UIKit
struct XCBActions {
// MARK: - Posts
// MARK: - Posts
static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let mentioning = url.arguments["mentioning"]
let text = url.arguments["text"]
@ -37,4 +37,19 @@ struct XCBActions {
UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: true)
}
}
// MARK: - Accounts
static func getCurrentUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let account = MastodonController.shared.account!
session.complete(with: .success, additionalData: [
"username": account.acct,
"displayName": account.displayName,
"locked": account.locked.description,
"followers": account.followersCount.description,
"following": account.followingCount.description,
"url": account.url.absoluteString,
"avatarURL": account.avatar.absoluteString,
"headerURL": account.header.absoluteString,
])
}
}

View File

@ -11,7 +11,10 @@ import UIKit
class XCBManager {
static var specs: [XCallbackURLSpec] = [
XCallbackURLSpec(path: "/postStatus", type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus)
// Statuses
XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus),
// Accounts
XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser)
]
static var currentSession: XCBSession?
@ -25,7 +28,7 @@ class XCBManager {
return false
}
static func createSession(type: XCBSessionType, url: XCallbackURL) -> XCBSession {
static func createSession(type: XCBActionType, url: XCallbackURL) -> XCBSession {
let session = XCBSession(type: type, success: url.success, error: url.error, cancel: url.cancel)
currentSession = session
return session

View File

@ -9,12 +9,12 @@
import UIKit
class XCBSession {
let type: XCBSessionType
let type: XCBActionType
let success: URL?
let error: URL?
let cancel: URL?
init(type: XCBSessionType, success: URL?, error: URL?, cancel: URL?) {
init(type: XCBActionType, success: URL?, error: URL?, cancel: URL?) {
self.type = type
self.success = success
self.error = error
@ -38,10 +38,6 @@ class XCBSession {
}
}
enum XCBSessionType {
case postStatus
}
enum XCBSessionResult {
case success, error, cancel
}

View File

@ -13,13 +13,13 @@ typealias XCBAction = (_ url: XCallbackURL, _ session: XCBSession, _ silent: Boo
struct XCallbackURLSpec {
let path: String
let type: XCBSessionType
let type: XCBActionType
let arguments: [String: Bool]
let canRunSilently: Bool
let action: XCBAction
init(path: String, type: XCBSessionType, arguments: [String: Bool], canRunSilently: Bool, action: @escaping XCBAction) {
self.path = path
init(type: XCBActionType, arguments: [String: Bool], canRunSilently: Bool, action: @escaping XCBAction) {
self.path = type.path
self.type = type
self.canRunSilently = canRunSilently
self.action = action