-
Notifications
You must be signed in to change notification settings - Fork 663
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
fix#2589: Migrate QrCodeReaderFragment from XML to compose #2590
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 0 additions & 134 deletions
134
app/src/main/java/org/mifos/mobile/ui/fragments/QrCodeReaderFragment.kt
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
app/src/main/java/org/mifos/mobile/ui/qrCodeReader/QrCodeReaderFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.mifos.mobile.ui.qrCodeReader | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.Toast | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.platform.ViewCompositionStrategy | ||
import com.google.gson.Gson | ||
import com.google.gson.JsonSyntaxException | ||
import com.google.zxing.Result | ||
import dagger.hilt.android.AndroidEntryPoint | ||
import me.dm7.barcodescanner.zxing.ZXingScannerView | ||
import me.dm7.barcodescanner.zxing.ZXingScannerView.ResultHandler | ||
import org.mifos.mobile.R | ||
import org.mifos.mobile.core.ui.theme.MifosMobileTheme | ||
import org.mifos.mobile.models.beneficiary.Beneficiary | ||
import org.mifos.mobile.ui.activities.base.BaseActivity | ||
import org.mifos.mobile.ui.enums.BeneficiaryState | ||
import org.mifos.mobile.ui.fragments.BeneficiaryApplicationFragment | ||
import org.mifos.mobile.ui.fragments.base.BaseFragment | ||
|
||
@AndroidEntryPoint | ||
class QrCodeReaderFragment : BaseFragment(), ResultHandler { | ||
|
||
private lateinit var scannerView: ZXingScannerView | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle?, | ||
): View { | ||
scannerView = ZXingScannerView(requireContext()).apply { | ||
setResultHandler(this@QrCodeReaderFragment) | ||
} | ||
return ComposeView(requireContext()).apply { | ||
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed) | ||
setContent { | ||
MifosMobileTheme { | ||
QrCodeReaderScreen( | ||
scannerView = scannerView, | ||
onBackPressed = { | ||
requireActivity().onBackPressedDispatcher.onBackPressed() | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun handleResult(result: Result?) { | ||
val gson = Gson() | ||
try { | ||
val beneficiary = gson.fromJson(result?.text, Beneficiary::class.java) | ||
activity?.supportFragmentManager?.popBackStack() | ||
(activity as BaseActivity?)?.replaceFragment( | ||
BeneficiaryApplicationFragment.newInstance( | ||
BeneficiaryState.CREATE_QR, | ||
beneficiary, | ||
), | ||
true, | ||
R.id.container, | ||
) | ||
} catch (e: JsonSyntaxException) { | ||
Toast.makeText( | ||
activity, | ||
getString(R.string.invalid_qr), | ||
Toast.LENGTH_SHORT, | ||
).show() | ||
scannerView.resumeCameraPreview(this) | ||
} | ||
} | ||
|
||
override fun onPause() { | ||
super.onPause() | ||
(activity as? BaseActivity)?.showToolbar() | ||
scannerView.stopCamera() | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
(activity as? BaseActivity)?.hideToolbar() | ||
scannerView.setResultHandler(this) | ||
scannerView.startCamera() | ||
} | ||
|
||
companion object { | ||
fun newInstance(): QrCodeReaderFragment { | ||
return QrCodeReaderFragment() | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
app/src/main/java/org/mifos/mobile/ui/qrCodeReader/QrCodeScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.mifos.mobile.ui.qrCodeReader | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.res.painterResource | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.viewinterop.AndroidView | ||
import me.dm7.barcodescanner.zxing.ZXingScannerView | ||
import org.mifos.mobile.MifosSelfServiceApp.Companion.context | ||
import org.mifos.mobile.R | ||
import org.mifos.mobile.core.ui.component.MifosTopBar | ||
import org.mifos.mobile.core.ui.theme.MifosMobileTheme | ||
import com.google.zxing.Result | ||
|
||
@Composable | ||
fun QrCodeReaderScreen( | ||
scannerView: ZXingScannerView, | ||
onBackPressed: () -> Unit, | ||
) { | ||
var flashOn by rememberSaveable { mutableStateOf(false) } | ||
|
||
DisposableEffect(Unit) { | ||
scannerView.startCamera() | ||
onDispose { | ||
scannerView.stopCamera() | ||
} | ||
} | ||
|
||
Scaffold( | ||
topBar = { | ||
MifosTopBar(navigateBack = { onBackPressed.invoke() }) { | ||
Text(text = stringResource(id = R.string.add_beneficiary)) | ||
} | ||
} | ||
) { | ||
Box( | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(it), | ||
) { | ||
AndroidView({ scannerView }) | ||
IconButton( | ||
modifier = Modifier | ||
.align(Alignment.BottomCenter) | ||
.padding(24.dp), | ||
onClick = { | ||
flashOn = !flashOn | ||
scannerView.flash = flashOn | ||
}) { | ||
val icon: Painter = if (flashOn) { | ||
painterResource(id = R.drawable.ic_flash_on) | ||
} else { | ||
painterResource(id = R.drawable.ic_flash_off) | ||
} | ||
Icon( | ||
painter = icon, | ||
contentDescription = null | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
@Preview(showBackground = true, showSystemUi = true) | ||
fun PreviewQrCodeScreen() { | ||
MifosMobileTheme { | ||
QrCodeReaderScreen( | ||
scannerView = ZXingScannerView(context), | ||
onBackPressed = {} | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
dont we have any other options than AndroidView?
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.
"Unfortunately, as of now, there's no direct replacement in Jetpack Compose for ZXingScannerView that doesn't use AndroidView."
I looked up online and it was the only way I found. Probably we don't have another way of doing it.