forked from zotero/zotero-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing "Scan Text" on Add By Identifier Screen which uses Googl…
…e's barcode scanner. Upping versionCode to 61.
- Loading branch information
1 parent
6539b41
commit 986f049
Showing
6 changed files
with
164 additions
and
1 deletion.
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
70 changes: 70 additions & 0 deletions
70
app/src/main/java/org/zotero/android/uicomponents/addbyidentifier/data/ISBNParser.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,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 | ||
} | ||
} |
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,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> |
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