Store history and team names persistent in shared preferences.

This commit is contained in:
2020-09-20 22:33:38 +02:00
parent f1dad279e0
commit 437fae633a
3 changed files with 16 additions and 32 deletions

View File

@@ -2,16 +2,10 @@
package me.zobrist.tichucounter
import android.os.Parcel
import android.os.Parcelable
class History() : Parcelable {
class History() {
private var scores: ArrayList<Round> = ArrayList()
constructor(parcel: Parcel) : this() {
scores = parcel.readSerializable() as ArrayList<Round>
}
fun getScoreA(): Int {
var tempScore = 0
scores.forEach {
@@ -31,7 +25,7 @@ class History() : Parcelable {
fun getHistoryA(): String {
var tempHistory = String()
scores.forEach {
tempHistory = tempHistory.plus(it.scoreA.toString()).plus("\n")
tempHistory += it.scoreA.toString() + "\n"
}
return tempHistory
}
@@ -39,7 +33,7 @@ class History() : Parcelable {
fun getHistoryB(): String {
var tempHistory = String()
scores.forEach {
tempHistory = tempHistory.plus(it.scoreB.toString()).plus("\n")
tempHistory += it.scoreB.toString() + "\n"
}
return tempHistory
}
@@ -61,23 +55,4 @@ class History() : Parcelable {
fun isEmpty(): Boolean {
return scores.isEmpty()
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeSerializable(scores)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<History> {
override fun createFromParcel(parcel: Parcel): History {
return History(parcel)
}
override fun newArray(size: Int): Array<History?> {
return arrayOfNulls(size)
}
}
}

View File

@@ -12,6 +12,7 @@ import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.widget.doOnTextChanged
import com.google.gson.Gson
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
@@ -35,9 +36,10 @@ class MainActivity : AppCompatActivity() {
.getBoolean("Screen_On", false)
)
history = savedInstanceState?.getParcelable("history") ?: History()
val json = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("history", "{\"scores\":[]}")
history = Gson().fromJson(json, History::class.java)
nameTeamA.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamA", "TeamA"))
nameTeamB.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamB", "TeamB"))
updateView()
@@ -292,7 +294,13 @@ class MainActivity : AppCompatActivity() {
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putParcelable("history", history)
val prefs = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).edit()
prefs.putString("history", Gson().toJson(history))
prefs.putString("nameTeamA", nameTeamA.text.toString())
prefs.putString("nameTeamB", nameTeamB.text.toString())
prefs.apply()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {