11package com.android.swingmusic.presentation.navigator
22
33import androidx.navigation.NavController
4- import androidx.navigation.NavOptionsBuilder
54import com.android.swingmusic.album.presentation.screen.destinations.AlbumWithInfoScreenDestination
65import com.android.swingmusic.artist.presentation.screen.destinations.ArtistInfoScreenDestination
76import com.android.swingmusic.artist.presentation.screen.destinations.ViewAllScreenOnArtistDestination
87import com.android.swingmusic.auth.presentation.screen.destinations.LoginWithQrCodeDestination
98import com.android.swingmusic.auth.presentation.screen.destinations.LoginWithUsernameScreenDestination
10- import com.android.swingmusic.auth.presentation.viewmodel.AuthViewModel
119import com.android.swingmusic.common.presentation.navigator.CommonNavigator
1210import com.android.swingmusic.folder.presentation.screen.destinations.FoldersAndTracksScreenDestination
11+ import com.android.swingmusic.home.presentation.destinations.HomeDestination
1312import com.android.swingmusic.player.presentation.screen.destinations.QueueScreenDestination
14- import com.android.swingmusic.player.presentation.viewmodel.MediaControllerViewModel
1513import com.android.swingmusic.search.presentation.screen.destinations.ViewAllSearchResultsDestination
16- import com.ramcosta.composedestinations.dynamic.within
1714import com.ramcosta.composedestinations.navigation.navigate
18- import com.ramcosta.composedestinations.spec.NavGraphSpec
1915
2016class CoreNavigator (
21- private val navGraph : NavGraphSpec ,
22- private val navController : NavController ,
23- private val mediaControllerViewModel : MediaControllerViewModel ,
24- private val authViewModel : AuthViewModel
17+ private val navController : NavController
2518) : CommonNavigator {
19+
2620 /* *----------------------------------- Auth Navigator ----------------------------------------*/
2721 override fun gotoLoginWithUsername () {
28- val currentDestination = navController.currentDestination?.route
2922 val targetDestination = LoginWithUsernameScreenDestination
3023
31- if (currentDestination != targetDestination.route) {
32- navController.navigate(targetDestination within navGraph,
33- fun NavOptionsBuilder .() {
34- launchSingleTop = true
35- restoreState = false
36-
37- popUpTo(navController.graph.startDestinationId) {
38- saveState = false
39- inclusive = true
40- }
41- }
42- )
24+ navController.navigate(targetDestination) {
25+ launchSingleTop = true
26+ restoreState = false
27+
28+ popUpTo(navController.graph.startDestinationId) {
29+ inclusive = true
30+ saveState = false
31+ }
4332 }
4433 }
4534
4635 override fun gotoLoginWithQrCode () {
47- val currentDestination = navController.currentDestination?.route
4836 val targetDestination = LoginWithQrCodeDestination
4937
50- if (currentDestination != targetDestination.route) {
51- navController.navigate(targetDestination within navGraph,
52- fun NavOptionsBuilder .() {
53- launchSingleTop = true
54- restoreState = false
55-
56- popUpTo(navController.graph.startDestinationId) {
57- saveState = false
58- inclusive = true
59- }
60- }
61- )
38+ navController.navigate(targetDestination) {
39+ launchSingleTop = true
40+ restoreState = false
41+
42+ popUpTo(navController.graph.startDestinationId) {
43+ inclusive = true
44+ saveState = false
45+ }
6246 }
6347 }
6448
65- override fun gotoHomeNavGraph () {
66- mediaControllerViewModel.refreshBaseUrl()
67- authViewModel.updateIsUserLoggedInFlow() // flow collected in the main activity
68-
69- val currentDestination = navController.currentDestination?.route
70- val homeNavGraph = NavGraphs .home
71-
72- if (currentDestination != homeNavGraph.route) {
73- navController.navigate(
74- homeNavGraph,
75- fun NavOptionsBuilder .() {
76- launchSingleTop = true
77- restoreState = true
78-
79- popUpTo(navController.graph.startDestinationId) {
80- saveState = true
81- inclusive = true
82- }
83- }
84- )
49+ override fun gotoHome () {
50+ val targetDestination = HomeDestination ()
51+
52+ navController.navigate(targetDestination) {
53+ launchSingleTop = true
54+ restoreState = false
55+
56+ popUpTo(navController.graph.startDestinationId) {
57+ inclusive = true
58+ saveState = false
59+ }
8560 }
8661 }
8762
8863 // Todo: Remove this after adding home content
89- // REMEMBER: Bug -> Sometimes the app enters ANR state after this method is called
90- override fun gotoFolderNavGraph () {
91- mediaControllerViewModel.refreshBaseUrl()
92- authViewModel.updateIsUserLoggedInFlow() // flow collected in the main activity
93-
94- val currentDestination = navController.currentDestination?.route
95- val folderNavGraph = NavGraphs .folder
96-
97- if (currentDestination != folderNavGraph.route) {
98- navController.navigate(
99- folderNavGraph,
100- fun NavOptionsBuilder .() {
101- launchSingleTop = true
102- restoreState = true
103-
104- popUpTo(navController.graph.startDestinationId) {
105- saveState = true
106- inclusive = true
107- }
108- }
109- )
64+ override fun gotoFolders () {
65+ val targetDestination = FoldersAndTracksScreenDestination ()
66+
67+ navController.navigate(targetDestination) {
68+ launchSingleTop = true
69+ restoreState = false
70+
71+ popUpTo(navController.graph.startDestinationId) {
72+ inclusive = true
73+ saveState = false
74+ }
11075 }
11176 }
11277
11378 /* *----------------------------------- Album Navigator --------------------------------------*/
11479 override fun gotoAlbumWithInfo (albumHash : String ) {
115- val currentDestination = navController.currentDestination?.route
11680 val targetDestination = AlbumWithInfoScreenDestination (albumHash)
11781
118- if (currentDestination != targetDestination.route) {
119- navController.navigate(
120- targetDestination within navGraph,
121- fun NavOptionsBuilder .() {
122- launchSingleTop = true
123- restoreState = true
124- }
125- )
82+ navController.navigate(targetDestination) {
83+ launchSingleTop = true
12684 }
12785 }
12886
@@ -132,29 +90,22 @@ class CoreNavigator(
13290
13391 /* *----------------------------------- Player Navigator -------------------------------------*/
13492 override fun gotoQueueScreen () {
135- val currentDestination = navController.currentDestination?.route
13693 val targetDestination = QueueScreenDestination
13794
138- if (currentDestination != targetDestination.route ) {
139- navController.navigate(targetDestination within navGraph)
95+ navController.navigate( targetDestination) {
96+ launchSingleTop = true
14097 }
98+
14199 }
142100
143101 override fun gotoArtistInfo (artistHash : String ) {
144- val currentDestination = navController.currentDestination?.route
145102 val targetDestination = ArtistInfoScreenDestination (
146103 artistHash = artistHash,
147104 loadNewArtist = true
148105 )
149106
150- if (currentDestination != targetDestination.route) {
151- navController.navigate(
152- targetDestination within navGraph,
153- fun NavOptionsBuilder .() {
154- launchSingleTop = true
155- restoreState = true
156- }
157- )
107+ navController.navigate(targetDestination) {
108+ launchSingleTop = true
158109 }
159110 }
160111
@@ -163,56 +114,35 @@ class CoreNavigator(
163114 artistName : String ,
164115 baseUrl : String
165116 ) {
166- val currentDestination = navController.currentDestination?.route
167- val targetDestination =
168- ViewAllScreenOnArtistDestination (viewAllType, artistName, baseUrl)
169-
170- if (currentDestination != targetDestination.route) {
171- navController.navigate(
172- targetDestination within navGraph,
173- fun NavOptionsBuilder .() {
174- launchSingleTop = true
175- restoreState = true
176- }
177- )
117+ val targetDestination = ViewAllScreenOnArtistDestination (
118+ viewAllType = viewAllType,
119+ artistName = artistName,
120+ baseUrl = baseUrl
121+ )
122+
123+ navController.navigate(targetDestination) {
124+ launchSingleTop = true
178125 }
179126 }
180127
181128 override fun gotoViewAllSearchResultsScreen (
182129 viewAllType : String ,
183130 searchParams : String
184131 ) {
185- val currentDestination = navController.currentDestination?.route
186- val targetDestination =
187- ViewAllSearchResultsDestination (
188- searchParams = searchParams,
189- viewAllType = viewAllType
190- )
191-
192- if (currentDestination != targetDestination.route) {
193- navController.navigate(
194- targetDestination within navGraph,
195- fun NavOptionsBuilder .() {
196- launchSingleTop = true
197- restoreState = true
198- }
199- )
132+ val targetDestination = ViewAllSearchResultsDestination (
133+ searchParams = searchParams,
134+ viewAllType = viewAllType
135+ )
136+
137+ navController.navigate(targetDestination) {
138+ launchSingleTop = true
200139 }
201140 }
202141
203142 override fun gotoSourceFolder (name : String , path : String ) {
204- val currentDestination = navController.currentDestination?.route
205- val targetDestination =
206- FoldersAndTracksScreenDestination (gotoFolderName = name, gotoFolderPath = path)
207-
208- if (currentDestination != targetDestination.route) {
209- navController.navigate(
210- targetDestination within navGraph,
211- fun NavOptionsBuilder .() {
212- launchSingleTop = true
213- restoreState = true
214- }
215- )
143+ val targetDestination = FoldersAndTracksScreenDestination (name, path)
144+ navController.navigate(targetDestination) {
145+ launchSingleTop = true
216146 }
217147 }
218148}
0 commit comments