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].
* - [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.
* - [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.room.*
import com.gaurav.avnc.model.LoginInfo
import com.gaurav.avnc.model.ServerProfile
@Dao
@ -32,9 +31,6 @@ interface ServerProfileDao {
@Query("SELECT * FROM profiles WHERE name = :name")
suspend fun getByName(name: String): List<ServerProfile>
@Query("SELECT name, host, username, password FROM profiles")
fun getCredentials(): LiveData<List<LoginInfo>>
@Insert
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.
*/
class CredentialFragment : DialogFragment() {
class LoginFragment : DialogFragment() {
val viewModel by activityViewModels<VncViewModel>()
lateinit var binding: FragmentCredentialBinding
@ -100,9 +100,10 @@ class CredentialFragment : DialogFragment() {
if (viewModel.pref.server.lockSavedServer)
return
viewModel.knownCredentials.observe(this) { list ->
val usernames = list.map { it.username }.filter { it.isNotEmpty() }.distinct()
val passwords = preparePasswordSuggestions(list)
viewModel.savedProfiles.observe(this) { profiles ->
val logins = profiles.map { LoginInfo(it.name, it.host, it.username, it.password) }
val usernames = logins.map { it.username }.filter { it.isNotEmpty() }.distinct()
val passwords = preparePasswordSuggestions(logins)
if (usernames.isNotEmpty()) {
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() }
//Observers
viewModel.credentialRequest.observe(this) { showCredentialDialog() }
viewModel.credentialRequest.observe(this) { showLoginDialog() }
viewModel.sshHostKeyVerifyRequest.observe(this) { showHostKeyDialog() }
viewModel.state.observe(this) { onClientStateChanged(it) }
}
@ -191,8 +191,8 @@ class VncActivity : AppCompatActivity() {
}
}
private fun showCredentialDialog() {
CredentialFragment().show(supportFragmentManager, "CredentialDialog")
private fun showLoginDialog() {
LoginFragment().show(supportFragmentManager, "LoginDialog")
}
private fun showHostKeyDialog() {

View file

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