Created
June 4, 2022 05:40
-
-
Save renanzinho/0eba66be33ca539be0a210bbc7be35f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// teste | |
// | |
// Created by Renan Lins on 6/4/22. | |
// | |
import SwiftUI | |
class ViewModel: ObservableObject { | |
@Published var selectedLangs: [String] = [] | |
let langList = ["fr", "en", "pt"] | |
} | |
struct ContentView: View { | |
let viewModel = ViewModel() | |
var body: some View { | |
VStack { | |
List(viewModel.langList, id:\.self) {lang in | |
TestToggle(title: lang) { isOn in | |
if isOn { | |
self.viewModel.selectedLangs.append(lang) | |
} else { | |
self.viewModel.selectedLangs = self.viewModel.selectedLangs.filter { $0 != lang } | |
} | |
} | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} | |
struct TestToggle: View { | |
let title: String | |
let completion: (Bool) -> Void | |
@State private var isOn: Bool = false | |
var body: some View { | |
Toggle(self.title, isOn: $isOn) | |
.onChange(of: self.isOn) { newValue in | |
completion(newValue) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment