Files
session-ios/Session/Settings/NotificationSoundViewModel.swift
Morgan Pretty 30210dc27b Merge branch 'session-pro-settings' into feature/libsession-message-wrapping
# Conflicts:
#	Session.xcodeproj/project.pbxproj
#	Session/Conversations/ConversationVC+Interaction.swift
#	Session/Conversations/Message Cells/VisibleMessageCell.swift
#	Session/Home/HomeViewModel.swift
#	Session/Home/Message Requests/MessageRequestsViewModel.swift
#	Session/Media Viewing & Editing/MessageInfoScreen.swift
#	Session/Settings/DeveloperSettings/DeveloperSettingsProViewModel.swift
#	Session/Settings/DeveloperSettings/DeveloperSettingsViewModel.swift
#	Session/Settings/SessionProSettings/SessionProSettingsViewModel.swift
#	Session/Settings/SettingsViewModel.swift
#	Session/Settings/Views/ThemeMessagePreviewView.swift
#	SessionMessagingKit/Shared Models/MessageViewModel.swift
#	SessionMessagingKit/Utilities/AttachmentManager.swift
#	SessionMessagingKit/Utilities/SessionProState.swift
#	SessionUIKit/Components/Input View/InputView.swift
#	SessionUIKit/Components/SwiftUI/ProCTAModal.swift
#	SessionUIKit/Components/SwiftUI/QuoteView_SwiftUI.swift
#	SessionUIKit/Screens/Settings/SessionProSettings/SessionProPaymentScreen+Models.swift
#	SessionUIKit/Screens/Settings/SessionProSettings/SessionProPaymentScreen+RequestRefund.swift
#	SessionUIKit/Style Guide/Constants+Apple.swift
#	SessionUIKit/Style Guide/Themes/ThemedAttributedString.swift
#	SessionUIKit/Types/Localization.swift
#	SessionUIKit/Utilities/MentionUtilities.swift
#	SessionUtilitiesKit/Dependency Injection/Dependencies.swift
#	SessionUtilitiesKit/General/Feature.swift
2025-12-01 11:04:31 +11:00

133 lines
4.9 KiB
Swift

// 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, NavigationItemSource, NavigatableStateHolder, ObservableTableSource {
typealias TableItem = Preferences.Sound
public let dependencies: Dependencies
public let navigatableState: NavigatableState = NavigatableState()
public let state: TableDataState<Section, TableItem> = TableDataState()
public let observableState: ObservableTableSourceState<Section, TableItem> = ObservableTableSourceState()
private let originalSelection: Preferences.Sound
private var audioPlayer: OWSAudioPlayer?
private var currentSelection: CurrentValueSubject<Preferences.Sound, Never>
// MARK: - Initialization
init(using dependencies: Dependencies) {
self.dependencies = dependencies
let originalSelection: Preferences.Sound = dependencies
.mutate(cache: .libSession, { $0.get(.defaultNotificationSound) })
.defaulting(to: .defaultNotificationSound)
self.originalSelection = originalSelection
self.currentSelection = CurrentValueSubject(originalSelection)
}
deinit {
Task { @MainActor [audioPlayer] in
audioPlayer?.stop()
}
}
// MARK: - Config
enum NavItem: Equatable {
case cancel
case save
}
public enum Section: SessionTableSection {
case content
}
// MARK: - Navigation
lazy var leftNavItems: AnyPublisher<[SessionNavItem<NavItem>], Never> = [
SessionNavItem(
id: .cancel,
systemItem: .cancel,
accessibilityIdentifier: "Cancel button"
) { [weak self] in self?.dismissScreen() }
]
lazy var rightNavItems: AnyPublisher<[SessionNavItem<NavItem>], Never> = currentSelection
.removeDuplicates()
.map { [originalSelection] currentSelection in (originalSelection != currentSelection) }
.map { isChanged in
guard isChanged else { return [] }
return [
SessionNavItem(
id: .save,
systemItem: .save,
accessibilityIdentifier: "Save button"
) { [weak self] in
self?.saveChanges()
self?.dismissScreen()
}
]
}
.eraseToAnyPublisher()
// MARK: - Content
let title: String = "notificationsSound".localized()
lazy var observation: TargetObservation = ObservationBuilderOld
.subject(currentSelection)
.map { [weak self, dependencies] selectedSound in
return [
SectionModel(
model: .content,
elements: Preferences.Sound.notificationSounds
.map { sound in
SessionCell.Info(
id: sound,
canReuseCell: true,
title: {
guard sound != .note else {
return "\(sound.displayName) (default)"
}
return sound.displayName
}(),
trailingAccessory: .radio(
isSelected: (selectedSound == sound)
),
onTap: {
self?.currentSelection.send(sound)
self?.audioPlayer?.stop() // Stop the old sound immediately
// Play the sound (to prevent UI lag we dispatch after a short delay)
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {
self?.audioPlayer = Preferences.Sound.audioPlayer(
for: sound,
behavior: .playback,
using: dependencies
)
self?.audioPlayer?.isLooping = false
self?.audioPlayer?.play()
}
}
)
}
)
]
}
// MARK: - Functions
private func saveChanges() {
dependencies.setAsync(.defaultNotificationSound, currentSelection.value)
}
}