JSON serialization, save and load routines
This commit is contained in:
parent
596fa67272
commit
2948e059de
@ -6,17 +6,23 @@ import android.view.View
|
||||
import android.widget.NumberPicker
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import it.danieleverducci.lunatracker.entities.Logbook
|
||||
import it.danieleverducci.lunatracker.entities.LunaEvent
|
||||
import it.danieleverducci.lunatracker.entities.LunaEventType
|
||||
import java.io.File
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
companion object {
|
||||
val TAG = "MainActivity"
|
||||
}
|
||||
|
||||
lateinit var logbook: Logbook
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
logbook = Logbook.load(this)
|
||||
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
findViewById<View>(R.id.button_bottle).setOnClickListener { askBabyBottleContent() }
|
||||
@ -68,7 +74,8 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
fun logEvent(event: LunaEvent) {
|
||||
Log.d(TAG, event.toString())
|
||||
logbook.logs.add(event)
|
||||
logbook.save(this)
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,42 @@
|
||||
package it.danieleverducci.lunatracker.entities
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import org.json.JSONArray
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileNotFoundException
|
||||
|
||||
class Logbook {
|
||||
companion object {
|
||||
val TAG = "Logbook"
|
||||
|
||||
fun load(context: Context): Logbook {
|
||||
val logbook = Logbook()
|
||||
val file = File(context.getFilesDir(), "data.json")
|
||||
try {
|
||||
val json = FileInputStream(file).bufferedReader().use { it.readText() }
|
||||
val ja = JSONArray(json)
|
||||
for (i in 0 until ja.length()) {
|
||||
val jo = ja.getJSONObject(i)
|
||||
val evt = LunaEvent.fromJson(jo)
|
||||
logbook.logs.add(evt)
|
||||
}
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.d(TAG, "No logbook file found")
|
||||
}
|
||||
return logbook
|
||||
}
|
||||
}
|
||||
|
||||
val logs = ArrayList<LunaEvent>()
|
||||
|
||||
fun save(context: Context) {
|
||||
val file = File(context.getFilesDir(), "data.json")
|
||||
val ja = JSONArray()
|
||||
for (l in logs) {
|
||||
ja.put(l.toJson())
|
||||
}
|
||||
file.writeText(ja.toString())
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
package it.danieleverducci.lunatracker.entities
|
||||
|
||||
import org.json.JSONObject
|
||||
import java.util.Date
|
||||
|
||||
class LunaEvent(
|
||||
val type: LunaEventType,
|
||||
val quantity: Int? = null
|
||||
val quantity: Int? = null,
|
||||
){
|
||||
val time: Long // In unix time (seconds since 1970)
|
||||
var time: Long // In unix time (seconds since 1970)
|
||||
|
||||
init {
|
||||
time = System.currentTimeMillis() / 1000
|
||||
@ -15,7 +16,43 @@ class LunaEvent(
|
||||
override fun toString(): String {
|
||||
return "${type.toString()} qty: $quantity time: ${Date(time * 1000)}"
|
||||
}
|
||||
}
|
||||
|
||||
fun toJson(): JSONObject {
|
||||
val jo = JSONObject()
|
||||
val type = when (type) {
|
||||
LunaEventType.BABY_BOTTLE -> "BABY_BOTTLE"
|
||||
LunaEventType.BREASTFEEDING_LEFT_NIPPLE -> "BREASTFEEDING_LEFT_NIPPLE"
|
||||
LunaEventType.BREASTFEEDING_BOTH_NIPPLE -> "BREASTFEEDING_BOTH_NIPPLE"
|
||||
LunaEventType.BREASTFEEDING_RIGHT_NIPPLE -> "BREASTFEEDING_RIGHT_NIPPLE"
|
||||
LunaEventType.DIAPERCHANGE_POO -> "DIAPERCHANGE_POO"
|
||||
LunaEventType.DIAPERCHANGE_PEE -> "DIAPERCHANGE_PEE"
|
||||
else -> "UNKNOWN"
|
||||
}
|
||||
jo.put("type", type)
|
||||
jo.put("quantity", quantity)
|
||||
jo.put("time", time)
|
||||
return jo
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun fromJson(j: JSONObject): LunaEvent {
|
||||
val type = when (j.getString("type")) {
|
||||
"BABY_BOTTLE" -> LunaEventType.BABY_BOTTLE
|
||||
"BREASTFEEDING_LEFT_NIPPLE" -> LunaEventType.BREASTFEEDING_LEFT_NIPPLE
|
||||
"BREASTFEEDING_BOTH_NIPPLE" -> LunaEventType.BREASTFEEDING_BOTH_NIPPLE
|
||||
"BREASTFEEDING_RIGHT_NIPPLE" -> LunaEventType.BREASTFEEDING_RIGHT_NIPPLE
|
||||
"DIAPERCHANGE_POO" -> LunaEventType.DIAPERCHANGE_POO
|
||||
"DIAPERCHANGE_PEE" -> LunaEventType.DIAPERCHANGE_PEE
|
||||
else -> LunaEventType.UNKNOWN
|
||||
}
|
||||
val quantity = j.optInt("quantity")
|
||||
val time = j.getLong("time")
|
||||
val evt = LunaEvent(type, quantity)
|
||||
evt.time = time
|
||||
return evt
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class LunaEventType {
|
||||
BABY_BOTTLE,
|
||||
@ -24,4 +61,5 @@ enum class LunaEventType {
|
||||
BREASTFEEDING_RIGHT_NIPPLE,
|
||||
DIAPERCHANGE_POO,
|
||||
DIAPERCHANGE_PEE,
|
||||
UNKNOWN
|
||||
}
|
Loading…
Reference in New Issue
Block a user