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

97 lines
3.1 KiB
Swift
Raw Normal View History

2023-01-17 10:36:01 +00:00
import AppAccount
2023-01-08 09:22:52 +00:00
import DesignSystem
2023-01-17 10:36:01 +00:00
import Env
import Models
2023-01-08 09:22:52 +00:00
import Network
2023-01-17 10:36:01 +00:00
import NukeUI
import SwiftUI
2023-01-08 09:22:52 +00:00
import UserNotifications
struct PushNotificationsView: View {
@EnvironmentObject private var theme: Theme
@EnvironmentObject private var appAccountsManager: AppAccountsManager
2023-01-08 13:16:43 +00:00
@EnvironmentObject private var pushNotifications: PushNotificationsService
2023-01-17 10:36:01 +00:00
2023-01-08 09:22:52 +00:00
@State private var subscriptions: [PushSubscription] = []
2023-01-17 10:36:01 +00:00
2023-01-08 09:22:52 +00:00
var body: some View {
Form {
Section {
Toggle(isOn: $pushNotifications.isPushEnabled) {
2023-01-09 17:52:53 +00:00
Text("Push notifications")
2023-01-08 09:22:52 +00:00
}
2023-01-08 13:16:43 +00:00
} footer: {
Text("Receive push notifications on new activities")
2023-01-08 09:57:58 +00:00
}
.listRowBackground(theme.primaryBackgroundColor)
2023-01-17 10:36:01 +00:00
2023-01-08 09:57:58 +00:00
if pushNotifications.isPushEnabled {
Section {
2023-01-08 13:16:43 +00:00
Toggle(isOn: $pushNotifications.isMentionNotificationEnabled) {
Label("Mentions", systemImage: "at")
}
2023-01-08 09:22:52 +00:00
Toggle(isOn: $pushNotifications.isFollowNotificationEnabled) {
2023-01-08 13:16:43 +00:00
Label("Follows", systemImage: "person.badge.plus")
2023-01-08 09:22:52 +00:00
}
Toggle(isOn: $pushNotifications.isFavoriteNotificationEnabled) {
2023-01-08 13:16:43 +00:00
Label("Favorites", systemImage: "star")
2023-01-08 09:22:52 +00:00
}
Toggle(isOn: $pushNotifications.isReblogNotificationEnabled) {
2023-01-08 13:16:43 +00:00
Label("Boosts", systemImage: "arrow.left.arrow.right.circle")
2023-01-08 09:22:52 +00:00
}
Toggle(isOn: $pushNotifications.isPollNotificationEnabled) {
2023-01-08 13:16:43 +00:00
Label("Polls Results", systemImage: "chart.bar")
}
Toggle(isOn: $pushNotifications.isNewPostsNotificationEnabled) {
Label("New Posts", systemImage: "bubble.right")
2023-01-08 09:22:52 +00:00
}
2023-01-08 09:57:58 +00:00
}
.listRowBackground(theme.primaryBackgroundColor)
.transition(.move(edge: .bottom))
2023-01-08 09:22:52 +00:00
}
}
.navigationTitle("Push Notifications")
.scrollContentBackground(.hidden)
.background(theme.secondaryBackgroundColor)
.onAppear {
Task {
await pushNotifications.fetchSubscriptions(accounts: appAccountsManager.pushAccounts)
}
}
.onChange(of: pushNotifications.isPushEnabled) { newValue in
pushNotifications.isUserPushEnabled = newValue
if !newValue {
Task {
await pushNotifications.deleteSubscriptions(accounts: appAccountsManager.pushAccounts)
}
} else {
updateSubscriptions()
}
}
.onChange(of: pushNotifications.isFollowNotificationEnabled) { _ in
updateSubscriptions()
}
.onChange(of: pushNotifications.isPollNotificationEnabled) { _ in
updateSubscriptions()
}
.onChange(of: pushNotifications.isReblogNotificationEnabled) { _ in
updateSubscriptions()
}
.onChange(of: pushNotifications.isMentionNotificationEnabled) { _ in
updateSubscriptions()
}
.onChange(of: pushNotifications.isFavoriteNotificationEnabled) { _ in
updateSubscriptions()
}
2023-01-08 13:16:43 +00:00
.onChange(of: pushNotifications.isNewPostsNotificationEnabled) { _ in
updateSubscriptions()
}
2023-01-08 09:22:52 +00:00
}
2023-01-17 10:36:01 +00:00
2023-01-08 09:22:52 +00:00
private func updateSubscriptions() {
Task {
await pushNotifications.updateSubscriptions(accounts: appAccountsManager.pushAccounts)
}
}
}