From 73b1cf09c03e07a7fa46dd98e26c8bad436ec28c Mon Sep 17 00:00:00 2001 From: Laurent Date: Fri, 10 Feb 2023 11:27:56 +0100 Subject: [PATCH] fix and cleanup --- LeCountdown/AppDelegate.swift | 4 +- .../Views/Components/SoundImageFormView.swift | 13 ++--- .../Views/Components/SoundSelectionView.swift | 7 --- LeCountdown/Views/Components/TimerModel.swift | 55 ++++++++----------- .../Views/Countdown/CountdownFormView.swift | 9 +-- LeCountdown/Views/DialView.swift | 3 + .../Views/Stopwatch/StopwatchFormView.swift | 2 +- 7 files changed, 34 insertions(+), 59 deletions(-) diff --git a/LeCountdown/AppDelegate.swift b/LeCountdown/AppDelegate.swift index fb87336..23f0e9c 100644 --- a/LeCountdown/AppDelegate.swift +++ b/LeCountdown/AppDelegate.swift @@ -16,7 +16,7 @@ class AppDelegate : NSObject, UIApplicationDelegate { Conductor.maestro.cleanupCountdowns() return true } - + } extension AppDelegate: UNUserNotificationCenterDelegate { @@ -31,7 +31,7 @@ extension AppDelegate: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print("willPresent notification") - completionHandler([.banner]) +// completionHandler([.banner]) let timerId = self._timerId(notificationId: notification.request.identifier) Conductor.maestro.notifyUser(countdownId: timerId) diff --git a/LeCountdown/Views/Components/SoundImageFormView.swift b/LeCountdown/Views/Components/SoundImageFormView.swift index 47859ce..9263787 100644 --- a/LeCountdown/Views/Components/SoundImageFormView.swift +++ b/LeCountdown/Views/Components/SoundImageFormView.swift @@ -58,7 +58,9 @@ struct SoundImageFormView : View { } NavigationLink { - PlaylistsView().environmentObject(self.model) + NavigationStack { + PlaylistsView().environmentObject(self.model) + } } label: { HStack { Text("Sound") @@ -106,19 +108,12 @@ struct SoundImageFormView : View { struct SoundImageFormView_Previews: PreviewProvider { - static func soundBinding(sound: Sound) -> Binding { - return .constant(true) - } - static func playlistBinding(playlist: Playlist) -> Binding { - return .constant(true) - } - static var previews: some View { Form { SoundImageFormView( imageBinding: .constant(.pic1), repeatCountBinding: .constant(2)) - .environmentObject(SoundHolderPlaceholder()) + .environmentObject(TimerModel()) } } } diff --git a/LeCountdown/Views/Components/SoundSelectionView.swift b/LeCountdown/Views/Components/SoundSelectionView.swift index ffb3a5f..3093ac1 100644 --- a/LeCountdown/Views/Components/SoundSelectionView.swift +++ b/LeCountdown/Views/Components/SoundSelectionView.swift @@ -117,13 +117,6 @@ struct SoundSelectionView: View { struct PlaylistsView_Previews: PreviewProvider { - static func soundBinding(sound: Sound) -> Binding { - return .constant(true) - } - static func playlistBinding(playlist: Playlist) -> Binding { - return .constant(true) - } - static var previews: some View { PlaylistsView() .environmentObject(TimerModel()) diff --git a/LeCountdown/Views/Components/TimerModel.swift b/LeCountdown/Views/Components/TimerModel.swift index 526fa9a..e2d24b1 100644 --- a/LeCountdown/Views/Components/TimerModel.swift +++ b/LeCountdown/Views/Components/TimerModel.swift @@ -11,15 +11,16 @@ import SwiftUI protocol SoundHolder { func selectSound(_ sound: Sound, selected: Bool) func selectPlaylist(_ playlist: Playlist, selected: Bool) - - func isSelected(sound: Sound) -> Bool - func isSelected(playlist: Playlist) -> Bool } class TimerModel : ObservableObject, SoundHolder { @Published var playlists: Set = [] - @Published var sounds: Set = [] + @Published var sounds: Set = [] { + didSet { + self._selectPlaylists() + } + } var soundSelection: String { if !sounds.isEmpty { @@ -50,11 +51,9 @@ class TimerModel : ObservableObject, SoundHolder { }) } - // MARK: - SoundHolder func selectSound(_ sound: Sound, selected: Bool) { - print("selectSound") if selected { self.sounds.insert(sound) @@ -62,8 +61,18 @@ class TimerModel : ObservableObject, SoundHolder { self.sounds.remove(sound) } + self._togglePlaylist(sound.playlist) + } + + fileprivate func _selectPlaylists() { + let playlists: Set = Set(self.sounds.map { $0.playlist }) + for playlist in playlists { + self._togglePlaylist(playlist) + } + } + + fileprivate func _togglePlaylist(_ playlist: Playlist) { // toggle playlist if necessary - let playlist = sound.playlist let playlistSounds: [Sound] = SoundCatalog.main.sounds(for: playlist) if self.sounds.isSuperset(of: playlistSounds) { self.playlists.insert(playlist) @@ -74,7 +83,6 @@ class TimerModel : ObservableObject, SoundHolder { } func selectPlaylist(_ playlist: Playlist, selected: Bool) { - print("selectPlaylist") let sounds: [Sound] = SoundCatalog.main.sounds(for: playlist) if selected { @@ -87,31 +95,14 @@ class TimerModel : ObservableObject, SoundHolder { } } - print("sounds = \(self.sounds.count)") } - func isSelected(sound: Sound) -> Bool { - self.sounds.contains(sound) - } - - func isSelected(playlist: Playlist) -> Bool { - self.playlists.contains(playlist) - } +// func isSelected(sound: Sound) -> Bool { +// self.sounds.contains(sound) +// } +// +// func isSelected(playlist: Playlist) -> Bool { +// self.playlists.contains(playlist) +// } } - -class SoundHolderPlaceholder : ObservableObject, SoundHolder { - func selectSound(_ sound: Sound, selected: Bool) { - } - - func selectPlaylist(_ playlist: Playlist, selected: Bool) { - } - - func isSelected(sound: Sound) -> Bool { - return false - } - - func isSelected(playlist: Playlist) -> Bool { - return false - } -} diff --git a/LeCountdown/Views/Countdown/CountdownFormView.swift b/LeCountdown/Views/Countdown/CountdownFormView.swift index 8159112..9f535a6 100644 --- a/LeCountdown/Views/Countdown/CountdownFormView.swift +++ b/LeCountdown/Views/Countdown/CountdownFormView.swift @@ -51,13 +51,6 @@ struct CountdownFormView_Previews: PreviewProvider { @FocusState static var textFieldIsFocused: Bool - static func soundBinding(sound: Sound) -> Binding { - return .constant(true) - } - static func playlistBinding(playlist: Playlist) -> Binding { - return .constant(true) - } - static var previews: some View { CountdownFormView( secondsBinding: .constant(""), @@ -66,6 +59,6 @@ struct CountdownFormView_Previews: PreviewProvider { imageBinding: .constant(.pic3), repeatCountBinding: .constant(2), textFieldIsFocused: $textFieldIsFocused) - .environmentObject(SoundHolderPlaceholder()) + .environmentObject(TimerModel()) } } diff --git a/LeCountdown/Views/DialView.swift b/LeCountdown/Views/DialView.swift index cb4e26b..38ad5d7 100644 --- a/LeCountdown/Views/DialView.swift +++ b/LeCountdown/Views/DialView.swift @@ -79,10 +79,13 @@ struct DialView: View { switch timer { case let countdown as Countdown: CountdownEditView(countdown: countdown, isPresented: isPresented) + .environment(\.managedObjectContext, viewContext) case let alarm as Alarm: AlarmEditView(alarm: alarm, isPresented: isPresented) + .environment(\.managedObjectContext, viewContext) case let stopwatch as Stopwatch: StopwatchEditView(stopwatch: stopwatch, isPresented: isPresented) + .environment(\.managedObjectContext, viewContext) default: Text("missing edit view") } diff --git a/LeCountdown/Views/Stopwatch/StopwatchFormView.swift b/LeCountdown/Views/Stopwatch/StopwatchFormView.swift index be485e6..2b60d42 100644 --- a/LeCountdown/Views/Stopwatch/StopwatchFormView.swift +++ b/LeCountdown/Views/Stopwatch/StopwatchFormView.swift @@ -43,6 +43,6 @@ struct StopwatchFormView_Previews: PreviewProvider { imageBinding: .constant(.pic1), playSoundBinding: .constant(true), textFieldIsFocused: $textFieldIsFocused) - .environmentObject(SoundHolderPlaceholder()) + .environmentObject(TimerModel()) } }