WeightTrack/app/src/main/java/com/eacenic/weighttrack/WeightTrackViewModel.kt

32 lines
1.0 KiB
Kotlin

package com.eacenic.weighttrack
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import java.time.LocalDateTime
const val TAG = "WeightTrackViewModel"
class WeightTrackViewModel: ViewModel() {
@RequiresApi(Build.VERSION_CODES.O)
private var _historyData = MutableStateFlow<List<WeightRecord>>(mutableListOf<WeightRecord>(
WeightRecord(62.6f, LocalDateTime.now()),
WeightRecord(63.6f, LocalDateTime.now()),
WeightRecord(65.6f, LocalDateTime.now())
))
@RequiresApi(Build.VERSION_CODES.O)
val historyData: StateFlow<List<WeightRecord>> = _historyData
@RequiresApi(Build.VERSION_CODES.O)
fun addWeightRecord(newRecord: WeightRecord) {
val newRecords = _historyData.value.toMutableList()
newRecords.add(newRecord)
_historyData.value = newRecords
Log.e(TAG, "$newRecord")
Log.e(TAG, "${_historyData.value}")
}
}