1 /*
2  * Copyright (C) 2022 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.home
18 
19 import androidx.lifecycle.LiveData
20 import androidx.lifecycle.MutableLiveData
21 import androidx.lifecycle.ViewModel
22 import androidx.lifecycle.viewModelScope
23 import com.android.healthconnect.controller.permissions.connectedapps.LoadHealthPermissionApps
24 import com.android.healthconnect.controller.shared.app.ConnectedAppMetadata
25 import com.android.healthconnect.controller.utils.postValueIfUpdated
26 import dagger.hilt.android.lifecycle.HiltViewModel
27 import javax.inject.Inject
28 import kotlinx.coroutines.launch
29 
30 @HiltViewModel
31 class HomeFragmentViewModel
32 @Inject
33 constructor(private val loadHealthPermissionApps: LoadHealthPermissionApps) : ViewModel() {
34 
35     private val _connectedApps = MutableLiveData<List<ConnectedAppMetadata>>()
36     val connectedApps: LiveData<List<ConnectedAppMetadata>>
37         get() = _connectedApps
38 
39     init {
40         loadConnectedApps()
41     }
42 
loadConnectedAppsnull43     fun loadConnectedApps() {
44         viewModelScope.launch {
45             _connectedApps.postValueIfUpdated(loadHealthPermissionApps.invoke())
46         }
47     }
48 }
49