Skip to content

Commit f270cb8

Browse files
committed
chore: add closeHelpHub function
1 parent e5df00c commit f270cb8

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

app/src/main/java/com/commandbarexample/mainactivity/MainActivity.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ class MainActivity : ComponentActivity() {
2424
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
2525
Button(
2626
onClick = {
27-
CommandBar.openHelpHub( (this@MainActivity), CommandBarOptions("ORG_ID"))
27+
CommandBar.openHelpHub( (this@MainActivity), CommandBarOptions("ORG_ID"), onFallbackAction = {
28+
println("Received fallback action")
29+
CommandBar.closeHelpHub()
30+
})
2831
},
2932
modifier = Modifier.fillMaxSize().wrapContentSize(Alignment.Center)
3033
) {

commandbar/src/main/java/com/commandbar/android/CommandBar.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@ package com.commandbar.android
33
import android.content.Context
44

55
object CommandBar {
6+
private var currentHelpHubWebView: HelpHubWebView? = null
7+
68
fun openHelpHub(context: Context, options: CommandBarOptions, articleId: Int? = null, onFallbackAction: FallbackActionCallback? = null) {
7-
val webView = HelpHubWebView(context, options, articleId, onFallbackAction)
8-
webView.openBottomSheetDialog()
9+
currentHelpHubWebView = HelpHubWebView(context, options, articleId, onFallbackAction)
10+
currentHelpHubWebView?.openBottomSheetDialog()
11+
}
12+
13+
fun closeHelpHub() {
14+
currentHelpHubWebView?.closeBottomSheetDialog()
15+
currentHelpHubWebView = null
916
}
1017
}

commandbar/src/main/java/com/commandbar/android/HelpHubWebView.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import android.graphics.Color
77
import android.view.View
88
import android.view.ViewGroup
99
import android.webkit.JavascriptInterface
10-
import android.webkit.ValueCallback
1110
import android.webkit.WebChromeClient
1211
import android.webkit.WebResourceRequest
12+
import android.webkit.WebSettings
1313
import android.webkit.WebView
1414
import android.webkit.WebViewClient
1515
import androidx.coordinatorlayout.widget.CoordinatorLayout
1616
import com.commandbar.R
1717
import com.google.android.material.bottomsheet.BottomSheetDialog
1818
import org.json.JSONObject
19-
import java.lang.Compiler.command
2019

2120

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

3737
init {
@@ -110,6 +110,10 @@ class HelpHubWebView(context: Context, options: CommandBarOptions? = null, artic
110110
ViewGroup.LayoutParams.MATCH_PARENT
111111
)
112112

113+
if (options.launchCode == "local") {
114+
settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
115+
}
116+
113117

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

142146
fun openBottomSheetDialog() {
143147
// Create the BottomSheetDialog
144-
var dialog = BottomSheetDialog(context, R.style.HelpHubBottomSheet)
148+
bottomSheetDialog = BottomSheetDialog(context, R.style.HelpHubBottomSheet)
145149
val coordinatorLayout = CoordinatorLayout(context).apply {
146150
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
147151
addView(this@HelpHubWebView)
148152
}
149153

150-
dialog.setContentView(coordinatorLayout)
154+
bottomSheetDialog?.setContentView(coordinatorLayout)
151155

152156
// Adjust the height of the dialog to match the screen height
153157
val windowHeight = Resources.getSystem().displayMetrics.heightPixels
154158
val sheetHeight = windowHeight - context.dpToPx(40)
155-
dialog.behavior.peekHeight = sheetHeight
159+
bottomSheetDialog?.behavior?.peekHeight = sheetHeight
156160
this.layoutParams.height = sheetHeight
157-
dialog.behavior.maxHeight = sheetHeight
161+
bottomSheetDialog?.behavior?.maxHeight = sheetHeight
158162

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

163167
if (bottomSheet != null) {
164168
bottomSheet.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
165169
}
166170
}
167171

172+
fun closeBottomSheetDialog() {
173+
bottomSheetDialog?.dismiss()
174+
bottomSheetDialog = null
175+
this.destroy()
176+
}
168177

169178
companion object {
170179

0 commit comments

Comments
 (0)