From e3d887001b2caf27c56bd6365d68f4858a75746b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 23 Sep 2018 21:35:33 -0400 Subject: [PATCH] Add getCurrentUser XCB action, refactor XCB support some more --- MyPlayground.playground/Contents.swift | 55 ------------------------- Tusker.xcodeproj/project.pbxproj | 6 ++- Tusker/XCallbackURL/XCBActionType.swift | 18 ++++++++ Tusker/XCallbackURL/XCBActions.swift | 17 +++++++- Tusker/XCallbackURL/XCBManager.swift | 7 +++- Tusker/XCallbackURL/XCBSession.swift | 8 +--- Tusker/XCallbackURL/XCallbackURL.swift | 6 +-- 7 files changed, 49 insertions(+), 68 deletions(-) create mode 100644 Tusker/XCallbackURL/XCBActionType.swift diff --git a/MyPlayground.playground/Contents.swift b/MyPlayground.playground/Contents.swift index dc558611..6bfb935c 100644 --- a/MyPlayground.playground/Contents.swift +++ b/MyPlayground.playground/Contents.swift @@ -1,58 +1,3 @@ import UIKit -class Client { - func test(_ 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 { -// 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 diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 8bd1a80d..f5cb14f3 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -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 = ""; }; D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Onboarding.storyboard; sourceTree = ""; }; D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = ""; }; + D64F80E1215875CC00BEF393 /* XCBActionType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBActionType.swift; sourceTree = ""; }; D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = ""; }; D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConversationMainStatusTableViewCell.xib; sourceTree = ""; }; D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = ""; }; @@ -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 */, diff --git a/Tusker/XCallbackURL/XCBActionType.swift b/Tusker/XCallbackURL/XCBActionType.swift new file mode 100644 index 00000000..1e534fd6 --- /dev/null +++ b/Tusker/XCallbackURL/XCBActionType.swift @@ -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)" + } +} diff --git a/Tusker/XCallbackURL/XCBActions.swift b/Tusker/XCallbackURL/XCBActions.swift index 30217e60..49f4e74d 100644 --- a/Tusker/XCallbackURL/XCBActions.swift +++ b/Tusker/XCallbackURL/XCBActions.swift @@ -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, + ]) + } } diff --git a/Tusker/XCallbackURL/XCBManager.swift b/Tusker/XCallbackURL/XCBManager.swift index 373e7200..ad7340f4 100644 --- a/Tusker/XCallbackURL/XCBManager.swift +++ b/Tusker/XCallbackURL/XCBManager.swift @@ -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 diff --git a/Tusker/XCallbackURL/XCBSession.swift b/Tusker/XCallbackURL/XCBSession.swift index 80c4c558..c4179c70 100644 --- a/Tusker/XCallbackURL/XCBSession.swift +++ b/Tusker/XCallbackURL/XCBSession.swift @@ -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 } diff --git a/Tusker/XCallbackURL/XCallbackURL.swift b/Tusker/XCallbackURL/XCallbackURL.swift index cb5cf1a6..7e383edc 100644 --- a/Tusker/XCallbackURL/XCallbackURL.swift +++ b/Tusker/XCallbackURL/XCallbackURL.swift @@ -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