Skip to content

Commit ef949b8

Browse files
committed
Add setting to set default supplier. Make supplier & mfg editable dropdowns
1 parent e9e4ef6 commit ef949b8

File tree

8 files changed

+371
-53
lines changed

8 files changed

+371
-53
lines changed

app/build.gradle.kts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
alias(libs.plugins.android.application)
33
alias(libs.plugins.jetbrains.kotlin.android)
4+
alias(libs.plugins.jetbrains.kotlin.serialization)
45
alias(libs.plugins.com.google.devtools.ksp)
56
alias(libs.plugins.compose.compiler)
67
}
@@ -13,8 +14,8 @@ android {
1314
applicationId = "com.codelv.inventory"
1415
minSdk = 24
1516
targetSdk = 34
16-
versionCode = 8
17-
versionName = "1.0.8"
17+
versionCode = 9
18+
versionName = "1.0.9"
1819

1920
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2021
vectorDrawables {
@@ -79,6 +80,7 @@ dependencies {
7980
implementation(libs.androidx.navigation.compose)
8081
implementation(libs.androidx.datastore)
8182
implementation(libs.androidx.datastore.android)
83+
implementation(libs.kotlinx.serialization.json)
8284

8385
testImplementation(libs.junit)
8486
androidTestImplementation(libs.androidx.junit)

app/proguard-rules.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
#-renamesourcefileattribute SourceFile
2222

2323
-dontwarn org.jetbrains.annotations.**
24+
-dontwarn org.intellij.lang.annotations.Language

app/src/main/java/com/codelv/inventory/MainActivity.kt

Lines changed: 273 additions & 48 deletions
Large diffs are not rendered by default.

app/src/main/java/com/codelv/inventory/Models.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import androidx.room.*
99
import com.journeyapps.barcodescanner.ScanOptions
1010
import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.delay
12+
import kotlinx.coroutines.flow.first
13+
import kotlinx.coroutines.flow.map
1214
import kotlinx.coroutines.withContext
1315
import org.jsoup.Jsoup
1416
import org.jsoup.nodes.Document
@@ -17,7 +19,6 @@ import java.io.OutputStream
1719
import java.io.InputStream
1820
import java.net.URLEncoder
1921
import java.util.*
20-
import java.util.concurrent.Callable
2122
import kotlin.math.max
2223

2324
val USER_AGENTS = listOf(
@@ -594,6 +595,12 @@ interface PartManager {
594595

595596
@Query("SELECT EXISTS(SELECT * FROM parts WHERE id = :id)")
596597
suspend fun withIdExists(id: Int): Boolean
598+
599+
@Query("SELECT DISTINCT supplier FROM parts ORDER BY supplier")
600+
suspend fun distinctSuppliers(): List<String>
601+
602+
@Query("SELECT DISTINCT manufacturer FROM parts ORDER BY manufacturer")
603+
suspend fun distinctManufacturers(): List<String>
597604
}
598605

599606
@Dao
@@ -719,6 +726,10 @@ class AppViewModel(val database: AppDatabase) : ViewModel() {
719726
var parts: MutableList<Part> = mutableStateListOf();
720727
var scans: MutableList<Scan> = mutableStateListOf();
721728
var scanOptions: ScanOptions = ScanOptions();
729+
var supplierOptions: MutableList<String> = mutableStateListOf();
730+
var manufacturerOptions: MutableList<String> = mutableStateListOf();
731+
var settings: Settings = Settings();
732+
722733

723734
init {
724735
scanOptions
@@ -737,9 +748,25 @@ class AppViewModel(val database: AppDatabase) : ViewModel() {
737748
load()
738749
}
739750

751+
suspend fun loadOptions() {
752+
supplierOptions.clear()
753+
supplierOptions.addAll(database.parts().distinctSuppliers().filter{ it.isNotBlank() })
754+
Log.d("DB", "Distinct suppliers: ${supplierOptions}")
755+
listOf("Arrow", "Digikey", "LCSC", "Mouser").forEach { supplier ->
756+
if (supplierOptions.find{it.contains(supplier, ignoreCase=true)} == null) {
757+
supplierOptions.add(supplier);
758+
}
759+
}
760+
761+
manufacturerOptions.clear()
762+
manufacturerOptions.addAll(database.parts().distinctManufacturers().filter{ it.isNotBlank() })
763+
Log.d("DB", "Distinct manufacturers: ${manufacturerOptions}")
764+
}
765+
740766
suspend fun load() {
741767
parts.addAll(database.parts().all())
742768
scans.addAll(database.scans().all())
769+
loadOptions()
743770
}
744771

745772
suspend fun addScan(scan: Scan): Boolean {
@@ -807,4 +834,16 @@ class AppViewModel(val database: AppDatabase) : ViewModel() {
807834
suspend fun importDb(context: Context, stream: InputStream): Long {
808835
return database.importDb(context, stream);
809836
}
837+
838+
suspend fun loadSettings(context: Context) {
839+
this.settings = context.dataStore.data.first();
840+
Log.d("DB", "Loaded settings ${this.settings}")
841+
}
842+
843+
suspend fun saveSettings(context: Context) {
844+
Log.d("DB", "Update settings ${this.settings}")
845+
context.dataStore.updateData { this.settings };
846+
Log.d("DB", "Save settings ${context.dataStore.data.first()}")
847+
}
848+
810849
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.codelv.inventory
2+
3+
import android.content.Context
4+
import androidx.datastore.core.CorruptionException
5+
import androidx.datastore.core.DataStore
6+
import androidx.datastore.core.Serializer
7+
import androidx.datastore.dataStore
8+
import kotlinx.serialization.Serializable
9+
import kotlinx.serialization.encodeToString
10+
import kotlinx.serialization.json.Json
11+
import org.jsoup.SerializationException
12+
import java.io.InputStream
13+
import java.io.OutputStream
14+
15+
@Serializable
16+
data class Settings(
17+
var defaultSupplier: String = ""
18+
)
19+
20+
object SettingsSerializer : Serializer<Settings> {
21+
22+
override val defaultValue: Settings = Settings()
23+
24+
override suspend fun readFrom(input: InputStream): Settings =
25+
try {
26+
Json.decodeFromString<Settings>(
27+
input.readBytes().decodeToString()
28+
)
29+
} catch (serialization: SerializationException) {
30+
throw CorruptionException("Unable to read Settings", serialization)
31+
}
32+
33+
override suspend fun writeTo(t: Settings, output: OutputStream) {
34+
output.write(
35+
Json.encodeToString(t)
36+
.encodeToByteArray()
37+
)
38+
}
39+
}
40+
41+
val Context.dataStore: DataStore<Settings> by dataStore(
42+
fileName = "settings.json",
43+
serializer = SettingsSerializer,
44+
)

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ plugins {
33
alias(libs.plugins.android.application) apply false
44
alias(libs.plugins.compose.compiler) apply false
55
alias(libs.plugins.jetbrains.kotlin.android) apply false
6+
alias(libs.plugins.jetbrains.kotlin.serialization) apply false
67
alias(libs.plugins.com.google.devtools.ksp) apply false
78
}

gradle/libs.versions.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ agp = "8.13.1"
33
kotlin = "2.0.21"
44
ksp = "2.0.21-1.0.26"
55
coreKtx = "1.13.1"
6+
json = "1.7.3"
67
junit = "4.13.2"
78
junitVersion = "1.2.1"
89
espressoCore = "3.6.1"
@@ -17,6 +18,7 @@ coil = "3.0.0-rc02"
1718
accompanist = "0.32.0"
1819
zxingEmbedded = "4.3.0"
1920
zxing = "3.5.3"
21+
foundationLayout = "1.9.4"
2022

2123
[libraries]
2224
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -44,6 +46,7 @@ androidx-room-gradle-plugin = {group = "androidx.room", name = "room-gradle-plug
4446
androidx-room-compiler = {group = "androidx.room", name = "room-compiler", version.ref = "room"}
4547
androidx-room-ktx = {group = "androidx.room", name = "room-ktx", version.ref = "room"}
4648

49+
kotlinx-serialization-json = {group ="org.jetbrains.kotlinx", name="kotlinx-serialization-json", version.ref = "json"}
4750

4851
com-google-accompanist = {group = "com.google.accompanist", name="accompanist-permissions", version.ref="accompanist"}
4952

@@ -55,9 +58,9 @@ com-journeyapps = {group = "com.journeyapps", name="zxing-android-embedded", ver
5558
com-google-zxing = {group = "com.google.zxing", name="core", version.ref="zxing"}
5659
org-jsoup = {group = "org.jsoup", name = "jsoup", version.ref = "jsoup"}
5760

58-
5961
[plugins]
6062
android-application = { id = "com.android.application", version.ref = "agp" }
6163
jetbrains-kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
6264
com-google-devtools-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
63-
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
65+
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
66+
jetbrains-kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}

metadata/en-US/changelogs/9.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Fix scanning from Digikey invoices
2+
- Add setting page to set default supplier
3+
- Make part supplier and manufacturer inputs editable dropdowns

0 commit comments

Comments
 (0)