mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-04 11:37:22 +00:00
Created SelectionRepository, cleaned up code
This commit is contained in:
1 parent
5d2a870319
commit
208c587ddf
12 files changed
+156
-130
No files matched your search
@@ -55,15 +55,13 @@ class DataRepository(
|
||||
|
||||
override fun getSatelliteTypes() = remoteSource.satelliteSourcesMap.keys.sorted()
|
||||
|
||||
override suspend fun getEntriesWithModes() = entrySource.getEntriesWithModes()
|
||||
override suspend fun getEntriesList(): List<SatItem> {
|
||||
val selectedIds = settingsRepository.satelliteSelection.value
|
||||
return entrySource.getEntriesList().map { it.copy(isSelected = it.catnum in selectedIds) }
|
||||
}
|
||||
|
||||
override suspend fun getEntriesWithIds(ids: List<Int>) = entrySource.getEntriesWithIds(ids)
|
||||
|
||||
override suspend fun getEntriesWithSelection(): List<SatItem> {
|
||||
val selectedIds = settingsRepository.satelliteSelection.value
|
||||
return getEntriesWithModes().onEach { it.isSelected = it.catnum in selectedIds }
|
||||
}
|
||||
|
||||
override suspend fun getRadiosWithId(id: Int) = radioSource.getRadiosWithId(id)
|
||||
|
||||
override fun updateFromFile(uri: String) {
|
||||
@@ -93,6 +91,7 @@ class DataRepository(
|
||||
settingsRepository.saveSatType(type, catnums)
|
||||
importedEntries.addAll(satellites)
|
||||
}
|
||||
|
||||
"McCants", "Classified" -> {
|
||||
// unzip and parse tle stream
|
||||
val unzipped = ZipInputStream(stream).apply { nextEntry }
|
||||
@@ -101,6 +100,7 @@ class DataRepository(
|
||||
settingsRepository.saveSatType(type, catnums)
|
||||
importedEntries.addAll(satellites)
|
||||
}
|
||||
|
||||
else -> {
|
||||
// parse csv stream
|
||||
val parsed = dataParser.parseCSVStream(stream)
|
||||
|
||||
@@ -26,7 +26,7 @@ interface IEntriesStorage {
|
||||
|
||||
fun getEntriesTotal(): Flow<Int>
|
||||
|
||||
suspend fun getEntriesWithModes(): List<SatItem>
|
||||
suspend fun getEntriesList(): List<SatItem>
|
||||
|
||||
suspend fun getEntriesWithIds(ids: List<Int>): List<Satellite>
|
||||
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
package com.rtbishop.look4sat.data
|
||||
|
||||
import com.rtbishop.look4sat.domain.IDataRepository
|
||||
import com.rtbishop.look4sat.domain.ISettingsRepository
|
||||
import com.rtbishop.look4sat.model.SatItem
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class SelectionRepository(
|
||||
private val dispatcher: CoroutineDispatcher,
|
||||
private val dataRepository: IDataRepository,
|
||||
private val settingsRepository: ISettingsRepository
|
||||
) {
|
||||
private val currentItems = MutableStateFlow<List<SatItem>>(emptyList())
|
||||
private val currentType = MutableStateFlow("All")
|
||||
private val currentQuery = MutableStateFlow("")
|
||||
private val itemsWithType = currentType.flatMapLatest { type ->
|
||||
currentItems.map { items -> items.filterByType(type) }
|
||||
}
|
||||
private val itemsWithQuery = currentQuery.flatMapLatest { query ->
|
||||
itemsWithType.map { items -> items.filterByQuery(query) }
|
||||
}
|
||||
|
||||
fun getCurrentType() = currentType.value
|
||||
|
||||
fun getTypesList() = dataRepository.getSatelliteTypes()
|
||||
|
||||
suspend fun getEntriesFlow() = withContext(dispatcher) {
|
||||
currentItems.value = dataRepository.getEntriesList()
|
||||
return@withContext itemsWithQuery
|
||||
}
|
||||
|
||||
suspend fun setType(type: String) = withContext(dispatcher) { currentType.value = type }
|
||||
|
||||
suspend fun setQuery(query: String) = withContext(dispatcher) { currentQuery.value = query }
|
||||
|
||||
suspend fun updateSelection(selectAll: Boolean) = withContext(dispatcher) {
|
||||
updateSelection(itemsWithQuery.first().map { item -> item.catnum }, selectAll)
|
||||
}
|
||||
|
||||
suspend fun updateSelection(catNums: List<Int>, isSelected: Boolean) = withContext(dispatcher) {
|
||||
currentItems.value = currentItems.value.map { item ->
|
||||
if (item.catnum in catNums) item.copy(isSelected = isSelected) else item
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveSelection() = withContext(dispatcher) {
|
||||
val currentSelection = currentItems.value.filter { it.isSelected }.map { it.catnum }
|
||||
settingsRepository.saveEntriesSelection(currentSelection)
|
||||
}
|
||||
|
||||
private suspend fun List<SatItem>.filterByType(type: String) = withContext(dispatcher) {
|
||||
if (type == "All") return@withContext this@filterByType
|
||||
val catnums = settingsRepository.loadSatType(type)
|
||||
if (catnums.isEmpty()) return@withContext this@filterByType
|
||||
return@withContext this@filterByType.filter { item -> item.catnum in catnums }
|
||||
}
|
||||
|
||||
private suspend fun List<SatItem>.filterByQuery(query: String) = withContext(dispatcher) {
|
||||
if (query.isBlank()) return@withContext this@filterByQuery
|
||||
return@withContext try {
|
||||
this@filterByQuery.filter { it.catnum == query.toInt() }
|
||||
} catch (e: Exception) {
|
||||
this@filterByQuery.filter { item -> item.name.lowercase().contains(query.lowercase()) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,12 +33,10 @@ interface IDataRepository {
|
||||
|
||||
fun getSatelliteTypes(): List<String>
|
||||
|
||||
suspend fun getEntriesWithModes(): List<SatItem>
|
||||
suspend fun getEntriesList(): List<SatItem>
|
||||
|
||||
suspend fun getEntriesWithIds(ids: List<Int>): List<Satellite>
|
||||
|
||||
suspend fun getEntriesWithSelection(): List<SatItem>
|
||||
|
||||
suspend fun getRadiosWithId(id: Int): List<SatRadio>
|
||||
|
||||
fun updateFromFile(uri: String)
|
||||
|
||||
@@ -20,6 +20,6 @@ package com.rtbishop.look4sat.model
|
||||
data class SatItem(
|
||||
val catnum: Int,
|
||||
val name: String,
|
||||
val modes: List<String>,
|
||||
var isSelected: Boolean
|
||||
// val modes: List<String>,
|
||||
val isSelected: Boolean
|
||||
)
|
||||
Reference in new issue
Block a user