Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 7 additions & 9 deletions LabelMaker/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
applicationId = "com.hestonliebowitz.labelmaker"
minSdk = 31
targetSdk = 34
versionCode = 4
versionName = "1.3"
versionCode = 5
versionName = "1.4"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down Expand Up @@ -68,8 +68,6 @@ android {
}
}

val ktor_version: String by project

dependencies {

implementation(libs.androidx.core.ktx)
Expand All @@ -91,8 +89,8 @@ dependencies {
implementation(libs.androidx.material.icons)

// https://ktor.io/docs/client-create-new-application.html#add-dependencies
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
}
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.cio)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.hestonliebowitz.labelmaker

import android.content.Context

const val HISTORY_DELIMITER = "~"
const val MAX_HISTORY_ITEMS = 10

class HistoryService(private val ctx: Context) {
private val _items : MutableList<String> = emptyList<String>().toMutableList()

fun getAll(): List<String> {
_items.clear()
val prefs = ctx.getSharedPreferences(DEFAULT, Context.MODE_PRIVATE)
val history = prefs.getString(
ctx.getString(R.string.pref_history),
""
)
if (!history.isNullOrEmpty()) {
_items.addAll(history.split(HISTORY_DELIMITER))
}
return _items.toList()
}

fun save(value: String) {
// If item already exists, remove it
val idx = _items.indexOf(value)
if (idx > -1) {
_items.removeAt(idx)
}

// Add item to front of list
_items.add(0, value)

// Ensure list of favorites never exceeds MAX_HISTORY_ITEMS in length
if (_items.size > MAX_HISTORY_ITEMS) {
_items.subList(MAX_HISTORY_ITEMS, _items.size).clear()
}

// Serialize list into a string and save it
val newItems = _items.joinToString(HISTORY_DELIMITER)
val prefs = ctx.getSharedPreferences(DEFAULT, Context.MODE_PRIVATE)
val editor = prefs.edit()
editor.putString(
ctx.getString(R.string.pref_history),
newItems
)
editor.apply()
}

fun deleteAll() {
val prefs = ctx.getSharedPreferences(DEFAULT, Context.MODE_PRIVATE)
val editor = prefs.edit()
editor.putString(
ctx.getString(R.string.pref_history),
""
)
editor.apply()
}
}
Loading