forked from KiARC/Sobriety
/
Addiction.kt
52 lines (46 loc) · 1.66 KB
/
Addiction.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.sixtyninefourtwenty.imdefinitelysober
import com.sixtyninefourtwenty.imdefinitelysober.internal.CircularBuffer
import com.sixtyninefourtwenty.imdefinitelysober.utils.secondsFromNow
import java.io.Serializable
import java.time.Instant
class Addiction(
val name: String,
var lastRelapse: Instant,
var isStopped: Boolean,
var timeStopped: Long, //in milliseconds
private val relapses: CircularBuffer<Long> = CircularBuffer(3) //Default is a new one, but you can provide your own (from a cache)
) : Serializable {
var averageRelapseDuration = if (relapses.get(0) == null) -1 else calculateAverageRelapseDuration()
private set
fun relapse() {
if (!isStopped)
relapses.update(lastRelapse.secondsFromNow())
else
relapses.update(Instant.ofEpochMilli(timeStopped).epochSecond - lastRelapse.epochSecond)
averageRelapseDuration = calculateAverageRelapseDuration()
lastRelapse = Instant.now()
}
private fun calculateAverageRelapseDuration(): Long {
return relapses.getAll().filterNotNull().sumOf { it } / 3L
}
fun toCacheable(): HashMap<Int, Any> {
val map = HashMap<Int, Any>()
map[0] = name
map[1] = lastRelapse
map[2] = isStopped
map[3] = timeStopped
map[4] = relapses
return map
}
companion object {
fun fromCacheable(map: HashMap<Int, Any>): Addiction {
return Addiction(
map[0] as String,
map[1] as Instant,
map[2] as Boolean,
map[3] as Long,
map[4] as CircularBuffer<Long>
)
}
}
}