84 lines
2.3 KiB
Swift
84 lines
2.3 KiB
Swift
//
|
|
// PushInstanceSettingsView.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 4/7/24.
|
|
// Copyright © 2024 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import SwiftUI
|
|
import UserAccounts
|
|
import Pachyderm
|
|
import PushNotifications
|
|
|
|
struct PushInstanceSettingsView: View {
|
|
let account: UserAccountInfo
|
|
let pushProxyRegistration: PushProxyRegistration
|
|
@State private var mode: TriStateToggle.Mode = .off
|
|
@State private var error: Error?
|
|
|
|
var body: some View {
|
|
VStack(alignment: .prefsAvatar) {
|
|
HStack {
|
|
PrefsAccountView(account: account)
|
|
TriStateToggle(titleKey: "\(account.instanceURL.host!) notifications enabled", mode: $mode, onChange: updateNotificationsEnabled(enabled:))
|
|
}
|
|
PushSubscriptionView(account: account)
|
|
}
|
|
.alertWithData("An Error Occurred", data: $error) { data in
|
|
Button("OK") {}
|
|
} message: { data in
|
|
Text(data.localizedDescription)
|
|
}
|
|
|
|
}
|
|
|
|
private func updateNotificationsEnabled(enabled: Bool) async {
|
|
if enabled {
|
|
do {
|
|
try await enableNotifications()
|
|
} catch {
|
|
self.error = .enabling(error)
|
|
}
|
|
} else {
|
|
do {
|
|
try await disableNotifications()
|
|
} catch {
|
|
self.error = .disabling(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func enableNotifications() async throws {
|
|
// let req = Pachyderm.PushSubscription.create(
|
|
// endpoint: pushProxyRegistration.endpoint,
|
|
// publicKey: <#T##Data#>,
|
|
// authSecret: <#T##Data#>,
|
|
// alerts: <#T##PushSubscription.Alerts#>,
|
|
// policy: <#T##PushSubscription.Policy#>
|
|
// )
|
|
}
|
|
|
|
private func disableNotifications() async throws {
|
|
|
|
}
|
|
}
|
|
|
|
private enum Error: LocalizedError {
|
|
case enabling(any Swift.Error)
|
|
case disabling(any Swift.Error)
|
|
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .enabling(let error):
|
|
"Enabling push: \(error.localizedDescription)"
|
|
case .disabling(let error):
|
|
"Disabling push: \(error.localizedDescription)"
|
|
}
|
|
}
|
|
}
|
|
|
|
//#Preview {
|
|
// PushInstanceSettingsView()
|
|
//}
|