-
-
Save productinfo/80bcbb5f1b5d5af607e5e6cc2ad01869 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
import SwiftUI | |
import Combine | |
class MyDatabase: BindableObject { | |
let didChange = PassthroughSubject<MyDatabase, Never>() | |
var contacts: [Contact] = [ | |
Contact(id: 1, name: "Anna"), Contact(id: 2, name: "Beto"), | |
Contact(id: 3, name: "Jack"), Contact(id: 4, name: "Sam") | |
] { | |
didSet { | |
didChange.send(self) | |
} | |
} | |
struct Contact { | |
var id: Int | |
var name: String | |
} | |
} | |
struct ContactsList: View { | |
@EnvironmentObject private var database: MyDatabase | |
var body: some View { | |
NavigationView { | |
List($database.contacts.identified(by: \.value.id)) { contact in | |
NavigationButton(destination: ContactDetail(contact: contact)) { | |
Text(verbatim: contact.value.name) | |
} | |
} | |
.navigationBarTitle(Text("Contacts")) | |
} | |
} | |
} | |
struct ContactDetail: View { | |
@Binding var contact: MyDatabase.Contact | |
var body: some View { | |
VStack { | |
TextField($contact[\.name]) | |
.textFieldStyle(.roundedBorder) | |
.font(.title) | |
.padding() | |
Spacer() | |
} | |
.navigationBarTitle(Text("Edit"), displayMode: .inline) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment