[TV] Set correct navigation start destination (#161)

* [TV] Fix not loading to the addServerFragment on first launch

* Don't query the database when startDestination has already changed
This commit is contained in:
Jarne Demeulemeester 2022-09-10 17:56:43 +02:00 committed by GitHub
parent 7d1ee07963
commit 0e3b42d8d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 9 deletions

View file

@ -6,6 +6,7 @@ import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavGraph
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
@ -25,7 +26,7 @@ class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var uiModeManager: UiModeManager
private val viewModel : MainViewModel by viewModels()
private val viewModel: MainViewModel by viewModels()
@Inject
lateinit var database: ServerDatabaseDao
@ -47,15 +48,14 @@ class MainActivity : AppCompatActivity() {
if (uiModeManager.currentModeType == Configuration.UI_MODE_TYPE_TELEVISION) {
graph.setStartDestination(R.id.homeFragmentTv)
navController.setGraph(graph, intent.extras)
}
val nServers = database.getServersCount()
if (nServers < 1) {
if (!viewModel.startDestinationChanged) {
graph.setStartDestination(R.id.addServerFragment)
checkServersEmpty(graph)
if (!viewModel.startDestinationTvChanged) {
viewModel.startDestinationTvChanged = true
navController.setGraph(graph, intent.extras)
}
} else {
checkServersEmpty(graph) {
navController.setGraph(graph, intent.extras)
viewModel.startDestinationChanged = true
}
}
@ -98,4 +98,15 @@ class MainActivity : AppCompatActivity() {
onBackPressed()
return true
}
private fun checkServersEmpty(graph: NavGraph, onServersEmpty: () -> Unit = {}) {
if (!viewModel.startDestinationChanged) {
val nServers = database.getServersCount()
if (nServers < 1) {
graph.setStartDestination(R.id.addServerFragment)
viewModel.startDestinationChanged = true
onServersEmpty()
}
}
}
}

View file

@ -4,4 +4,5 @@ import androidx.lifecycle.ViewModel
class MainViewModel : ViewModel() {
var startDestinationChanged = false
var startDestinationTvChanged = false
}