diff --git a/app/src/main/java/com/h/pixeldroid/utils/db/DBUtils.kt b/app/src/main/java/com/h/pixeldroid/utils/db/DBUtils.kt index 9193bf07..08d3a490 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/db/DBUtils.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/db/DBUtils.kt @@ -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) } \ No newline at end of file diff --git a/app/src/main/java/com/h/pixeldroid/utils/db/dao/InstanceDao.kt b/app/src/main/java/com/h/pixeldroid/utils/db/dao/InstanceDao.kt index dad64a5a..c4887174 100644 --- a/app/src/main/java/com/h/pixeldroid/utils/db/dao/InstanceDao.kt +++ b/app/src/main/java/com/h/pixeldroid/utils/db/dao/InstanceDao.kt @@ -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 - @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) + } + } } \ No newline at end of file