Browse Source

Add getCurrentUser XCB action, refactor XCB support some more

swiftui-preferences
Shadowfacts 1 year ago
parent
commit
9a41d44e9f
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5

+ 0
- 55
MyPlayground.playground/Contents.swift View File

@@ -1,58 +1,3 @@
1 1
 import UIKit
2 2
 
3
-class Client {
4
-    func test<A>(_ thing: A) {
5
-        if var thing = thing as? ClientModel {
6
-            thing.client = self
7
-        } else if var arr = thing as? [ClientModel] {
8
-            arr.client = self
9
-        }
10
-//        } else if let arr = thing as? Array<Any> {
11
-//            for el in arr {
12
-//                if var el = el as? ClientModel {
13
-//                    el.client = self
14
-//                }
15
-//            }
16
-//        }
17
-    }
18
-}
19 3
 
20
-protocol ClientModel {
21
-    var client: Client! { get set }
22
-}
23
-
24
-class Something: ClientModel {
25
-    var client: Client!
26
-}
27
-
28
-extension Array: ClientModel where Element: ClientModel {
29
-    var client: Client! {
30
-        get {
31
-            return first?.client
32
-        }
33
-        set {
34
-            for var el in self {
35
-                el.client = newValue
36
-            }
37
-        }
38
-    }
39
-}
40
-//extension Array: ClientModel where Element == ClientModel {
41
-//    var client: Client! {
42
-//        get {
43
-//            return first?.client
44
-//        }
45
-//        set {
46
-//            for var el in self {
47
-//                el.client = newValue
48
-//            }
49
-//        }
50
-//    }
51
-//}
52
-
53
-var array = [Something(), Something()]
54
-
55
-let client = Client()
56
-client.test(array)
57
-array[0].client
58
-array[1].client

+ 5
- 1
Tusker.xcodeproj/project.pbxproj View File

@@ -68,6 +68,7 @@
68 68
 		D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AAC2128D88B005A6F37 /* LocalData.swift */; };
69 69
 		D64D0AAF2128D954005A6F37 /* Onboarding.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */; };
70 70
 		D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */; };
71
+		D64F80E2215875CC00BEF393 /* XCBActionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64F80E1215875CC00BEF393 /* XCBActionType.swift */; };
71 72
 		D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */; };
72 73
 		D65A37F321472F300087646E /* SwiftSoup.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; };
73 74
 		D663625D2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */; };
@@ -228,6 +229,7 @@
228 229
 		D64D0AAC2128D88B005A6F37 /* LocalData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalData.swift; sourceTree = "<group>"; };
229 230
 		D64D0AAE2128D954005A6F37 /* Onboarding.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Onboarding.storyboard; sourceTree = "<group>"; };
230 231
 		D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = "<group>"; };
232
+		D64F80E1215875CC00BEF393 /* XCBActionType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBActionType.swift; sourceTree = "<group>"; };
231 233
 		D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = "<group>"; };
232 234
 		D663625C2135C74800C9CBA2 /* ConversationMainStatusTableViewCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ConversationMainStatusTableViewCell.xib; sourceTree = "<group>"; };
233 235
 		D663625E2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationMainStatusTableViewCell.swift; sourceTree = "<group>"; };
@@ -578,8 +580,9 @@
578 580
 			isa = PBXGroup;
579 581
 			children = (
580 582
 				D6757A7B2157E01900721E32 /* XCBManager.swift */,
581
-				D6757A812157E8FA00721E32 /* XCBSession.swift */,
582 583
 				D6757A7D2157E02600721E32 /* XCallbackURL.swift */,
584
+				D6757A812157E8FA00721E32 /* XCBSession.swift */,
585
+				D64F80E1215875CC00BEF393 /* XCBActionType.swift */,
583 586
 				D679C09E215850EF00DA27FE /* XCBActions.swift */,
584 587
 			);
585 588
 			path = XCallbackURL;
@@ -971,6 +974,7 @@
971 974
 				D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */,
972 975
 				D663626821360E2C00C9CBA2 /* PreferencesTableViewController.swift in Sources */,
973 976
 				D66362732136FFC600C9CBA2 /* UITextView+Placeholder.swift in Sources */,
977
+				D64F80E2215875CC00BEF393 /* XCBActionType.swift in Sources */,
974 978
 				D66362752137068A00C9CBA2 /* Visibility+Helpers.swift in Sources */,
975 979
 				D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */,
976 980
 				D6F953EC212519E700CF0F2B /* TimelineTableViewController.swift in Sources */,

+ 18
- 0
Tusker/XCallbackURL/XCBActionType.swift View File

