Hello,
I’m learning RxSwift and I’m trying to make as many changes as I can just for learning to work with this awesome library. I added a property to TaskItem called type as a string. I create everything to show this new property on QuickTodo application, the problem is don’t know how to save the new data when the user taps Create item. For now I’m saving the title on the type property. I have the following code:
class TaskItem: Object {
@objc dynamic var uid: Int = 0
**@objc dynamic var title: String = ""**
@objc dynamic var type: String = ""
@objc dynamic var added: Date = Date()
@objc dynamic var checked: Date? = nil
override class func primaryKey() -> String? {
return "uid"
}
}
struct TasksViewModel {
let sceneCoordinator: SceneCoordinatorType
let taskService: TaskServiceType
…
func onUpdate(task: TaskItem) -> Action<String, Void> {
return Action { **newTitle** in
return self.taskService.update(task: task, title: newTitle, **type: newTitle**).map { _ in }
}
}
struct EditTaskViewModel {
let itemTitle: String
**let itemType: String**
let onUpdate: Action<String, Void>
let onCancel: CocoaAction!
let disposeBag = DisposeBag()
...
And in the EditTaskViewController class I have a Text Field called typeTextField.
class EditTaskViewController: UIViewController, BindableType {
@IBOutlet var titleView: UITextView!
**@IBOutlet weak var typeTextField: UITextField!**
@IBOutlet var okButton: UIBarButtonItem!
@IBOutlet var cancelButton: UIBarButtonItem!
var viewModel: EditTaskViewModel!
func bindViewModel() {
titleView.text = viewModel.itemTitle
typeTextField.text = viewModel.itemType
cancelButton.rx.action = viewModel.onCancel
okButton.rx.tap
.withLatestFrom(titleView.rx.text.orEmpty)
.subscribe(viewModel.onUpdate.inputs)
.disposed(by: self.rx.disposeBag)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
titleView.becomeFirstResponder()
}
}
What I don’t know how send more data when the user taps the Ok button in the following code:
okButton.rx.tap
.withLatestFrom(titleView.rx.text.orEmpty)
.subscribe(viewModel.onUpdate.inputs)
.disposed(by: self.rx.disposeBag)
How to tell the onUpdate Action that receive the titleView and typeTextField data?
I did it updating the TaskItem directly in the EditTaskViewController, but I don’t want to do that, I want the controller doesn’t know anything about the model, I want to send data back through viewModel object.
Thank you very much.