Skip to content

Commit

Permalink
Fix initial state
Browse files Browse the repository at this point in the history
  • Loading branch information
artnc committed Dec 27, 2021
1 parent 97bef02 commit 9c311bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/src/main/java/com/chaidarun/chronofile/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ class Config(

fun saveFile() {
val textToWrite = serialize()
if (file.exists() && file.readText() == textToWrite) {
if (!file.exists()) {
file.parentFile.mkdirs()
file.createNewFile()
}
if (file.readText() == textToWrite) {
Log.i(TAG, "File unchanged; skipping write")
} else {
file.writeText(textToWrite)
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/com/chaidarun/chronofile/History.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ data class History(val entries: List<Entry>, val currentActivityStartTime: Long)
private fun save(text: String) {
AsyncTask.execute {
Log.i(TAG, "Saving history")
if (file.exists() && file.readText() == text) {
if (!file.exists()) {
file.parentFile.mkdirs()
file.createNewFile()
}
if (file.readText() == text) {
Log.i(TAG, "File unchanged; skipping write")
} else {
val start = System.currentTimeMillis()
Expand Down Expand Up @@ -137,7 +141,7 @@ data class History(val entries: List<Entry>, val currentActivityStartTime: Long)
fun fromFile(): History {
// Read lines
var currentActivityStartTime = epochSeconds()
val lines = if (file.exists()) file.readLines() else listOf("\t\t\t\t$currentActivityStartTime\n")
val lines = if (file.exists()) file.readLines() else listOf("\t\t\t\t$currentActivityStartTime")

// Parse lines
val entries = mutableListOf<Entry>()
Expand Down

0 comments on commit 9c311bf

Please sign in to comment.