refactor: migrate QuickChat to Compose (#1419)

This commit is contained in:
Andre K 2024-11-19 11:59:28 -03:00 committed by GitHub
parent 4855576248
commit 475e9fc22c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 375 additions and 362 deletions

View file

@ -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 upsert(action: QuickChatAction) = withContext(dispatchers.io) {
quickChatActionDao.upsert(action)
}
suspend fun insert(action: QuickChatAction) = withContext(Dispatchers.IO) {
quickChatActionDao.insert(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)
}
}

View file

@ -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
@ -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()
@ -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)
}
}

View file

@ -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 {