-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
509 additions
and
70 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
common/src/main/java/io/novafoundation/nova/common/utils/CollectionDiffer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.novafoundation.nova.common.utils | ||
|
||
interface Identifiable { | ||
|
||
val identifier: String | ||
} | ||
|
||
object CollectionDiffer { | ||
|
||
class Diff<T>( | ||
val newOrUpdated: List<T>, | ||
val removed: List<T> | ||
) | ||
|
||
fun <T : Identifiable> findDiff(newItems: List<T>, oldItems: List<T>): Diff<T> { | ||
|
||
val newKeys: Set<String> = newItems.mapTo(mutableSetOf()) { it.identifier } | ||
val oldMapping = oldItems.associateBy { it.identifier } | ||
|
||
val newOrUpdated = newItems.mapNotNull { new -> | ||
val old = oldMapping[new.identifier] | ||
|
||
when { | ||
old == null -> new // new | ||
old != new -> new // updated | ||
else -> null // same | ||
} | ||
} | ||
|
||
val removed = oldItems.filter { it.identifier !in newKeys } | ||
|
||
return Diff(newOrUpdated, removed) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
core-db/src/main/java/io/novafoundation/nova/core_db/converters/NftTypeConverters.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.novafoundation.nova.core_db.converters | ||
|
||
import androidx.room.TypeConverter | ||
import io.novafoundation.nova.core_db.model.NftLocal | ||
|
||
class NftTypeConverters { | ||
|
||
@TypeConverter | ||
fun from(type: NftLocal.Type): String { | ||
return type.name | ||
} | ||
|
||
@TypeConverter | ||
fun to(name: String): NftLocal.Type { | ||
return enumValueOf(name) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
core-db/src/main/java/io/novafoundation/nova/core_db/dao/NftDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.novafoundation.nova.core_db.dao | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
import androidx.room.Update | ||
import io.novafoundation.nova.common.utils.CollectionDiffer | ||
import io.novafoundation.nova.core_db.model.NftLocal | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
interface NftDao { | ||
|
||
@Query("SELECT * FROM nfts WHERE metaId = :metaId") | ||
fun nftsFlow(metaId: Long): Flow<List<NftLocal>> | ||
|
||
@Query("SELECT * FROM nfts WHERE metaId = :metaId AND type = :type") | ||
suspend fun getNfts(metaId: Long, type: NftLocal.Type): List<NftLocal> | ||
|
||
@Delete | ||
suspend fun deleteNfts(nfts: List<NftLocal>) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insertNfts(nfts: List<NftLocal>) | ||
|
||
@Update | ||
suspend fun updateNft(nft: NftLocal) | ||
|
||
@Transaction | ||
suspend fun insertNftsDiff(nftType: NftLocal.Type, metaId: Long, newNfts: List<NftLocal>) { | ||
val oldNfts = getNfts(metaId, nftType) | ||
|
||
val diff = CollectionDiffer.findDiff(newNfts, oldNfts) | ||
|
||
deleteNfts(diff.removed) | ||
|
||
insertNfts(diff.newOrUpdated) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
core-db/src/main/java/io/novafoundation/nova/core_db/migrations/5_6_AddNfts.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package io.novafoundation.nova.core_db.migrations | ||
|
||
import androidx.room.migration.Migration | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
|
||
val AddNfts_5_6 = object : Migration(5, 6) { | ||
|
||
override fun migrate(database: SupportSQLiteDatabase) { | ||
database.execSQL(""" | ||
CREATE TABLE IF NOT EXISTS `nfts` ( | ||
`identifier` TEXT NOT NULL, | ||
`metaId` INTEGER NOT NULL, | ||
`chainId` TEXT NOT NULL, | ||
`collectionId` TEXT, | ||
`instanceId` TEXT, | ||
`metadata` BLOB, | ||
`name` TEXT, | ||
`label` TEXT, | ||
`media` TEXT, | ||
`price` TEXT, | ||
`type` TEXT NOT NULL, | ||
PRIMARY KEY(`identifier`)) | ||
""" | ||
) | ||
database.execSQL("CREATE INDEX IF NOT EXISTS `index_nfts_metaId` ON `nfts` (`metaId`)") | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
core-db/src/main/java/io/novafoundation/nova/core_db/model/NftLocal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.novafoundation.nova.core_db.model | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import io.novafoundation.nova.common.utils.Identifiable | ||
import io.novafoundation.nova.common.utils.optionalContentEquals | ||
import java.math.BigInteger | ||
|
||
@Entity(tableName = "nfts") | ||
class NftLocal( | ||
@PrimaryKey | ||
override val identifier: String, | ||
@ColumnInfo(index = true) | ||
val metaId: Long, | ||
val chainId: String, | ||
val collectionId: String?, | ||
val instanceId: String?, | ||
val metadata: ByteArray?, | ||
// --- metadata fields --- | ||
// name is always be present. null in case it is not loaded (nft is partially loaded) | ||
val name: String? = null, | ||
val label: String? = null, | ||
val media: String? = null, | ||
val price: BigInteger? = null, | ||
// --- metadata fields --- | ||
|
||
val type: Type | ||
): Identifiable { | ||
|
||
enum class Type { | ||
UNIQUES, RMRK1 | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
return other is NftLocal && | ||
identifier == other.identifier && | ||
// metadata is either direct data or a link to immutable distributed storage | ||
metadata.optionalContentEquals(other.metadata) | ||
&& price == other.price | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.