-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2e3ed4
commit 74ce257
Showing
3 changed files
with
123 additions
and
21 deletions.
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
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,9 @@ | ||
package org.mifos.mobile.utils | ||
|
||
import com.google.zxing.Result | ||
|
||
sealed class QrCodeUiState { | ||
object Loading : QrCodeUiState() | ||
data class ShowError(val message: Int) : QrCodeUiState() | ||
data class HandleDecodedResult(val result: Result?) : QrCodeUiState() | ||
} |
77 changes: 77 additions & 0 deletions
77
app/src/main/java/org/mifos/mobile/viewModels/QrCodeImportViewModel.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,77 @@ | ||
package org.mifos.mobile.viewModels | ||
|
||
import android.net.Uri | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.google.zxing.* | ||
import com.google.zxing.common.HybridBinarizer | ||
import com.isseiaoki.simplecropview.CropImageView | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import io.reactivex.Single | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.disposables.CompositeDisposable | ||
import io.reactivex.schedulers.Schedulers | ||
import org.mifos.mobile.R | ||
import org.mifos.mobile.utils.QrCodeUiState | ||
import java.util.* | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class QrCodeImportViewModel @Inject constructor() : ViewModel() { | ||
|
||
private val compositeDisposables: CompositeDisposable = CompositeDisposable() | ||
|
||
private var result: Result? = null | ||
private var hasErrorOccurred = false | ||
|
||
private val _qrCodeUiState = MutableLiveData<QrCodeUiState>() | ||
val qrCodeUiState: LiveData<QrCodeUiState> = _qrCodeUiState | ||
|
||
fun getDecodedResult(sourceUri: Uri?, cropImageView: CropImageView?) { | ||
_qrCodeUiState.value = QrCodeUiState.Loading | ||
cropImageView?.crop(sourceUri) | ||
?.executeAsSingle() | ||
?.flatMap { bMap -> | ||
val intArray = IntArray(bMap.width * bMap.height) | ||
// copy pixel data from the Bitmap into the 'intArray' array | ||
bMap.getPixels( | ||
intArray, | ||
0, | ||
bMap.width, | ||
0, | ||
0, | ||
bMap.width, | ||
bMap.height, | ||
) | ||
val source: LuminanceSource = RGBLuminanceSource( | ||
bMap.width, | ||
bMap.height, | ||
intArray, | ||
) | ||
val bitmap = BinaryBitmap(HybridBinarizer(source)) | ||
// flags for decoding since it is large file | ||
val tmpHintsMap: MutableMap<DecodeHintType, Boolean?> = | ||
EnumMap(DecodeHintType::class.java) | ||
tmpHintsMap[DecodeHintType.TRY_HARDER] = java.lang.Boolean.TRUE | ||
tmpHintsMap[DecodeHintType.PURE_BARCODE] = java.lang.Boolean.TRUE | ||
val reader: Reader = MultiFormatReader() | ||
try { | ||
result = reader.decode(bitmap, tmpHintsMap) | ||
} catch (e: Exception) { | ||
_qrCodeUiState.value = QrCodeUiState.ShowError(R.string.error_reading_qr) | ||
} | ||
Single.just(result) | ||
} | ||
?.subscribeOn(Schedulers.newThread()) | ||
?.observeOn(AndroidSchedulers.mainThread()) | ||
?.subscribe({ result -> | ||
_qrCodeUiState.value = QrCodeUiState.HandleDecodedResult(result) | ||
}) { | ||
if (!hasErrorOccurred) { | ||
_qrCodeUiState.value = QrCodeUiState.ShowError(R.string.error_reading_qr) | ||
} | ||
hasErrorOccurred = false | ||
}?.let { compositeDisposables.add(it) } | ||
} | ||
} |