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 package com.android.settings.remoteauth.settings
18 
19 import android.os.Bundle
20 import android.view.View
21 import androidx.constraintlayout.widget.ConstraintLayout
22 import androidx.fragment.app.Fragment
23 import androidx.fragment.app.viewModels
24 import androidx.lifecycle.Lifecycle
25 import androidx.lifecycle.lifecycleScope
26 import androidx.lifecycle.repeatOnLifecycle
27 import androidx.navigation.fragment.NavHostFragment.Companion.findNavController
28 import androidx.recyclerview.widget.LinearLayoutManager
29 import androidx.recyclerview.widget.RecyclerView
30 import com.android.settings.R
31 import kotlinx.coroutines.launch
32 
33 class RemoteAuthSettings : Fragment(R.layout.remote_auth_settings) {
34 
35     val viewModel: RemoteAuthSettingsViewModel by viewModels()
36     private val adapter = RemoteAuthSettingsRecyclerViewAdapter()
<lambda>null37     private val recyclerView by lazy {
38         view!!.requireViewById<RecyclerView>(R.id.registered_authenticator_list)
39     }
40 
<lambda>null41     private val addAuthenticatorLayout by lazy {
42         view!!.requireViewById<ConstraintLayout>(R.id.add_authenticator_layout)
43     }
44 
onViewCreatednull45     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
46         super.onViewCreated(view, savedInstanceState)
47 
48         recyclerView.layoutManager = LinearLayoutManager(context)
49         recyclerView.adapter = adapter
50 
51         // Add new remote authenticator click listener
52         addAuthenticatorLayout.setOnClickListener {
53             findNavController(this).navigate(R.id.action_settings_to_introduction)
54         }
55 
56         // Collect UIState and update UI on changes.
57         lifecycleScope.launch {
58             repeatOnLifecycle(Lifecycle.State.STARTED) {
59                 viewModel.uiState.collect {
60                     updateUi(it)
61                 }
62             }
63         }
64 
65     }
66 
updateUinull67     private fun updateUi(uiState: RemoteAuthSettingsUiState) {
68         adapter.uiStates = uiState.registeredAuthenticatorUiStates
69     }
70 
71 }
72