2024-04-08 03:14:12 +00:00
|
|
|
//
|
|
|
|
// PushInstanceSettingsView.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 4/7/24.
|
|
|
|
// Copyright © 2024 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import UserAccounts
|
|
|
|
import Pachyderm
|
2024-04-08 13:48:40 +00:00
|
|
|
import PushNotifications
|
2024-04-08 22:45:54 +00:00
|
|
|
import TuskerComponents
|
2024-04-08 03:14:12 +00:00
|
|
|
|
|
|
|
struct PushInstanceSettingsView: View {
|
|
|
|
let account: UserAccountInfo
|
|
|
|
let pushProxyRegistration: PushProxyRegistration
|
2024-04-08 22:45:54 +00:00
|
|
|
@State private var mode: AsyncToggle.Mode
|
2024-04-08 03:14:12 +00:00
|
|
|
@State private var error: Error?
|
2024-04-08 16:25:39 +00:00
|
|
|
@State private var subscription: PushNotifications.PushSubscription?
|
2024-04-09 23:05:31 +00:00
|
|
|
@State private var showReLoginRequiredAlert = false
|
2024-04-08 16:25:39 +00:00
|
|
|
|
|
|
|
@MainActor
|
|
|
|
init(account: UserAccountInfo, pushProxyRegistration: PushProxyRegistration) {
|
|
|
|
self.account = account
|
|
|
|
self.pushProxyRegistration = pushProxyRegistration
|
|
|
|
let subscription = PushManager.shared.pushSubscription(account: account)
|
|
|
|
self.subscription = subscription
|
|
|
|
self.mode = subscription == nil ? .off : .on
|
|
|
|
}
|
2024-04-08 03:14:12 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack(alignment: .prefsAvatar) {
|
|
|
|
HStack {
|
|
|
|
PrefsAccountView(account: account)
|
2024-04-08 16:25:39 +00:00
|
|
|
Spacer()
|
2024-04-08 22:45:54 +00:00
|
|
|
AsyncToggle("\(account.instanceURL.host!) notifications enabled", labelHidden: true, mode: $mode, onChange: updateNotificationsEnabled(enabled:))
|
2024-04-08 22:44:56 +00:00
|
|
|
.labelsHidden()
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
2024-04-08 22:44:56 +00:00
|
|
|
PushSubscriptionView(account: account, subscription: subscription, updateSubscription: updateSubscription)
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
.alertWithData("An Error Occurred", data: $error) { data in
|
|
|
|
Button("OK") {}
|
|
|
|
} message: { data in
|
|
|
|
Text(data.localizedDescription)
|
|
|
|
}
|
2024-04-09 23:05:31 +00:00
|
|
|
.alert("Re-Login Required", isPresented: $showReLoginRequiredAlert) {
|
|
|
|
Button("Cancel", role: .cancel) {}
|
|
|
|
Button("Login") {
|
|
|
|
NotificationCenter.default.post(name: .reLogInRequired, object: account)
|
|
|
|
}
|
|
|
|
} message: {
|
|
|
|
Text("You must grant permission on \(account.instanceURL.host!) to turn on push notifications.")
|
|
|
|
}
|
2024-04-11 16:58:43 +00:00
|
|
|
.onReceive(NotificationCenter.default
|
|
|
|
.publisher(for: .pushSubscriptionRemoved)
|
|
|
|
.filter { ($0.object as? String) == account.id }
|
|
|
|
) { _ in
|
|
|
|
mode = .off
|
|
|
|
subscription = nil
|
|
|
|
}
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 15:49:03 +00:00
|
|
|
private func updateNotificationsEnabled(enabled: Bool) async -> Bool {
|
2024-04-08 03:14:12 +00:00
|
|
|
if enabled {
|
|
|
|
do {
|
2024-04-09 23:05:31 +00:00
|
|
|
return try await enableNotifications()
|
2024-04-08 03:14:12 +00:00
|
|
|
} catch {
|
2024-04-08 16:25:39 +00:00
|
|
|
PushManager.logger.error("Error creating instance subscription: \(String(describing: error))")
|
2024-04-08 03:14:12 +00:00
|
|
|
self.error = .enabling(error)
|
2024-04-09 15:49:03 +00:00
|
|
|
return false
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
try await disableNotifications()
|
|
|
|
} catch {
|
2024-04-08 16:25:39 +00:00
|
|
|
PushManager.logger.error("Error removing instance subscription: \(String(describing: error))")
|
2024-04-08 03:14:12 +00:00
|
|
|
self.error = .disabling(error)
|
2024-04-09 15:49:03 +00:00
|
|
|
return false
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-09 15:49:03 +00:00
|
|
|
return true
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 23:05:31 +00:00
|
|
|
private func enableNotifications() async throws -> Bool {
|
|
|
|
guard account.scopes?.contains(Scope.push.rawValue) == true else {
|
|
|
|
showReLoginRequiredAlert = true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-04-08 16:25:39 +00:00
|
|
|
let subscription = try await PushManager.shared.createSubscription(account: account)
|
|
|
|
let mastodonController = await MastodonController.getForAccount(account)
|
|
|
|
do {
|
2024-04-09 16:35:00 +00:00
|
|
|
let result = try await mastodonController.createPushSubscription(subscription: subscription)
|
2024-04-08 16:25:39 +00:00
|
|
|
PushManager.logger.debug("Push subscription \(result.id) created on \(account.instanceURL)")
|
|
|
|
self.subscription = subscription
|
2024-04-09 23:05:31 +00:00
|
|
|
return true
|
2024-04-08 16:25:39 +00:00
|
|
|
} catch {
|
|
|
|
// if creation failed, remove the subscription locally as well
|
|
|
|
await PushManager.shared.removeSubscription(account: account)
|
|
|
|
throw error
|
|
|
|
}
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func disableNotifications() async throws {
|
2024-04-08 16:25:39 +00:00
|
|
|
let mastodonController = await MastodonController.getForAccount(account)
|
2024-04-09 16:35:00 +00:00
|
|
|
try await mastodonController.deletePushSubscription()
|
2024-04-08 16:25:39 +00:00
|
|
|
await PushManager.shared.removeSubscription(account: account)
|
|
|
|
subscription = nil
|
|
|
|
PushManager.logger.debug("Push subscription removed on \(account.instanceURL)")
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
2024-04-08 22:44:56 +00:00
|
|
|
|
2024-04-09 15:49:03 +00:00
|
|
|
private func updateSubscription(alerts: PushNotifications.PushSubscription.Alerts, policy: PushNotifications.PushSubscription.Policy) async -> Bool {
|
2024-04-08 22:44:56 +00:00
|
|
|
let mastodonController = await MastodonController.getForAccount(account)
|
|
|
|
do {
|
2024-04-09 16:35:00 +00:00
|
|
|
let result = try await mastodonController.updatePushSubscription(alerts: alerts, policy: policy)
|
2024-04-08 22:44:56 +00:00
|
|
|
PushManager.logger.debug("Push subscription \(result.id) updated on \(account.instanceURL)")
|
2024-04-10 01:07:14 +00:00
|
|
|
await PushManager.shared.updateSubscription(account: account, alerts: alerts, policy: policy)
|
2024-04-08 22:44:56 +00:00
|
|
|
subscription?.alerts = alerts
|
|
|
|
subscription?.policy = policy
|
2024-04-09 15:49:03 +00:00
|
|
|
return true
|
2024-04-08 22:44:56 +00:00
|
|
|
} catch {
|
|
|
|
PushManager.logger.error("Error updating subscription: \(String(describing: error))")
|
|
|
|
self.error = .updating(error)
|
2024-04-09 15:49:03 +00:00
|
|
|
return false
|
2024-04-08 22:44:56 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private enum Error: LocalizedError {
|
|
|
|
case enabling(any Swift.Error)
|
|
|
|
case disabling(any Swift.Error)
|
2024-04-08 22:44:56 +00:00
|
|
|
case updating(any Swift.Error)
|
|
|
|
|
2024-04-08 03:14:12 +00:00
|
|
|
var errorDescription: String? {
|
|
|
|
switch self {
|
|
|
|
case .enabling(let error):
|
|
|
|
"Enabling push: \(error.localizedDescription)"
|
|
|
|
case .disabling(let error):
|
|
|
|
"Disabling push: \(error.localizedDescription)"
|
2024-04-08 22:44:56 +00:00
|
|
|
case .updating(let error):
|
|
|
|
"Updating settings: \(error.localizedDescription)"
|
2024-04-08 03:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//#Preview {
|
|
|
|
// PushInstanceSettingsView()
|
|
|
|
//}
|