Skip to content

Commit

Permalink
Merge pull request #32 from hotwired/better-web-dialogs
Browse files Browse the repository at this point in the history
Present better looking Material web dialogs
  • Loading branch information
jayohms authored Apr 25, 2024
2 parents 3dbb786 + b81d09b commit 9793132
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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>

0 comments on commit 9793132

Please sign in to comment.