RxKotlinFX
RxKotlinFX copied to clipboard
how to handler with custom alert action
i try to create an custom alert with 3 button like this `eventController.saveDanhMucDVKT .map { table.items } .flatMapSingle { lstDMDVKT -> val dialog = Alert(Alert.AlertType.CONFIRMATION, "Lựa chọn hình thức lưu dữ liệu", ButtonType("Save", ButtonBar.ButtonData.YES), ButtonType("Save and delete", ButtonBar.ButtonData.OK_DONE), ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE))
//save but not remove old data
dialog.toMaybe().filter { it == ButtonType.YES }
.map { lstDMDVKT }
.flatMapSingle {
danhMucDVKTController.createList(it)
}.toObservable().toSet()
//save and remove old data
dialog.toMaybe().filter { it == ButtonType.OK }
.map { lstDMDVKT }
.flatMapSingle {
danhMucDVKTController.delete()
danhMucDVKTController.createList(it)
}.toObservable().toSet()
}.publish()`
but seem it not save data to database. which way i wrong
Did you actually subscribe to it or connect it?
yes i has been do it same with your example, but when i debug, it not run into .flatMapSingle { danhMucDVKTController.createList(it) } and i don't know why
Here is my full code for alert dialog. but after i update new data into my database it always show dialog 2 times and when i restart this form it always show dialog again before run into form `//handler save data into database val saveData = eventController.saveDanhMucDVKT .map { table.items.toList() } .flatMapSingle { lstDMDVKT -> Alert(Alert.AlertType.CONFIRMATION, "Lựa chọn hình thức lưu dữ liệu", ButtonType("Thêm", ButtonBar.ButtonData.YES), ButtonType("Lưu và xóa", ButtonBar.ButtonData.NO), ButtonType("Bỏ qua", ButtonBar.ButtonData.CANCEL_CLOSE)) .toMaybe().toObservable() .switchMap { when { it.buttonData == ButtonBar.ButtonData.YES -> danhMucDVKTController.createList(lstDMDVKT.observable()).toObservable() it.buttonData == ButtonBar.ButtonData.NO -> { danhMucDVKTController.delete().subscribe() danhMucDVKTController.createList(lstDMDVKT.observable()).toObservable() } else -> Observable.empty() } }.toSet() }.publish() //publish() to prevent multiple subscriptions triggering alert multiple times
saveData.subscribe(eventController.savedDanhMucDVKT)
//add data into datase
eventController.savedDanhMucDVKT
.map { Unit }
.subscribe(eventController.refreshDanhMucDVKT)
saveData.connect()`