feat(model): Log, DataPoint, LogAdapter

parent a031ebcf
package fr.plnech.lifelogger.model
import java.util.*
import kotlin.collections.HashMap
private const val NONE: Int = Int.MIN_VALUE
data class DataPoint(val date: Date = Date(), private val metadata: HashMap<String, Any> = HashMap()) {
constructor(name: String, count: Int = NONE) : this() {
metadata["name"] = name
metadata["count"] = count
}
private val name: String
get() = metadata["name"] as String
private val count: Int?
get() = metadata["count"].let {
if (it == NONE) null else it
} as Int?
override fun toString(): String {
val b = StringBuilder(name)
count?.let { b.append("($count)")}
return b.toString()
}
}
\ No newline at end of file
package fr.plnech.lifelogger.model
class Log(val name: String, private val dataPoints : ArrayList<DataPoint> = ArrayList()) {
fun addPoint(dataPoint: DataPoint) {
dataPoints.add(dataPoint)
}
override fun toString(): String {
val b = StringBuilder("$name: ")
dataPoints.forEach({b.append(it).append(" | ")})
return b.removeSuffix("| ").toString()
}
}
\ No newline at end of file
package fr.plnech.lifelogger.model
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
class LogAdapter(private var activity : Activity, private var logs : List<Log>) : BaseAdapter() {
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val view: View
val viewHolder: ViewHolder
if (convertView == null) {
val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = inflater.inflate(android.R.layout.simple_list_item_1, null)
viewHolder = ViewHolder(view)
view?.tag = viewHolder
} else {
view = convertView
viewHolder = view.tag as ViewHolder
}
var log = logs[position]
viewHolder.text1.text = log.toString()
return view
}
override fun getItem(position: Int): Any {
return logs[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getCount(): Int {
return logs.size
}
class ViewHolder(row: View) {
var text1: TextView = row.findViewById(android.R.id.text1)!!
}
}
\ No newline at end of file
package fr.plnech.lifelogger.model;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import java.util.List;
public class LogViewModel extends ViewModel {
private MutableLiveData<List<Log>> logs;
public LiveData<List<Log>> getLogs() {
if (logs == null) {
logs = new MutableLiveData<>();
loadLogs();
}
return logs;
}
private void loadLogs() {
// TODO: Fetch from Room asyncronously.
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment