Add position via dragging the recyclerview

This commit is contained in:
Douile 2022-08-16 11:46:57 +01:00
parent 1bdb6bf340
commit 01e24ff6a4
No known key found for this signature in database
GPG key ID: DC9D70626CEF33D0
7 changed files with 46 additions and 49 deletions

View file

@ -32,7 +32,7 @@ class QuickChatActionRepository @Inject constructor(private val quickChatDaoLazy
quickChatActionDao.update(action)
}
suspend fun moveAction(action: QuickChatAction, newPos: Int) = withContext(Dispatchers.IO) {
quickChatActionDao.moveAction(action, newPos)
suspend fun setItemPosition(uuid: Long, newPos: Int) = withContext(Dispatchers.IO) {
quickChatActionDao.updateActionPosition(uuid, newPos)
}
}

View file

@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface QuickChatActionDao {
@Query("Select * from quick_chat")
@Query("Select * from quick_chat order by position asc")
fun getAll(): Flow<List<QuickChatAction>>
@Insert
@ -31,27 +31,7 @@ interface QuickChatActionDao {
@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>=:startInclusive and position<:endExclusive")
fun incrementPositionsBetween(startInclusive: Int, endExclusive: Int)
@Query("Update quick_chat SET position=position-1 where position>:startExclusive and position<=:endInclusive")
fun decrementPositionsBetween(startExclusive: Int, endInclusive: Int)
@Query("Update quick_chat set position=position-1 where position>=:position")
fun decrementPositionsAfter(position: Int)
@Transaction
fun moveAction(action: QuickChatAction, newPos: Int) {
// FIXME: Check newPos is valid
if (newPos < action.position) {
incrementPositionsBetween(newPos, action.position)
updateActionPosition(action.uuid, newPos)
} else if (newPos > action.position) {
decrementPositionsBetween(action.position, newPos)
updateActionPosition(action.uuid, newPos)
} else {
// Do nothing: moving to same position
}
}
}

View file

@ -5,7 +5,7 @@ import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
@Entity(tableName = "quick_chat", indices = [Index(value=["position"], unique=true)])
@Entity(tableName = "quick_chat")
data class QuickChatAction(
@PrimaryKey(autoGenerate = true) val uuid: Long,
@ColumnInfo(name="name") val name: String,