You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Session/Settings/NotificationSettingsViewMod...

151 lines
6.7 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import DifferenceKit
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
class NotificationSettingsViewModel: SettingsTableViewModel<NoNav, NotificationSettingsViewModel.Section, NotificationSettingsViewModel.Setting> {
// MARK: - Config
public enum Section: SettingSection {
case strategy
case style
case content
var title: String? {
switch self {
case .strategy: return "NOTIFICATIONS_SECTION_STRATEGY".localized()
case .style: return "NOTIFICATIONS_SECTION_STYLE".localized()
case .content: return nil
}
}
var style: SettingSectionHeaderStyle {
switch self {
case .content: return .padding
default: return .title
}
}
}
public enum Setting: Differentiable {
case strategyUseFastMode
case styleSound
case styleSoundWhenAppIsOpen
case content
}
// MARK: - Content
override var title: String { "NOTIFICATIONS_TITLE".localized() }
private var _settingsData: [SectionModel] = []
public override var settingsData: [SectionModel] { _settingsData }
public override var observableSettingsData: ObservableData { _observableSettingsData }
/// This is all the data the screen needs to populate itself, please see the following link for tips to help optimise
/// performance https://github.com/groue/GRDB.swift#valueobservation-performance
///
/// **Note:** This observation will be triggered twice immediately (and be de-duped by the `removeDuplicates`)
/// this is due to the behaviour of `ValueConcurrentObserver.asyncStartObservation` which triggers it's own
/// fetch (after the ones in `ValueConcurrentObserver.asyncStart`/`ValueConcurrentObserver.syncStart`)
/// just in case the database has changed between the two reads - unfortunately it doesn't look like there is a way to prevent this
private lazy var _observableSettingsData: ObservableData = ValueObservation
.trackingConstantRegion { db -> [SectionModel] in
return [
SectionModel(
model: .strategy,
elements: [
SettingInfo(
id: .strategyUseFastMode,
title: "NOTIFICATIONS_STRATEGY_FAST_MODE_TITLE".localized(),
subtitle: "NOTIFICATIONS_STRATEGY_FAST_MODE_DESCRIPTION".localized(),
action: .userDefaultsBool(
defaults: UserDefaults.standard,
key: "isUsingFullAPNs",
onChange: {
// Force sync the push tokens on change
SyncPushTokensJob.run(uploadOnlyIfStale: false)
}
),
extraActionTitle: { theme, primaryColor in
NSMutableAttributedString()
.appending(
NSAttributedString(
string: "NOTIFICATIONS_STRATEGY_FAST_MODE_ACTION_1".localized(),
attributes: [
.foregroundColor: primaryColor.color
]
)
)
.appending(
NSAttributedString(
string: "NOTIFICATIONS_STRATEGY_FAST_MODE_ACTION_2".localized(),
attributes: [
Merge remote-tracking branch 'upstream/dev' into feature/theming # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Closed Groups/NewClosedGroupVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/Message Cells/CallMessageCell.swift # Session/Conversations/Views & Modals/JoinOpenGroupModal.swift # Session/Home/HomeVC.swift # Session/Home/New Conversation/NewDMVC.swift # Session/Home/NewConversationButtonSet.swift # Session/Meta/Translations/de.lproj/Localizable.strings # Session/Meta/Translations/en.lproj/Localizable.strings # Session/Meta/Translations/es.lproj/Localizable.strings # Session/Meta/Translations/fa.lproj/Localizable.strings # Session/Meta/Translations/fi.lproj/Localizable.strings # Session/Meta/Translations/fr.lproj/Localizable.strings # Session/Meta/Translations/hi.lproj/Localizable.strings # Session/Meta/Translations/hr.lproj/Localizable.strings # Session/Meta/Translations/id-ID.lproj/Localizable.strings # Session/Meta/Translations/it.lproj/Localizable.strings # Session/Meta/Translations/ja.lproj/Localizable.strings # Session/Meta/Translations/nl.lproj/Localizable.strings # Session/Meta/Translations/pl.lproj/Localizable.strings # Session/Meta/Translations/pt_BR.lproj/Localizable.strings # Session/Meta/Translations/ru.lproj/Localizable.strings # Session/Meta/Translations/si.lproj/Localizable.strings # Session/Meta/Translations/sk.lproj/Localizable.strings # Session/Meta/Translations/sv.lproj/Localizable.strings # Session/Meta/Translations/th.lproj/Localizable.strings # Session/Meta/Translations/vi-VN.lproj/Localizable.strings # Session/Meta/Translations/zh-Hant.lproj/Localizable.strings # Session/Meta/Translations/zh_CN.lproj/Localizable.strings # Session/Open Groups/JoinOpenGroupVC.swift # Session/Open Groups/OpenGroupSuggestionGrid.swift # Session/Settings/SettingsVC.swift # Session/Shared/BaseVC.swift # Session/Shared/OWSQRCodeScanningViewController.m # Session/Shared/ScanQRCodeWrapperVC.swift # Session/Shared/UserCell.swift # SessionMessagingKit/Configuration.swift # SessionShareExtension/SAEScreenLockViewController.swift # SessionUIKit/Style Guide/Gradients.swift # SignalUtilitiesKit/Media Viewing & Editing/OWSViewController+ImageEditor.swift # SignalUtilitiesKit/Screen Lock/ScreenLockViewController.m
2 years ago
.foregroundColor: (theme.color(for: .textPrimary) ?? .white)
]
)
)
},
onExtraAction: { UIApplication.shared.openSystemSettings() }
)
]
),
SectionModel(
model: .style,
elements: [
SettingInfo(
id: .styleSound,
title: "NOTIFICATIONS_STYLE_SOUND_TITLE".localized(),
action: .settingEnum(
db,
type: Preferences.Sound.self,
key: .defaultNotificationSound,
titleGenerator: { $0.defaulting(to: .defaultNotificationSound).displayName },
createUpdateScreen: {
SettingsTableViewController(viewModel: NotificationSoundViewModel())
}
)
),
SettingInfo(
id: .styleSoundWhenAppIsOpen,
title: "NOTIFICATIONS_STYLE_SOUND_WHEN_OPEN_TITLE".localized(),
action: .settingBool(key: .playNotificationSoundInForeground)
)
]
),
SectionModel(
model: .content,
elements: [
SettingInfo(
id: .content,
title: "NOTIFICATIONS_STYLE_CONTENT_TITLE".localized(),
subtitle: "NOTIFICATIONS_STYLE_CONTENT_DESCRIPTION".localized(),
action: .settingEnum(
db,
type: Preferences.NotificationPreviewType.self,
key: .preferencesNotificationPreviewType,
titleGenerator: { $0.defaulting(to: .defaultPreviewType).name },
createUpdateScreen: {
SettingsTableViewController(viewModel: NotificationContentViewModel())
}
)
)
]
)
]
}
.removeDuplicates()
.publisher(in: Storage.shared)
// MARK: - Functions
public override func updateSettings(_ updatedSettings: [SectionModel]) {
self._settingsData = updatedSettings
}
}