1 /*
<lambda>null2  * 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 package com.android.settings.remoteauth.enrolling
18 
19 import androidx.lifecycle.ViewModel
20 import kotlinx.coroutines.flow.MutableStateFlow
21 import kotlinx.coroutines.flow.StateFlow
22 import kotlinx.coroutines.flow.asStateFlow
23 import kotlinx.coroutines.flow.update
24 import kotlin.properties.Delegates
25 
26 class RemoteAuthEnrollEnrollingViewModel : ViewModel() {
27     private val _uiState = MutableStateFlow(RemoteAuthEnrollEnrollingUiState())
28     val uiState: StateFlow<RemoteAuthEnrollEnrollingUiState> = _uiState.asStateFlow()
29 
30     private var errorMessage: String? = null
31         set(value) {
32             field = value
33             _uiState.update { currentState ->
34                 currentState.copy(
35                     errorMsg = value,
36                 )
37             }
38         }
39 
40     // TODO(b/293906744): Change to RemoteAuthManager.DiscoveredDevice.
41     private var selectedDevice: Any? by Delegates.observable(null) { _, _, _ -> discoverDevices() }
42 
43 
44     /** Returns if a device has been selected */
45     fun isDeviceSelected() = selectedDevice != null
46 
47     /**
48      * Starts searching for nearby authenticators that are currently not enrolled. The devices
49      * and the state of the searching are both returned in uiState.
50      */
51     fun discoverDevices() {
52         _uiState.update { currentState ->
53             currentState.copy(enrollmentUiState = EnrollmentUiState.FINDING_DEVICES)
54         }
55 
56         // TODO(b/293906744): Map RemoteAuthManager discovered devices to
57         // DiscoveredAuthenticatorUiState in viewModelScope.
58         val discoveredDeviceUiStates = listOf<DiscoveredAuthenticatorUiState>()
59 
60         _uiState.update { currentState ->
61             currentState.copy(
62                 discoveredDeviceUiStates = discoveredDeviceUiStates,
63                 enrollmentUiState = EnrollmentUiState.NONE
64             )
65         }
66     }
67 
68     /** Registers the selected discovered device, if one is selected. */
69     fun registerAuthenticator() {
70         // TODO(b/293906744): Call RemoteAuthManager.register with selected device and update
71         //  _uiState.
72     }
73 }