Store history and team names persistent in shared preferences.
This commit is contained in:
@@ -38,6 +38,7 @@ dependencies {
|
|||||||
implementation 'androidx.core:core-ktx:1.3.1'
|
implementation 'androidx.core:core-ktx:1.3.1'
|
||||||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||||
implementation 'com.google.android.material:material:1.2.0'
|
implementation 'com.google.android.material:material:1.2.0'
|
||||||
|
implementation 'com.google.code.gson:gson:2.8.5'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
|
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
|
||||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
|
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.0'
|
||||||
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
|
implementation 'androidx.navigation:navigation-ui-ktx:2.3.0'
|
||||||
|
|||||||
@@ -2,16 +2,10 @@
|
|||||||
|
|
||||||
package me.zobrist.tichucounter
|
package me.zobrist.tichucounter
|
||||||
|
|
||||||
import android.os.Parcel
|
|
||||||
import android.os.Parcelable
|
|
||||||
|
|
||||||
class History() : Parcelable {
|
class History() {
|
||||||
private var scores: ArrayList<Round> = ArrayList()
|
private var scores: ArrayList<Round> = ArrayList()
|
||||||
|
|
||||||
constructor(parcel: Parcel) : this() {
|
|
||||||
scores = parcel.readSerializable() as ArrayList<Round>
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getScoreA(): Int {
|
fun getScoreA(): Int {
|
||||||
var tempScore = 0
|
var tempScore = 0
|
||||||
scores.forEach {
|
scores.forEach {
|
||||||
@@ -31,7 +25,7 @@ class History() : Parcelable {
|
|||||||
fun getHistoryA(): String {
|
fun getHistoryA(): String {
|
||||||
var tempHistory = String()
|
var tempHistory = String()
|
||||||
scores.forEach {
|
scores.forEach {
|
||||||
tempHistory = tempHistory.plus(it.scoreA.toString()).plus("\n")
|
tempHistory += it.scoreA.toString() + "\n"
|
||||||
}
|
}
|
||||||
return tempHistory
|
return tempHistory
|
||||||
}
|
}
|
||||||
@@ -39,7 +33,7 @@ class History() : Parcelable {
|
|||||||
fun getHistoryB(): String {
|
fun getHistoryB(): String {
|
||||||
var tempHistory = String()
|
var tempHistory = String()
|
||||||
scores.forEach {
|
scores.forEach {
|
||||||
tempHistory = tempHistory.plus(it.scoreB.toString()).plus("\n")
|
tempHistory += it.scoreB.toString() + "\n"
|
||||||
}
|
}
|
||||||
return tempHistory
|
return tempHistory
|
||||||
}
|
}
|
||||||
@@ -61,23 +55,4 @@ class History() : Parcelable {
|
|||||||
fun isEmpty(): Boolean {
|
fun isEmpty(): Boolean {
|
||||||
return scores.isEmpty()
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ import android.widget.ScrollView
|
|||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import androidx.core.widget.doOnTextChanged
|
import androidx.core.widget.doOnTextChanged
|
||||||
|
import com.google.gson.Gson
|
||||||
import kotlinx.android.synthetic.main.content_main.*
|
import kotlinx.android.synthetic.main.content_main.*
|
||||||
|
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
@@ -35,9 +36,10 @@ class MainActivity : AppCompatActivity() {
|
|||||||
.getBoolean("Screen_On", false)
|
.getBoolean("Screen_On", false)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val json = this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("history", "{\"scores\":[]}")
|
||||||
|
history = Gson().fromJson(json, History::class.java)
|
||||||
history = savedInstanceState?.getParcelable("history") ?: History()
|
nameTeamA.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamA", "TeamA"))
|
||||||
|
nameTeamB.setText(this.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString("nameTeamB", "TeamB"))
|
||||||
updateView()
|
updateView()
|
||||||
|
|
||||||
|
|
||||||
@@ -292,7 +294,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
override fun onSaveInstanceState(outState: Bundle) {
|
override fun onSaveInstanceState(outState: Bundle) {
|
||||||
super.onSaveInstanceState(outState)
|
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 {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user