// // SoundSelectionView.swift // LeCountdown // // Created by Laurent Morvillier on 08/02/2023. // import SwiftUI struct PlaylistsView: View { @EnvironmentObject var model: TimerModel var body: some View { Form { ForEach(Playlist.allCases) { playlist in PlaylistSectionView(playlist: playlist) .environmentObject(self.model) } } } } struct PlaylistSectionView: View { @EnvironmentObject var model: TimerModel var playlist: Playlist var body: some View { Section { let sounds = SoundCatalog.main.sounds(for: self.playlist) List(sounds) { sound in ToggleRow(item: sound, selected: self.model.binding(sound: sound)) { selected in self.model.selectSound(sound, selected: selected) } } } header: { ToggleRow(item: self.playlist, selected: self.model.binding(playlist: playlist)) { selected in self.model.selectPlaylist(self.playlist, selected: selected) } } } } struct ToggleRow: View { var item: T @Binding var selected: Bool var handleSelection: (Bool) -> () var body: some View { Toggle(item.localizedString, isOn: $selected) .onChange(of: self.selected, perform: handleSelection) } } //struct PlaylistRow: View { // var playlist: Playlist // @State var selected: Bool // var handleSelection: (Bool) -> () // // var body: some View { // Toggle(playlist.localizedString, isOn: $selected) // .onChange(of: selected, perform: handleSelection) // } //} // //struct SoundRow: View { // var sound: Sound // @State var selected: Bool // var handleSelection: (Bool) -> () // // var body: some View { // Toggle(sound.localizedString, isOn: $selected) // .onChange(of: selected, perform: handleSelection) // } //} struct SoundSelectionView: View { var body: some View { Form { // PlaylistsView() // ForEach($playlistBinding, id: \.id) { $ps in // // // Section { // ForEach(ps.playlist.sounds) { sound in // // SoundRow(sound: sound, selected: ps.sounds.contains(sound)) { selected in // if selected { // ps.sounds.append(sound) // } else { // ps.sounds.removeAll(where: { $0 == sound }) // } // } // } // } header: { // Toggle(ps.playlist.localizedString, isOn: $ps.selected) // } // }.navigationTitle("Sounds") } } } 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(SoundHolderPlaceholder()) } } struct PlaylistSectionView_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 { PlaylistSectionView(playlist: .stephanBodzin) .environmentObject(SoundHolderPlaceholder()) } } }