Use push client package
This commit is contained in:
parent
25fbaef134
commit
0dbc9c9bb7
@ -9,15 +9,14 @@
|
|||||||
/* Begin PBXBuildFile section */
|
/* Begin PBXBuildFile section */
|
||||||
881E0B26284B74E200435EC2 /* Data+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 881E0B25284B74E200435EC2 /* Data+Extensions.swift */; };
|
881E0B26284B74E200435EC2 /* Data+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 881E0B25284B74E200435EC2 /* Data+Extensions.swift */; };
|
||||||
88DBE72A284B989C00D1573B /* DeviceList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88DBE729284B989C00D1573B /* DeviceList.swift */; };
|
88DBE72A284B989C00D1573B /* DeviceList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88DBE729284B989C00D1573B /* DeviceList.swift */; };
|
||||||
E2349959284E0695002B55F8 /* PushAPI in Frameworks */ = {isa = PBXBuildFile; productRef = E2349958284E0695002B55F8 /* PushAPI */; };
|
|
||||||
E234995C284E1D02002B55F8 /* SFSafeSymbols in Frameworks */ = {isa = PBXBuildFile; productRef = E234995B284E1D02002B55F8 /* SFSafeSymbols */; };
|
E234995C284E1D02002B55F8 /* SFSafeSymbols in Frameworks */ = {isa = PBXBuildFile; productRef = E234995B284E1D02002B55F8 /* SFSafeSymbols */; };
|
||||||
E234995F284E372B002B55F8 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E2349961284E372B002B55F8 /* Localizable.strings */; };
|
E234995F284E372B002B55F8 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = E2349961284E372B002B55F8 /* Localizable.strings */; };
|
||||||
E2349964284F3133002B55F8 /* TextEntryField.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2349963284F3133002B55F8 /* TextEntryField.swift */; };
|
E2349964284F3133002B55F8 /* TextEntryField.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2349963284F3133002B55F8 /* TextEntryField.swift */; };
|
||||||
|
E2349968284F78E3002B55F8 /* Push in Frameworks */ = {isa = PBXBuildFile; productRef = E2349967284F78E3002B55F8 /* Push */; };
|
||||||
E29A7E47284B6143000B908A /* FlurSchnapsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E29A7E46284B6143000B908A /* FlurSchnapsApp.swift */; };
|
E29A7E47284B6143000B908A /* FlurSchnapsApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E29A7E46284B6143000B908A /* FlurSchnapsApp.swift */; };
|
||||||
E29A7E49284B6143000B908A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E29A7E48284B6143000B908A /* ContentView.swift */; };
|
E29A7E49284B6143000B908A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E29A7E48284B6143000B908A /* ContentView.swift */; };
|
||||||
E29A7E4B284B6144000B908A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E29A7E4A284B6144000B908A /* Assets.xcassets */; };
|
E29A7E4B284B6144000B908A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E29A7E4A284B6144000B908A /* Assets.xcassets */; };
|
||||||
E29A7E4E284B6144000B908A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E29A7E4D284B6144000B908A /* Preview Assets.xcassets */; };
|
E29A7E4E284B6144000B908A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E29A7E4D284B6144000B908A /* Preview Assets.xcassets */; };
|
||||||
E29A7E55284B619A000B908A /* API.swift in Sources */ = {isa = PBXBuildFile; fileRef = E29A7E54284B619A000B908A /* API.swift */; };
|
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXFileReference section */
|
/* Begin PBXFileReference section */
|
||||||
@ -33,7 +32,6 @@
|
|||||||
E29A7E48284B6143000B908A /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
E29A7E48284B6143000B908A /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
|
||||||
E29A7E4A284B6144000B908A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
E29A7E4A284B6144000B908A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
|
||||||
E29A7E4D284B6144000B908A /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
E29A7E4D284B6144000B908A /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
|
||||||
E29A7E54284B619A000B908A /* API.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = API.swift; sourceTree = "<group>"; };
|
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
@ -41,8 +39,8 @@
|
|||||||
isa = PBXFrameworksBuildPhase;
|
isa = PBXFrameworksBuildPhase;
|
||||||
buildActionMask = 2147483647;
|
buildActionMask = 2147483647;
|
||||||
files = (
|
files = (
|
||||||
|
E2349968284F78E3002B55F8 /* Push in Frameworks */,
|
||||||
E234995C284E1D02002B55F8 /* SFSafeSymbols in Frameworks */,
|
E234995C284E1D02002B55F8 /* SFSafeSymbols in Frameworks */,
|
||||||
E2349959284E0695002B55F8 /* PushAPI in Frameworks */,
|
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
@ -75,7 +73,6 @@
|
|||||||
E29A7E48284B6143000B908A /* ContentView.swift */,
|
E29A7E48284B6143000B908A /* ContentView.swift */,
|
||||||
E2349963284F3133002B55F8 /* TextEntryField.swift */,
|
E2349963284F3133002B55F8 /* TextEntryField.swift */,
|
||||||
88DBE729284B989C00D1573B /* DeviceList.swift */,
|
88DBE729284B989C00D1573B /* DeviceList.swift */,
|
||||||
E29A7E54284B619A000B908A /* API.swift */,
|
|
||||||
881E0B25284B74E200435EC2 /* Data+Extensions.swift */,
|
881E0B25284B74E200435EC2 /* Data+Extensions.swift */,
|
||||||
E29A7E4A284B6144000B908A /* Assets.xcassets */,
|
E29A7E4A284B6144000B908A /* Assets.xcassets */,
|
||||||
E29A7E4C284B6144000B908A /* Preview Content */,
|
E29A7E4C284B6144000B908A /* Preview Content */,
|
||||||
@ -108,8 +105,8 @@
|
|||||||
);
|
);
|
||||||
name = FlurSchnaps;
|
name = FlurSchnaps;
|
||||||
packageProductDependencies = (
|
packageProductDependencies = (
|
||||||
E2349958284E0695002B55F8 /* PushAPI */,
|
|
||||||
E234995B284E1D02002B55F8 /* SFSafeSymbols */,
|
E234995B284E1D02002B55F8 /* SFSafeSymbols */,
|
||||||
|
E2349967284F78E3002B55F8 /* Push */,
|
||||||
);
|
);
|
||||||
productName = FlurSchnaps;
|
productName = FlurSchnaps;
|
||||||
productReference = E29A7E43284B6143000B908A /* FlurSchnaps.app */;
|
productReference = E29A7E43284B6143000B908A /* FlurSchnaps.app */;
|
||||||
@ -141,8 +138,8 @@
|
|||||||
);
|
);
|
||||||
mainGroup = E29A7E3A284B6143000B908A;
|
mainGroup = E29A7E3A284B6143000B908A;
|
||||||
packageReferences = (
|
packageReferences = (
|
||||||
E2349957284E0695002B55F8 /* XCRemoteSwiftPackageReference "Push-API" */,
|
|
||||||
E234995A284E1D02002B55F8 /* XCRemoteSwiftPackageReference "SFSafeSymbols" */,
|
E234995A284E1D02002B55F8 /* XCRemoteSwiftPackageReference "SFSafeSymbols" */,
|
||||||
|
E2349966284F78E3002B55F8 /* XCRemoteSwiftPackageReference "Push-iOS" */,
|
||||||
);
|
);
|
||||||
productRefGroup = E29A7E44284B6143000B908A /* Products */;
|
productRefGroup = E29A7E44284B6143000B908A /* Products */;
|
||||||
projectDirPath = "";
|
projectDirPath = "";
|
||||||
@ -174,7 +171,6 @@
|
|||||||
E2349964284F3133002B55F8 /* TextEntryField.swift in Sources */,
|
E2349964284F3133002B55F8 /* TextEntryField.swift in Sources */,
|
||||||
E29A7E49284B6143000B908A /* ContentView.swift in Sources */,
|
E29A7E49284B6143000B908A /* ContentView.swift in Sources */,
|
||||||
881E0B26284B74E200435EC2 /* Data+Extensions.swift in Sources */,
|
881E0B26284B74E200435EC2 /* Data+Extensions.swift in Sources */,
|
||||||
E29A7E55284B619A000B908A /* API.swift in Sources */,
|
|
||||||
88DBE72A284B989C00D1573B /* DeviceList.swift in Sources */,
|
88DBE72A284B989C00D1573B /* DeviceList.swift in Sources */,
|
||||||
E29A7E47284B6143000B908A /* FlurSchnapsApp.swift in Sources */,
|
E29A7E47284B6143000B908A /* FlurSchnapsApp.swift in Sources */,
|
||||||
);
|
);
|
||||||
@ -399,14 +395,6 @@
|
|||||||
/* End XCConfigurationList section */
|
/* End XCConfigurationList section */
|
||||||
|
|
||||||
/* Begin XCRemoteSwiftPackageReference section */
|
/* Begin XCRemoteSwiftPackageReference section */
|
||||||
E2349957284E0695002B55F8 /* XCRemoteSwiftPackageReference "Push-API" */ = {
|
|
||||||
isa = XCRemoteSwiftPackageReference;
|
|
||||||
repositoryURL = "https://christophhagen.de/git/ch/Push-API";
|
|
||||||
requirement = {
|
|
||||||
branch = main;
|
|
||||||
kind = branch;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
E234995A284E1D02002B55F8 /* XCRemoteSwiftPackageReference "SFSafeSymbols" */ = {
|
E234995A284E1D02002B55F8 /* XCRemoteSwiftPackageReference "SFSafeSymbols" */ = {
|
||||||
isa = XCRemoteSwiftPackageReference;
|
isa = XCRemoteSwiftPackageReference;
|
||||||
repositoryURL = "https://github.com/SFSafeSymbols/SFSafeSymbols";
|
repositoryURL = "https://github.com/SFSafeSymbols/SFSafeSymbols";
|
||||||
@ -415,19 +403,27 @@
|
|||||||
minimumVersion = 3.0.0;
|
minimumVersion = 3.0.0;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
E2349966284F78E3002B55F8 /* XCRemoteSwiftPackageReference "Push-iOS" */ = {
|
||||||
|
isa = XCRemoteSwiftPackageReference;
|
||||||
|
repositoryURL = "https://christophhagen.de/git/ch/Push-iOS";
|
||||||
|
requirement = {
|
||||||
|
kind = upToNextMajorVersion;
|
||||||
|
minimumVersion = 0.2.0;
|
||||||
|
};
|
||||||
|
};
|
||||||
/* End XCRemoteSwiftPackageReference section */
|
/* End XCRemoteSwiftPackageReference section */
|
||||||
|
|
||||||
/* Begin XCSwiftPackageProductDependency section */
|
/* Begin XCSwiftPackageProductDependency section */
|
||||||
E2349958284E0695002B55F8 /* PushAPI */ = {
|
|
||||||
isa = XCSwiftPackageProductDependency;
|
|
||||||
package = E2349957284E0695002B55F8 /* XCRemoteSwiftPackageReference "Push-API" */;
|
|
||||||
productName = PushAPI;
|
|
||||||
};
|
|
||||||
E234995B284E1D02002B55F8 /* SFSafeSymbols */ = {
|
E234995B284E1D02002B55F8 /* SFSafeSymbols */ = {
|
||||||
isa = XCSwiftPackageProductDependency;
|
isa = XCSwiftPackageProductDependency;
|
||||||
package = E234995A284E1D02002B55F8 /* XCRemoteSwiftPackageReference "SFSafeSymbols" */;
|
package = E234995A284E1D02002B55F8 /* XCRemoteSwiftPackageReference "SFSafeSymbols" */;
|
||||||
productName = SFSafeSymbols;
|
productName = SFSafeSymbols;
|
||||||
};
|
};
|
||||||
|
E2349967284F78E3002B55F8 /* Push */ = {
|
||||||
|
isa = XCSwiftPackageProductDependency;
|
||||||
|
package = E2349966284F78E3002B55F8 /* XCRemoteSwiftPackageReference "Push-iOS" */;
|
||||||
|
productName = Push;
|
||||||
|
};
|
||||||
/* End XCSwiftPackageProductDependency section */
|
/* End XCSwiftPackageProductDependency section */
|
||||||
};
|
};
|
||||||
rootObject = E29A7E3B284B6143000B908A /* Project object */;
|
rootObject = E29A7E3B284B6143000B908A /* Project object */;
|
||||||
|
@ -21,10 +21,19 @@
|
|||||||
{
|
{
|
||||||
"identity" : "push-api",
|
"identity" : "push-api",
|
||||||
"kind" : "remoteSourceControl",
|
"kind" : "remoteSourceControl",
|
||||||
"location" : "https://christophhagen.de/git/ch/Push-API",
|
"location" : "https://christophhagen.de/git/ch/Push-API.git",
|
||||||
"state" : {
|
"state" : {
|
||||||
"branch" : "main",
|
"revision" : "50e74bf15637cbcab3f76ff1255db355cc87bc4e",
|
||||||
"revision" : "d5fb765ac998e2f731ec6fe8bf473a396af9b61c"
|
"version" : "0.5.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"identity" : "push-ios",
|
||||||
|
"kind" : "remoteSourceControl",
|
||||||
|
"location" : "https://christophhagen.de/git/ch/Push-iOS",
|
||||||
|
"state" : {
|
||||||
|
"revision" : "910f7e5f91408ddf54ea15fb3ccef182b5daedff",
|
||||||
|
"version" : "0.2.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1,131 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
import CryptoKit
|
|
||||||
import PushAPI
|
|
||||||
import SwiftUI
|
|
||||||
|
|
||||||
final class API {
|
|
||||||
|
|
||||||
@AppStorage("server")
|
|
||||||
var server: String = ""
|
|
||||||
|
|
||||||
var url: URL? {
|
|
||||||
URL(string: server)
|
|
||||||
}
|
|
||||||
|
|
||||||
init() {
|
|
||||||
}
|
|
||||||
|
|
||||||
init(server: URL) {
|
|
||||||
self.server = server.path
|
|
||||||
}
|
|
||||||
|
|
||||||
init(server: URL, application: ApplicationId) {
|
|
||||||
self.server = server.path
|
|
||||||
self.application = application
|
|
||||||
}
|
|
||||||
|
|
||||||
@AppStorage("application")
|
|
||||||
var application: ApplicationId = ""
|
|
||||||
|
|
||||||
private static let encoder = JSONEncoder()
|
|
||||||
private static let decoder = JSONDecoder()
|
|
||||||
|
|
||||||
func register(token: PushToken, name: String) async -> AuthenticationToken? {
|
|
||||||
let device = DeviceRegistration(
|
|
||||||
pushToken: token,
|
|
||||||
application: application,
|
|
||||||
name: name)
|
|
||||||
guard let token = await post(.registerNewDevice, body: device) else {
|
|
||||||
print("Failed to register")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
guard token.count == 16 else {
|
|
||||||
print("Failed to register: Unexpected token length: \(token.count)")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return token
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDeviceList(pushToken: PushToken, authToken: AuthenticationToken) async -> [DeviceRegistration] {
|
|
||||||
let device = DeviceAuthentication(pushToken: pushToken, authentication: authToken)
|
|
||||||
guard let data = await post(.listDevicesInApplication, body: device) else {
|
|
||||||
print("Devices: Failed")
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
return try API.decoder.decode([DeviceRegistration].self, from: data)
|
|
||||||
} catch {
|
|
||||||
print("Devices: Failed to decode response")
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func getUnconfirmedDevices(masterKey: String) async -> [DeviceRegistration] {
|
|
||||||
let hash = hash(masterKey)
|
|
||||||
guard let data = await post(.listUnapprovedDevices, bodyData: hash) else {
|
|
||||||
print("Devices: Failed")
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
return try API.decoder.decode([DeviceRegistration].self, from: data)
|
|
||||||
} catch {
|
|
||||||
print("Devices: Failed to decode response")
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private func hash(_ masterKey: String) -> Data {
|
|
||||||
Data(SHA256.hash(data: masterKey.data(using: .utf8)!))
|
|
||||||
}
|
|
||||||
|
|
||||||
func confirm(pushToken: PushToken, with masterKey: String) async -> Bool {
|
|
||||||
let hash = hash(masterKey)
|
|
||||||
let device = DeviceDecision(pushToken: pushToken, masterKeyHash: hash)
|
|
||||||
return await post(.approveDevice, body: device) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func reject(pushToken: PushToken, with masterKey: String) async -> Bool {
|
|
||||||
let hash = hash(masterKey)
|
|
||||||
let device = DeviceDecision(pushToken: pushToken, masterKeyHash: hash)
|
|
||||||
return await post(.rejectDevice, body: device) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func isConfirmed(token: PushToken, authentication: AuthenticationToken) async -> Bool {
|
|
||||||
let device = DeviceAuthentication(pushToken: token, authentication: authentication)
|
|
||||||
return await post(.isDeviceApproved, body: device) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func send(push: AuthenticatedPushMessage) async -> Bool {
|
|
||||||
await post(.sendPushNotification, body: push) != nil
|
|
||||||
}
|
|
||||||
|
|
||||||
private func post<T>(_ route: Route, body: T) async -> Data? where T: Encodable {
|
|
||||||
let bodyData = try! API.encoder.encode(body)
|
|
||||||
return await post(route, bodyData: bodyData)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func post(_ route: Route, bodyData: Data) async -> Data? {
|
|
||||||
guard let url = url else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var request = URLRequest(url: url.appendingPathComponent(route.rawValue))
|
|
||||||
request.httpBody = bodyData
|
|
||||||
request.httpMethod = "POST"
|
|
||||||
do {
|
|
||||||
let (data, response) = try await URLSession.shared.data(for: request)
|
|
||||||
guard let httpResponse = response as? HTTPURLResponse else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
guard httpResponse.statusCode == 200 else {
|
|
||||||
print("Failed with code: \(httpResponse.statusCode)")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return data
|
|
||||||
} catch {
|
|
||||||
print("Failed with error: \(error)")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,5 +1,6 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
import APNSwift
|
import APNSwift
|
||||||
|
import Push
|
||||||
import PushAPI
|
import PushAPI
|
||||||
import SFSafeSymbols
|
import SFSafeSymbols
|
||||||
|
|
||||||
@ -25,7 +26,13 @@ struct ContentView: View {
|
|||||||
var showDeviceList = false
|
var showDeviceList = false
|
||||||
|
|
||||||
@State
|
@State
|
||||||
var api = API()
|
var api: PushClient?
|
||||||
|
|
||||||
|
@AppStorage("server")
|
||||||
|
var server: String = ""
|
||||||
|
|
||||||
|
@AppStorage("application")
|
||||||
|
var application = ""
|
||||||
|
|
||||||
@AppStorage("deviceName")
|
@AppStorage("deviceName")
|
||||||
var deviceName: String = ""
|
var deviceName: String = ""
|
||||||
@ -125,10 +132,10 @@ struct ContentView: View {
|
|||||||
} else if authToken == nil {
|
} else if authToken == nil {
|
||||||
Text("register-device-text")
|
Text("register-device-text")
|
||||||
.padding()
|
.padding()
|
||||||
TextEntryField("Server url", placeholder: "register-device-server-placeholder", symbol: .network, showClearButton: true, text: $api.server)
|
TextEntryField("Server url", placeholder: "register-device-server-placeholder", symbol: .network, showClearButton: true, text: $server)
|
||||||
.padding(.horizontal, 50)
|
.padding(.horizontal, 50)
|
||||||
.padding(.top)
|
.padding(.top)
|
||||||
TextEntryField("Application", placeholder: "register-device-application-placeholder", symbol: .questionmarkApp, showClearButton: true, text: $api.application)
|
TextEntryField("Application", placeholder: "register-device-application-placeholder", symbol: .questionmarkApp, showClearButton: true, text: $application)
|
||||||
.padding(.horizontal, 50)
|
.padding(.horizontal, 50)
|
||||||
.padding(.top)
|
.padding(.top)
|
||||||
TextEntryField("Device name", placeholder: "register-device-name-placeholder", symbol: .iphone, text: $deviceName)
|
TextEntryField("Device name", placeholder: "register-device-name-placeholder", symbol: .iphone, text: $deviceName)
|
||||||
@ -161,7 +168,7 @@ struct ContentView: View {
|
|||||||
.navigationTitle("FlurSchnaps")
|
.navigationTitle("FlurSchnaps")
|
||||||
}
|
}
|
||||||
.sheet(isPresented: $showDeviceList) {
|
.sheet(isPresented: $showDeviceList) {
|
||||||
if let push = pushToken, let auth = authToken {
|
if let push = pushToken, let auth = authToken, let api = api {
|
||||||
DeviceList(pushToken: push,
|
DeviceList(pushToken: push,
|
||||||
authToken: auth,
|
authToken: auth,
|
||||||
api: api,
|
api: api,
|
||||||
@ -226,6 +233,11 @@ struct ContentView: View {
|
|||||||
print("No token to register")
|
print("No token to register")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
guard let url = URL(string: server) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
let api = PushClient(server: url, application: application)
|
||||||
|
self.api = api
|
||||||
let name = deviceName
|
let name = deviceName
|
||||||
Task {
|
Task {
|
||||||
print("Registering...")
|
print("Registering...")
|
||||||
@ -246,13 +258,14 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkPushRegistrationStatus() {
|
func checkPushRegistrationStatus() {
|
||||||
guard let token = pushToken, let authToken = authToken else {
|
guard let token = pushToken,
|
||||||
|
let authToken = authToken,
|
||||||
|
let api = api else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Task {
|
Task {
|
||||||
let confirmed = await api.isConfirmed(token: token, authentication: authToken)
|
let confirmed = await api.isConfirmed(token: token, authentication: authToken)
|
||||||
if !confirmed {
|
if !confirmed {
|
||||||
print("Not confirmed by server: \(api.url?.path ?? "No server") (\(api.server))")
|
|
||||||
print(token.base64EncodedString())
|
print(token.base64EncodedString())
|
||||||
print(authToken.base64EncodedString())
|
print(authToken.base64EncodedString())
|
||||||
}
|
}
|
||||||
@ -264,13 +277,14 @@ struct ContentView: View {
|
|||||||
|
|
||||||
func updateDeviceList() {
|
func updateDeviceList() {
|
||||||
guard let authToken = authToken,
|
guard let authToken = authToken,
|
||||||
let pushToken = pushToken else {
|
let pushToken = pushToken,
|
||||||
|
let api = api else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
Task {
|
Task {
|
||||||
let devices = await api.getDeviceList(pushToken: pushToken, authToken: authToken)
|
let devices = await api.getDeviceList(pushToken: pushToken, authToken: authToken)
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.deviceList = devices
|
self.deviceList = devices ?? []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -280,10 +294,9 @@ struct ContentView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendPush() {
|
func sendPush() {
|
||||||
guard let authToken = authToken else {
|
guard let authToken = authToken,
|
||||||
return
|
let pushToken = pushToken,
|
||||||
}
|
let api = api else {
|
||||||
guard let pushToken = pushToken else {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
import PushAPI
|
import PushAPI
|
||||||
|
import Push
|
||||||
|
|
||||||
extension String {
|
extension String {
|
||||||
|
|
||||||
@ -14,7 +15,7 @@ struct DeviceList: View {
|
|||||||
|
|
||||||
let authToken: AuthenticationToken
|
let authToken: AuthenticationToken
|
||||||
|
|
||||||
let api: API
|
let api: PushClient
|
||||||
|
|
||||||
@Binding
|
@Binding
|
||||||
var isPresented: Bool
|
var isPresented: Bool
|
||||||
@ -58,9 +59,9 @@ struct DeviceList: View {
|
|||||||
|
|
||||||
private func updateList() async {
|
private func updateList() async {
|
||||||
let devices = await api.getDeviceList(pushToken: pushToken, authToken: authToken)
|
let devices = await api.getDeviceList(pushToken: pushToken, authToken: authToken)
|
||||||
print("Updated device list: \(devices.count)")
|
print("Updated device list: \(devices?.count ?? -1)")
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.devices = devices
|
self.devices = devices ?? []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ struct DeviceList_Previews: PreviewProvider {
|
|||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
DeviceList(pushToken: Data(repeating: 42, count: 32),
|
DeviceList(pushToken: Data(repeating: 42, count: 32),
|
||||||
authToken: Data(repeating: 42, count: 16),
|
authToken: Data(repeating: 42, count: 16),
|
||||||
api: .init(server: URL(string: "https://christophhagen.de/push")!),
|
api: .init(server: URL(string: "https://christophhagen.de/push")!, application: "some"),
|
||||||
isPresented: .constant(true),
|
isPresented: .constant(true),
|
||||||
devices: [DeviceRegistration(
|
devices: [DeviceRegistration(
|
||||||
pushToken: Data([1,2,3,4,5]),
|
pushToken: Data([1,2,3,4,5]),
|
||||||
|
Loading…
Reference in New Issue
Block a user