Skip to content

Commit da188d0

Browse files
committed
Only insert initial record if db is empty
And not everytime the history value is 0.
1 parent 531561c commit da188d0

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

app/src/main/kotlin/de/markusfisch/android/screentime/activity/MainActivity.kt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,7 @@ class MainActivity : Activity() {
125125
val d = days + 1
126126
val daysString = resources.getQuantityString(R.plurals.days, d, d)
127127
scope.launch {
128-
val now = System.currentTimeMillis()
129-
val availableHistoryInDays = db.getAvailableHistoryInDays(now)
130-
if (availableHistoryInDays < 1) {
131-
// Insert an initial SCREEN_ON event if the database is
132-
// empty because we can only find an empty database if
133-
// the user has started this app for the first time.
134-
db.insertScreenEvent(now, true, 0f)
135-
}
128+
val availableHistoryInDays = db.getAvailableHistoryInDays()
136129
val bitmap = chart.draw(
137130
timestamp,
138131
days,

app/src/main/kotlin/de/markusfisch/android/screentime/data/Database.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ class Database {
1616
db = OpenHelper(context).writableDatabase
1717
}
1818

19-
fun getAvailableHistoryInDays(now: Long): Int {
19+
fun getAvailableHistoryInDays(
20+
now: Long = System.currentTimeMillis()
21+
): Int {
2022
val earliestTimestamp = db.getEarliestTimestamp()
2123
if (earliestTimestamp > -1) {
2224
val msBetween = startOfDay(now) - startOfDay(earliestTimestamp)
2325
return ceil(msBetween / 86400000.0).toInt()
26+
} else {
27+
// Insert an initial SCREEN_ON event if the database is
28+
// empty because we can only find an empty database if
29+
// the user has started this app for the first time,
30+
// in which case the screen must be on.
31+
insertScreenEvent(now, true, 0f)
2432
}
2533
return 0
2634
}

0 commit comments

Comments
 (0)