Skip to content

Commit 0bfc853

Browse files
Migrate DailyGameHistory to use LocalDate
1 parent 8f74d70 commit 0bfc853

File tree

8 files changed

+70
-75
lines changed

8 files changed

+70
-75
lines changed

app/schemas/org.wikipedia.database.AppDatabase/31.json

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"formatVersion": 1,
33
"database": {
44
"version": 31,
5-
"identityHash": "d99be9ecca43c23bc048ef363e1a06c1",
5+
"identityHash": "aa09f2aa65c69b2e843a5c9d15d51895",
66
"entities": [
77
{
88
"tableName": "HistoryEntry",
@@ -598,7 +598,7 @@
598598
},
599599
{
600600
"tableName": "DailyGameHistory",
601-
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `gameName` INTEGER NOT NULL, `language` TEXT NOT NULL, `year` INTEGER NOT NULL, `month` INTEGER NOT NULL, `day` INTEGER NOT NULL, `score` INTEGER NOT NULL, `playType` INTEGER NOT NULL, `gameData` TEXT)",
601+
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `gameName` INTEGER NOT NULL, `language` TEXT NOT NULL, `date` TEXT NOT NULL, `score` INTEGER NOT NULL, `playType` INTEGER NOT NULL, `gameData` TEXT)",
602602
"fields": [
603603
{
604604
"fieldPath": "id",
@@ -619,21 +619,9 @@
619619
"notNull": true
620620
},
621621
{
622-
"fieldPath": "year",
623-
"columnName": "year",
624-
"affinity": "INTEGER",
625-
"notNull": true
626-
},
627-
{
628-
"fieldPath": "month",
629-
"columnName": "month",
630-
"affinity": "INTEGER",
631-
"notNull": true
632-
},
633-
{
634-
"fieldPath": "day",
635-
"columnName": "day",
636-
"affinity": "INTEGER",
622+
"fieldPath": "date",
623+
"columnName": "date",
624+
"affinity": "TEXT",
637625
"notNull": true
638626
},
639627
{
@@ -734,7 +722,7 @@
734722
],
735723
"setupQueries": [
736724
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
737-
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'd99be9ecca43c23bc048ef363e1a06c1')"
725+
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'aa09f2aa65c69b2e843a5c9d15d51895')"
738726
]
739727
}
740728
}

app/src/main/java/org/wikipedia/database/AppDatabase.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ const val DATABASE_VERSION = 31
6262
DateTypeConverter::class,
6363
WikiSiteTypeConverter::class,
6464
NamespaceTypeConverter::class,
65-
NotificationTypeConverters::class
65+
NotificationTypeConverters::class,
66+
LocalDateTypeConverter::class
6667
)
6768
abstract class AppDatabase : RoomDatabase() {
6869

@@ -347,13 +348,45 @@ abstract class AppDatabase : RoomDatabase() {
347348
db.execSQL("ALTER TABLE Category_temp RENAME TO Category")
348349
}
349350
}
351+
val MIGRATION_31_32 = object : Migration(31, 32) {
352+
override fun migrate(db: SupportSQLiteDatabase) {
353+
// Step 1: Create a temporary table
354+
db.execSQL("""
355+
CREATE TABLE DailyGameHistory_temp (
356+
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
357+
gameName INTEGER NOT NULL,
358+
language TEXT NOT NULL,
359+
date STRING NOT NULL,
360+
score INTEGER NOT NULL,
361+
playType INTEGER NOT NULL,
362+
gameData TEXT
363+
)
364+
""".trimIndent())
365+
366+
// Step 2: Populate the new table with the transformed data from the old table
367+
db.execSQL("""
368+
INSERT INTO DailyGameHistory_temp (
369+
id, gameName, language,
370+
year || '-' || printf('%02d', month) || '-' || printf('%02d', day),
371+
score, playType, gameData
372+
)
373+
FROM DailyGameHistory
374+
""".trimIndent())
375+
376+
// Step 3: Drop the old table
377+
db.execSQL("DROP TABLE DailyGameHistory")
378+
379+
// Step 4: Rename the temporary table to the original table name
380+
db.execSQL("ALTER TABLE DailyGameHistory_temp RENAME TO DailyGameHistory")
381+
}
382+
}
350383

