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 package com.android.healthconnect.controller.migration 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.migration.api.LoadMigrationStateUseCase 23 import com.android.healthconnect.controller.migration.api.MigrationState 24 import dagger.hilt.android.lifecycle.HiltViewModel 25 import javax.inject.Inject 26 import kotlinx.coroutines.launch 27 28 @HiltViewModel 29 class MigrationViewModel 30 @Inject 31 constructor( 32 private val loadMigrationStateUseCase: LoadMigrationStateUseCase, 33 ) : ViewModel() { 34 35 private val _migrationState = MutableLiveData<MigrationFragmentState>() 36 val migrationState: LiveData<MigrationFragmentState> 37 get() = _migrationState 38 39 init { 40 loadHealthConnectMigrationUiState() 41 } 42 loadHealthConnectMigrationUiStatenull43 fun loadHealthConnectMigrationUiState() { 44 viewModelScope.launch { 45 _migrationState.postValue( 46 MigrationFragmentState.WithData(loadMigrationStateUseCase.invoke())) 47 } 48 } 49 getCurrentMigrationUiStatenull50 suspend fun getCurrentMigrationUiState(): MigrationState { 51 return loadMigrationStateUseCase.invoke() 52 } 53 54 sealed class MigrationFragmentState { 55 object Loading : MigrationFragmentState() 56 57 object Error : MigrationFragmentState() 58 59 data class WithData(val migrationState: MigrationState) : MigrationFragmentState() 60 } 61 } 62