@@ -0,0 +1,18 @@
1
+//
2
+//  XCBActionType.swift
3
+//  Tusker
4
+//
5
+//  Created by Shadowfacts on 9/23/18.
6
+//  Copyright © 2018 Shadowfacts. All rights reserved.
7
+//
8
+
9
+import Foundation
10
+
11
+enum XCBActionType: String {
12
+    case postStatus
13
+    case getCurrentUser
14
+    
15
+    var path: String {
16
+        return "/\(rawValue)"
17
+    }
18
+}

+ 16
- 1
Tusker/XCallbackURL/XCBActions.swift View File

@@ -9,8 +9,8 @@
9 9
 import UIKit
10 10
 
11 11
 struct XCBActions {
12
-    // MARK: - Posts
13 12
     
13
+    // MARK: - Posts
14 14
     static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
15 15
         let mentioning = url.arguments["mentioning"]
16 16
         let text = url.arguments["text"]
@@ -37,4 +37,19 @@ struct XCBActions {
37 37
             UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: true)
38 38
         }
39 39
     }
40
+    
41
+    // MARK: - Accounts
42
+    static func getCurrentUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
43
+        let account = MastodonController.shared.account!
44
+        session.complete(with: .success, additionalData: [
45
+            "username": account.acct,
46
+            "displayName": account.displayName,
47
+            "locked": account.locked.description,
48
+            "followers": account.followersCount.description,
49
+            "following": account.followingCount.description,
50
+            "url": account.url.absoluteString,
51
+            "avatarURL": account.avatar.absoluteString,
52
+            "headerURL": account.header.absoluteString,
53
+            ])
54
+    }
40 55
 }

+ 5
- 2
Tusker/XCallbackURL/XCBManager.swift View File

@@ -11,7 +11,10 @@ import UIKit
11 11
 class XCBManager {
12 12
     
13 13
     static var specs: [XCallbackURLSpec] = [
14
-        XCallbackURLSpec(path: "/postStatus", type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus)
14
+        // Statuses
15
+        XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus),
16
+        // Accounts
17
+        XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser)
15 18
     ]
16 19
     
17 20
     static var currentSession: XCBSession?
@@ -25,7 +28,7 @@ class XCBManager {
25 28
         return false
26 29
     }
27 30
     
28
-    static func createSession(type: XCBSessionType, url: XCallbackURL) -> XCBSession {
31
+    static func createSession(type: XCBActionType, url: XCallbackURL) -> XCBSession {
29 32
         let session = XCBSession(type: type, success: url.success, error: url.error, cancel: url.cancel)
30 33
         currentSession = session
31 34
         return session

+ 2
- 6
Tusker/XCallbackURL/XCBSession.swift View File

@@ -9,12 +9,12 @@
9 9
 import UIKit
10 10
 
11 11
 class XCBSession {
12
-    let type: XCBSessionType
12
+    let type: XCBActionType
13 13
     let success: URL?
14 14
     let error: URL?
15 15
     let cancel: URL?
16 16
     
17
-    init(type: XCBSessionType, success: URL?, error: URL?, cancel: URL?) {
17
+    init(type: XCBActionType, success: URL?, error: URL?, cancel: URL?) {
18 18
         self.type = type
19 19
         self.success = success
20 20
         self.error = error
@@ -38,10 +38,6 @@ class XCBSession {
38 38
     }
39 39
 }
40 40
 
41
-enum XCBSessionType {
42
-    case postStatus
43
-}
44
-
45 41
 enum XCBSessionResult {
46 42
     case success, error, cancel
47 43
 }

+ 3
- 3
Tusker/XCallbackURL/XCallbackURL.swift View File

@@ -13,13 +13,13 @@ typealias XCBAction = (_ url: XCallbackURL, _ session: XCBSession, _ silent: Boo
13 13
 struct XCallbackURLSpec {
14 14
     
15 15
     let path: String
16
-    let type: XCBSessionType
16
+    let type: XCBActionType
17 17
     let arguments: [String: Bool]
18 18
     let canRunSilently: Bool
19 19
     let action: XCBAction
20 20
     
21
-    init(path: String, type: XCBSessionType, arguments: [String: Bool], canRunSilently: Bool, action: @escaping XCBAction) {
22
-        self.path = path
21
+    init(type: XCBActionType, arguments: [String: Bool], canRunSilently: Bool, action: @escaping XCBAction) {
22
+        self.path = type.path
23 23
         self.type = type
24 24
         self.canRunSilently = canRunSilently
25 25
         self.action = action

Loading…
Cancel
Save