Skip to content

Commit

Permalink
"fix" history crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkempire78 committed Jan 19, 2025
1 parent 08c6faa commit 45fbaf6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
resourceConfigurations += listOf("ar", "az", "be", "bn", "bs", "cs", "de", "el", "es", "fa", "fr", "hi", "hr", "hu", "in", "it", "ja", "kn", "mk", "ml", "nb-rNO", "nl", "or", "pl", "pt-rBR", "ro", "ru", "sat", "sr", "sv", "tr", "uk", "vi", "zh-rCN", "zh-rHK", "zh-rTW")
minSdk = 21
targetSdk = 34
versionCode = 49
versionName = "3.1.1"
versionCode = 50
versionName = "3.1.2"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
34 changes: 14 additions & 20 deletions app/src/main/java/com/darkempire78/opencalculator/MyPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.appcompat.app.AppCompatDelegate.*
import androidx.preference.PreferenceManager
import com.darkempire78.opencalculator.history.History
import com.google.gson.Gson
import java.util.UUID

class MyPreferences(context: Context) {

Expand All @@ -17,7 +16,7 @@ class MyPreferences(context: Context) {
private const val FORCE_DAY_NIGHT = "darkempire78.opencalculator.FORCE_DAY_NIGHT"

private const val KEY_VIBRATION_STATUS = "darkempire78.opencalculator.KEY_VIBRATION_STATUS"
private const val KEY_HISTORY = "darkempire78.opencalculator.HISTORY"
private const val KEY_HISTORY = "darkempire78.opencalculator.HISTORY_ELEMENTS"
private const val KEY_PREVENT_PHONE_FROM_SLEEPING = "darkempire78.opencalculator.PREVENT_PHONE_FROM_SLEEPING"
private const val KEY_HISTORY_SIZE = "darkempire78.opencalculator.HISTORY_SIZE"
private const val KEY_SCIENTIFIC_MODE_ENABLED_BY_DEFAULT = "darkempire78.opencalculator.SCIENTIFIC_MODE_ENABLED_BY_DEFAULT"
Expand Down Expand Up @@ -67,32 +66,27 @@ class MyPreferences(context: Context) {
set(value) = preferences.edit().putBoolean(KEY_AUTO_SAVE_CALCULATION_WITHOUT_EQUAL_BUTTON, value).apply()




fun getHistory(): MutableList<History> {
val gson = Gson()
val historyList = if (preferences.getString(KEY_HISTORY, null) != null) {
gson.fromJson(history, Array<History>::class.java).asList().toMutableList()
} else {
mutableListOf()
}

// Add IDs to history elements that don't have them (migration from older versions)
var hasChanges = false
for (element in historyList) {
if (element.id.isNullOrEmpty()) {
element.id = UUID.randomUUID().toString()
hasChanges = true
}
}
val historyJson = preferences.getString(KEY_HISTORY, null)

// Save history if changes were made
if (hasChanges) {
saveHistory(historyList)
return if (historyJson != null) {
try {
val list = gson.fromJson(historyJson, Array<History>::class.java).asList().toMutableList()
list
} catch (e: Exception) {
mutableListOf()
}
} else {
mutableListOf()
}

return historyList
}



fun saveHistory(history: List<History>){
val gson = Gson()
val history2 = history.toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ class MainActivity : AppCompatActivity() {
this // Assuming this is an Activity or Fragment with a Context
)
binding.historyRecylcleView.adapter = historyAdapter
// Set values
val historyList = MyPreferences(this).getHistory()
historyAdapter.appendHistory(historyList)

// Scroll to the bottom of the recycle view
if (historyAdapter.itemCount > 0) {
binding.historyRecylcleView.scrollToPosition(historyAdapter.itemCount - 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.darkempire78.opencalculator.history

import com.google.gson.annotations.SerializedName
import java.util.UUID

data class History(
@SerializedName("calculation") var calculation: String,
@SerializedName("result") var result: String,
@SerializedName("time") var time: String,
@SerializedName("id") var id: String
@SerializedName("id") var id: String = UUID.randomUUID().toString()
)

0 comments on commit 45fbaf6

Please sign in to comment.