Skip to content

Instantly share code, notes, and snippets.

@jackhl
Created June 7, 2019 00:03
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jackhl/632935a2e90e3796e38c2143d5dadc96 to your computer and use it in GitHub Desktop.
Save jackhl/632935a2e90e3796e38c2143d5dadc96 to your computer and use it in GitHub Desktop.
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