IceCubes/IceCubesApp/App/Tabs/Settings/PushNotificationsView.swift

126 lines
3.8 KiB
Swift
Raw Normal View History

2023-01-17 11:36:01 +01:00
import AppAccount
2023-01-08 10:22:52 +01:00
import DesignSystem
2023-01-17 11:36:01 +01:00
import Env
import Models
2023-01-08 10:22:52 +01:00
import Network
2023-01-17 11:36:01 +01:00
import NukeUI
import SwiftUI
2023-01-08 10:22:52 +01:00
import UserNotifications
struct PushNotificationsView: View {
@EnvironmentObject private var theme: Theme
@EnvironmentObject private var appAccountsManager: AppAccountsManager
2023-01-08 14:16:43 +01:00
@EnvironmentObject private var pushNotifications: PushNotificationsService
2023-01-17 11:36:01 +01:00
@StateObject public var subscription: PushNotificationSubscriptionSettings
2023-01-17 11:36:01 +01:00
2023-01-08 10:22:52 +01:00
var body: some View {
Form {
Section {
Toggle(isOn: .init(get: {
subscription.isEnabled
}, set: { newValue in
subscription.isEnabled = newValue
if newValue {
updateSubscription()
} else {
deleteSubscription()
}
})) {
Text("settings.push.main-toggle")
2023-01-08 10:22:52 +01:00
}
2023-01-08 14:16:43 +01:00
} footer: {
Text("settings.push.main-toggle.description")
2023-01-08 10:57:58 +01:00
}
.listRowBackground(theme.primaryBackgroundColor)
if subscription.isEnabled {
2023-01-08 10:57:58 +01:00
Section {
Toggle(isOn: .init(get: {
subscription.isMentionNotificationEnabled
}, set: { newValue in
subscription.isMentionNotificationEnabled = newValue
updateSubscription()
})) {
Label("settings.push.mentions", systemImage: "at")
2023-01-08 14:16:43 +01:00
}
Toggle(isOn: .init(get: {
subscription.isFollowNotificationEnabled
}, set: { newValue in
subscription.isFollowNotificationEnabled = newValue
updateSubscription()
})) {
Label("settings.push.follows", systemImage: "person.badge.plus")
2023-01-08 10:22:52 +01:00
}
Toggle(isOn: .init(get: {
subscription.isFavoriteNotificationEnabled
}, set: { newValue in
subscription.isFavoriteNotificationEnabled = newValue
updateSubscription()
})) {
Label("settings.push.favorites", systemImage: "star")
2023-01-08 10:22:52 +01:00
}
Toggle(isOn: .init(get: {
subscription.isReblogNotificationEnabled
}, set: { newValue in
subscription.isReblogNotificationEnabled = newValue
updateSubscription()
})) {
Label("settings.push.boosts", systemImage: "arrow.left.arrow.right.circle")
2023-01-08 10:22:52 +01:00
}
Toggle(isOn: .init(get: {
subscription.isPollNotificationEnabled
}, set: { newValue in
subscription.isPollNotificationEnabled = newValue
updateSubscription()
})) {
Label("settings.push.polls", systemImage: "chart.bar")
2023-01-08 14:16:43 +01:00
}
Toggle(isOn: .init(get: {
subscription.isNewPostsNotificationEnabled
}, set: { newValue in
subscription.isNewPostsNotificationEnabled = newValue
updateSubscription()
})) {
Label("settings.push.new-posts", systemImage: "bubble.right")
2023-01-08 10:22:52 +01:00
}
2023-01-08 10:57:58 +01:00
}
.listRowBackground(theme.primaryBackgroundColor)
2023-01-08 10:22:52 +01:00
}
Section {
Button("settings.push.duplicate.button.fix") {
Task {
await subscription.deleteSubscription()
await subscription.updateSubscription()
}
}
} header: {
Text("settings.push.duplicate.title")
} footer: {
Text("settings.push.duplicate.footer")
}
.listRowBackground(theme.primaryBackgroundColor)
2023-01-08 10:22:52 +01:00
}
.navigationTitle("settings.push.navigation-title")
2023-01-08 10:22:52 +01:00
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.task {
await subscription.fetchSubscription()
2023-01-08 14:16:43 +01:00
}
2023-01-08 10:22:52 +01:00
}
2023-01-17 11:36:01 +01:00
private func updateSubscription() {
Task {
await subscription.updateSubscription()
}
}
private func deleteSubscription() {
2023-01-08 10:22:52 +01:00
Task {
await subscription.deleteSubscription()
2023-01-08 10:22:52 +01:00
}
}
}