1 /*
2  * Copyright (C) 2024 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 
17 package com.android.healthconnect.controller.exportimport.api
18 
19 import androidx.lifecycle.LiveData
20 import androidx.lifecycle.MutableLiveData
21 import androidx.lifecycle.ViewModel
22 import androidx.lifecycle.viewModelScope
23 import dagger.hilt.android.lifecycle.HiltViewModel
24 import javax.inject.Inject
25 import kotlinx.coroutines.launch
26 
27 /** View model for Export status. */
28 @HiltViewModel
29 class ExportStatusViewModel
30 @Inject
31 constructor(
32     private val loadScheduledExportStatusUseCase: ILoadScheduledExportStatusUseCase,
33 ) : ViewModel() {
34     private val _storedScheduledExportStatus = MutableLiveData<ScheduledExportUiStatus>()
35 
36     /** Holds the export status that is stored in the Health Connect service. */
37     val storedScheduledExportStatus: LiveData<ScheduledExportUiStatus>
38         get() = _storedScheduledExportStatus
39 
40     init {
41 
42         loadScheduledExportStatus()
43     }
44 
45     /** Triggers a load of scheduled export status. */
loadScheduledExportStatusnull46     fun loadScheduledExportStatus() {
47         _storedScheduledExportStatus.postValue(ScheduledExportUiStatus.Loading)
48         viewModelScope.launch {
49             when (val result = loadScheduledExportStatusUseCase.invoke()) {
50                 is ExportImportUseCaseResult.Success -> {
51                     _storedScheduledExportStatus.postValue(
52                         ScheduledExportUiStatus.WithData(result.data))
53                 }
54                 is ExportImportUseCaseResult.Failed -> {
55                     _storedScheduledExportStatus.postValue(ScheduledExportUiStatus.LoadingFailed)
56                 }
57             }
58         }
59     }
60 }
61