Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to handler with custom alert action #21

Open
amasahara opened this issue Jul 25, 2019 · 3 comments
Open

how to handler with custom alert action #21

amasahara opened this issue Jul 25, 2019 · 3 comments

Comments

@amasahara
Copy link

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

@thomasnield
Copy link
Owner

Did you actually subscribe to it or connect it?

@amasahara
Copy link
Author

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

@amasahara
Copy link
Author

amasahara commented Jul 30, 2019

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()`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants