-
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.
* Add persistence * Add persistence test
- Loading branch information
Showing
26 changed files
with
520 additions
and
610 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.ifmo.balda | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.ifmo.balda.model.Topic | ||
import com.ifmo.balda.model.dao.StatDao | ||
import com.ifmo.balda.model.dao.WordDao | ||
import com.ifmo.balda.model.entity.Stat | ||
import com.ifmo.balda.model.entity.Word | ||
import kotlin.streams.asSequence | ||
|
||
@Database(entities = [Word::class, Stat::class], version = 1, exportSchema = false) | ||
abstract class AppDatabase : RoomDatabase() { | ||
abstract fun wordDao(): WordDao | ||
abstract fun statDao(): StatDao | ||
} | ||
|
||
private fun initDb(ctx: Context): AppDatabase = Room.databaseBuilder(ctx, AppDatabase::class.java, "balda.db") | ||
.addCallback(object : RoomDatabase.Callback() { | ||
override fun onCreate(db: SupportSQLiteDatabase) { | ||
ctx.resources.openRawResource(R.raw.russian_nouns) | ||
.bufferedReader() | ||
.lines().asSequence() | ||
.chunked(900) // SQLITE_MAX_VARIABLE_NUMBER is 999 | ||
.forEach { nouns -> | ||
db.execSQL( | ||
"insert into word (word, topic) values ${nouns.joinToString(", ") { "(?, '${Topic.COMMON.name}')" }}", | ||
nouns.toTypedArray() | ||
) | ||
} | ||
} | ||
|
||
override fun onDestructiveMigration(db: SupportSQLiteDatabase) = onCreate(db) | ||
}) | ||
.build() | ||
|
||
@Volatile | ||
private var dbInstance: AppDatabase? = null | ||
|
||
val Context.db: AppDatabase | ||
get() = dbInstance ?: synchronized(applicationContext) { | ||
dbInstance ?: initDb(applicationContext).also { dbInstance = it } | ||
} |
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,9 +1,7 @@ | ||
package com.ifmo.balda | ||
|
||
class IntentExtraNames { | ||
companion object { | ||
const val GAME_MODE = "GAME_MODE" | ||
const val PLAYER_1_NAME = "PLAYER_1_NAME" | ||
const val PLAYER_2_NAME = "PLAYER_2_NAME" | ||
} | ||
object IntentExtraNames { | ||
const val GAME_MODE = "GAME_MODE" | ||
const val PLAYER_1_NAME = "PLAYER_1_NAME" | ||
const val PLAYER_2_NAME = "PLAYER_2_NAME" | ||
} |
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,10 @@ | ||
package com.ifmo.balda | ||
|
||
object PreferencesKeys { | ||
const val preferencesFileKey = "prefFile" | ||
const val difficultyKey = "difficulty" | ||
const val topicKey = "topic" | ||
const val singlePlayerNameKey = "singlePlayerName" | ||
const val multiPlayer1PlayerNameKey = "multiPlayer1PlayerName" | ||
const val multiPlayer2PlayerNameKey = "multiPlayer2PlayerName" | ||
} |
Oops, something went wrong.