Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. struct Contact {
  2. //holds whichever properties you need to create a CNContact
  3. }
  4.  
  5. protocol ContactsHolder {
  6. func save(contact: Contact)
  7. func add(contact: Contact)
  8. func delete(contact: Contact)
  9. func update(contact: Contact)
  10. //Maybe more methods, the important thing is that you abstract yourself away from CNContactStore and other Contact kit classes
  11. }
  12.  
  13. enum ContactsUpdateMethod {
  14. case save(Contact)
  15. case add(Contact)
  16. case delete(Contact)
  17. case update(Contact)
  18. }
  19.  
  20. protocol ContactsHolder {
  21. func execute(_ method: ContactsUpdateMethod)
  22. }
  23.  
  24. class CNContactsHolder: ContactsHolder {
  25. func save(contact: Contact) {
  26. //1. create a `CNContact` from your `Contact`
  27. //2. create a saveRequest
  28. //3. execute: CNContactStore().execute(saveRequest)
  29. }
  30.  
  31. ....
  32. }
  33.  
  34. let contactsHolder: ContactsHolder
  35.  
  36. init(contactsHolder: ContactsHolder = CNContactsHolder()) {
  37. self.contactsHolder = contactsHolder
  38. }
  39.  
  40. let contactsHolder: ContactsHolder
  41.  
  42. var contactsHolder: ContactsHolder = CNContactsHolder()
  43.  
  44. struct MockContactsHolder: ContactsHolder {
  45. var saveWasCalled = false
  46. func save(contact: Contact) {
  47. saveWasCalled = true
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement