1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.healthconnect.controller.autodelete
17 
18 import android.util.Log
19 import androidx.lifecycle.LiveData
20 import androidx.lifecycle.MutableLiveData
21 import androidx.lifecycle.ViewModel
22 import androidx.lifecycle.viewModelScope
23 import com.android.healthconnect.controller.autodelete.api.LoadAutoDeleteUseCase
24 import com.android.healthconnect.controller.autodelete.api.UpdateAutoDeleteUseCase
25 import com.android.healthconnect.controller.shared.usecase.UseCaseResults
26 import dagger.hilt.android.lifecycle.HiltViewModel
27 import javax.inject.Inject
28 import kotlinx.coroutines.launch
29 
30 /** View model for [AutoDeleteFragment]. */
31 @HiltViewModel
32 class AutoDeleteViewModel
33 @Inject
34 constructor(
35     private val loadAutoDeleteUseCase: LoadAutoDeleteUseCase,
36     private val updateAutoDeleteUseCase: UpdateAutoDeleteUseCase
37 ) : ViewModel() {
38 
39     companion object {
40         private const val TAG = "AutoDeleteRange"
41     }
42 
43     private val _storedAutoDeleteRange = MutableLiveData<AutoDeleteState>()
44     private val _newAutoDeleteRange = MutableLiveData<AutoDeleteRange>()
45     private val _oldAutoDeleteRange = MutableLiveData<AutoDeleteRange>()
46 
47     /** Holds the auto-delete range value that is stored in the database. */
48     val storedAutoDeleteRange: LiveData<AutoDeleteState>
49         get() = _storedAutoDeleteRange
50 
51     /** Holds the new proposed auto-delete range that the user has to confirm. */
52     val newAutoDeleteRange: LiveData<AutoDeleteRange>
53         get() = _newAutoDeleteRange
54 
55     /** Holds the old auto-delete range that is restored if the user cancels the update. */
56     val oldAutoDeleteRange: LiveData<AutoDeleteRange>
57         get() = _oldAutoDeleteRange
58 
59     init {
60         loadAutoDeleteRange()
61     }
62 
loadAutoDeleteRangenull63     private fun loadAutoDeleteRange() {
64         _storedAutoDeleteRange.postValue(AutoDeleteState.Loading)
65         viewModelScope.launch {
66             when (val result = loadAutoDeleteUseCase.invoke()) {
67                 is UseCaseResults.Success -> {
68                     _storedAutoDeleteRange.postValue(
69                         AutoDeleteState.WithData(fromNumberOfMonths(result.data)))
70                 }
71                 is UseCaseResults.Failed -> {
72                     Log.e(TAG, "Load error ", result.exception)
73                     _storedAutoDeleteRange.postValue(AutoDeleteState.LoadingFailed)
74                 }
75             }
76         }
77     }
78 
updateAutoDeleteRangenull79     fun updateAutoDeleteRange(autoDeleteRange: AutoDeleteRange) {
80         viewModelScope.launch {
81             when (val result = updateAutoDeleteUseCase.invoke(autoDeleteRange.numberOfMonths)) {
82                 is UseCaseResults.Success -> {
83                     _storedAutoDeleteRange.postValue(AutoDeleteState.WithData(autoDeleteRange))
84                 }
85                 is UseCaseResults.Failed -> {
86                     Log.e(TAG, "Update error ", result.exception)
87                     _storedAutoDeleteRange.postValue(AutoDeleteState.LoadingFailed)
88                 }
89             }
90         }
91     }
92 
updateAutoDeleteDialogArgumentsnull93     fun updateAutoDeleteDialogArguments(
94         newAutoDeleteRange: AutoDeleteRange,
95         oldAutoDeleteRange: AutoDeleteRange
96     ) {
97         _newAutoDeleteRange.value = newAutoDeleteRange
98         _oldAutoDeleteRange.value = oldAutoDeleteRange
99     }
100 
101     sealed class AutoDeleteState {
102         object Loading : AutoDeleteState()
103         object LoadingFailed : AutoDeleteState()
104         data class WithData(val autoDeleteRange: AutoDeleteRange) : AutoDeleteState()
105     }
106 }
107