You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LeCountdown/LeCountdown/Views/Reusable/SoundSelectionView.swift

195 lines
5.1 KiB

//
// 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.selectable) { playlist in
PlaylistSectionView(playlist: playlist)
.environmentObject(self.model)
}
}
.navigationTitle("Sounds")
}
}
struct PlaylistSectionView: View {
@EnvironmentObject var model: TimerModel
var playlist: Playlist
var body: some View {
Section {
let sounds = SoundCatalog.main.sounds(for: self.playlist)
ForEach(sounds) { sound in
HStack {
HStack {
Image(systemName: "play.circle")
.foregroundColor(Color.accentColor)
Text(sound.localizedString)
}.onTapGesture {
self._playSound(sound)
}
Spacer()
RightAlignToggleRow(item: sound, selected: self.model.binding(sound: sound), keyPath: \.formattedDuration) { selected in
self.model.selectSound(sound, selected: selected)
}.frame(width: 120.0)
.foregroundColor(.gray)
.font(.caption)
}
}
} header: {
ToggleRow(item: self.playlist, selected: self.model.binding(playlist: playlist)) { selected in
self.model.selectPlaylist(self.playlist, selected: selected)
}
}
}
fileprivate func _playSound(_ sound: Sound) {
Conductor.maestro.playSound(sound)
}
}
struct ToggleRow<T : Localized>: View {
var item: T
@Binding var selected: Bool
var handleSelection: (Bool) -> ()
var body: some View {
Toggle(self.item.localizedString, isOn: $selected)
.onChange(of: self.selected, perform: handleSelection)
}
}
struct RightAlignToggleRow<T : Localized>: View {
var item: T
@Binding var selected: Bool
var keyPath: KeyPath<T, String>
var handleSelection: (Bool) -> ()
var body: some View {
Toggle(isOn: $selected) {
HStack {
Spacer()
Text(self.item[keyPath: keyPath])
}
}.onChange(of: self.selected, perform: handleSelection)
// if let keyPath {
//
//
// Toggle(item[keyPath: keyPath], isOn: $selected)
// .onChange(of: self.selected, perform: handleSelection)
// } else {
// Toggle(item.localizedString, isOn: $selected)
// .onChange(of: self.selected, perform: handleSelection)
// }
}
}
struct ImageToggleRow<T : Localized>: View {
var item: T
@Binding var selected: Bool
var handleSelection: (Bool) -> ()
var body: some View {
HStack {
Image(systemName: "play.circle")
.foregroundColor(Color.accentColor)
ToggleRow(item: item, selected: $selected, handleSelection: 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 var previews: some View {
PlaylistsView()
.environmentObject(TimerModel())
}
}
struct PlaylistSectionView_Previews: PreviewProvider {
static var previews: some View {
Form {
PlaylistSectionView(playlist: .stephanBodzin)
.environmentObject(TimerModel())
}
}
}