diff --git a/app/src/main/java/fr/plnech/lifelogger/model/DataPoint.kt b/app/src/main/java/fr/plnech/lifelogger/model/DataPoint.kt new file mode 100644 index 0000000..6c55fa7 --- /dev/null +++ b/app/src/main/java/fr/plnech/lifelogger/model/DataPoint.kt @@ -0,0 +1,28 @@ +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 = 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 diff --git a/app/src/main/java/fr/plnech/lifelogger/model/Log.kt b/app/src/main/java/fr/plnech/lifelogger/model/Log.kt new file mode 100644 index 0000000..0db0f65 --- /dev/null +++ b/app/src/main/java/fr/plnech/lifelogger/model/Log.kt @@ -0,0 +1,14 @@ +package fr.plnech.lifelogger.model + +class Log(val name: String, private val dataPoints : ArrayList = 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 diff --git a/app/src/main/java/fr/plnech/lifelogger/model/LogAdapter.kt b/app/src/main/java/fr/plnech/lifelogger/model/LogAdapter.kt new file mode 100644 index 0000000..ce376b4 --- /dev/null +++ b/app/src/main/java/fr/plnech/lifelogger/model/LogAdapter.kt @@ -0,0 +1,46 @@ +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) : 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 diff --git a/app/src/main/java/fr/plnech/lifelogger/model/LogViewModel.java b/app/src/main/java/fr/plnech/lifelogger/model/LogViewModel.java new file mode 100644 index 0000000..9433d83 --- /dev/null +++ b/app/src/main/java/fr/plnech/lifelogger/model/LogViewModel.java @@ -0,0 +1,22 @@ +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> logs; + public LiveData> getLogs() { + if (logs == null) { + logs = new MutableLiveData<>(); + loadLogs(); + } + return logs; + } + + private void loadLogs() { + // TODO: Fetch from Room asyncronously. + } +}