This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class BindingTextField : UITextField { | |
var textChanged :(String) -> () = { _ in } | |
func bind(callback :@escaping (String) -> ()) { | |
self.textChanged = callback |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBOutlet weak var titleTextField :BindingTextField! { | |
didSet { | |
titleTextField.bind { self.viewModel.title.value = $0 } | |
} | |
} | |
@IBOutlet weak var descriptionTextField :BindingTextField! { | |
didSet { | |
descriptionTextField.bind { self.viewModel.description.value = $0 } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ChangePasswordViewModel { | |
var newPassword = Dynamic<String>("") | |
var confirmPassword = Dynamic<String>("") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ChangePasswordTableViewController : UITableViewController { | |
private var viewModel :ChangePasswordViewModel = ChangePasswordViewModel() | |
@IBOutlet weak var newPasswordTextField :BindingTextField! { | |
didSet { | |
self.newPasswordTextField.bind { self.viewModel.newPassword.value = $0 } | |
} | |
} | |
@IBOutlet weak var confirmPasswordTextField :BindingTextField! { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func save() { | |
var errors = [String]() | |
if (viewModel.newPassword.value?.count)! < 8 { | |
errors.append("Password should be at least 8 characters long") | |
} | |
if viewModel.newPassword.value != viewModel.confirmPassword.value { | |
errors.append("Password is not matching") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BrokenRule { | |
var propertyName :String | |
var message :String | |
} | |
protocol ViewModel { | |
var brokenRules :[BrokenRule] { get set} | |
var isValid :Bool { mutating get } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ChangePasswordViewModel : ViewModel { | |
var brokenRules: [BrokenRule] = [BrokenRule]() | |
var newPassword = Dynamic<String>("") | |
var confirmPassword = Dynamic<String>("") | |
var isValid :Bool { | |
mutating get { | |
self.brokenRules = [BrokenRule]() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBAction func save() { | |
if self.viewModel.isValid { | |
// update the password | |
print("update the user's password") | |
} else { | |
// show errors | |
print(self.viewModel.brokenRules) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ChangePasswordViewModel : ViewModel { | |
@Required(message: "New Password is required", minLength:8) | |
var newPassword = Dynamic<String>("") | |
@Required(message: "Confirm Password is required", minLength: 8) | |
var confirmPassword = Dynamic<String>("") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ArticleListViewModel { | |
private(set) var webservice :Webservice! | |
var title :String? = "Articles" | |
var articles :[ArticleViewModel] = [ArticleViewModel]() | |
init(articles :[ArticleViewModel]) { | |
self.articles = articles | |
} |