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.annotation.VisibleForTesting 20 import androidx.lifecycle.LiveData 21 import androidx.lifecycle.MutableLiveData 22 import androidx.lifecycle.ViewModel 23 import androidx.lifecycle.viewModelScope 24 import dagger.hilt.android.lifecycle.HiltViewModel 25 import javax.inject.Inject 26 import kotlinx.coroutines.launch 27 28 /** View model for import status. */ 29 @HiltViewModel 30 class ImportStatusViewModel 31 @Inject 32 constructor( 33 private val loadImportStatusUseCase: ILoadImportStatusUseCase, 34 ) : ViewModel() { 35 private val _storedImportStatus = MutableLiveData<ImportUiStatus>() 36 37 /** Holds the import status that is stored in the Health Connect service. */ 38 val storedImportStatus: LiveData<ImportUiStatus> 39 get() = _storedImportStatus 40 41 init { 42 loadImportStatus() 43 } 44 45 /** Triggers a load of import status. */ 46 @VisibleForTesting loadImportStatusnull47 fun loadImportStatus() { 48 _storedImportStatus.postValue(ImportUiStatus.Loading) 49 viewModelScope.launch { 50 when (val result = loadImportStatusUseCase.invoke()) { 51 is ExportImportUseCaseResult.Success -> { 52 _storedImportStatus.postValue(ImportUiStatus.WithData(result.data)) 53 } 54 is ExportImportUseCaseResult.Failed -> { 55 _storedImportStatus.postValue(ImportUiStatus.LoadingFailed) 56 } 57 } 58 } 59 } 60 } 61