Skip to content

Commit

Permalink
Merge pull request #31 from KiARC/dev
Browse files Browse the repository at this point in the history
5.2.1
  • Loading branch information
KiARC authored Sep 13, 2022
2 parents 62e897a + cf646ea commit e3db4c2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Android CI

on:
[push, pull_request]
push:
pull_request:
types: [opened, reopened]

jobs:
fastlane-validation:
Expand Down
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ android {

defaultConfig {
applicationId "com.katiearose.sobriety"
minSdk 30
minSdk 26
targetSdk 30
versionCode 11
versionName "v5.1.5"
versionCode 12
versionName "v5.2.1"
setProperty("archivesBaseName", "Sobriety $versionName")
}

Expand All @@ -35,8 +35,8 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
sourceCompatibility JavaVersion.VERSION_16
targetCompatibility JavaVersion.VERSION_16
}
kotlinOptions {
jvmTarget = '17'
Expand Down
21 changes: 19 additions & 2 deletions app/src/main/java/com/katiearose/sobriety/Addiction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.katiearose.sobriety
import java.io.Serializable
import java.time.Instant

class Addiction(val name: String, var lastRelapse: Instant) : Serializable {
class Addiction(
val name: String,
var lastRelapse: Instant,
var relapses: CircularBuffer<Long> = CircularBuffer(3) //Default is a new one, but you can provide your own (from a cache)
) : Serializable {
var averageRelapseDuration = Main.timeSinceInstant(lastRelapse)
private set
private var relapses = CircularBuffer<Long>(3)

fun relapse() {
relapses.update(Main.timeSinceInstant(lastRelapse))
Expand All @@ -17,4 +20,18 @@ class Addiction(val name: String, var lastRelapse: Instant) : Serializable {
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] = 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 CircularBuffer<Long>)
}
}
}
16 changes: 14 additions & 2 deletions app/src/main/java/com/katiearose/sobriety/CacheHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,33 @@ class CacheHandler(private val activity: Main) {
val result = ArrayList<Addiction>()
val cache = input.readBytes()
try {
InflaterInputStream(cache.inputStream()).use { iis ->
ObjectInputStream(iis).use { ois ->
val cacheData = ois.readObject() as ArrayList<HashMap<Int, Any>>
cacheData.forEach {
result.add(Addiction.fromCacheable(it))
}
}
}
} catch (e: Exception) {
InflaterInputStream(cache.inputStream()).use { iis ->
ObjectInputStream(iis).use {
result.addAll(it.readObject() as ArrayList<Addiction>)
}
}
} catch (e: Exception) {
}
return result
}

fun writeCache() {
val output = ArrayList<HashMap<Int, Any>>()
Main.addictions.forEach {
output.add(it.toCacheable())
}
activity.openFileOutput("Sobriety.cache", AppCompatActivity.MODE_PRIVATE).use { fos ->
DeflaterOutputStream(fos, true).use { dos ->
ObjectOutputStream(dos).use {
it.writeObject(Main.addictions)
it.writeObject(output)
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/katiearose/sobriety/CircularBuffer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.katiearose.sobriety

import java.io.Serializable

internal class CircularBuffer<T>(size: Int) : Serializable {
class CircularBuffer<T>(size: Int) : Serializable {
private val buffer: ArrayList<T?> = ArrayList(size)

init {
Expand All @@ -12,8 +12,9 @@ internal class CircularBuffer<T>(size: Int) : Serializable {
}

fun update(obj: T) {
buffer[2] = buffer[1]
buffer[1] = buffer[0]
for (i in buffer.indices.reversed()) {
if (i > 0) buffer[i] = buffer[i - 1]
}
buffer[0] = obj
}

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.1.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
classpath 'com.android.tools.build:gradle:7.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
5 changes: 5 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
5.2.1 Patches and tweaks
- Made the app usable on API versions as old as Oreo
- Updated gradle
- Fixed up stupid code, no bugs though
- New cache format (Unprecedented, I know /s) but this time it should be able to be used easily even if features get added in the future

0 comments on commit e3db4c2

Please sign in to comment.