mirror of
https://github.com/atsunatsu/Look4Sat.git
synced 2026-10-02 03:15:37 +00:00
Moved satellite selections storage to SharedPreferences
This commit is contained in:
1 parent
a7c840d532
commit
92eec290b1
10 files changed
+88
-96
No files matched your search
@@ -44,6 +44,7 @@ class SettingsHandler @Inject constructor(private val prefs: SharedPreferences)
|
||||
const val keyLongitude = "stationLon"
|
||||
// const val keyPositionGPS = "setPositionGPS"
|
||||
// const val keyPositionQTH = "setPositionQTH"
|
||||
const val keySelection = "selection"
|
||||
}
|
||||
|
||||
override fun loadStationPosition(): GeoPos {
|
||||
@@ -60,6 +61,16 @@ class SettingsHandler @Inject constructor(private val prefs: SharedPreferences)
|
||||
}
|
||||
}
|
||||
|
||||
override fun saveSatelliteSelection(catnums: List<Int>) {
|
||||
val stringList = catnums.map { catnum -> catnum.toString() }
|
||||
prefs.edit { putStringSet(keySelection, stringList.toSet()) }
|
||||
}
|
||||
|
||||
override fun loadSatelliteSelection(): List<Int> {
|
||||
val catnums = prefs.getStringSet(keySelection, emptySet())?.map { catnum -> catnum.toInt() }
|
||||
return catnums?.sorted() ?: emptyList()
|
||||
}
|
||||
|
||||
override fun getHoursAhead(): Int {
|
||||
return prefs.getInt(keyHoursAhead, 8)
|
||||
}
|
||||
|
||||
@@ -20,48 +20,29 @@ package com.rtbishop.look4sat.framework.local
|
||||
import androidx.room.*
|
||||
import com.rtbishop.look4sat.framework.model.SatEntry
|
||||
import com.rtbishop.look4sat.framework.model.SatItem
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface EntriesDao {
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT catnum, name, isSelected FROM entries ORDER BY name ASC")
|
||||
fun getSatelliteItems(): Flow<List<SatItem>>
|
||||
suspend fun getAllSatellites(): List<SatItem>
|
||||
|
||||
@Query("SELECT catnum FROM entries WHERE isSelected = 1")
|
||||
suspend fun getEntriesSelection(): List<Int>
|
||||
@Transaction
|
||||
suspend fun getSelectedSatellites(catnums: List<Int>): List<SatEntry> {
|
||||
val selectedSatellites = mutableListOf<SatEntry>()
|
||||
catnums.chunked(999).forEach { chunkedList ->
|
||||
selectedSatellites.addAll(getSelectedSatellitesChunked(chunkedList))
|
||||
}
|
||||
return selectedSatellites
|
||||
}
|
||||
|
||||
@Query("SELECT * FROM entries WHERE isSelected = 1")
|
||||
suspend fun getSelectedSatellites(): List<SatEntry>
|
||||
@Query("SELECT * FROM entries WHERE catnum IN (:catnums)")
|
||||
suspend fun getSelectedSatellitesChunked(catnums: List<Int>): List<SatEntry>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertEntries(entries: List<SatEntry>)
|
||||
|
||||
@Transaction
|
||||
suspend fun updateEntries(entries: List<SatEntry>) {
|
||||
val entriesSelection = getEntriesSelection()
|
||||
insertEntries(entries)
|
||||
restoreEntriesSelection(entriesSelection)
|
||||
}
|
||||
|
||||
@Transaction
|
||||
suspend fun restoreEntriesSelection(catnums: List<Int>) {
|
||||
updateEntriesSelection(catnums)
|
||||
}
|
||||
|
||||
@Transaction
|
||||
suspend fun updateEntriesSelection(catnums: List<Int>) {
|
||||
clearEntriesSelection()
|
||||
catnums.forEach { catnum -> updateEntrySelection(catnum) }
|
||||
}
|
||||
|
||||
@Query("UPDATE entries SET isSelected = 1 WHERE catnum = :catnum")
|
||||
suspend fun updateEntrySelection(catnum: Int)
|
||||
|
||||
@Query("UPDATE entries SET isSelected = 0")
|
||||
suspend fun clearEntriesSelection()
|
||||
|
||||
@Query("DELETE FROM entries")
|
||||
suspend fun deleteEntries()
|
||||
}
|
||||
@@ -29,8 +29,6 @@ import com.rtbishop.look4sat.framework.toDomainItems
|
||||
import com.rtbishop.look4sat.framework.toFramework
|
||||
import com.rtbishop.look4sat.framework.toFrameworkEntries
|
||||
import kotlinx.coroutines.CoroutineDispatcher
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.InputStream
|
||||
|
||||
@@ -41,8 +39,16 @@ class LocalSource(
|
||||
private val transmittersDao: TransmittersDao,
|
||||
) : ILocalSource {
|
||||
|
||||
override fun getSatelliteItems(): Flow<List<SatItem>> {
|
||||
return entriesDao.getSatelliteItems().map { items -> items.toDomainItems() }
|
||||
override suspend fun getAllSatellites(): List<SatItem> {
|
||||
return entriesDao.getAllSatellites().toDomainItems()
|
||||
}
|
||||
|
||||
override suspend fun getSelectedSatellites(catnums: List<Int>): List<Satellite> {
|
||||
return entriesDao.getSelectedSatellites(catnums).map { entry -> entry.tle.createSat() }
|
||||
}
|
||||
|
||||
override suspend fun getTransmitters(catnum: Int): List<Transmitter> {
|
||||
return transmittersDao.getTransmitters(catnum).toDomain()
|
||||
}
|
||||
|
||||
@Suppress("BlockingMethodInNonBlockingContext")
|
||||
@@ -50,27 +56,15 @@ class LocalSource(
|
||||
return withContext(ioDispatcher) { resolver.openInputStream(Uri.parse(uri)) }
|
||||
}
|
||||
|
||||
override suspend fun getSelectedSatellites(): List<Satellite> {
|
||||
return entriesDao.getSelectedSatellites().map { entry -> entry.tle.createSat() }
|
||||
}
|
||||
|
||||
override suspend fun getTransmitters(catnum: Int): List<Transmitter> {
|
||||
return transmittersDao.getTransmitters(catnum).toDomain()
|
||||
}
|
||||
|
||||
override suspend fun updateEntries(entries: List<SatEntry>) {
|
||||
entriesDao.updateEntries(entries.toFrameworkEntries())
|
||||
}
|
||||
|
||||
override suspend fun updateEntriesSelection(catnums: List<Int>) {
|
||||
entriesDao.updateEntriesSelection(catnums)
|
||||
entriesDao.insertEntries(entries.toFrameworkEntries())
|
||||
}
|
||||
|
||||
override suspend fun updateTransmitters(transmitters: List<Transmitter>) {
|
||||
transmittersDao.updateTransmitters(transmitters.toFramework())
|
||||
}
|
||||
|
||||
override suspend fun clearData() {
|
||||
override suspend fun clearAllData() {
|
||||
entriesDao.deleteEntries()
|
||||
transmittersDao.deleteTransmitters()
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import androidx.room.Relation
|
||||
data class SatItem(
|
||||
val catnum: Int,
|
||||
val name: String,
|
||||
var isSelected: Boolean,
|
||||
var isSelected: Boolean = false,
|
||||
@Relation(
|
||||
parentColumn = "catnum",
|
||||
entity = Transmitter::class,
|
||||
|
||||
+3
-5
@@ -34,9 +34,7 @@ class EntriesViewModel @Inject constructor(
|
||||
|
||||
private val transModes = MutableLiveData(preferences.loadModesSelection())
|
||||
private val currentQuery = MutableLiveData(String())
|
||||
private val itemsFromRepo = liveData {
|
||||
repository.getSatelliteItems().collect { itemsAll -> emit(itemsAll) }
|
||||
} as MutableLiveData
|
||||
private val itemsFromRepo = liveData { emit(repository.getAllSatellites()) } as MutableLiveData
|
||||
private val itemsWithModes = transModes.switchMap { modes ->
|
||||
itemsFromRepo.map { items -> filterByModes(items, modes) }
|
||||
}
|
||||
@@ -69,8 +67,8 @@ class EntriesViewModel @Inject constructor(
|
||||
|
||||
fun saveSelection() {
|
||||
itemsFromRepo.value?.let { itemsAll ->
|
||||
val selectedIds = itemsAll.filter { it.isSelected }.map { it.catnum }
|
||||
repository.updateSelection(selectedIds)
|
||||
val filteredItems = itemsAll.filter { item -> item.isSelected }
|
||||
repository.updatesSelection(filteredItems.map { item -> item.catnum })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -18,15 +18,15 @@ class SettingsViewModel @Inject constructor(
|
||||
) : ViewModel() {
|
||||
|
||||
fun updateDataFromFile(uri: String) {
|
||||
repository.updateDataFromFile(uri)
|
||||
repository.updateFromFile(uri)
|
||||
}
|
||||
|
||||
fun updateDataFromWeb(sources: List<String>) {
|
||||
repository.updateDataFromWeb(sources)
|
||||
repository.updateFromWeb(sources)
|
||||
}
|
||||
|
||||
fun clearData() {
|
||||
repository.clearData()
|
||||
repository.clearAllData()
|
||||
}
|
||||
|
||||
fun getUseUTC(): Boolean = settings.getUseUTC()
|
||||
@@ -53,9 +53,9 @@ class SettingsViewModel @Inject constructor(
|
||||
|
||||
fun setRotatorPort(value: String) = settings.setRotatorPort(value)
|
||||
|
||||
fun getUpdateState() = repository.dataUpdateState
|
||||
fun getUpdateState() = repository.updateState
|
||||
|
||||
fun setUpdateHandled() = repository.setDataUpdateHandled()
|
||||
fun setUpdateHandled() = repository.setUpdateStateHandled()
|
||||
|
||||
val stationPosition: SharedFlow<DataState<GeoPos>> = locationHandler.stationPosition
|
||||
|
||||
|
||||
Reference in new issue
Block a user