session-ios/Session/Settings/NotificationSoundViewModel....

182 lines
7.0 KiB
Swift
Raw Normal View History

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import Combine
import GRDB
import DifferenceKit
import SessionUIKit
import SessionMessagingKit
import SessionUtilitiesKit
class NotificationSoundViewModel: SessionTableViewModel<NotificationSoundViewModel.NavButton, NotificationSettingsViewModel.Section, Preferences.Sound> {
// MARK: - Config
enum NavButton: Equatable {
case cancel
case save
}
public enum Section: SessionTableSection {
case content
}
// FIXME: Remove `threadId` once we ditch the per-thread notification sound
private let threadId: String?
private let dependencies: Dependencies
private var audioPlayer: OWSAudioPlayer?
private var storedSelection: Preferences.Sound?
private var currentSelection: CurrentValueSubject<Preferences.Sound?, Never> = CurrentValueSubject(nil)
// MARK: - Initialization
init(
threadId: String? = nil,
using dependencies: Dependencies = Dependencies()
) {
self.threadId = threadId
self.dependencies = dependencies
super.init()
}
deinit {
self.audioPlayer?.stop()
self.audioPlayer = nil
}
// MARK: - Navigation
override var leftNavItems: AnyPublisher<[NavItem]?, Never> {
Just([
NavItem(
id: .cancel,
systemItem: .cancel,
accessibilityIdentifier: "Cancel button"
) { [weak self] in
self?.dismissScreen()
}
]).eraseToAnyPublisher()
}
override var rightNavItems: AnyPublisher<[NavItem]?, Never> {
currentSelection
.removeDuplicates()
.map { [weak self] currentSelection in (self?.storedSelection != currentSelection) }
.map { isChanged in
guard isChanged else { return [] }
return [
NavItem(
id: .save,
systemItem: .save,
accessibilityIdentifier: "Save button"
) { [weak self] in
self?.saveChanges()
self?.dismissScreen()
}
]
}
.eraseToAnyPublisher()
}
// MARK: - Content
override var title: String { "NOTIFICATIONS_STYLE_SOUND_TITLE".localized() }
public override var observableTableData: ObservableData { _observableTableData }
/// 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 _observableTableData: ObservableData = ValueObservation
.trackingConstantRegion { [weak self] db -> [SectionModel] in
self?.storedSelection = try {
guard let threadId: String = self?.threadId else {
return db[.defaultNotificationSound]
.defaulting(to: .defaultNotificationSound)
}
return try SessionThread
.filter(id: threadId)
.select(.notificationSound)
.asRequest(of: Preferences.Sound.self)
.fetchOne(db)
.defaulting(
to: db[.defaultNotificationSound]
.defaulting(to: .defaultNotificationSound)
)
}()
self?.currentSelection.send(self?.currentSelection.value ?? self?.storedSelection)
return [
SectionModel(
model: .content,
elements: Preferences.Sound.notificationSounds
.map { sound in
SessionCell.Info(
id: sound,
title: {
guard sound != .note else {
return String(
format: "SETTINGS_AUDIO_DEFAULT_TONE_LABEL_FORMAT".localized(),
sound.displayName
)
}
return sound.displayName
}(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value == sound) }
),
onTap: {
self?.currentSelection.send(sound)
// Play the sound (to prevent UI lag we dispatch this to the next
// run loop
DispatchQueue.main.async {
self?.audioPlayer?.stop()
self?.audioPlayer = Preferences.Sound.audioPlayer(
for: sound,
behavior: .playback
)
self?.audioPlayer?.isLooping = false
self?.audioPlayer?.play()
}
}
)
}
)
]
}
.removeDuplicates()
Fixed a number of issues found during internal testing Added copy for an unrecoverable startup case Added some additional logs to better debug ValueObservation query errors Increased the pageSize to 20 on iPad devices (to prevent it immediately loading a second page) Cleaned up a bunch of threading logic (try to avoid overriding subscribe/receive threads specified at subscription) Consolidated the 'sendMessage' and 'sendAttachments' functions Updated the various frameworks to use 'DAWRF with DSYM' to allow for better debugging during debug mode (at the cost of a longer build time) Updated the logic to optimistically insert messages when sending to avoid any database write delays Updated the logic to avoid sending notifications for messages which are already marked as read by the config Fixed an issue where multiple paths could incorrectly get built at the same time in some cases Fixed an issue where other job queues could be started before the blockingQueue finishes Fixed a potential bug with the snode version comparison (was just a string comparison which would fail when getting to double-digit values) Fixed a bug where you couldn't remove the last reaction on a message Fixed the broken media message zoom animations Fixed a bug where the last message read in a conversation wouldn't be correctly detected as already read Fixed a bug where the QuoteView had no line limits (resulting in the '@You' mention background highlight being incorrectly positioned in the quote preview) Fixed a bug where a large number of configSyncJobs could be scheduled (only one would run at a time but this could result in performance impacts)
2023-06-23 09:54:29 +02:00
.handleEvents(didFail: { SNLog("[NotificationSoundViewModel] Observation failed with error: \($0)") })
.publisher(in: dependencies[singleton: .storage], scheduling: dependencies[singleton: .scheduler])
.mapToSessionTableViewData(for: self)
// MARK: - Functions
private func saveChanges() {
guard let currentSelection: Preferences.Sound = self.currentSelection.value else { return }
let threadId: String? = self.threadId
dependencies[singleton: .storage].writeAsync { db in
guard let threadId: String = threadId else {
db[.defaultNotificationSound] = currentSelection
return
}
try SessionThread
.filter(id: threadId)
.updateAll(
db,
SessionThread.Columns.notificationSound.set(to: currentSelection)
)
}
}
}