Skip to main content
Became Hot Network Question
The terminating "```" must be followed by a newline.
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
struct SoundsView: View {
    
    @EnvironmentObject var theme: ThemeManager
    
    //var soundManager = SoundManager()
    @StateObject var soundViewVM = SoundViewViewModel()
    
    var body: some View {
        List {
            ForEach(soundViewVM.sounds) { sound in
                Button {
                    //soundManager.playSound(name: sound.name)
                    soundViewVM.toggleItems(soundItem: sound)

                } label: {
                    HStack(alignment: .center, spacing: 2) {
                        Text(sound.displayName)
                        Spacer()
                        sound.isActive ? Circle().frame(width: 10).foregroundColor(theme.lightColor) : nil
                    }
                }
            }
        }
        .listStyle(.elliptical)
        .navigationTitle {
            HStack {
                Text("Sounds")
                Spacer()
            }
        }
    }
}
```
struct SoundsView: View {
    
    @EnvironmentObject var theme: ThemeManager
    
    //var soundManager = SoundManager()
    @StateObject var soundViewVM = SoundViewViewModel()
    
    var body: some View {
        List {
            ForEach(soundViewVM.sounds) { sound in
                Button {
                    //soundManager.playSound(name: sound.name)
                    soundViewVM.toggleItems(soundItem: sound)

                } label: {
                    HStack(alignment: .center, spacing: 2) {
                        Text(sound.displayName)
                        Spacer()
                        sound.isActive ? Circle().frame(width: 10).foregroundColor(theme.lightColor) : nil
                    }
                }
            }
        }
        .listStyle(.elliptical)
        .navigationTitle {
            HStack {
                Text("Sounds")
                Spacer()
            }
        }
    }
}
```
struct SoundsView: View {
    
    @EnvironmentObject var theme: ThemeManager
    
    //var soundManager = SoundManager()
    @StateObject var soundViewVM = SoundViewViewModel()
    
    var body: some View {
        List {
            ForEach(soundViewVM.sounds) { sound in
                Button {
                    //soundManager.playSound(name: sound.name)
                    soundViewVM.toggleItems(soundItem: sound)

                } label: {
                    HStack(alignment: .center, spacing: 2) {
                        Text(sound.displayName)
                        Spacer()
                        sound.isActive ? Circle().frame(width: 10).foregroundColor(theme.lightColor) : nil
                    }
                }
            }
        }
        .listStyle(.elliptical)
        .navigationTitle {
            HStack {
                Text("Sounds")
                Spacer()
            }
        }
    }
}
deleted 4 characters in body
Source Link
class SoundViewViewModel: ObservableObject {
    
    @Published var sounds: [SoundModel] = []
    
    init() {
        sounds.append(contentsOf: [
            SoundModel(name: "alarm1", displayName: "Alarm 1", isActive: true),
            SoundModel(name: "alarm2", displayName: "Alarm 2", isActive: false),
            SoundModel(name: "alarm3", displayName: "Alarm 3", isActive: false),
            SoundModel(name: "bird1", displayName: "Bird", isActive: false),
            SoundModel(name: "carhorn1", displayName: "Carhorn", isActive: false),
            SoundModel(name: "fireAlarm1", displayName: "Fire Alarm", isActive: false),
            SoundModel(name: "mellow", displayName: "Mellow", isActive: false),
            SoundModel(name: "meltdown1", displayName: "Meltdown", isActive: false),
            SoundModel(name: "schoolbel1", displayName: "Schoolbel", isActive: false),
            SoundModel(name: "siren1", displayName: "Siren", isActive: false),
            SoundModel(name: "ufo1", displayName: "Ufo", isActive: false),
        ]
        )
    }

    func toggleItems(soundItem: SoundModel) {

        if let index = sounds.firstIndex(where: { $0.id == soundItem.id } ) {
            if !soundItem.isActive {
                sounds[index] = soundItem.updateActiveStateToTrue()
                
            }
        }
        
        for sound in sounds {
            if let index = sounds.firstIndex(where: { $0.id == sound.id && $0.id != soundItem.id }) {
                if sounds[index].isActive {
                    sounds[index] = soundItemsound.updateActiveStateToFalse()
                }
            }
        }
    }
}
class SoundViewViewModel: ObservableObject {
    
    @Published var sounds: [SoundModel] = []
    
    init() {
        sounds.append(contentsOf: [
            SoundModel(name: "alarm1", displayName: "Alarm 1", isActive: true),
            SoundModel(name: "alarm2", displayName: "Alarm 2", isActive: false),
            SoundModel(name: "alarm3", displayName: "Alarm 3", isActive: false),
            SoundModel(name: "bird1", displayName: "Bird", isActive: false),
            SoundModel(name: "carhorn1", displayName: "Carhorn", isActive: false),
            SoundModel(name: "fireAlarm1", displayName: "Fire Alarm", isActive: false),
            SoundModel(name: "mellow", displayName: "Mellow", isActive: false),
            SoundModel(name: "meltdown1", displayName: "Meltdown", isActive: false),
            SoundModel(name: "schoolbel1", displayName: "Schoolbel", isActive: false),
            SoundModel(name: "siren1", displayName: "Siren", isActive: false),
            SoundModel(name: "ufo1", displayName: "Ufo", isActive: false),
        ]
        )
    }

