Skip to content

Instantly share code, notes, and snippets.

@GeekTree0101
Last active July 12, 2019 15:06
Show Gist options
  • Save GeekTree0101/130247251949810a131e205dc15cffc7 to your computer and use it in GitHub Desktop.
Save GeekTree0101/130247251949810a131e205dc15cffc7 to your computer and use it in GitHub Desktop.
Model - View - Intention (MVI)
struct User {
var id: String
var username: String
}
extension User: ServiceLogics {
static func loadUser(id: String) -> Promise<User> { ... }
static func loadUserFromDB(id: String) -> Promise<User> { ... }
}
protocol UserListCellIntentLogics {
func didTapUser()
}
class UserListCellIntent: UserListCellIntentLogics {
weak var view: UserListCellNodeDisplayLogics?
weak var repository: UserListRepository?
func didTapUser() {
let user = User.loadUser(id: id)
user.then {
UserListCellViewState(user: $0)
}
.done {
view?.state($0)
}
user.then {
repository.replace(user: $0)
}
}
}
protocol UserListCellNodeDisplayLogics {
func state(viewState: UserListCellViewState)
}
class UserListCellNode: ASCellNode {
var usernameTextNode = ASTextNode()
var intent: UserListCellIntentLogics = UserListCellIntent()
override init() {
super.init()
intent.view = self
}
func state(viewState: UserListCellViewState) {
usernameTextNode.attributedText = viewState.username.attributeText()
}
@objc func didTapUser() {
self.intent.didTapUser()
}
}
class UserListRepository {
var userSequence: [User] = []
func append(user: User) { ... }
func delete(id: String) { ... }
func append(users: [User]) { ... }
func replace(user: User) { ... }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment