1 /* 2 * Copyright (C) 2023 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 */ 18 package com.android.healthconnect.controller.data.alldata 19 20 import androidx.lifecycle.LiveData 21 import androidx.lifecycle.MutableLiveData 22 import androidx.lifecycle.ViewModel 23 import androidx.lifecycle.viewModelScope 24 import com.android.healthconnect.controller.data.appdata.AppDataUseCase 25 import com.android.healthconnect.controller.data.appdata.PermissionTypesPerCategory 26 import com.android.healthconnect.controller.permissions.data.HealthPermissionType 27 import com.android.healthconnect.controller.shared.usecase.UseCaseResults 28 import dagger.hilt.android.lifecycle.HiltViewModel 29 import javax.inject.Inject 30 import kotlinx.coroutines.launch 31 32 /** View model for the [AllDataFragment] . */ 33 @HiltViewModel 34 class AllDataViewModel 35 @Inject 36 constructor( 37 private val loadAppDataUseCase: AppDataUseCase, 38 ) : ViewModel() { 39 40 companion object { 41 private const val TAG = "AllDataViewModel" 42 } 43 44 private val _allData = MutableLiveData<AllDataState>() 45 46 private var setOfPermissionTypesToBeDeleted: MutableSet<HealthPermissionType> = mutableSetOf() 47 48 private var isDeletionState: Boolean = false 49 50 /** Provides a list of [PermissionTypesPerCategory]s to be displayed in [AllDataFragment]. */ 51 val allData: LiveData<AllDataState> 52 get() = _allData 53 loadAllDatanull54 fun loadAllData() { 55 _allData.postValue(AllDataState.Loading) 56 viewModelScope.launch { 57 when (val result = loadAppDataUseCase.loadAllData()) { 58 is UseCaseResults.Success -> { 59 _allData.postValue(AllDataState.WithData(result.data)) 60 } 61 is UseCaseResults.Failed -> { 62 _allData.postValue(AllDataState.Error) 63 } 64 } 65 } 66 } 67 resetDeleteSetnull68 fun resetDeleteSet() { 69 setOfPermissionTypesToBeDeleted.clear() 70 } 71 addToDeleteSetnull72 fun addToDeleteSet(permissionType: HealthPermissionType) { 73 setOfPermissionTypesToBeDeleted.add(permissionType) 74 } 75 removeFromDeleteSetnull76 fun removeFromDeleteSet(permissionType: HealthPermissionType) { 77 setOfPermissionTypesToBeDeleted.remove(permissionType) 78 } 79 getDeleteSetnull80 fun getDeleteSet(): Set<HealthPermissionType> { 81 return setOfPermissionTypesToBeDeleted.toSet() 82 } 83 setDeletionStatenull84 fun setDeletionState(boolean: Boolean) { 85 isDeletionState = boolean 86 if (!isDeletionState) { 87 setOfPermissionTypesToBeDeleted.clear() 88 } 89 } 90 getDeletionStatenull91 fun getDeletionState(): Boolean { 92 return isDeletionState 93 } 94 95 sealed class AllDataState { 96 object Loading : AllDataState() 97 98 object Error : AllDataState() 99 100 data class WithData(val dataMap: List<PermissionTypesPerCategory>) : AllDataState() 101 } 102 } 103