Last active
August 8, 2023 20:18
-
-
Save ingconti/49497419e5edd84a5f3be52397c76eb4 to your computer and use it in GitHub Desktop.
list with check and actions, na navigations
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 | |
// aaaa | |
// | |
// Created by ing.conti on 08/08/23. | |
// | |
// | |
// ContentView.swift | |
// ListdemoWithSelection | |
// | |
// Created by ing.conti on 21/01/21. | |
import SwiftUI | |
struct ResultView: View { | |
var choice: String | |
var body: some View { | |
Text("You eat \(choice)") | |
} | |
} | |
typealias ID = String | |
typealias IDSet = Set<ID> | |
enum ActionType : Int { | |
case Select | |
case Smile | |
} | |
typealias BtnCallBack = ( (String, ActionType)->() )? | |
struct CellView : View{ | |
var id: ID // for now the same as text.. | |
var text: String | |
var checked: Bool | |
var callback: BtnCallBack | |
var body: some View { | |
let checkStr = checked ? "checkmark.circle" : "circle" | |
NavigationLink(destination: ResultView(choice: id)) { | |
HStack { | |
Image(systemName: checkStr).frame(width: 20) | |
.onTapGesture { | |
print("tapped on") | |
callback?(id, .Select) | |
} | |
Text(text).onTapGesture { | |
print("No access!") | |
} | |
Spacer() | |
Text("😍").onTapGesture { | |
print("smile!") | |
callback?("smile", .Smile) | |
} | |
} // HStack | |
} | |
} | |
} | |
// navigation hint: | |
// https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui | |
struct ListDemo: View { | |
@State var items = ["Pizza", "Spaghetti", "Caviar"] | |
@State var selection = IDSet() | |
@State var tapped = "" | |
var body: some View { | |
let callBack = { (id: String, action: ActionType) in | |
print(id) | |
if action == .Select{ | |
if selection.contains(id) { | |
selection.remove(id) | |
} | |
else{ | |
selection.insert(id) | |
} | |
print(selection) | |
} | |
} // action | |
NavigationView { | |
VStack { | |
List(items, id: \.self) { (item : String) in | |
let checked = selection.contains(item) | |
CellView(id: item, text: item, | |
checked: checked, | |
callback: callBack) | |
} | |
//.listStyle(GroupedListStyle()) | |
} | |
#if os(iOS) | |
.navigationBarTitle(Text("-"), displayMode: .inline) | |
.navigationBarHidden(true) | |
#endif | |
}// NavigationView | |
} | |
} | |
struct ContentView: View { | |
var body: some View { | |
ListDemo() | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment