-
Notifications
You must be signed in to change notification settings - Fork 0
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
17 changed files
with
269 additions
and
328 deletions.
There are no files selected for viewing
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
58 changes: 9 additions & 49 deletions
58
.../src/androidMain/kotlin/me/frauenfelderflorian/tournamentscompose/common/data/Database.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 |
---|---|---|
@@ -1,52 +1,12 @@ | ||
package me.frauenfelderflorian.tournamentscompose.common.data | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Database | ||
import androidx.room.Delete | ||
import androidx.room.Insert | ||
import androidx.room.Query | ||
import androidx.room.RoomDatabase | ||
import androidx.room.Transaction | ||
import androidx.room.Update | ||
import androidx.room.Upsert | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@Dao | ||
actual interface TournamentDao { | ||
@Transaction | ||
@Query("SELECT * FROM tournament") | ||
actual fun getTournamentsWithGames(): Flow<List<TournamentWithGames>> | ||
|
||
@Insert | ||
actual suspend fun insert(vararg tournaments: Tournament) | ||
|
||
@Update | ||
actual suspend fun update(vararg tournaments: Tournament) | ||
|
||
@Upsert | ||
actual suspend fun upsert(vararg tournaments: Tournament) | ||
|
||
@Delete | ||
actual suspend fun delete(tournament: Tournament) | ||
} | ||
|
||
@Dao | ||
actual interface GameDao { | ||
@Insert | ||
actual suspend fun insert(vararg games: Game) | ||
|
||
@Update | ||
actual suspend fun update(vararg games: Game) | ||
|
||
@Upsert | ||
actual suspend fun upsert(vararg games: Game) | ||
|
||
@Delete | ||
actual suspend fun delete(game: Game) | ||
} | ||
|
||
@Database(entities = [Tournament::class, Game::class], version = 1) | ||
actual abstract class TournamentsDatabase : RoomDatabase() { | ||
actual abstract fun tournamentDao(): TournamentDao | ||
actual abstract fun gameDao(): GameDao | ||
import android.content.Context | ||
import app.cash.sqldelight.db.SqlDriver | ||
import app.cash.sqldelight.driver.android.AndroidSqliteDriver | ||
import me.frauenfelderflorian.tournamentscompose.TournamentsDB | ||
|
||
actual class DriverFactory(private val context: Context) { | ||
actual fun createDriver(): SqlDriver { | ||
return AndroidSqliteDriver(TournamentsDB.Schema, context, "TournamentsDB") | ||
} | ||
} |
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
104 changes: 90 additions & 14 deletions
104
...n/src/commonMain/kotlin/me/frauenfelderflorian/tournamentscompose/common/data/Database.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 |
---|---|---|
@@ -1,23 +1,99 @@ | ||
package me.frauenfelderflorian.tournamentscompose.common.data | ||
|
||
import app.cash.sqldelight.coroutines.asFlow | ||
import app.cash.sqldelight.coroutines.mapToList | ||
import app.cash.sqldelight.db.SqlDriver | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import me.frauenfelderflorian.tournamentscompose.GameQueries | ||
import me.frauenfelderflorian.tournamentscompose.TournamentQueries | ||
import me.frauenfelderflorian.tournamentscompose.TournamentsDB | ||
import java.nio.ByteBuffer | ||
import java.util.UUID | ||
|
||
expect interface TournamentDao { | ||
fun getTournamentsWithGames(): Flow<List<TournamentWithGames>> | ||
suspend fun insert(vararg tournaments: Tournament) | ||
suspend fun update(vararg tournaments: Tournament) | ||
suspend fun upsert(vararg tournaments: Tournament) | ||
suspend fun delete(tournament: Tournament) | ||
expect class DriverFactory { | ||
fun createDriver(): SqlDriver | ||
} | ||
|
||
expect interface GameDao { | ||
suspend fun insert(vararg games: Game) | ||
suspend fun update(vararg games: Game) | ||
suspend fun upsert(vararg games: Game) | ||
suspend fun delete(game: Game) | ||
fun createDatabase(driverFactory: DriverFactory): TournamentsDB { | ||
return TournamentsDB(driverFactory.createDriver()) | ||
} | ||
|
||
expect abstract class TournamentsDatabase { | ||
abstract fun tournamentDao(): TournamentDao | ||
abstract fun gameDao(): GameDao | ||
class TournamentDao(private val queries: TournamentQueries) { | ||
fun getTournaments(): Flow<List<Tournament>> { | ||
return queries.getTournaments { id, name, start, end, useAdaptivePoints, firstPoints, playersString -> | ||
Tournament( | ||
id = id.asUuid(), | ||
name = name, | ||
start = start, | ||
end = end, | ||
useAdaptivePoints = useAdaptivePoints.toInt() == 1, | ||
firstPoints = firstPoints.toInt(), | ||
playersString = playersString | ||
) | ||
}.asFlow().mapToList(Dispatchers.IO) | ||
} | ||
|
||
fun upsert(vararg tournaments: Tournament) { | ||
tournaments.forEach { | ||
queries.upsert( | ||
name = it.name, | ||
start = it.start, | ||
end = it.end, | ||
useAdaptivePoints = it.useAdaptivePoints.toInt().toLong(), | ||
firstPoints = it.firstPoints.toLong(), | ||
playersString = it.playersString, | ||
id = it.id.asByteArray() | ||
) | ||
} | ||
} | ||
|
||
fun delete(tournament: Tournament) { | ||
queries.delete(tournament.id.asByteArray()) | ||
} | ||
} | ||
|
||
class GameDao(private val queries: GameQueries) { | ||
fun getGames(tournamentId: UUID): Flow<List<Game>> { | ||
return queries.getTournamentGames( | ||
tournamentId.asByteArray() | ||
) { id, tId, date, hoops, hoopReached, difficulty, rankingString -> | ||
Game( | ||
id = id.asUuid(), | ||
tournamentId = tId.asUuid(), | ||
date = date, | ||
hoops = hoops.toInt(), | ||
hoopReached = hoopReached.toInt(), | ||
difficulty = difficulty, | ||
rankingString = rankingString | ||
) | ||
}.asFlow().mapToList(Dispatchers.IO) | ||
} | ||
|
||
fun upsert(vararg games: Game) { | ||
games.forEach { | ||
queries.upsert( | ||
date = it.date, | ||
hoops = it.hoops.toLong(), | ||
hoopReached = it.hoopReached.toLong(), | ||
difficulty = it.difficulty, | ||
rankingString = it.rankingString, | ||
id = it.id.asByteArray(), | ||
tournamentId = it.tournamentId.asByteArray() | ||
) | ||
} | ||
} | ||
|
||
fun delete(game: Game) { | ||
queries.delete(game.id.asByteArray()) | ||
} | ||
} | ||
|
||
private fun UUID.asByteArray(): ByteArray = | ||
ByteBuffer.wrap(ByteArray(16)).putLong(this.mostSignificantBits) | ||
.putLong(this.leastSignificantBits).array() | ||
|
||
private fun ByteArray.asUuid(): UUID = | ||
UUID(ByteBuffer.wrap(this).getLong(0), ByteBuffer.wrap(this).getLong(8)) | ||
|
||
private fun Boolean.toInt(): Int = if (this) 1 else 0 |
Oops, something went wrong.