351384
val instance: AppDatabase by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
352385
Room.databaseBuilder(WikipediaApp.instance, AppDatabase::class.java, DATABASE_NAME)
353386
.addMigrations(MIGRATION_19_20, MIGRATION_20_21, MIGRATION_21_22, MIGRATION_22_23,
354387
MIGRATION_23_24, MIGRATION_24_25, MIGRATION_25_26, MIGRATION_26_27,
355388
MIGRATION_26_28, MIGRATION_27_28, MIGRATION_28_29, MIGRATION_29_30,
356-
MIGRATION_30_31)
389+
MIGRATION_30_31, MIGRATION_31_32)
357390
.fallbackToDestructiveMigration(false)
358391
.build()
359392
}

app/src/main/java/org/wikipedia/database/LocalDateTimeTypeConverter.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.wikipedia.database
2+
3+
import androidx.room.TypeConverter
4+
import java.time.LocalDate
5+
6+
object LocalDateTypeConverter {
7+
@TypeConverter
8+
fun dateToString(date: LocalDate) = date.toString()
9+
10+
@TypeConverter
11+
fun stringToDate(dateString: String): LocalDate = LocalDate.parse(dateString)
12+
}
Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.wikipedia.games.db
22

33
import androidx.room.Entity
4-
import androidx.room.Ignore
54
import androidx.room.PrimaryKey
65
import java.time.LocalDate
76

@@ -10,13 +9,8 @@ data class DailyGameHistory(
109
@PrimaryKey(autoGenerate = true) val id: Int = 0,
1110
val gameName: Int,
1211
val language: String,
13-
val year: Int,
14-
var month: Int,
15-
var day: Int,
12+
val date: LocalDate,
1613
var score: Int,
1714
var playType: Int,
1815
var gameData: String?
19-
) {
20-
@Ignore
21-
val date: LocalDate = LocalDate.of(year, month, day)
22-
}
16+
)

app/src/main/java/org/wikipedia/games/db/DailyGameHistoryDao.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import androidx.room.Query
88
import androidx.room.Update
99
import org.wikipedia.games.PlayTypes
1010
import java.time.LocalDate
11-
import java.time.Month
1211

