feat: preserve map center and zoom state in ViewModel

closes #1150
This commit is contained in:
andrekir 2024-09-30 19:13:56 -03:00
parent bfa2eaef11
commit 3bd4132089
2 changed files with 46 additions and 19 deletions

View file

@ -24,7 +24,6 @@ import com.geeksville.mesh.database.QuickChatActionRepository
import com.geeksville.mesh.database.entity.NodeEntity
import com.geeksville.mesh.database.entity.Packet
import com.geeksville.mesh.database.entity.QuickChatAction
import com.geeksville.mesh.database.entity.toNodeInfo
import com.geeksville.mesh.repository.datastore.RadioConfigRepository
import com.geeksville.mesh.repository.radio.RadioInterfaceService
import com.geeksville.mesh.service.MeshService
@ -43,8 +42,10 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.osmdroid.util.GeoPoint
import java.io.BufferedWriter
import java.io.FileNotFoundException
import java.io.FileWriter
@ -119,6 +120,15 @@ data class NodesUiState(
}
}
data class MapState(
val center: GeoPoint? = null,
val zoom: Double = 0.0,
) {
companion object {
val Empty = MapState()
}
}
data class Contact(
val contactKey: String,
val shortName: String,
@ -231,8 +241,13 @@ class UIViewModel @Inject constructor(
val myNodeInfo: StateFlow<MyNodeInfo?> get() = nodeDB.myNodeInfo
val ourNodeInfo: StateFlow<NodeEntity?> get() = nodeDB.ourNodeInfo
// FIXME only used in MapFragment
val initialNodes get() = nodeDB.nodeDBbyNum.value.values.map { it.toNodeInfo() }
val nodesWithPosition get() = nodeDB.nodeDBbyNum.value.values.filter { it.validPosition != null }
private val _mapState = MutableStateFlow(MapState.Empty)
val mapState: StateFlow<MapState> get() = _mapState
fun updateMapCenterAndZoom(center: GeoPoint, zoom: Double) =
_mapState.update { it.copy(center = center, zoom = zoom) }
fun getUser(userId: String?) = nodeDB.getUser(userId) ?: user {
id = userId.orEmpty()