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(private val loadScheduledExportStatusUseCase: ILoadScheduledExportStatusUseCase) : 32 ViewModel() { 33 private val _storedScheduledExportStatus = MutableLiveData<ScheduledExportUiStatus>() 34 private val _storedNextExportSequentialNumber = MutableLiveData<Int>() 35 36 /** Holds the export status that is stored in the Health Connect service. */ 37 val storedScheduledExportStatus: LiveData<ScheduledExportUiStatus> 38 get() = _storedScheduledExportStatus 39 40 /** Holds the next export sequential number is stored in the Health Connect service. */ 41 val storedNextExportSequentialNumber: LiveData<Int> 42 get() = _storedNextExportSequentialNumber 43 44 init { 45 loadScheduledExportStatus() 46 } 47 48 /** Triggers a load of scheduled export status. */ loadScheduledExportStatusnull49 fun loadScheduledExportStatus() { 50 _storedScheduledExportStatus.postValue(ScheduledExportUiStatus.Loading) 51 viewModelScope.launch { 52 when (val result = loadScheduledExportStatusUseCase.invoke()) { 53 is ExportImportUseCaseResult.Success -> { 54 _storedScheduledExportStatus.postValue( 55 ScheduledExportUiStatus.WithData(result.data) 56 ) 57 _storedNextExportSequentialNumber.value = result.data.nextExportSequentialNumber 58 } 59 is ExportImportUseCaseResult.Failed -> { 60 _storedScheduledExportStatus.postValue(ScheduledExportUiStatus.LoadingFailed) 61 } 62 } 63 } 64 } 65 } 66