1312
@Dao
1413
interface DailyGameHistoryDao {
@@ -18,20 +17,19 @@ interface DailyGameHistoryDao {
1817
@Insert(onConflict = OnConflictStrategy.REPLACE)
1918
suspend fun insertAll(dailyGameHistories: List<DailyGameHistory>)
2019

21-
@Query("SELECT * FROM DailyGameHistory ORDER BY year DESC, month DESC, day DESC, id DESC LIMIT 1")
20+
@Query("SELECT * FROM DailyGameHistory ORDER BY date DESC, id DESC LIMIT 1")
2221
suspend fun findLastGameHistory(): DailyGameHistory?
2322

24-
@Query("SELECT * FROM DailyGameHistory WHERE gameName = :gameName AND language = :language " +
25-
"AND year = :year AND month = :month AND day = :day")
26-
suspend fun findGameHistoryByDate(gameName: Int, language: String, year: Int, month: Int, day: Int): DailyGameHistory?
23+
@Query("SELECT * FROM DailyGameHistory WHERE gameName = :gameName AND language = :language AND date = :date")
24+
suspend fun findGameHistoryByDate(gameName: Int, language: String, date: LocalDate): DailyGameHistory?
2725

2826
@Query("SELECT COUNT(*) FROM DailyGameHistory WHERE gameName = :gameName AND language = :language")
2927
suspend fun getTotalGamesPlayed(gameName: Int, language: String): Int
3028

3129
@Query("SELECT AVG(score) FROM DailyGameHistory WHERE gameName = :gameName AND language = :language")
3230
suspend fun getAverageScore(gameName: Int, language: String): Double?
3331

34-
@Query("SELECT * FROM DailyGameHistory WHERE gameName = :gameName AND language = :language ORDER BY year DESC, month DESC, day DESC")
32+
@Query("SELECT * FROM DailyGameHistory WHERE gameName = :gameName AND language = :language ORDER BY date DESC")
3533
suspend fun getGameHistory(gameName: Int, language: String): List<DailyGameHistory>
3634

3735
@Update
@@ -50,12 +48,10 @@ interface DailyGameHistoryDao {
5048
var expectedDate = LocalDate.now() // Start with today's date
5149

5250
for (record in history) {
53-
val recordDate = LocalDate.of(record.year, Month.of(record.month), record.day)
54-
55-
if (recordDate == expectedDate) {
51+
if (record.date == expectedDate) {
5652
currentStreak++
5753
expectedDate = expectedDate.minusDays(1) // Move to the previous day
58-
} else if (recordDate.isBefore(expectedDate)) {
54+
} else if (record.date < expectedDate) {
5955
break
6056
}
6157
}
@@ -74,15 +70,13 @@ interface DailyGameHistoryDao {
7470
var expectedDate = LocalDate.now() // Start with today's date
7571

7672
for (record in history) {
77-
val recordDate = LocalDate.of(record.year, Month.of(record.month), record.day)
78-
79-
if (recordDate == expectedDate) {
73+
if (record.date == expectedDate) {
8074
currentStreak++
8175
expectedDate = expectedDate.minusDays(1) // Move to the previous day
82-
} else if (recordDate.isBefore(expectedDate)) {
76+
} else if (record.date < expectedDate) {
8377
bestStreak = maxOf(bestStreak, currentStreak)
8478
currentStreak = 0
85-
expectedDate = recordDate.minusDays(1) // Reset to the day before the record date
79+
expectedDate = record.date.minusDays(1) // Reset to the day before the record date
8680
}
8781
}
8882

app/src/main/java/org/wikipedia/games/onthisday/OnThisDayGameBaseFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import java.time.ZoneId
2525
import java.time.temporal.ChronoUnit
2626

2727
abstract class OnThisDayGameBaseFragment : Fragment() {
28-
private var scoreData = emptyMap<LocalDate, Int>()
28+
private var scoreData: Map<LocalDate, Int> = emptyMap()
2929

3030
private val fragmentLifecycleCallbacks = object : FragmentManager.FragmentLifecycleCallbacks() {
3131
@SuppressLint("RestrictedApi")

app/src/main/java/org/wikipedia/games/onthisday/OnThisDayGameViewModel.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ class OnThisDayGameViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
102102
val dbGameResults = AppDatabase.instance.dailyGameHistoryDao().findGameHistoryByDate(
103103
gameName = WikiGames.WHICH_CAME_FIRST.ordinal,
104104
language = wikiSite.languageCode,
105-
year = currentDate.year,
106-
month = currentMonth,
107-
day = currentDay
105+
date = currentDate
108106
)
109107

110108
val eventsFromApi = ServiceFactory.getRest(wikiSite).getOnThisDay(currentMonth, currentDay).events
@@ -212,17 +210,13 @@ class OnThisDayGameViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
212210
AppDatabase.instance.dailyGameHistoryDao().findGameHistoryByDate(
213211
gameName = WikiGames.WHICH_CAME_FIRST.ordinal,
214212
language = wikiSite.languageCode,
215-
year = currentDate.year,
216-
month = currentDate.monthValue,
217-
day = currentDate.dayOfMonth
213+
date = currentDate
218214
) ?: run {
219215
shouldInsert = true
220216
DailyGameHistory(
221217
gameName = WikiGames.WHICH_CAME_FIRST.ordinal,
222218
language = wikiSite.languageCode,
223-
year = currentDate.year,
224-
month = currentDate.monthValue,
225-
day = currentDate.dayOfMonth,
219+
date = currentDate,
226220
score = 0,
227221
playType = if (isArchiveGame) PlayTypes.PLAYED_ON_ARCHIVE.ordinal else PlayTypes.PLAYED_ON_SAME_DAY.ordinal,
228222
gameData = null
@@ -360,10 +354,8 @@ class OnThisDayGameViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
360354
val gameHistory = DailyGameHistory(
361355
gameName = WikiGames.WHICH_CAME_FIRST.ordinal,
362356
language = lang,
363-
year = year,
364-
month = month,
365-
day = day,
366-
score = answers.count { it }.toInt(),
357+
date = LocalDate.of(year, month, day),
358+
score = answers.count { it },
367359
playType = PlayTypes.PLAYED_ON_SAME_DAY.ordinal,
368360
gameData = JsonUtil.encodeToString(answers)
369361
)

0 commit comments

Comments
 (0)