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

Present better looking Material web dialogs #32

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,53 @@ package dev.hotwire.core.turbo.views

import android.net.Uri
import android.os.Message
import android.webkit.JsResult
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dev.hotwire.core.R
import dev.hotwire.core.turbo.session.Session
import dev.hotwire.core.turbo.util.toJson
import dev.hotwire.core.turbo.visit.VisitOptions

open class TurboWebChromeClient(val session: Session) : WebChromeClient() {
override fun onJsAlert(view: WebView?, url: String?, message: String?, result: JsResult?): Boolean {
val context = view?.context ?: return false

MaterialAlertDialogBuilder(context)
.setMessage(message)
.setPositiveButton(context.getString(R.string.turbo_dialog_ok)) { dialog, _ ->
dialog.cancel()
result?.confirm()
}
.setCancelable(false)
.create()
.show()

return true
}

override fun onJsConfirm(view: WebView?, url: String?, message: String?, result: JsResult?): Boolean {
val context = view?.context ?: return false

MaterialAlertDialogBuilder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.turbo_dialog_cancel)) { dialog, _ ->
dialog.cancel()
result?.cancel()
}
.setPositiveButton(context.getString(R.string.turbo_dialog_ok)) { dialog, _ ->
dialog.cancel()
result?.confirm()
}
.setCancelable(false)
.create()
.show()

return true
}

override fun onShowFileChooser(
webView: WebView,
filePathCallback: ValueCallback<Array<Uri>>,
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<string name="turbo_error_message">Error loading page</string>
<string name="turbo_file_chooser_select">Select file</string>
<string name="turbo_file_chooser_select_multiple">Select file(s)</string>
<string name="turbo_dialog_ok">OK</string>
<string name="turbo_dialog_cancel">Cancel</string>
</resources>