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 package com.android.healthconnect.controller.selectabledeletion 17 18 import android.util.Log 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 com.android.healthconnect.controller.permissions.data.HealthPermissionType 25 import com.android.healthconnect.controller.selectabledeletion.api.DeletePermissionTypesUseCase 26 import dagger.hilt.android.lifecycle.HiltViewModel 27 import javax.inject.Inject 28 import kotlinx.coroutines.launch 29 30 @HiltViewModel 31 class DeletionViewModel 32 @Inject 33 constructor(private val deletePermissionTypesUseCase: DeletePermissionTypesUseCase) : ViewModel() { 34 35 companion object { 36 private const val TAG = "DeletionViewModel" 37 } 38 39 private lateinit var deletionType: DeletionType 40 41 private var setOfPermissionTypesToBeDeleted: Set<HealthPermissionType> = setOf() 42 43 private var _permissionTypesReloadNeeded = MutableLiveData(false) 44 45 private var _deletionProgress = MutableLiveData(DeletionProgress.NOT_STARTED) 46 47 val deletionProgress: LiveData<DeletionProgress> 48 get() = _deletionProgress 49 50 val permissionTypesReloadNeeded: LiveData<Boolean> 51 get() = _permissionTypesReloadNeeded 52 deletenull53 fun delete() { 54 viewModelScope.launch { 55 _deletionProgress.value = (DeletionProgress.STARTED) 56 57 try { 58 _deletionProgress.value = (DeletionProgress.PROGRESS_INDICATOR_CAN_START) 59 60 when (deletionType) { 61 is DeletionType.DeletionTypeHealthPermissionTypes -> { 62 deletePermissionTypesUseCase.invoke( 63 deletionType as DeletionType.DeletionTypeHealthPermissionTypes) 64 _permissionTypesReloadNeeded.postValue(true) 65 } 66 else -> { 67 // do nothing 68 } 69 } 70 71 _deletionProgress.value = (DeletionProgress.COMPLETED) 72 } catch (error: Exception) { 73 Log.e(TAG, "Failed to delete data", error) 74 75 _deletionProgress.value = (DeletionProgress.FAILED) 76 } finally { 77 _deletionProgress.value = (DeletionProgress.PROGRESS_INDICATOR_CAN_END) 78 } 79 } 80 } 81 resetPermissionTypesReloadNeedednull82 fun resetPermissionTypesReloadNeeded() { 83 _permissionTypesReloadNeeded.postValue(false) 84 } 85 setDeleteSetnull86 fun setDeleteSet(permissionTypes: Set<HealthPermissionType>) { 87 if (permissionTypes.isNotEmpty()) { 88 setOfPermissionTypesToBeDeleted = permissionTypes.toSet() 89 deletionType = DeletionType.DeletionTypeHealthPermissionTypes(setOfPermissionTypesToBeDeleted.toList()) 90 } 91 } 92 93 @VisibleForTesting getDeleteSetnull94 fun getDeleteSet(): Set<HealthPermissionType> { 95 return setOfPermissionTypesToBeDeleted 96 } 97 98 enum class DeletionProgress { 99 NOT_STARTED, 100 STARTED, 101 PROGRESS_INDICATOR_CAN_START, 102 PROGRESS_INDICATOR_CAN_END, 103 COMPLETED, 104 FAILED 105 } 106 } 107