Skip to content

Commit

Permalink
chore: add closeHelpHub function
Browse files Browse the repository at this point in the history
  • Loading branch information
Cavallando committed Aug 15, 2024
1 parent e5df00c commit f270cb8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class MainActivity : ComponentActivity() {
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
Button(
onClick = {
CommandBar.openHelpHub( (this@MainActivity), CommandBarOptions("ORG_ID"))
CommandBar.openHelpHub( (this@MainActivity), CommandBarOptions("ORG_ID"), onFallbackAction = {
println("Received fallback action")
CommandBar.closeHelpHub()
})
},
modifier = Modifier.fillMaxSize().wrapContentSize(Alignment.Center)
) {
Expand Down
11 changes: 9 additions & 2 deletions commandbar/src/main/java/com/commandbar/android/CommandBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ package com.commandbar.android
import android.content.Context

object CommandBar {
private var currentHelpHubWebView: HelpHubWebView? = null

fun openHelpHub(context: Context, options: CommandBarOptions, articleId: Int? = null, onFallbackAction: FallbackActionCallback? = null) {
val webView = HelpHubWebView(context, options, articleId, onFallbackAction)
webView.openBottomSheetDialog()
currentHelpHubWebView = HelpHubWebView(context, options, articleId, onFallbackAction)
currentHelpHubWebView?.openBottomSheetDialog()
}

fun closeHelpHub() {
currentHelpHubWebView?.closeBottomSheetDialog()
currentHelpHubWebView = null
}
}
25 changes: 17 additions & 8 deletions commandbar/src/main/java/com/commandbar/android/HelpHubWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.commandbar.R
import com.google.android.material.bottomsheet.BottomSheetDialog
import org.json.JSONObject
import java.lang.Compiler.command


typealias FallbackActionCallback = ((action: Map<String, Any>) -> Unit)
Expand All @@ -32,6 +31,7 @@ fun Context.pxToDp(px: Int): Float {
class HelpHubWebView(context: Context, options: CommandBarOptions? = null, articleId: Int? = null, onFallbackAction: FallbackActionCallback? = null) : WebView(context) {
private lateinit var options: CommandBarOptions;
private lateinit var onFallbackAction: FallbackActionCallback
private var bottomSheetDialog: BottomSheetDialog? = null
private var articleId: Int?

init {
Expand Down Expand Up @@ -110,6 +110,10 @@ class HelpHubWebView(context: Context, options: CommandBarOptions? = null, artic
ViewGroup.LayoutParams.MATCH_PARENT
)

if (options.launchCode == "local") {
settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
}


webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
Expand Down Expand Up @@ -141,30 +145,35 @@ class HelpHubWebView(context: Context, options: CommandBarOptions? = null, artic

fun openBottomSheetDialog() {
// Create the BottomSheetDialog
var dialog = BottomSheetDialog(context, R.style.HelpHubBottomSheet)
bottomSheetDialog = BottomSheetDialog(context, R.style.HelpHubBottomSheet)
val coordinatorLayout = CoordinatorLayout(context).apply {
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
addView(this@HelpHubWebView)
}

dialog.setContentView(coordinatorLayout)
bottomSheetDialog?.setContentView(coordinatorLayout)

// Adjust the height of the dialog to match the screen height
val windowHeight = Resources.getSystem().displayMetrics.heightPixels
val sheetHeight = windowHeight - context.dpToPx(40)
dialog.behavior.peekHeight = sheetHeight
bottomSheetDialog?.behavior?.peekHeight = sheetHeight
this.layoutParams.height = sheetHeight
dialog.behavior.maxHeight = sheetHeight
bottomSheetDialog?.behavior?.maxHeight = sheetHeight

val bottomSheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet);
val bottomSheet = bottomSheetDialog?.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet);
// Show the dialog using the post method to wait for the view to be fully measured and laid out
dialog.show()
bottomSheetDialog?.show()

if (bottomSheet != null) {
bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
}
}

fun closeBottomSheetDialog() {
bottomSheetDialog?.dismiss()
bottomSheetDialog = null
this.destroy()
}

companion object {

Expand Down

0 comments on commit f270cb8

Please sign in to comment.