refactor: convert NodeDB to repository

This commit is contained in:
andrekir 2023-10-20 18:31:13 -03:00 committed by Andre K
parent d1d2c6cf3d
commit c489717ad1
13 changed files with 110 additions and 101 deletions

View file

@ -87,7 +87,7 @@ class ContactsFragment : ScreenFragment("Messages"), Logging {
val toBroadcast = contact.to == DataPacket.ID_BROADCAST
// grab usernames from NodeInfo
val nodes = model.nodeDB.nodes.value!!
val nodes = model.nodeDB.nodes.value
val node = nodes[if (fromLocal) contact.to else contact.from]
//grab channel names from DeviceConfig
@ -212,7 +212,7 @@ class ContactsFragment : ScreenFragment("Messages"), Logging {
contactsAdapter.onChannelsChanged()
}
model.nodeDB.nodes.observe(viewLifecycleOwner) {
model.nodeDB.nodes.asLiveData().observe(viewLifecycleOwner) {
contactsAdapter.notifyDataSetChanged()
}

View file

@ -93,7 +93,7 @@ class MessagesFragment : Fragment(), Logging {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val packet = messages[position]
val msg = packet.data
val nodes = model.nodeDB.nodes.value!!
val nodes = model.nodeDB.nodes.value
val node = nodes[msg.from]
// Determine if this is my message (originated on this device)
val isLocal = msg.from == DataPacket.ID_LOCAL

View file

@ -321,7 +321,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
}
// Also watch myNodeInfo because it might change later
model.myNodeInfo.observe(viewLifecycleOwner) {
model.myNodeInfo.asLiveData().observe(viewLifecycleOwner) {
updateNodeInfo()
}

View file

@ -322,7 +322,7 @@ class UsersFragment : ScreenFragment("Users"), Logging {
values
}.toTypedArray()
model.nodeDB.nodes.observe(viewLifecycleOwner) {
model.nodeDB.nodes.asLiveData().observe(viewLifecycleOwner) {
nodesAdapter.onNodesChanged(it.perhapsReindexBy(model.myNodeNum))
}

View file

@ -32,6 +32,7 @@ import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.geeksville.mesh.BuildConfig
import com.geeksville.mesh.DataPacket
@ -187,7 +188,7 @@ fun MapView(model: UIViewModel = viewModel()) {
requestPermissionAndToggleLauncher.launch(context.getLocationPermissions())
}
val nodes by model.nodeDB.nodes.observeAsState(emptyMap())
val nodes by model.nodeDB.nodes.collectAsStateWithLifecycle()
val waypoints by model.waypoints.observeAsState(emptyMap())
var showDownloadButton: Boolean by remember { mutableStateOf(false) }
@ -256,8 +257,7 @@ fun MapView(model: UIViewModel = viewModel()) {
}
fun getUsername(id: String?) = if (id == DataPacket.ID_LOCAL) context.getString(R.string.you)
else model.nodeDB.nodes.value?.get(id)?.user?.longName
?: context.getString(R.string.unknown_username)
else model.nodeDB.nodes.value[id]?.user?.longName ?: context.getString(R.string.unknown_username)
fun MapView.onWaypointChanged(waypoints: Collection<Packet>): List<MarkerWithLabel> {
return waypoints.mapNotNull { waypoint ->