Skip to content

Commit

Permalink
Refactor connection state tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gujjwal00 committed Feb 23, 2024
1 parent 923575c commit 19f22bd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/gaurav/avnc/ui/vnc/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.gaurav.avnc.databinding.FragmentCredentialBinding
import com.gaurav.avnc.model.LoginInfo
import com.gaurav.avnc.model.ServerProfile
import com.gaurav.avnc.viewmodel.VncViewModel
import com.gaurav.avnc.viewmodel.VncViewModel.State.Companion.isConnected
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputLayout

Expand Down Expand Up @@ -114,7 +115,7 @@ class LoginFragment : DialogFragment() {
// Use activity as owner because this fragment will likely be destroyed before connecting
viewModel.state.observe(requireActivity(), object : Observer<VncViewModel.State> {
override fun onChanged(value: VncViewModel.State) {
if (value == VncViewModel.State.Connected) {
if (value.isConnected) {
setLoginInfoInProfile(viewModel.profile, loginInfo)
viewModel.saveProfile()
viewModel.state.removeObserver(this)
Expand Down
9 changes: 4 additions & 5 deletions app/src/main/java/com/gaurav/avnc/ui/vnc/Toolbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import androidx.drawerlayout.widget.DrawerLayout
import androidx.lifecycle.lifecycleScope
import com.gaurav.avnc.R
import com.gaurav.avnc.viewmodel.VncViewModel.State
import com.gaurav.avnc.viewmodel.VncViewModel.State.Companion.isConnected
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -131,15 +132,13 @@ class Toolbar(private val activity: VncActivity, private val dispatcher: Dispatc
}

private fun onStateChange(state: State) {
val isConnected = (state == State.Connected)

if (isConnected)
if (state.isConnected)
highlightForFirstTimeUser()

if (Build.VERSION.SDK_INT >= 29)
updateGestureExclusionRect()

updateLockMode(isConnected)
updateLockMode(state.isConnected)
}

/**
Expand Down Expand Up @@ -204,7 +203,7 @@ class Toolbar(private val activity: VncActivity, private val dispatcher: Dispatc
*/
@RequiresApi(29)
private fun updateGestureExclusionRect() {
if (!openWithSwipe || viewModel.state.value != State.Connected) {
if (!openWithSwipe || !viewModel.state.value.isConnected) {
drawerLayout.systemGestureExclusionRects = listOf()
} else {
// Area covered by primaryButtons, in drawerLayout's coordinate space
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/gaurav/avnc/ui/vnc/VncActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.gaurav.avnc.model.ServerProfile
import com.gaurav.avnc.util.DeviceAuthPrompt
import com.gaurav.avnc.util.SamsungDex
import com.gaurav.avnc.viewmodel.VncViewModel
import com.gaurav.avnc.viewmodel.VncViewModel.State.Companion.isConnected
import com.gaurav.avnc.vnc.VncUri
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -185,7 +186,7 @@ class VncActivity : AppCompatActivity() {
}

private fun onClientStateChanged(newState: VncViewModel.State) {
val isConnected = (newState == VncViewModel.State.Connected)
val isConnected = newState.isConnected

binding.frameView.isVisible = isConnected
binding.frameView.keepScreenOn = isConnected && viewModel.pref.viewer.keepScreenOn
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/com/gaurav/avnc/viewmodel/VncViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ class VncViewModel(val profile: ServerProfile, app: Application) : BaseViewModel
Created,
Connecting,
Connected,
Disconnected,
Disconnected;

companion object {
val State?.isConnected get() = (this == Connected)
val State?.isDisconnected get() = (this == Disconnected)
}
}

val client = VncClient(this)
Expand Down

0 comments on commit 19f22bd

Please sign in to comment.