1 /** 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * ``` 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * ``` 10 * 11 * Unless required by applicable law or agreed to in writing, software distributed under the License 12 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 13 * or implied. See the License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.android.healthconnect.controller.categories 17 18 import androidx.lifecycle.LiveData 19 import androidx.lifecycle.MutableLiveData 20 import androidx.lifecycle.ViewModel 21 import androidx.lifecycle.viewModelScope 22 import com.android.healthconnect.controller.shared.usecase.UseCaseResults 23 import dagger.hilt.android.lifecycle.HiltViewModel 24 import javax.inject.Inject 25 import kotlinx.coroutines.launch 26 27 @HiltViewModel 28 @Deprecated("This won't be used once the NEW_INFORMATION_ARCHITECTURE feature is enabled.") 29 class HealthDataCategoryViewModel 30 @Inject 31 constructor( 32 private val loadCategoriesUseCase: LoadHealthCategoriesUseCase, 33 ) : ViewModel() { 34 35 private val _categoriesData = MutableLiveData<CategoriesFragmentState>() 36 /** 37 * Provides a list of HealthDataCategories displayed in {@link HealthDataCategoriesFragment}. 38 */ 39 val categoriesData: LiveData<CategoriesFragmentState> 40 get() = _categoriesData 41 42 init { 43 loadCategories() 44 } 45 loadCategoriesnull46 fun loadCategories() { 47 _categoriesData.postValue(CategoriesFragmentState.Loading) 48 viewModelScope.launch { 49 when (val result = loadCategoriesUseCase()) { 50 is UseCaseResults.Success -> { 51 _categoriesData.postValue(CategoriesFragmentState.WithData(result.data)) 52 } 53 is UseCaseResults.Failed -> { 54 _categoriesData.postValue(CategoriesFragmentState.Error) 55 } 56 } 57 } 58 } 59 60 sealed class CategoriesFragmentState { 61 object Loading : CategoriesFragmentState() 62 object Error : CategoriesFragmentState() 63 data class WithData(val categories: List<HealthCategoryUiState>) : 64 CategoriesFragmentState() 65 } 66 } 67