feat(view model): 增加 network client
增加了一个第三方库 Moya,用于处理网络请求 Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
parent
bea4505eaa
commit
872c69df6b
@ -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 = "<group>"; };
|
||||
24D4D3CF29ABC01E0064E566 /* AddAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddAccountView.swift; sourceTree = "<group>"; };
|
||||
24D4D3D329AF97A40064E566 /* InstanceAppViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstanceAppViewModel.swift; sourceTree = "<group>"; };
|
||||
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 = "<group>";
|
||||
};
|
||||
24D4D3D729B10FB90064E566 /* Managers */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
24D4D3D529AFA1970064E566 /* InstanceAppManager.swift */,
|
||||
);
|
||||
path = Managers;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* 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 */;
|
||||
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>PreviewsEnabled</key>
|
||||
<false/>
|
||||
</dict>
|
||||
</plist>
|
||||
@ -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
|
||||
|
||||
@ -6,9 +6,72 @@
|
||||
<dict>
|
||||
<key>DUDUJI.xcscheme_^#shared#^_</key>
|
||||
<dict>
|
||||
<key>orderHint</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
<key>ReactiveSwift (Playground) 1.xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>2</integer>
|
||||
</dict>
|
||||
<key>ReactiveSwift (Playground) 2.xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>3</integer>
|
||||
</dict>
|
||||
<key>ReactiveSwift (Playground).xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>0</integer>
|
||||
</dict>
|
||||
<key>ReactiveSwift-UIExamples (Playground) 1.xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>5</integer>
|
||||
</dict>
|
||||
<key>ReactiveSwift-UIExamples (Playground) 2.xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>6</integer>
|
||||
</dict>
|
||||
<key>ReactiveSwift-UIExamples (Playground).xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>4</integer>
|
||||
</dict>
|
||||
<key>Rx (Playground) 1.xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>8</integer>
|
||||
</dict>
|
||||
<key>Rx (Playground) 2.xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>9</integer>
|
||||
</dict>
|
||||
<key>Rx (Playground).xcscheme</key>
|
||||
<dict>
|
||||
<key>isShown</key>
|
||||
<false/>
|
||||
<key>orderHint</key>
|
||||
<integer>7</integer>
|
||||
</dict>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
|
||||
@ -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"
|
||||
}
|
||||
|
||||
34
DUDUJI/Managers/InstanceAppManager.swift
Normal file
34
DUDUJI/Managers/InstanceAppManager.swift
Normal file
@ -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)")
|
||||
}
|
||||
}
|
||||
94
DUDUJI/MastodonService.swift
Normal file
94
DUDUJI/MastodonService.swift
Normal file
@ -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<MastodonSevice>
|
||||
|
||||
init(baseURL: String) {
|
||||
service = MoyaProvider<MastodonSevice>(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
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user