    func toggleItems(soundItem: SoundModel) {

        if let index = sounds.firstIndex(where: { $0.id == soundItem.id } ) {
            if !soundItem.isActive {
                sounds[index] = soundItem.updateActiveStateToTrue()
                
            }
        }
        
        for sound in sounds {
            if let index = sounds.firstIndex(where: { $0.id == sound.id && $0.id != soundItem.id }) {
                if sounds[index].isActive {
                    sounds[index] = soundItem.updateActiveStateToFalse()
                }
            }
        }
    }
}
class SoundViewViewModel: ObservableObject {
    
    @Published var sounds: [SoundModel] = []
    
    init() {
        sounds.append(contentsOf: [
            SoundModel(name: "alarm1", displayName: "Alarm 1", isActive: true),
            SoundModel(name: "alarm2", displayName: "Alarm 2", isActive: false),
            SoundModel(name: "alarm3", displayName: "Alarm 3", isActive: false),
            SoundModel(name: "bird1", displayName: "Bird", isActive: false),
            SoundModel(name: "carhorn1", displayName: "Carhorn", isActive: false),
            SoundModel(name: "fireAlarm1", displayName: "Fire Alarm", isActive: false),
            SoundModel(name: "mellow", displayName: "Mellow", isActive: false),
            SoundModel(name: "meltdown1", displayName: "Meltdown", isActive: false),
            SoundModel(name: "schoolbel1", displayName: "Schoolbel", isActive: false),
            SoundModel(name: "siren1", displayName: "Siren", isActive: false),
            SoundModel(name: "ufo1", displayName: "Ufo", isActive: false),
        ]
        )
    }

    func toggleItems(soundItem: SoundModel) {

        if let index = sounds.firstIndex(where: { $0.id == soundItem.id } ) {
            if !soundItem.isActive {
                sounds[index] = soundItem.updateActiveStateToTrue()
                
            }
        }
        
        for sound in sounds {
            if let index = sounds.firstIndex(where: { $0.id == sound.id && $0.id != soundItem.id }) {
                if sounds[index].isActive {
                    sounds[index] = sound.updateActiveStateToFalse()
                }
            }
        }
    }
}
added 2 characters in body
Source Link
struct SoundsView: View {
    
    @EnvironmentObject var theme: ThemeManager
    
    //var soundManager = SoundManager()
    @StateObject var soundViewVM = SoundViewViewModel()
    
    var body: some View {
        List {
            ForEach(soundViewVM.sounds) { sound in
                Button {
                    //soundManager.playSound(name: sound.name)
                    soundDatasoundViewVM.toggleItems(soundItem: sound)

                } label: {
                    HStack(alignment: .center, spacing: 2) {
                        Text(sound.displayName)
                        Spacer()
                        sound.isActive ? Circle().frame(width: 10).foregroundColor(theme.lightColor) : nil
                    }
                }
            }
        }
        .listStyle(.elliptical)
        .navigationTitle {
            HStack {
                Text("Sounds")
                Spacer()
            }
        }
    }
}
```
struct SoundsView: View {
    
    @EnvironmentObject var theme: ThemeManager
    
    //var soundManager = SoundManager()
    @StateObject var soundViewVM = SoundViewViewModel()
    
    var body: some View {
        List {
            ForEach(soundViewVM.sounds) { sound in
                Button {
                    //soundManager.playSound(name: sound.name)
                    soundData.toggleItems(soundItem: sound)

                } label: {
                    HStack(alignment: .center, spacing: 2) {
                        Text(sound.displayName)
                        Spacer()
                        sound.isActive ? Circle().frame(width: 10).foregroundColor(theme.lightColor) : nil
                    }
                }
            }
        }
        .listStyle(.elliptical)
        .navigationTitle {
            HStack {
                Text("Sounds")
                Spacer()
            }
        }
    }
}
```
struct SoundsView: View {
    
    @EnvironmentObject var theme: ThemeManager
    
    //var soundManager = SoundManager()
    @StateObject var soundViewVM = SoundViewViewModel()
    
    var body: some View {
        List {
            ForEach(soundViewVM.sounds) { sound in
                Button {
                    //soundManager.playSound(name: sound.name)
                    soundViewVM.toggleItems(soundItem: sound)

                } label: {
                    HStack(alignment: .center, spacing: 2) {
                        Text(sound.displayName)
                        Spacer()
                        sound.isActive ? Circle().frame(width: 10).foregroundColor(theme.lightColor) : nil
                    }
                }
            }
        }
        .listStyle(.elliptical)
        .navigationTitle {
            HStack {
                Text("Sounds")
                Spacer()
            }
        }
    }
}
```
added 2 characters in body
Source Link
Loading
added 40 characters in body
Source Link
Loading
deleted 24 characters in body
Source Link
Loading
deleted 24 characters in body
Source Link
Loading
Source Link
Loading