diff --git a/DUDUJI.xcodeproj/project.pbxproj b/DUDUJI.xcodeproj/project.pbxproj index 65f5f11..575bae7 100644 --- a/DUDUJI.xcodeproj/project.pbxproj +++ b/DUDUJI.xcodeproj/project.pbxproj @@ -19,6 +19,8 @@ 24D4D3D029ABC01E0064E566 /* AddAccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24D4D3CF29ABC01E0064E566 /* AddAccountView.swift */; }; 24D4D3D429AF97A40064E566 /* InstanceAppViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24D4D3D329AF97A40064E566 /* InstanceAppViewModel.swift */; }; 24D4D3D629AFA1970064E566 /* InstanceAppManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24D4D3D529AFA1970064E566 /* InstanceAppManager.swift */; }; + 24D4D3DA29B2EB7B0064E566 /* Moya in Frameworks */ = {isa = PBXBuildFile; productRef = 24D4D3D929B2EB7B0064E566 /* Moya */; }; + 24D4D3DC29B2EC160064E566 /* MastodonService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24D4D3DB29B2EC160064E566 /* MastodonService.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -34,7 +36,8 @@ 24D4D3CD29ABBC030064E566 /* InstanceApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceApp.swift; sourceTree = ""; }; 24D4D3CF29ABC01E0064E566 /* AddAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddAccountView.swift; sourceTree = ""; }; 24D4D3D329AF97A40064E566 /* InstanceAppViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceAppViewModel.swift; sourceTree = ""; }; - 24D4D3D529AFA1970064E566 /* InstanceAppManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InstanceAppManager.swift; path = DUDUJI/Models/InstanceAppManager.swift; sourceTree = SOURCE_ROOT; }; + 24D4D3D529AFA1970064E566 /* InstanceAppManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = InstanceAppManager.swift; path = DUDUJI/Managers/InstanceAppManager.swift; sourceTree = SOURCE_ROOT; }; + 24D4D3DB29B2EC160064E566 /* MastodonService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = MastodonService.swift; path = DUDUJI/MastodonService.swift; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -42,6 +45,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 24D4D3DA29B2EB7B0064E566 /* Moya in Frameworks */, 24D4D3CC29ABBA800064E566 /* MastodonSwift in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -68,6 +72,8 @@ 24D4D3B329AB83E80064E566 /* DUDUJI */ = { isa = PBXGroup; children = ( + 24D4D3DB29B2EC160064E566 /* MastodonService.swift */, + 24D4D3D729B10FB90064E566 /* Managers */, 24D4D3D229AF8E9D0064E566 /* ViewModels */, 24D4D3D129ABC0280064E566 /* Views */, 24D4D3C529ABB5830064E566 /* Models */, @@ -92,7 +98,6 @@ 24D4D3C529ABB5830064E566 /* Models */ = { isa = PBXGroup; children = ( - 24D4D3D529AFA1970064E566 /* InstanceAppManager.swift */, 24D4D3C329ABAE5A0064E566 /* AccountInfo.swift */, 24D4D3C629ABB6500064E566 /* OauthToken.swift */, 24D4D3CD29ABBC030064E566 /* InstanceApp.swift */, @@ -116,6 +121,14 @@ path = ViewModels; sourceTree = ""; }; + 24D4D3D729B10FB90064E566 /* Managers */ = { + isa = PBXGroup; + children = ( + 24D4D3D529AFA1970064E566 /* InstanceAppManager.swift */, + ); + path = Managers; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -134,6 +147,7 @@ name = DUDUJI; packageProductDependencies = ( 24D4D3CB29ABBA800064E566 /* MastodonSwift */, + 24D4D3D929B2EB7B0064E566 /* Moya */, ); productName = DUDUJI; productReference = 24D4D3B129AB83E80064E566 /* DUDUJI.app */; @@ -165,6 +179,7 @@ mainGroup = 24D4D3A829AB83E80064E566; packageReferences = ( 24D4D3CA29ABBA800064E566 /* XCRemoteSwiftPackageReference "Mastodon" */, + 24D4D3D829B2EB7B0064E566 /* XCRemoteSwiftPackageReference "Moya" */, ); productRefGroup = 24D4D3B229AB83E80064E566 /* Products */; projectDirPath = ""; @@ -196,6 +211,7 @@ 24D4D3C729ABB6500064E566 /* OauthToken.swift in Sources */, 24D4D3CE29ABBC030064E566 /* InstanceApp.swift in Sources */, 24D4D3B729AB83E80064E566 /* DUDUJIView.swift in Sources */, + 24D4D3DC29B2EC160064E566 /* MastodonService.swift in Sources */, 24D4D3B529AB83E80064E566 /* DUDUJIApp.swift in Sources */, 24D4D3C929ABB7590064E566 /* Const.swift in Sources */, 24D4D3D029ABC01E0064E566 /* AddAccountView.swift in Sources */, @@ -424,6 +440,14 @@ minimumVersion = 2.0.0; }; }; + 24D4D3D829B2EB7B0064E566 /* XCRemoteSwiftPackageReference "Moya" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/Moya/Moya"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 15.0.0; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -432,6 +456,11 @@ package = 24D4D3CA29ABBA800064E566 /* XCRemoteSwiftPackageReference "Mastodon" */; productName = MastodonSwift; }; + 24D4D3D929B2EB7B0064E566 /* Moya */ = { + isa = XCSwiftPackageProductDependency; + package = 24D4D3D829B2EB7B0064E566 /* XCRemoteSwiftPackageReference "Moya" */; + productName = Moya; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 24D4D3A929AB83E80064E566 /* Project object */; diff --git a/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings b/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings new file mode 100644 index 0000000..f9b0d7c --- /dev/null +++ b/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/WorkspaceSettings.xcsettings @@ -0,0 +1,8 @@ + + + + + PreviewsEnabled + + + diff --git a/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 9200442..6c39df1 100644 --- a/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DUDUJI.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,14 @@ { "pins" : [ + { + "identity" : "alamofire", + "kind" : "remoteSourceControl", + "location" : "https://github.com/Alamofire/Alamofire.git", + "state" : { + "revision" : "78424be314842833c04bc3bef5b72e85fff99204", + "version" : "5.6.4" + } + }, { "identity" : "mastodon.swift", "kind" : "remoteSourceControl", @@ -8,6 +17,33 @@ "revision" : "26d970a92d31cbb0b797f8031746e07f656aee2b", "version" : "2.1.2" } + }, + { + "identity" : "moya", + "kind" : "remoteSourceControl", + "location" : "https://github.com/Moya/Moya", + "state" : { + "revision" : "c263811c1f3dbf002be9bd83107f7cdc38992b26", + "version" : "15.0.3" + } + }, + { + "identity" : "reactiveswift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/ReactiveCocoa/ReactiveSwift.git", + "state" : { + "revision" : "c43bae3dac73fdd3cb906bd5a1914686ca71ed3c", + "version" : "6.7.0" + } + }, + { + "identity" : "rxswift", + "kind" : "remoteSourceControl", + "location" : "https://github.com/ReactiveX/RxSwift.git", + "state" : { + "revision" : "b4307ba0b6425c0ba4178e138799946c3da594f8", + "version" : "6.5.0" + } } ], "version" : 2 diff --git a/DUDUJI.xcodeproj/xcuserdata/ching.xcuserdatad/xcschemes/xcschememanagement.plist b/DUDUJI.xcodeproj/xcuserdata/ching.xcuserdatad/xcschemes/xcschememanagement.plist index c6a792d..dec2069 100644 --- a/DUDUJI.xcodeproj/xcuserdata/ching.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/DUDUJI.xcodeproj/xcuserdata/ching.xcuserdatad/xcschemes/xcschememanagement.plist @@ -6,9 +6,72 @@ DUDUJI.xcscheme_^#shared#^_ + orderHint + 1 + + ReactiveSwift (Playground) 1.xcscheme + + isShown + + orderHint + 2 + + ReactiveSwift (Playground) 2.xcscheme + + isShown + + orderHint + 3 + + ReactiveSwift (Playground).xcscheme + + isShown + orderHint 0 + ReactiveSwift-UIExamples (Playground) 1.xcscheme + + isShown + + orderHint + 5 + + ReactiveSwift-UIExamples (Playground) 2.xcscheme + + isShown + + orderHint + 6 + + ReactiveSwift-UIExamples (Playground).xcscheme + + isShown + + orderHint + 4 + + Rx (Playground) 1.xcscheme + + isShown + + orderHint + 8 + + Rx (Playground) 2.xcscheme + + isShown + + orderHint + 9 + + Rx (Playground).xcscheme + + isShown + + orderHint + 7 + diff --git a/DUDUJI/Const.swift b/DUDUJI/Const.swift index 39382a0..dafb50d 100644 --- a/DUDUJI/Const.swift +++ b/DUDUJI/Const.swift @@ -15,3 +15,10 @@ enum Const { enum DUDUConst { static let name: String = "嘟嘟机" } + + +enum HttpMethod { + static let get: String = "GET" + static let post: String = "POST" + static let put: String = "PUT" +} diff --git a/DUDUJI/Managers/InstanceAppManager.swift b/DUDUJI/Managers/InstanceAppManager.swift new file mode 100644 index 0000000..2849382 --- /dev/null +++ b/DUDUJI/Managers/InstanceAppManager.swift @@ -0,0 +1,34 @@ +// +// InstanceAppManager.swift +// DUDUJI +// +// Created by ching on 2023/3/1. +// + +import Foundation + +class InstanceAppManager { + func getClientIdAndClientSecret(at serverAddress: String) -> (clientId: String, clientSecret: String) { + return ("123", "321") + } + + func getInstanceInfo(at serverAddress: String) { + let client = MastodonClient(baseURL: serverAddress) + client.getInstance() +// print("服务器信息:\()") + } + + func createInstanceApp(at serverAddress: String) { + print("服务器地址是\(serverAddress)") + // save things to InstanceApp model + // let instanceApp = InstanceApp(id: xxx....) + let instanceApp = InstanceApp( + name: DUDUConst.name, + website: nil, + redirectUri: serverAddress, + clientId: "abc", + clientSecret: "cba") + // save the instanceApp to database + print("客户端名称:\(instanceApp.name)") + } +} diff --git a/DUDUJI/MastodonService.swift b/DUDUJI/MastodonService.swift new file mode 100644 index 0000000..cd64acc --- /dev/null +++ b/DUDUJI/MastodonService.swift @@ -0,0 +1,94 @@ +// +// MastodonService.swift +// DUDUJI +// +// Created by ching on 2023/3/4. +// + +import Foundation +import Moya + +enum MastodonSevice { + case instance +} + +extension MastodonSevice: TargetType { + var baseURL: URL { + switch self { + case .instance: + return URL(string: "")! // base URL will be set later + } + } + + var path: String { + switch self { + case .instance: + return "/api/v2/instance" + } + } + + var method: Moya.Method { + switch self { + case .instance: + return .get + } + } + + var sampleData: Data { + return Data() + } + + var task: Task { + switch self { + case .instance: + return .requestPlain + } + } + + var headers: [String: String]? { + return nil + } +} + +class MastodonClient { + private let service: MoyaProvider + + init(baseURL: String) { + service = MoyaProvider(endpointClosure: { (target: MastodonSevice) -> Endpoint in + Endpoint( + url: baseURL + target.path, + sampleResponseClosure: { .networkResponse(200, target.sampleData) }, + method: target.method, + task: target.task, + httpHeaderFields: target.headers + ) + }) + } + + func getInstance() { +// var instanceData = [Any]() + service.request(.instance) { result in + switch result { + case let .success(response): + do { +// let data = try response.map(Data.self) + let json = try response.mapJSON() +// let stringData = String(data: data, encoding: .utf8) +// print(stringData) +// print("服务器信息:\(String(describing: stringData))") + if let dict = json as? [String: Any], + let title = dict["title"] as? String + { + print(title) + } + + } catch { + print("Error parsing JSON, response: \(response)") + } + case let .failure(error): + print(error.localizedDescription) + } + } +// return instanceData + } +} diff --git a/DUDUJI/Models/InstanceAppManager.swift b/DUDUJI/Models/InstanceAppManager.swift deleted file mode 100644 index 739a6d1..0000000 --- a/DUDUJI/Models/InstanceAppManager.swift +++ /dev/null @@ -1,19 +0,0 @@ -// -// InstanceAppManager.swift -// DUDUJI -// -// Created by ching on 2023/3/1. -// - -import Foundation - -class InstanceAppManager { - func createInstanceApp(at serverAddress: String) { - print("服务器地址是\(serverAddress)") - // save things to InstanceApp model - // let instanceApp = InstanceApp(id: xxx....) - let instanceApp = InstanceApp(name: "name", website: nil, redirectUri: serverAddress, clientId: "abc", clientSecret: "cba") - // save the instanceApp to database - print(instanceApp.name) - } -} diff --git a/DUDUJI/ViewModels/InstanceAppViewModel.swift b/DUDUJI/ViewModels/InstanceAppViewModel.swift index 9794c9c..5593f17 100644 --- a/DUDUJI/ViewModels/InstanceAppViewModel.swift +++ b/DUDUJI/ViewModels/InstanceAppViewModel.swift @@ -12,7 +12,11 @@ class InstanceAppViewModel: ObservableObject { // MARK: intents - func createInstanceApp(at url: String) { + /// 提交实例地址 + /// 在该实例中创建对应的 App + /// - Parameter url: 实例地址 + func submitServerAddress(with url: String) { + instanceAppManager.getInstanceInfo(at: url) instanceAppManager.createInstanceApp(at: url) } } diff --git a/DUDUJI/Views/AddAccountView.swift b/DUDUJI/Views/AddAccountView.swift index 9467205..b2aac38 100644 --- a/DUDUJI/Views/AddAccountView.swift +++ b/DUDUJI/Views/AddAccountView.swift @@ -21,13 +21,15 @@ struct AddAccountView: View { var serverAddressSection: some View { Section(header: Text("服务器地址")) { TextField("nofan.xyz", text: $serverAddress) + .autocorrectionDisabled(true) + .autocapitalization(.none) } } var submitButtonSection: some View { Section { Button("提交") { - instanceAccountViewModel.createInstanceApp(at: serverAddress) + instanceAccountViewModel.submitServerAddress(with: serverAddress) } } }