Skip to content

Commit

Permalink
PR feedback: Move BackendService handling to ViewModel
Browse files Browse the repository at this point in the history
  • Loading branch information
dzolnai committed Oct 13, 2023
1 parent 0b54337 commit d26285f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 31 deletions.
52 changes: 21 additions & 31 deletions app/src/main/java/nl/eduvpn/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
private const val KEY_BACK_NAVIGATION_ENABLED = "back_navigation_enabled"
}

@Inject
protected lateinit var historyService: HistoryService

@Inject
protected lateinit var vpnService: Optional<VPNService>

@Inject
protected lateinit var eduVPNOpenVPNService: EduVPNOpenVPNService

@Inject
protected lateinit var backendService: BackendService

@Inject
protected lateinit var viewModelFactory : ViewModelFactory

Expand Down Expand Up @@ -105,37 +99,36 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
EduVPNApplication.get(this).component().inject(this)
backendService.register(
startOAuth = { oAuthUrl ->
openLink(oAuthUrl)
},
selectCountry = { cookie ->
selectCountry(cookie)
},
selectProfiles = {
openFragment(ProfileSelectionFragment.newInstance(it), false)
},
connectWithConfig = { config ->
runOnUiThread {
viewModel.parseConfigAndStartConnection(this, config)
setSupportActionBar(binding.toolbar.toolbar)
viewModel.mainParentAction.observe(this) { parentAction ->
when (parentAction) {
is MainViewModel.MainParentAction.OpenLink -> {
openLink(parentAction.oAuthUrl)
}
is MainViewModel.MainParentAction.SelectCountry -> {
selectCountry(parentAction.cookie)
}
is MainViewModel.MainParentAction.SelectProfiles -> {
openFragment(ProfileSelectionFragment.newInstance(parentAction.profileList), false)
}
is MainViewModel.MainParentAction.ConnectWithConfig -> {
viewModel.parseConfigAndStartConnection(this, parentAction.config)
val currentFragment = supportFragmentManager.findFragmentById(R.id.content_frame)
if (currentFragment !is ConnectionStatusFragment) {
openFragment(ConnectionStatusFragment(), false)
}
}
},
showError = { throwable ->
show(this, throwable)
is MainViewModel.MainParentAction.ShowError -> {
show(this, parentAction.throwable)
}
}
)
setSupportActionBar(binding.toolbar.toolbar)
historyService.load()
}
eduVPNOpenVPNService.onCreate(this)
if (savedInstanceState == null) {
// If there's an ongoing VPN connection, open the status screen.
if (vpnService.isPresent && vpnService.get().getStatus() != VPNService.VPNStatus.DISCONNECTED) {
openFragment(ConnectionStatusFragment(), false)
} else if (historyService.addedServers?.hasServers() == true) {
} else if (viewModel.hasServers()) {
openFragment(ServerSelectionFragment.newInstance(false), false)
} else if (BuildConfig.API_DISCOVERY_ENABLED) {
openFragment(OrganizationSelectionFragment(), false)
Expand Down Expand Up @@ -185,8 +178,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {

override fun onResume() {
super.onResume()
historyService.load()
backendService.cancelPendingRedirect()
viewModel.onResume()
}

override fun onStart() {
Expand All @@ -207,7 +199,7 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
super.onNewIntent(intent)
this.lifecycleScope.launch(Dispatchers.IO) {
try {
if (backendService.handleRedirection(intent.data)) {
if (viewModel.handleRedirection(intent.data)) {
// Remove it so we don't parse it again.
intent.data = null
val currentFragment =
Expand Down Expand Up @@ -242,14 +234,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
)
)
}
backendService.cancelPendingRedirect()
}
}

override fun onDestroy() {
super.onDestroy()
eduVPNOpenVPNService.onDestroy(this)
backendService.deregister()
}

fun openFragment(fragment: Fragment?, openOnTop: Boolean) {
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/nl/eduvpn/app/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package nl.eduvpn.app.viewmodel

import android.content.Context
import android.net.Uri
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.wireguard.config.Config
import kotlinx.coroutines.Dispatchers
Expand All @@ -10,6 +12,7 @@ import kotlinx.coroutines.withContext
import nl.eduvpn.app.MainActivity
import nl.eduvpn.app.R
import nl.eduvpn.app.entity.AuthorizationType
import nl.eduvpn.app.entity.Profile
import nl.eduvpn.app.entity.SerializedVpnConfig
import nl.eduvpn.app.entity.VPNConfig
import nl.eduvpn.app.service.BackendService
Expand All @@ -19,6 +22,7 @@ import nl.eduvpn.app.service.OrganizationService
import nl.eduvpn.app.service.PreferencesService
import nl.eduvpn.app.service.VPNConnectionService
import nl.eduvpn.app.utils.getCountryText
import nl.eduvpn.app.utils.toSingleEvent
import org.eduvpn.common.Protocol
import java.io.BufferedReader
import java.io.StringReader
Expand All @@ -39,6 +43,52 @@ class MainViewModel @Inject constructor(
preferencesService,
vpnConnectionService
) {
sealed class MainParentAction {
data class OpenLink(val oAuthUrl: String) : MainParentAction()
data class SelectCountry(val cookie: Int?) : MainParentAction()
data class SelectProfiles(val profileList: List<Profile>): MainParentAction()
data class ConnectWithConfig(val config: SerializedVpnConfig) : MainParentAction()

data class ShowError(val throwable: Throwable) : MainParentAction()
}

private val _mainParentAction = MutableLiveData<MainParentAction>()
val mainParentAction = _mainParentAction.toSingleEvent()

init {
backendService.register(
startOAuth = { oAuthUrl ->
_mainParentAction.postValue(MainParentAction.OpenLink(oAuthUrl))
},
selectCountry = { cookie ->
_mainParentAction.postValue(MainParentAction.SelectCountry(cookie))
},
selectProfiles = { profileList ->
_mainParentAction.postValue(MainParentAction.SelectProfiles(profileList))
},
connectWithConfig = { config ->
_mainParentAction.postValue(MainParentAction.ConnectWithConfig(config))
},
showError = { throwable ->
_mainParentAction.postValue(MainParentAction.ShowError(throwable))
}
)
historyService.load()
}

override fun onResume() {
historyService.load()
backendService.cancelPendingRedirect()
super.onResume()
}

override fun onCleared() {
super.onCleared()
backendService.deregister()
}

fun hasServers() = historyService.addedServers?.hasServers() == true

fun parseConfigAndStartConnection(activity: MainActivity, config: SerializedVpnConfig) {
preferencesService.setCurrentProtocol(config.protocol)
val parsedConfig = if (config.protocol == Protocol.OpenVPN.nativeValue) {
Expand All @@ -56,6 +106,12 @@ class MainViewModel @Inject constructor(
vpnConnectionService.connectionToConfig(viewModelScope, activity, parsedConfig)
}

suspend fun handleRedirection(data: Uri?) : Boolean {
val result = backendService.handleRedirection(data)
backendService.cancelPendingRedirect()
return result
}

fun useCustomTabs() = preferencesService.getAppSettings().useCustomTabs()
fun getCountryList(activity: MainActivity, cookie: Int? = null) {
viewModelScope.launch(Dispatchers.IO) {
Expand Down Expand Up @@ -90,4 +146,6 @@ class MainViewModel @Inject constructor(
}
}
}


}

0 comments on commit d26285f

Please sign in to comment.