@@ -37,26 +37,35 @@ import com.google.gson.JsonObject
3737import java.io.ByteArrayOutputStream
3838import java.io.InputStream
3939
40-
4140class FormbricksFragment : BottomSheetDialogFragment () {
42-
4341 private lateinit var binding: FragmentFormbricksBinding
4442 private lateinit var surveyId: String
4543 private val viewModel: FormbricksViewModel by viewModels()
44+ private var isDismissing = false
4645
4746 private var webAppInterface = WebAppInterface (object : WebAppInterface .WebAppCallback {
4847 override fun onClose () {
4948 Handler (Looper .getMainLooper()).post {
50- dismiss ()
49+ safeDismiss ()
5150 }
5251 }
5352
5453 override fun onDisplayCreated () {
55- SurveyManager .onNewDisplay(surveyId)
54+ try {
55+ SurveyManager .onNewDisplay(surveyId)
56+ } catch (e: Exception ) {
57+ val error = SDKError .couldNotCreateDisplayError
58+ Logger .e(error)
59+ }
5660 }
5761
5862 override fun onResponseCreated () {
59- SurveyManager .postResponse(surveyId)
63+ try {
64+ SurveyManager .postResponse(surveyId)
65+ } catch (e: Exception ) {
66+ val error = SDKError .couldNotCreateResponseError
67+ Logger .e(error)
68+ }
6069 }
6170
6271 override fun onFilePick (data : FileUploadData ) {
@@ -71,7 +80,7 @@ class FormbricksFragment : BottomSheetDialogFragment() {
7180 override fun onSurveyLibraryLoadError () {
7281 val error = SDKError .unableToLoadFormbicksJs
7382 Logger .e(error)
74- dismiss ()
83+ safeDismiss ()
7584 }
7685 })
7786
@@ -110,6 +119,13 @@ class FormbricksFragment : BottomSheetDialogFragment() {
110119 }
111120 }
112121
122+ override fun onCreate (savedInstanceState : Bundle ? ) {
123+ super .onCreate(savedInstanceState)
124+ arguments?.let {
125+ surveyId = it.getString(ARG_SURVEY_ID ) ? : throw IllegalArgumentException (" Survey ID is required" )
126+ }
127+ }
128+
113129 override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
114130 binding = FragmentFormbricksBinding .inflate(inflater).apply {
115131 lifecycleOwner = viewLifecycleOwner
@@ -147,28 +163,24 @@ class FormbricksFragment : BottomSheetDialogFragment() {
147163 dialog?.window?.setDimAmount(0.0f )
148164 binding.formbricksWebview.setBackgroundColor(Color .TRANSPARENT )
149165 binding.formbricksWebview.let {
166+ // First configure the WebView
167+ it.settings.apply {
168+ javaScriptEnabled = true
169+ domStorageEnabled = true
170+ loadWithOverviewMode = true
171+ useWideViewPort = true
172+ }
173+
150174 it.webChromeClient = object : WebChromeClient () {
151175 override fun onConsoleMessage (consoleMessage : ConsoleMessage ? ): Boolean {
152176 consoleMessage?.let { cm ->
153- if (cm.messageLevel() == ConsoleMessage .MessageLevel .ERROR ) {
154- val error = SDKError .surveyDisplayFetchError
155- Logger .e(error)
156- dismiss()
157- }
158177 val log = " [CONSOLE:${cm.messageLevel()} ] \" ${cm.message()} \" , source: ${cm.sourceId()} (${cm.lineNumber()} )"
159178 Logger .d(log)
160179 }
161180 return super .onConsoleMessage(consoleMessage)
162181 }
163182 }
164183
165- it.settings.apply {
166- javaScriptEnabled = true
167- domStorageEnabled = true
168- loadWithOverviewMode = true
169- useWideViewPort = true
170- }
171-
172184 it.webViewClient = object : WebViewClient () {
173185 override fun onReceivedError (
174186 view : WebView ? ,
@@ -192,11 +204,9 @@ class FormbricksFragment : BottomSheetDialogFragment() {
192204 }
193205
194206 it.setInitialScale(1 )
195-
196207 it.addJavascriptInterface(webAppInterface, WebAppInterface .INTERFACE_NAME )
208+ viewModel.loadHtml(surveyId)
197209 }
198-
199- viewModel.loadHtml(surveyId)
200210 }
201211
202212 private fun getFileName (uri : Uri ): String? {
@@ -231,15 +241,40 @@ class FormbricksFragment : BottomSheetDialogFragment() {
231241 }
232242 }
233243
244+ private fun safeDismiss () {
245+ if (isDismissing) return
246+ isDismissing = true
247+
248+ try {
249+ if (isAdded && ! isStateSaved) {
250+ dismiss()
251+ } else {
252+ // If we can't dismiss safely, just finish the activity
253+ activity?.finish()
254+ }
255+ } catch (e: Exception ) {
256+ val error = SDKError .somethingWentWrongError
257+ Logger .e(error)
258+ activity?.finish()
259+ }
260+ }
261+
262+ override fun onDestroy () {
263+ super .onDestroy()
264+ isDismissing = false
265+ }
266+
234267 companion object {
235268 private val TAG : String by lazy { FormbricksFragment ::class .java.simpleName }
269+ private const val ARG_SURVEY_ID = " survey_id"
236270
237271 fun show (childFragmentManager : FragmentManager , surveyId : String ) {
238- val fragment = FormbricksFragment ()
239- fragment.surveyId = surveyId
272+ val fragment = FormbricksFragment ().apply {
273+ arguments = Bundle ().apply {
274+ putString(ARG_SURVEY_ID , surveyId)
275+ }
276+ }
240277 fragment.show(childFragmentManager, TAG )
241278 }
242-
243- private const val CLOSING_TIMEOUT_IN_SECONDS = 5L
244279 }
245280}
0 commit comments