Updated on 2026-08-14
This commit is contained in:
parent
32325886fd
commit
78c38ff089
4 changed files with 22 additions and 22 deletions
|
|
@ -25,6 +25,8 @@ import com.tangem.feature.qrscanning.viewmodel.QrScanningViewModel
|
|||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import javax.inject.Inject
|
||||
|
|
@ -62,8 +64,15 @@ internal class QrScanningFragment : ComposeFragment() {
|
|||
private val galleryLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) {
|
||||
val selectedImage = it ?: Uri.EMPTY
|
||||
if (selectedImage != Uri.EMPTY) {
|
||||
val image = InputImage.fromFilePath(requireContext(), selectedImage)
|
||||
analyzer.analyze(image)
|
||||
val mimeType = requireContext().contentResolver.getType(selectedImage)
|
||||
if (mimeType.isImageMimeType()) {
|
||||
try {
|
||||
val image = InputImage.fromFilePath(requireContext(), selectedImage)
|
||||
analyzer.analyze(image)
|
||||
} catch (e: IOException) {
|
||||
Timber.e(e, "Unable to get image $selectedImage from gallery")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +89,7 @@ internal class QrScanningFragment : ComposeFragment() {
|
|||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
viewModel.launchGalleryEvent
|
||||
.collect {
|
||||
galleryLauncher.launch(it.imageFilter)
|
||||
galleryLauncher.launch(GALLERY_IMAGE_FILTER)
|
||||
delay(timeMillis = 2000)
|
||||
}
|
||||
}
|
||||
|
|
@ -137,8 +146,13 @@ internal class QrScanningFragment : ComposeFragment() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun String?.isImageMimeType() = this?.startsWith(prefix = "$IMAGE_MIME_TYPE/") == true
|
||||
|
||||
companion object {
|
||||
|
||||
private const val IMAGE_MIME_TYPE = "image"
|
||||
private const val GALLERY_IMAGE_FILTER = "$IMAGE_MIME_TYPE/*"
|
||||
|
||||
fun create() = QrScanningFragment()
|
||||
}
|
||||
}
|
||||
|
|
@ -11,9 +11,9 @@ import kotlinx.coroutines.flow.SharedFlow
|
|||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
interface QrScanningClickIntents {
|
||||
internal interface QrScanningClickIntents {
|
||||
|
||||
val launchGallery: SharedFlow<GalleryRequest>
|
||||
val launchGallery: SharedFlow<Unit>
|
||||
|
||||
fun onBackClick()
|
||||
|
||||
|
|
@ -22,11 +22,6 @@ interface QrScanningClickIntents {
|
|||
fun onGalleryClicked()
|
||||
}
|
||||
|
||||
@JvmInline
|
||||
value class GalleryRequest(
|
||||
val imageFilter: String,
|
||||
)
|
||||
|
||||
@ViewModelScoped
|
||||
internal class QrScanningClickIntentsImplementor @Inject constructor(
|
||||
private val stateHolder: QrScanningStateController,
|
||||
|
|
@ -36,7 +31,7 @@ internal class QrScanningClickIntentsImplementor @Inject constructor(
|
|||
|
||||
private var isScanned = false
|
||||
|
||||
override val launchGallery = MutableSharedFlow<GalleryRequest>(
|
||||
override val launchGallery = MutableSharedFlow<Unit>(
|
||||
extraBufferCapacity = 1,
|
||||
onBufferOverflow = BufferOverflow.DROP_LATEST,
|
||||
)
|
||||
|
|
@ -56,13 +51,9 @@ internal class QrScanningClickIntentsImplementor @Inject constructor(
|
|||
}
|
||||
|
||||
override fun onGalleryClicked() {
|
||||
launchGallery.tryEmit(GalleryRequest(imageFilter = GALLERY_IMAGE_FILTER))
|
||||
launchGallery.tryEmit(Unit)
|
||||
if (stateHolder.value.bottomSheetConfig != null) {
|
||||
stateHolder.update(DismissBottomSheetTransformer())
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val GALLERY_IMAGE_FILTER = "image/*"
|
||||
}
|
||||
}
|
||||
|
|
@ -31,7 +31,7 @@ internal class QrScanningViewModel @Inject constructor(
|
|||
private val network: String? = savedStateHandle[AppRoute.QrScanning.NETWORK_KEY]
|
||||
|
||||
val uiState: StateFlow<QrScanningState> = stateHolder.uiState
|
||||
val launchGalleryEvent: SharedFlow<GalleryRequest> = clickIntents.launchGallery
|
||||
val launchGalleryEvent: SharedFlow<Unit> = clickIntents.launchGallery
|
||||
|
||||
init {
|
||||
// samsung for some reason disables reader mode, and then it works unstable
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue