Refactor login fragment

This commit is contained in:
Gaurav Ujjwal 2023-04-03 16:18:01 +05:30
parent 8261a295a7
commit 22bad9c320
5 changed files with 12 additions and 15 deletions

View file

@ -95,7 +95,7 @@ import com.gaurav.avnc.vnc.VncClient
* - Connection to VNC server is managed by [VncViewModel], using [VncClient]. * - Connection to VNC server is managed by [VncViewModel], using [VncClient].
* - [VncClient] is a wrapper around native `rfbClient` from LibVNCClient. * - [VncClient] is a wrapper around native `rfbClient` from LibVNCClient.
* *
* - [CredentialFragment] is used to ask username & password from user. * - [LoginFragment] is used to ask username & password from user.
* - [SshTunnel] is used to create a SSH tunnel, which can be used for connection. * - [SshTunnel] is used to create a SSH tunnel, which can be used for connection.
* - [HostKeyFragment] is used to verify unknown SSH hosts. * - [HostKeyFragment] is used to verify unknown SSH hosts.
* - * -

View file

@ -10,7 +10,6 @@ package com.gaurav.avnc.model.db
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.room.* import androidx.room.*
import com.gaurav.avnc.model.LoginInfo
import com.gaurav.avnc.model.ServerProfile import com.gaurav.avnc.model.ServerProfile
@Dao @Dao
@ -32,9 +31,6 @@ interface ServerProfileDao {
@Query("SELECT * FROM profiles WHERE name = :name") @Query("SELECT * FROM profiles WHERE name = :name")
suspend fun getByName(name: String): List<ServerProfile> suspend fun getByName(name: String): List<ServerProfile>
@Query("SELECT name, host, username, password FROM profiles")
fun getCredentials(): LiveData<List<LoginInfo>>
@Insert @Insert
suspend fun insert(profile: ServerProfile): Long suspend fun insert(profile: ServerProfile): Long

View file

@ -25,7 +25,7 @@ import com.google.android.material.textfield.TextInputLayout
/** /**
* Allows user to enter credentials for remote server. * Allows user to enter credentials for remote server.
*/ */
class CredentialFragment : DialogFragment() { class LoginFragment : DialogFragment() {
val viewModel by activityViewModels<VncViewModel>() val viewModel by activityViewModels<VncViewModel>()
lateinit var binding: FragmentCredentialBinding lateinit var binding: FragmentCredentialBinding
@ -100,9 +100,10 @@ class CredentialFragment : DialogFragment() {
if (viewModel.pref.server.lockSavedServer) if (viewModel.pref.server.lockSavedServer)
return return
viewModel.knownCredentials.observe(this) { list -> viewModel.savedProfiles.observe(this) { profiles ->
val usernames = list.map { it.username }.filter { it.isNotEmpty() }.distinct() val logins = profiles.map { LoginInfo(it.name, it.host, it.username, it.password) }
val passwords = preparePasswordSuggestions(list) val usernames = logins.map { it.username }.filter { it.isNotEmpty() }.distinct()
val passwords = preparePasswordSuggestions(logins)
if (usernames.isNotEmpty()) { if (usernames.isNotEmpty()) {
val usernameAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, usernames) val usernameAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_list_item_1, usernames)

View file

@ -113,7 +113,7 @@ class VncActivity : AppCompatActivity() {
binding.reconnectBtn.setOnClickListener { retryConnection() } binding.reconnectBtn.setOnClickListener { retryConnection() }
//Observers //Observers
viewModel.credentialRequest.observe(this) { showCredentialDialog() } viewModel.credentialRequest.observe(this) { showLoginDialog() }
viewModel.sshHostKeyVerifyRequest.observe(this) { showHostKeyDialog() } viewModel.sshHostKeyVerifyRequest.observe(this) { showHostKeyDialog() }
viewModel.state.observe(this) { onClientStateChanged(it) } viewModel.state.observe(this) { onClientStateChanged(it) }
} }
@ -191,8 +191,8 @@ class VncActivity : AppCompatActivity() {
} }
} }
private fun showCredentialDialog() { private fun showLoginDialog() {
CredentialFragment().show(supportFragmentManager, "CredentialDialog") LoginFragment().show(supportFragmentManager, "LoginDialog")
} }
private fun showHostKeyDialog() { private fun showHostKeyDialog() {

View file

@ -116,10 +116,10 @@ class VncViewModel(val profile: ServerProfile, app: Application) : BaseViewModel
val serverUnlockRequest = LiveRequest<Any?, Boolean>(false, viewModelScope) val serverUnlockRequest = LiveRequest<Any?, Boolean>(false, viewModelScope)
/** /**
* List of known credentials. Used for providing suggestion when * List of saved profiles.
* new credentials are required. * Used by login-autocompletion.
*/ */
val knownCredentials by lazy { serverProfileDao.getCredentials() } val savedProfiles by lazy { serverProfileDao.getLiveList() }
/** /**
* Holds a weak reference to [FrameView] instance. * Holds a weak reference to [FrameView] instance.