Skip to content

Commit

Permalink
refactor: migrate QuickChat to Compose (#1419)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir authored Nov 19, 2024
1 parent 4855576 commit 475e9fc
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 338 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
package com.geeksville.mesh.database

import com.geeksville.mesh.CoroutineDispatchers
import com.geeksville.mesh.database.dao.QuickChatActionDao
import com.geeksville.mesh.database.entity.QuickChatAction
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext
import javax.inject.Inject

class QuickChatActionRepository @Inject constructor(private val quickChatDaoLazy: dagger.Lazy<QuickChatActionDao>) {
class QuickChatActionRepository @Inject constructor(
private val quickChatDaoLazy: dagger.Lazy<QuickChatActionDao>,
private val dispatchers: CoroutineDispatchers,
) {
private val quickChatActionDao by lazy {
quickChatDaoLazy.get()
}

suspend fun getAllActions(): Flow<List<QuickChatAction>> = withContext(Dispatchers.IO) {
quickChatActionDao.getAll()
}
fun getAllActions() = quickChatActionDao.getAll().flowOn(dispatchers.io)

suspend fun insert(action: QuickChatAction) = withContext(Dispatchers.IO) {
quickChatActionDao.insert(action)
suspend fun upsert(action: QuickChatAction) = withContext(dispatchers.io) {
quickChatActionDao.upsert(action)
}

suspend fun deleteAll() = withContext(Dispatchers.IO) {
suspend fun deleteAll() = withContext(dispatchers.io) {
quickChatActionDao.deleteAll()
}

suspend fun delete(action: QuickChatAction) = withContext(Dispatchers.IO) {
suspend fun delete(action: QuickChatAction) = withContext(dispatchers.io) {
quickChatActionDao.delete(action)
}

suspend fun update(action: QuickChatAction) = withContext(Dispatchers.IO) {
quickChatActionDao.update(action)
}

suspend fun setItemPosition(uuid: Long, newPos: Int) = withContext(Dispatchers.IO) {
suspend fun setItemPosition(uuid: Long, newPos: Int) = withContext(dispatchers.io) {
quickChatActionDao.updateActionPosition(uuid, newPos)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.geeksville.mesh.database.dao

import androidx.room.*
import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Upsert
import com.geeksville.mesh.database.entity.QuickChatAction
import kotlinx.coroutines.flow.Flow

Expand All @@ -10,8 +13,8 @@ interface QuickChatActionDao {
@Query("Select * from quick_chat order by position asc")
fun getAll(): Flow<List<QuickChatAction>>

@Insert
fun insert(action: QuickChatAction)
@Upsert
fun upsert(action: QuickChatAction)

@Query("Delete from quick_chat")
fun deleteAll()
Expand All @@ -25,13 +28,9 @@ interface QuickChatActionDao {
decrementPositionsAfter(action.position)
}

@Update
fun update(action: QuickChatAction)

@Query("Update quick_chat set position=:position WHERE uuid=:uuid")
fun updateActionPosition(uuid: Long, position: Int)

@Query("Update quick_chat set position=position-1 where position>=:position")
fun decrementPositionsAfter(position: Int)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import androidx.room.PrimaryKey

@Entity(tableName = "quick_chat")
data class QuickChatAction(
@PrimaryKey(autoGenerate = true) val uuid: Long,
@ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "message") val message: String,
@ColumnInfo(name = "mode") val mode: Mode,
@PrimaryKey(autoGenerate = true) val uuid: Long = 0L,
@ColumnInfo(name = "name") val name: String = "",
@ColumnInfo(name = "message") val message: String = "",
@ColumnInfo(name = "mode") val mode: Mode = Mode.Instant,
@ColumnInfo(name = "position") val position: Int
) {
enum class Mode {
Expand Down
50 changes: 11 additions & 39 deletions app/src/main/java/com/geeksville/mesh/model/UIState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted.Companion.Eagerly
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
Expand Down Expand Up @@ -186,8 +186,8 @@ class UIViewModel @Inject constructor(
private val _channels = MutableStateFlow(channelSet {})
val channels: StateFlow<AppOnlyProtos.ChannelSet> get() = _channels

private val _quickChatActions = MutableStateFlow<List<QuickChatAction>>(emptyList())
val quickChatActions: StateFlow<List<QuickChatAction>> = _quickChatActions
val quickChatActions get() = quickChatActionRepository.getAllActions()
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

private val _focusedNode = MutableStateFlow<NodeEntity?>(null)
val focusedNode: StateFlow<NodeEntity?> = _focusedNode
Expand Down Expand Up @@ -230,7 +230,7 @@ class UIViewModel @Inject constructor(
)
}.stateIn(
scope = viewModelScope,
started = Eagerly,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = NodesUiState.Empty,
)

Expand All @@ -239,7 +239,7 @@ class UIViewModel @Inject constructor(
nodeDB.getNodes(state.sort, state.filter, state.includeUnknown)
}.stateIn(
scope = viewModelScope,
started = Eagerly,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = emptyList(),
)

Expand Down Expand Up @@ -270,11 +270,6 @@ class UIViewModel @Inject constructor(
radioConfigRepository.moduleConfigFlow.onEach { config ->
_moduleConfig.value = config
}.launchIn(viewModelScope)
viewModelScope.launch {
quickChatActionRepository.getAllActions().collect { actions ->
_quickChatActions.value = actions
}
}
radioConfigRepository.channelSetFlow.onEach { channelSet ->
_channels.value = channelSet
}.launchIn(viewModelScope)
Expand Down Expand Up @@ -327,7 +322,7 @@ class UIViewModel @Inject constructor(
}
}.stateIn(
scope = viewModelScope,
started = Eagerly,
started = SharingStarted.WhileSubscribed(5_000),
initialValue = emptyList(),
)

Expand Down Expand Up @@ -699,39 +694,16 @@ class UIViewModel @Inject constructor(
}
}

fun addQuickChatAction(name: String, value: String, mode: QuickChatAction.Mode) {
viewModelScope.launch(Dispatchers.Main) {
val action = QuickChatAction(0, name, value, mode, _quickChatActions.value.size)
quickChatActionRepository.insert(action)
}
}

fun deleteQuickChatAction(action: QuickChatAction) {
viewModelScope.launch(Dispatchers.Main) {
quickChatActionRepository.delete(action)
}
fun addQuickChatAction(action: QuickChatAction) = viewModelScope.launch(Dispatchers.IO) {
quickChatActionRepository.upsert(action)
}

fun updateQuickChatAction(
action: QuickChatAction,
name: String?,
message: String?,
mode: QuickChatAction.Mode?
) {
viewModelScope.launch(Dispatchers.Main) {
val newAction = QuickChatAction(
action.uuid,
name ?: action.name,
message ?: action.message,
mode ?: action.mode,
action.position
)
quickChatActionRepository.update(newAction)
}
fun deleteQuickChatAction(action: QuickChatAction) = viewModelScope.launch(Dispatchers.IO) {
quickChatActionRepository.delete(action)
}

fun updateActionPositions(actions: List<QuickChatAction>) {
viewModelScope.launch(Dispatchers.Main) {
viewModelScope.launch(Dispatchers.IO) {
for (position in actions.indices) {
quickChatActionRepository.setItemPosition(actions[position].uuid, position)
}
Expand Down
Loading

0 comments on commit 475e9fc

Please sign in to comment.