Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove loop on every second #85

Merged
merged 2 commits into from
Jan 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions app/src/main/java/com/katiearose/sobriety/activities/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ class Main : AppCompatActivity() {
val priority =
Addiction.Priority.values()[data.getInt("priority")]
val addiction = Addiction.newInstance(name, instant, priority)
if (!addiction.isFuture())
if (addiction.isFuture()) {
// Adds to history at that point in time
addTimerForFutureAddiction(addiction)
} else {
addiction.history[instant] = 0
}
addictions.add(addiction)
addictions.sortWith { a1, a2 -> a1.priority.compareTo(a2.priority) }
cacheHandler.write()
Expand Down Expand Up @@ -242,21 +246,19 @@ class Main : AppCompatActivity() {
})
binding.recyclerAddictions.layoutManager = LinearLayoutManager(this)
binding.recyclerAddictions.adapter = adapterAddictions
//main handler to refresh all cards in sync
val mainHandler = Handler(Looper.getMainLooper())
mainHandler.postDelayed(object : Runnable {
override fun run() {
//if the future time has elapsed, insert that time into history.
//Note that to handle cases where the time elapses while the app is open,
//i have to do this catch-all check. it's ugly, but it works.
for (addiction in addictions) {
if (addiction.history.isEmpty() && addiction.lastRelapse.epochSeconds < Instant.now().epochSecond) {
addiction.history[addiction.lastRelapse.toEpochMilliseconds()] = 0
}
}
mainHandler.postDelayed(this, 1000L)
}
}, 1000L)

// Add a timer to start an attempt on future addictions
for (addiction in addictions) {
if (addiction.isFuture())
addTimerForFutureAddiction(addiction)
}
}

private fun addTimerForFutureAddiction(addiction: Addiction) {
val delay = addiction.lastRelapse.toEpochMilliseconds() - Instant.now().toEpochMilli()
Handler(Looper.getMainLooper()).postDelayed({
addiction.history[addiction.lastRelapse.toEpochMilliseconds()] = 0
}, delay)
}

private fun showAddNoteAfterRelapseDialogIfEnabled(addiction: Addiction) {
Expand Down