1
0
mirror of https://gitlab.shinice.net/pixeldroid/PixelDroid synced 2024-12-18 03:09:34 +01:00

Update instance if it already exists (to enable multiple accounts of same instance)

This commit is contained in:
Matthieu 2021-03-15 19:54:42 +01:00
parent 960eb0cdc1
commit 0fdcdf3c21
2 changed files with 17 additions and 7 deletions

View File

@ -49,5 +49,5 @@ fun storeInstance(db: AppDatabase, nodeInfo: NodeInfo?, instance: Instance? = nu
)
} ?: throw IllegalArgumentException("Cannot store instance where both are null")
db.instanceDao().insertInstance(dbInstance)
db.instanceDao().insertOrUpdate(dbInstance)
}

View File

@ -1,9 +1,6 @@
package com.h.pixeldroid.utils.db.dao
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.*
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity
@Dao
@ -11,6 +8,19 @@ interface InstanceDao {
@Query("SELECT * FROM instances")
fun getAll(): List<InstanceDatabaseEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertInstance(instance: InstanceDatabaseEntity)
/**
* Insert an instance, if it already exists return -1
*/
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertInstance(instance: InstanceDatabaseEntity): Long
@Update
fun updateInstance(instance: InstanceDatabaseEntity)
@Transaction
fun insertOrUpdate(instance: InstanceDatabaseEntity) {
if (insertInstance(instance) == -1L) {
updateInstance(instance)
}
}
}