diff --git a/.gitignore b/.gitignore index adf78c8..f365056 100644 --- a/.gitignore +++ b/.gitignore @@ -85,3 +85,6 @@ fabric.properties */build/ /build /kotlin-js-store/ +/default.realm +/default.realm.lock +/default.realm.management/ diff --git a/carddb/build.gradle.kts b/carddb/build.gradle.kts index d742ba5..0703ce3 100644 --- a/carddb/build.gradle.kts +++ b/carddb/build.gradle.kts @@ -3,6 +3,7 @@ plugins { kotlin("plugin.serialization") // id("com.android.library") id("com.squareup.sqldelight") + id("io.realm.kotlin") version "1.6.0" } kotlin { @@ -18,6 +19,7 @@ kotlin { dependencies { implementation(libs.bundles.base) implementation(libs.sqldelight.coroutines) + implementation("io.realm.kotlin:library-base:1.6.0") } } val jvmMain by getting { diff --git a/carddb/src/commonMain/kotlin/DatabaseHelper.kt b/carddb/src/commonMain/kotlin/DatabaseHelper.kt index 030a56e..160bbdd 100644 --- a/carddb/src/commonMain/kotlin/DatabaseHelper.kt +++ b/carddb/src/commonMain/kotlin/DatabaseHelper.kt @@ -1,21 +1,8 @@ -import com.mfriend.db.MTGDb -import com.squareup.sqldelight.db.SqlDriver -import com.squareup.sqldelight.runtime.coroutines.asFlow -import com.squareup.sqldelight.runtime.coroutines.mapToList import commfrienddb.Card import kotlinx.coroutines.flow.Flow interface DatabaseHelper { suspend fun insertCard(card: Card) - fun getCards(): Flow> -} - -class DatabaseHelperImpl(sqlDriver: SqlDriver) : DatabaseHelper { - private val database = MTGDb(sqlDriver) - override suspend fun insertCard(card: Card) { - database.cardQueries.insertCard(card) - } - override fun getCards(): Flow> { - return database.cardQueries.selectAll().asFlow().mapToList() - } -} + fun getCardsFlow(): Flow> + suspend fun getCards(): List +} \ No newline at end of file diff --git a/carddb/src/commonMain/kotlin/Koin.kt b/carddb/src/commonMain/kotlin/Koin.kt index c7e7fa6..ea276b5 100644 --- a/carddb/src/commonMain/kotlin/Koin.kt +++ b/carddb/src/commonMain/kotlin/Koin.kt @@ -6,6 +6,6 @@ import org.koin.dsl.module val databaseModule = module { includes(platformModule) - singleOf(::DatabaseHelperImpl) { bind() } + singleOf(::RealmDatabase) { bind() } } internal expect val platformModule: Module \ No newline at end of file diff --git a/carddb/src/commonMain/kotlin/RealmDatabase.kt b/carddb/src/commonMain/kotlin/RealmDatabase.kt new file mode 100644 index 0000000..7c4850d --- /dev/null +++ b/carddb/src/commonMain/kotlin/RealmDatabase.kt @@ -0,0 +1,32 @@ +import commfrienddb.Card +import io.realm.kotlin.Realm +import io.realm.kotlin.RealmConfiguration +import io.realm.kotlin.ext.query +import io.realm.kotlin.notifications.UpdatedResults +import io.realm.kotlin.query.RealmResults +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filterIsInstance +import kotlinx.coroutines.flow.map +import realm.RealmCard +import realm.toCard +import realm.toRealm + +class RealmDatabase : DatabaseHelper { + private val config = RealmConfiguration.create(schema = setOf(RealmCard::class)) + val realm = Realm.open(config) + override suspend fun insertCard(card: Card) { + realm.write { + copyToRealm(card.toRealm()) + } + } + + override fun getCardsFlow(): Flow> { + val results: RealmResults = realm.query().find() + return results.asFlow().filterIsInstance>().map { it.list.map(RealmCard::toCard) } + } + + override suspend fun getCards(): List { + val results: RealmResults = realm.query().find() + return results.map(RealmCard::toCard) + } +} \ No newline at end of file diff --git a/carddb/src/commonMain/kotlin/SqlDatabase.kt b/carddb/src/commonMain/kotlin/SqlDatabase.kt new file mode 100644 index 0000000..42a6253 --- /dev/null +++ b/carddb/src/commonMain/kotlin/SqlDatabase.kt @@ -0,0 +1,21 @@ +import com.mfriend.db.MTGDb +import com.squareup.sqldelight.db.SqlDriver +import com.squareup.sqldelight.runtime.coroutines.asFlow +import com.squareup.sqldelight.runtime.coroutines.mapToList +import commfrienddb.Card +import kotlinx.coroutines.flow.Flow + +class SqlDatabase(sqlDriver: SqlDriver) : DatabaseHelper { + private val database = MTGDb(sqlDriver) + override suspend fun insertCard(card: Card) { + database.cardQueries.insertCard(card) + } + + override fun getCardsFlow(): Flow> { + return database.cardQueries.selectAll().asFlow().mapToList() + } + + override suspend fun getCards(): List { + return database.cardQueries.selectAll().executeAsList() + } +} \ No newline at end of file diff --git a/carddb/src/commonMain/kotlin/realm/RealmCard.kt b/carddb/src/commonMain/kotlin/realm/RealmCard.kt new file mode 100644 index 0000000..2df4761 --- /dev/null +++ b/carddb/src/commonMain/kotlin/realm/RealmCard.kt @@ -0,0 +1,37 @@ +package realm + +import commfrienddb.Card +import io.realm.kotlin.types.RealmObject +import io.realm.kotlin.types.annotations.PrimaryKey +import org.mongodb.kbson.ObjectId + +class RealmCard( +) : RealmObject { + @PrimaryKey + var _id: ObjectId = ObjectId() + var name: String = "" + var set_code: String = "" + var set_name: String = "" + var scryfall_url: String = "" + var image_url: String? = null + var price: Double? = null + + constructor( + name: String, + set_code: String, + set_name: String, + scryfall_url: String, + image_url: String? = null, + price: Double? = null + ) : this() { + this.name = name + this.set_code = set_code + this.set_name = set_name + this.scryfall_url = scryfall_url + this.image_url = image_url + this.price = price + } +} + +internal fun Card.toRealm() = RealmCard(name, set_code, set_name, scryfall_url, image_url, price) +internal fun RealmCard.toCard() = Card(name, set_code, set_name, image_url, scryfall_url, price) \ No newline at end of file diff --git a/cli/src/main/kotlin/Cli.kt b/cli/src/main/kotlin/Cli.kt index ae31c6d..cb5ae4d 100644 --- a/cli/src/main/kotlin/Cli.kt +++ b/cli/src/main/kotlin/Cli.kt @@ -11,7 +11,7 @@ suspend fun main() = coroutineScope { val client: ScryfallApi = koin.get() val databaseHelper:DatabaseHelper = koin.get() launch { - databaseHelper.getCards().collect { + databaseHelper.getCardsFlow().collect { println(it) println() } @@ -25,7 +25,6 @@ suspend fun main() = coroutineScope { 2 -> searchCard(client) else -> break } - println(result) result.map { databaseHelper.insertCard(it) } } } @@ -38,6 +37,17 @@ suspend fun getCardNamed(client: ScryfallApi): Either { suspend fun searchCard(client: ScryfallApi): Either { return client.searchCard(readln()).map { + it.singleOrNull()?.let { selection -> + return@map Card( + selection.name, + selection.set, + selection.setName, + selection.imageUris?.large?.url, + selection.scryfallUrl.url, + selection.prices.usd.toDouble() + ) + } + it.forEachIndexed { i, card -> println("$i) $card") }