Skip to content

Commit d1f11eb

Browse files
authored
fix: fixes the pixel fold issue (#24)
* fixes the pixel fold issue * adds errors * fixes log
1 parent 5016f9b commit d1f11eb

File tree

3 files changed

+68
-36
lines changed

3 files changed

+68
-36
lines changed

android/src/main/java/com/formbricks/android/model/error/SDKError.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ object SDKError {
2424
val surveyNotFoundError = RuntimeException("No survey found matching the action class.")
2525
val noUserIdSetError = RuntimeException("No userId is set, please set a userId first using the setUserId function")
2626

27+
val couldNotCreateDisplayError = RuntimeException("Something went wrong while creating a display. Please try again later")
28+
val couldNotCreateResponseError = RuntimeException("Something went wrong while creating a response. Please try again later")
29+
val somethingWentWrongError = RuntimeException("Something went wrong. Please try again later")
2730
}

android/src/main/java/com/formbricks/android/webview/FormbricksFragment.kt

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,35 @@ import com.google.gson.JsonObject
3737
import java.io.ByteArrayOutputStream
3838
import java.io.InputStream
3939

40-
4140
class 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
}

android/src/main/java/com/formbricks/android/webview/FormbricksViewModel.kt

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class FormbricksViewModel : ViewModel() {
3434
</head>
3535
3636
<body style="overflow: hidden; height: 100vh; display: flex; flex-direction: column; justify-content: flex-end;">
37-
<div id="formbricks-react-native" style="width: 100%;"></div>
37+
<div id="formbricks-android" style="width: 100%;"></div>
3838
</body>
3939
4040
<script type="text/javascript">
41-
var json = `{{WEBVIEW_DATA}}`
41+
const json = `{{WEBVIEW_DATA}}`;
4242
4343
function onClose() {
4444
FormbricksJavascript.message(JSON.stringify({ event: "onClose" }));
@@ -62,9 +62,8 @@ class FormbricksViewModel : ViewModel() {
6262
};
6363
6464
window.formbricksSurveys.renderSurvey(surveyProps);
65-
}
65+
};
6666
67-
// Function to attach click listener to file inputs
6867
function attachFilePickerOverride() {
6968
const inputs = document.querySelectorAll('input[type="file"]');
7069
inputs.forEach(input => {
@@ -81,23 +80,20 @@ class FormbricksViewModel : ViewModel() {
8180
fileUploadParams: {
8281
allowedFileExtensions: allowedFileExtensions,
8382
allowMultipleFiles: allowMultipleFiles === "true",
84-
},
83+
}
8584
}));
8685
});
8786
}
8887
});
89-
}
88+
};
9089
91-
// Initially attach the override
9290
attachFilePickerOverride();
9391
94-
// Set up a MutationObserver to catch dynamically added file inputs
9592
const observer = new MutationObserver(function (mutations) {
9693
attachFilePickerOverride();
9794
});
9895
9996
observer.observe(document.body, { childList: true, subtree: true });
100-
10197
const script = document.createElement("script");
10298
script.src = "${Formbricks.appUrl}/js/surveys.umd.cjs";
10399
script.async = true;
@@ -149,8 +145,6 @@ class FormbricksViewModel : ViewModel() {
149145
.replace("#", "%23") // Hex color code's # breaks the JSON
150146
.replace("\\\"","'") // " is replaced to ' in the html codes in the JSON
151147
}
152-
153-
154148
}
155149

156150
@BindingAdapter("htmlText")

0 commit comments

Comments
 (0)