parent
9fda00f4df
commit
de9d14ae8d
@ -0,0 +1,84 @@ |
||||
// |
||||
// SoundHolder.swift |
||||
// LeCountdown |
||||
// |
||||
// Created by Laurent Morvillier on 09/02/2023. |
||||
// |
||||
|
||||
import Foundation |
||||
|
||||
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<Playlist> = [] |
||||
@Published var sounds: Set<Sound> = [] |
||||
|
||||
// MARK: - SoundHolder |
||||
|
||||
func selectSound(_ sound: Sound, selected: Bool) { |
||||
print("selectSound") |
||||
|
||||
if selected { |
||||
self.sounds.insert(sound) |
||||
} else { |
||||
self.sounds.remove(sound) |
||||
} |
||||
|
||||
// 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) |
||||
} else { |
||||
self.playlists.remove(playlist) |
||||
} |
||||
|
||||
} |
||||
|
||||
func selectPlaylist(_ playlist: Playlist, selected: Bool) { |
||||
print("selectPlaylist") |
||||
|
||||
if selected { |
||||
self.playlists.insert(playlist) |
||||
} else { |
||||
self.playlists.remove(playlist) |
||||
} |
||||
|
||||
let sounds: [Sound] = SoundCatalog.main.sounds(for: playlist) |
||||
self.sounds.formSymmetricDifference(sounds) |
||||
|
||||
print("sounds = \(self.sounds.count)") |
||||
} |
||||
|
||||
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 |
||||
} |
||||
} |
||||
Loading…
Reference in new issue