/
SleepTrackerViewModel.kt
142 lines (117 loc) · 3.82 KB
/
SleepTrackerViewModel.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2018, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.trackmysleepquality.sleeptracker
import android.app.Application
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.example.android.trackmysleepquality.database.SleepDatabaseDao
import com.example.android.trackmysleepquality.database.SleepNight
import com.example.android.trackmysleepquality.formatNights
import kotlinx.coroutines.*
/**
* ViewModel for SleepTrackerFragment.
*/
class SleepTrackerViewModel(
val database: SleepDatabaseDao,
application: Application) : AndroidViewModel(application) {
/**
*/
/**
*
*
*/
private var tonight = MutableLiveData<SleepNight?>()
private val nights = database.getAllNights()
/**
* Converted nights to Spanned for displaying.
*/
val nightsString = Transformations.map(nights) { nights ->
formatNights(nights, application.resources)
}
init {
initializeTonight()
}
private fun initializeTonight() {
viewModelScope.launch {
tonight.value = getTonightFromDatabase()
}
}
/**
* Handling the case of the stopped app or forgotten recording,
* the start and end times will be the same.j
*
* If the start time and end time are not the same, then we do not have an unfinished
* recording.
*/
private suspend fun getTonightFromDatabase(): SleepNight? {
var night = database.getTonight()
if (night?.endTimeMilli != night?.startTimeMilli) {
night = null
}
return night
}
private suspend fun clear() {
database.clear()
}
private suspend fun update(night: SleepNight) {
database.update(night)
}
private suspend fun insert(night: SleepNight) {
database.insert(night)
}
/**
* Executes when the START button is clicked.
*/
fun onStartTracking() {
viewModelScope.launch {
// Create a new night, which captures the current time,
// and insert it into the database.
val newNight = SleepNight()
insert(newNight)
tonight.value = getTonightFromDatabase()
}
}
/**
* Executes when the STOP button is clicked.
*/
fun onStopTracking() {
viewModelScope.launch {
// In Kotlin, the return@label syntax is used for specifying which function among
// several nested ones this statement returns from.
// In this case, we are specifying to return from launch(),
// not the lambda.
val oldNight = tonight.value ?: return@launch
// Update the night in the database to add the end time.
oldNight.endTimeMilli = System.currentTimeMillis()
update(oldNight)
}
}
/**
* Executes when the CLEAR button is clicked.
*/
fun onClear() {
viewModelScope.launch {
// Clear the database table.
clear()
// And clear tonight since it's no longer in the database
tonight.value = null
}
}
/**
*/
}