-
Notifications
You must be signed in to change notification settings - Fork 2
[TASK][YD-6940] Migrate webapp module UI to Jetpack Compose #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: YD-6939
Are you sure you want to change the base?
Changes from 1 commit
2e88364
523a09f
80437c0
22cf789
f4c4f26
fd9d33c
c0216c1
0f121ce
00b87cc
74bddca
a78069a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.yoti.mobile.android.sdk.yotidocscan.websample | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.text.KeyboardOptions | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.OutlinedTextField | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.input.ImeAction | ||
import androidx.compose.ui.text.input.KeyboardType | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.yoti.mobile.android.sdk.yotidocscan.websample.ui.YotiDocScanWebSampleAppTheme | ||
|
||
@Composable | ||
fun MainScreen( | ||
sessionUrl: String, | ||
onSessionUrlChanged: (String) -> Unit, | ||
onStartSessionClicked: () -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
Column( | ||
modifier = modifier | ||
.fillMaxSize() | ||
.padding(16.dp), | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
verticalArrangement = Arrangement.Center | ||
) { | ||
OutlinedTextField( | ||
value = sessionUrl, | ||
onValueChange = onSessionUrlChanged, | ||
label = { | ||
Text(text = stringResource(id = R.string.session_url_hint)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No color or font size/style ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, just use the default text style, should suffice given it's simply a demo app for YDS. |
||
}, | ||
keyboardOptions = KeyboardOptions( | ||
keyboardType = KeyboardType.Text, | ||
imeAction = ImeAction.Done | ||
), | ||
modifier = Modifier.fillMaxWidth() | ||
) | ||
Spacer(Modifier.size(16.dp)) | ||
Button( | ||
onClick = onStartSessionClicked, | ||
enabled = sessionUrl.isNotBlank() | ||
) { | ||
Text(text = stringResource(id = R.string.start_session_button)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No color or font size/style ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please see comment above. |
||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun PreviewEmptyMainScreen() { | ||
YotiDocScanWebSampleAppTheme { | ||
MainScreen( | ||
sessionUrl = "", | ||
onSessionUrlChanged = {}, | ||
onStartSessionClicked = {} | ||
) | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
fun PreviewMainScreen() { | ||
YotiDocScanWebSampleAppTheme { | ||
MainScreen( | ||
sessionUrl = "https://example.com/session", | ||
onSessionUrlChanged = {}, | ||
onStartSessionClicked = {} | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.yoti.mobile.android.sdk.yotidocscan.websample | ||
|
||
import android.annotation.SuppressLint | ||
import android.net.Uri | ||
import android.webkit.PermissionRequest | ||
import android.webkit.ValueCallback | ||
import android.webkit.WebChromeClient | ||
import android.webkit.WebChromeClient.FileChooserParams | ||
import android.webkit.WebSettings | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
|
||
@Composable | ||
fun WebScreen( | ||
sessionUrl: String, | ||
onPageCommitVisible: (String?) -> Unit, | ||
onFilePathCallbackReady: (ValueCallback<Array<Uri>>?) -> Unit, | ||
onShowCameraAndFilePickerChooser: (FileChooserParams) -> Unit, | ||
modifier: Modifier = Modifier | ||
) { | ||
AndroidView( | ||
modifier = modifier.fillMaxSize(), | ||
factory = { context -> | ||
WebView(context).apply { | ||
configureSettings(settings) | ||
configureWebViewClient(this, onPageCommitVisible) | ||
configureWebChromeClient( | ||
this, | ||
onFilePathCallbackReady, | ||
onShowCameraAndFilePickerChooser | ||
) | ||
} | ||
}, | ||
update = { webView -> | ||
sessionUrl.takeIf { it.isNotBlank() }?.let { webView.loadUrl(it) } | ||
} | ||
) | ||
} | ||
|
||
mircea-yoti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@SuppressLint("SetJavaScriptEnabled") | ||
private fun configureSettings(settings: WebSettings) { | ||
with(settings) { | ||
javaScriptEnabled = true | ||
allowFileAccess = true | ||
allowUniversalAccessFromFileURLs = true | ||
domStorageEnabled = true | ||
javaScriptCanOpenWindowsAutomatically = true | ||
mediaPlaybackRequiresUserGesture = false | ||
} | ||
} | ||
|
||
private fun configureWebViewClient(webView: WebView, onPageCommitVisible: (String?) -> Unit) { | ||
webView.webViewClient = object : WebViewClient() { | ||
override fun onPageCommitVisible(view: WebView?, url: String?) { | ||
super.onPageCommitVisible(view, url) | ||
onPageCommitVisible(url) | ||
} | ||
} | ||
} | ||
|
||
private fun configureWebChromeClient( | ||
webView: WebView, | ||
onFilePathCallbackReady: (ValueCallback<Array<Uri>>?) -> Unit, | ||
onShowCameraAndFilePickerChooser: (FileChooserParams) -> Unit | ||
) { | ||
webView.webChromeClient = object : WebChromeClient() { | ||
// Launch file picker or camera intent and set the | ||
// filePathCallback to set the capture results to the webview | ||
override fun onShowFileChooser( | ||
webView: WebView?, | ||
filePathCallback: ValueCallback<Array<Uri>>?, | ||
fileChooserParams: FileChooserParams? | ||
): Boolean { | ||
onFilePathCallbackReady(filePathCallback) | ||
return if (fileChooserParams?.mode == FileChooserParams.MODE_OPEN) { | ||
onShowCameraAndFilePickerChooser(fileChooserParams) | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
// Grant permission requested | ||
override fun onPermissionRequest(request: PermissionRequest?) { | ||
request?.grant(request.resources) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.yoti.mobile.android.sdk.yotidocscan.websample.ui | ||
|
||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.lightColorScheme | ||
import androidx.compose.runtime.Composable | ||
|
||
@Composable | ||
fun YotiDocScanWebSampleAppTheme(content: @Composable () -> Unit) { | ||
MaterialTheme( | ||
colorScheme = lightColorScheme(), | ||
content = content | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<resources> | ||
<string name="app_name">webapp</string> | ||
<string name="session_url_hint">Session URL</string> | ||
<string name="start_session_button">Start session</string> | ||
</resources> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we declare the versions in a dedicated file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done here: 74bddca 🚀