Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active January 5, 2018 13:44
Show Gist options
  • Save vialyx/aecc301449b6d3b32fe928fa4d0fb006 to your computer and use it in GitHub Desktop.
Save vialyx/aecc301449b6d3b32fe928fa4d0fb006 to your computer and use it in GitHub Desktop.
protocol UserType {
var id: UInt { get set }
var name: String { get set }
}
struct User: UserType {
var id: UInt
var name: String
init(id: UInt, name: String) {
self.id = id
self.name = name
}
}
protocol Storage {
func add(_ user: UserType)
func delete(_ user: UserType)
}
final class RealmStorage: Storage {
func add(_ user: UserType) {}
func delete(_ user: UserType) {}
}
final class KeychainStorage: Storage {
func add(_ user: UserType) {}
func delete(_ user: UserType) {}
}
protocol UsersProtocol {
func didLoad(_ users: [UserType])
func didRemove(_ user: UserType)
}
final class UsersInteractor: UsersProtocol {
let storage: Storage
init(storage: Storage) {
self.storage = storage
}
func didLoad(_ users: [UserType]) {
users.forEach { storage.add($0) }
}
func didRemove(_ user: UserType) {
storage.delete(user)
}
}
let author = User(id: 999, name: "Maxim Vialykh")
let guest = User(id: 1, name: "Guest")
let users = [author, guest]
let usersInteractor = UsersInteractor(storage: RealmStorage())
usersInteractor.didLoad(users)
let spyInteractor = UsersInteractor(storage: KeychainStorage())
spyInteractor.didRemove(guest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment