@@ -9,6 +9,8 @@ import androidx.room.*
99import com.journeyapps.barcodescanner.ScanOptions
1010import kotlinx.coroutines.Dispatchers
1111import kotlinx.coroutines.delay
12+ import kotlinx.coroutines.flow.first
13+ import kotlinx.coroutines.flow.map
1214import kotlinx.coroutines.withContext
1315import org.jsoup.Jsoup
1416import org.jsoup.nodes.Document
@@ -17,7 +19,6 @@ import java.io.OutputStream
1719import java.io.InputStream
1820import java.net.URLEncoder
1921import java.util.*
20- import java.util.concurrent.Callable
2122import kotlin.math.max
2223
2324val 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}
0 commit comments