[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:
parent
7d1ee07963
commit
0e3b42d8d1
2 changed files with 21 additions and 9 deletions
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,4 +4,5 @@ import androidx.lifecycle.ViewModel
|
|||
|
||||
class MainViewModel : ViewModel() {
|
||||
var startDestinationChanged = false
|
||||
var startDestinationTvChanged = false
|
||||
}
|
Loading…
Reference in a new issue