Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import SwiftUI
  2. import Combine
  3.  
  4. class MyDatabase: BindableObject {
  5. let didChange = PassthroughSubject<MyDatabase, Never>()
  6.  
  7. var contacts: [Contact] = [
  8. Contact(id: 1, name: "Anna"), Contact(id: 2, name: "Beto"),
  9. Contact(id: 3, name: "Jack"), Contact(id: 4, name: "Sam")
  10. ] {
  11. didSet {
  12. didChange.send(self)
  13. }
  14. }
  15.  
  16. struct Contact {
  17. var id: Int
  18. var name: String
  19. }
  20. }
  21.  
  22. struct ContactsList: View {
  23. @EnvironmentObject private var database: MyDatabase
  24.  
  25. var body: some View {
  26. NavigationView {
  27. List($database.contacts.identified(by: \.value.id)) { contact in
  28. NavigationButton(destination: ContactDetail(contact: contact)) {
  29. Text(verbatim: contact.value.name)
  30. }
  31. }
  32. .navigationBarTitle(Text("Contacts"))
  33. }
  34. }
  35. }
  36.  
  37. struct ContactDetail: View {
  38. @Binding var contact: MyDatabase.Contact
  39.  
  40. var body: some View {
  41. VStack {
  42. TextField($contact[\.name])
  43. .textFieldStyle(.roundedBorder)
  44. .font(.title)
  45. .padding()
  46. Spacer()
  47. }
  48. .navigationBarTitle(Text("Edit"), displayMode: .inline)
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement