Skip to content

Commit

Permalink
Implementing "Scan Text" on Add By Identifier Screen which uses Googl…
Browse files Browse the repository at this point in the history
…e's barcode scanner.

Upping versionCode to 61.
  • Loading branch information
Dima-Android committed May 15, 2024
1 parent 6539b41 commit 986f049
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ dependencies {
implementation(Libs.jodaTime)
implementation(Libs.eventBus)
implementation(Libs.keyboardVisibility)
implementation("com.google.android.gms:play-services-code-scanner:16.1.0")

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.zotero.android.uicomponents.addbyidentifier

import android.content.Context
import androidx.lifecycle.viewModelScope
import com.google.mlkit.vision.codescanner.GmsBarcodeScanning
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
Expand All @@ -16,6 +18,7 @@ import org.zotero.android.database.objects.FieldKeys
import org.zotero.android.files.FileStore
import org.zotero.android.sync.LibraryIdentifier
import org.zotero.android.sync.SchemaController
import org.zotero.android.uicomponents.addbyidentifier.data.ISBNParser
import org.zotero.android.uicomponents.addbyidentifier.data.LookupRow
import org.zotero.android.uicomponents.addbyidentifier.data.LookupRowItem
import timber.log.Timber
Expand All @@ -28,8 +31,12 @@ internal class AddByIdentifierViewModel @Inject constructor(
private val attachmentDownloaderEventStream: RemoteAttachmentDownloaderEventStream,
private val schemaController: SchemaController,
private val remoteFileDownloader: RemoteAttachmentDownloader,
private val context: Context,
) : BaseViewModel2<AddByIdentifierViewState, AddByIdentifierViewEffect>(AddByIdentifierViewState()) {

private val scannerPatternRegex =
"10.\\d{4,9}\\/[-._;()\\/:a-zA-Z0-9]+"

fun init() = initOnce {
setupAttachmentObserving()
val collectionKeys =
Expand Down Expand Up @@ -125,6 +132,45 @@ internal class AddByIdentifierViewModel @Inject constructor(
updateLookupState(State.waitingInput)
}

fun process(scannedText: String) {
val identifiers = Regex(scannerPatternRegex).findAll(scannedText).map { it.value }.toMutableList()
val isbns = ISBNParser.isbns(scannedText)
if (isbns.isNotEmpty()) {
identifiers.addAll(isbns)
}

if (identifiers.isEmpty()) {
return
}

val scannedText = identifiers.joinToString(", ")

var newText = viewState.identifierText
if (newText.isEmpty()) {
newText = scannedText
} else {
newText += ", " + scannedText
}
updateState {
copy(identifierText = newText)
}
}

fun onScanText() {
val scanner = GmsBarcodeScanning.getClient(context)
scanner.startScan()
.addOnSuccessListener { barcode ->
val scannedString = barcode.rawValue ?: ""
process(scannedString)
}
.addOnCanceledListener {
// Task canceled
}
.addOnFailureListener { e ->
Timber.e(e, "Barcode scanning failed")
}
}

fun onLookup() {
val identifier = viewState.identifierText.trim()
if (identifier.isBlank()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.zotero.android.uicomponents.addbyidentifier.data

object ISBNParser {

private val isbnRegexPattern = "\\b(?:97[89]\\s*(?:\\d\\s*){9}\\d|(?:\\d\\s*){9}[\\dX])\\b"

fun isbns(string: String): List<String> {
val cleanedString = string.replace(Regex("[\\x2D\\xAD\\u2010-\\u2015\\u2043\\u2212]+"), "")
val matches =
Regex(isbnRegexPattern).findAll(cleanedString).map { it.value }.toMutableList()

val isbns = mutableListOf<String>()

for (match in matches) {
val isbn = match.replace(Regex("\\s+"), "")

if (if (isbn.length == 10) validate10(isbn) else validate13(isbn)) {
isbns.add(isbn)
}
}

return isbns
}

private fun validate10(isbn: String): Boolean {
var sum = 0

for (idx in 0..<10) {
val startIndex = idx
val endIndex = idx + 1
val character = isbn.substring(startIndex, endIndex)

val intValue = character.toIntOrNull()
if (intValue != null) {
sum += intValue * (10 - idx)
} else if (idx == 9 && character == "X") {
sum += 10
} else {
sum = 1
break
}
}

return sum % 11 == 0
}

private fun validate13(isbn: String): Boolean {
var sum = 0

for (idx in 0..<13) {
val startIndex = idx
val endIndex = idx + 1
val character = isbn.substring(startIndex, endIndex)

val intValue = character.toIntOrNull()
if (intValue == null) {
sum = 1
break
}

if (idx % 2 == 0) {
sum += intValue
} else {
sum += intValue * 3
}
}

return sum % 10 == 0
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package org.zotero.android.uicomponents.addbyidentifier.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import org.zotero.android.uicomponents.Drawables
import org.zotero.android.uicomponents.Strings
import org.zotero.android.uicomponents.addbyidentifier.AddByIdentifierViewModel
import org.zotero.android.uicomponents.addbyidentifier.AddByIdentifierViewState
Expand All @@ -41,6 +48,10 @@ internal fun LazyListScope.addByIdentifierTitleEditFieldAndError(
identifierText = viewState.identifierText,
onIdentifierTextChange = viewModel::onIdentifierTextChange,
)

Spacer(modifier = Modifier.height(20.dp))
ScanTextButton(onClick = viewModel::onScanText)

if (failedState != null) {
val errorText = when (failedState.error) {
is AddByIdentifierViewModel.Error.noIdentifiersDetectedAndNoLookupData -> {
Expand Down Expand Up @@ -121,3 +132,33 @@ internal fun LazyListScope.addByIdentifierLoadingIndicator() {
}
}
}

@Composable
internal fun ScanTextButton(onClick: () -> Unit) {
Column(modifier = Modifier.fillMaxWidth()) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null,
onClick = onClick
)
.align(Alignment.CenterHorizontally)
) {
Icon(
modifier = Modifier.size(24.dp),
painter = painterResource(id = Drawables.baseline_qr_code_scanner_24),
contentDescription = null,
tint = CustomTheme.colors.zoteroDefaultBlue,
)
Spacer(modifier = Modifier.width(12.dp))
Text(
text = stringResource(id = Strings.scan_text),
color = CustomTheme.colors.zoteroDefaultBlue,
style = CustomTheme.typography.newBody,
)
}

}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/baseline_qr_code_scanner_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M9.5,6.5v3h-3v-3H9.5M11,5H5v6h6V5L11,5zM9.5,14.5v3h-3v-3H9.5M11,13H5v6h6V13L11,13zM17.5,6.5v3h-3v-3H17.5M19,5h-6v6h6V5L19,5zM13,13h1.5v1.5H13V13zM14.5,14.5H16V16h-1.5V14.5zM16,13h1.5v1.5H16V13zM13,16h1.5v1.5H13V16zM14.5,17.5H16V19h-1.5V17.5zM16,16h1.5v1.5H16V16zM17.5,14.5H19V16h-1.5V14.5zM17.5,17.5H19V19h-1.5V17.5zM22,7h-2V4h-3V2h5V7zM22,22v-5h-2v3h-3v2H22zM2,22h5v-2H4v-3H2V22zM2,2v5h2V4h3V2H2z"/>

</vector>
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object BuildConfig {
const val compileSdkVersion = 34
const val targetSdk = 33

val versionCode = 60 // Must be updated on every build
val versionCode = 61 // Must be updated on every build
val version = Version(
major = 1,
minor = 0,
Expand Down

0 comments on commit 986f049

Please sign in to comment.