-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMainActivity.kt
More file actions
390 lines (360 loc) · 18.7 KB
/
Copy pathMainActivity.kt
File metadata and controls
390 lines (360 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
@file:Suppress("DEPRECATION")
package com.android.swingmusic.presentation.activity
import android.annotation.SuppressLint
import android.content.ComponentName
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import androidx.media3.session.MediaController
import androidx.media3.session.SessionToken
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import com.android.swingmusic.album.presentation.screen.destinations.AlbumWithInfoScreenDestination
import com.android.swingmusic.album.presentation.screen.destinations.AllAlbumScreenDestination
import com.android.swingmusic.artist.presentation.screen.destinations.AllArtistsScreenDestination
import com.android.swingmusic.artist.presentation.screen.destinations.ArtistInfoScreenDestination
import com.android.swingmusic.artist.presentation.screen.destinations.ViewAllScreenOnArtistDestination
import com.android.swingmusic.artist.presentation.viewmodel.ArtistInfoViewModel
import com.android.swingmusic.auth.data.workmanager.scheduleTokenRefreshWork
import com.android.swingmusic.auth.presentation.screen.destinations.LoginWithQrCodeDestination
import com.android.swingmusic.auth.presentation.screen.destinations.LoginWithUsernameScreenDestination
import com.android.swingmusic.auth.presentation.viewmodel.AuthViewModel
import com.android.swingmusic.folder.presentation.event.FolderUiEvent
import com.android.swingmusic.folder.presentation.screen.destinations.FoldersAndTracksScreenDestination
import com.android.swingmusic.folder.presentation.viewmodel.FoldersViewModel
import com.android.swingmusic.player.presentation.screen.MiniPlayer
import com.android.swingmusic.player.presentation.screen.destinations.NowPlayingScreenDestination
import com.android.swingmusic.player.presentation.screen.destinations.QueueScreenDestination
import com.android.swingmusic.player.presentation.viewmodel.MediaControllerViewModel
import com.android.swingmusic.presentation.navigator.BottomNavItem
import com.android.swingmusic.presentation.navigator.CoreNavigator
import com.android.swingmusic.presentation.navigator.NavGraphs
import com.android.swingmusic.presentation.navigator.scaleInEnterTransition
import com.android.swingmusic.presentation.navigator.scaleInPopEnterTransition
import com.android.swingmusic.presentation.navigator.scaleOutExitTransition
import com.android.swingmusic.presentation.navigator.scaleOutPopExitTransition
import com.android.swingmusic.search.presentation.event.SearchUiEvent
import com.android.swingmusic.search.presentation.screen.destinations.SearchScreenDestination
import com.android.swingmusic.search.presentation.screen.destinations.ViewAllSearchResultsDestination
import com.android.swingmusic.search.presentation.viewmodel.SearchViewModel
import com.android.swingmusic.service.PlaybackService
import com.android.swingmusic.service.SessionTokenManager
import com.android.swingmusic.uicomponent.presentation.theme.SwingMusicTheme
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.google.common.util.concurrent.ListenableFuture
import com.google.common.util.concurrent.MoreExecutors
import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.animations.defaults.NestedNavGraphDefaultAnimations
import com.ramcosta.composedestinations.animations.defaults.RootNavGraphDefaultAnimations
import com.ramcosta.composedestinations.animations.rememberAnimatedNavHostEngine
import com.ramcosta.composedestinations.navigation.dependency
import com.ramcosta.composedestinations.utils.destination
import android.Manifest
import android.os.Build
import com.android.swingmusic.BuildConfig
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
@Deprecated(
message = "Legacy MainActivity. Use MainActivityWithAnimatedPlayer instead.",
replaceWith = ReplaceWith("MainActivityWithAnimatedPlayer")
)
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
private val mediaControllerViewModel: MediaControllerViewModel by viewModels<MediaControllerViewModel>()
private val authViewModel: AuthViewModel by viewModels<AuthViewModel>()
private val foldersViewModel: FoldersViewModel by viewModels<FoldersViewModel>()
private val artistInfoViewModel: ArtistInfoViewModel by viewModels<ArtistInfoViewModel>()
private val searchViewModel: SearchViewModel by viewModels<SearchViewModel>()
private lateinit var controllerFuture: ListenableFuture<MediaController>
override fun onStart() {
super.onStart()
lifecycleScope.launch {
authViewModel.isUserLoggedIn.collectLatest {
if (it == true) {
initializeMediaController()
}
}
}
}
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@OptIn(ExperimentalAnimationApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (BuildConfig.DEBUG && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), 0)
}
scheduleTokenRefreshWork(applicationContext)
setContent {
val isUserLoggedIn by authViewModel.isUserLoggedIn.collectAsState()
val playerState = mediaControllerViewModel.playerUiState.collectAsState()
val navController = rememberNavController()
val newBackStackEntry by navController.currentBackStackEntryAsState()
val route = newBackStackEntry?.destination?.route
val hideForDestination = listOf(
LoginWithUsernameScreenDestination,
LoginWithQrCodeDestination,
NowPlayingScreenDestination,
QueueScreenDestination
)
val showBottomNav =
route != null && newBackStackEntry?.destination() !in hideForDestination
val bottomNavItems: List<BottomNavItem> = listOf(
// BottomNavItem.Home,
BottomNavItem.Folder,
BottomNavItem.Album,
// BottomNavItem.Playlist,
BottomNavItem.Artist,
BottomNavItem.Search,
)
// Map of BottomNavItem to their route prefixes
val bottomNavRoutePrefixes = mapOf(
// BottomNavItem.Home to listOf(HomeDestination.route),
BottomNavItem.Folder to listOf(FoldersAndTracksScreenDestination.route),
BottomNavItem.Album to listOf(
AllAlbumScreenDestination.route,
AlbumWithInfoScreenDestination.route
),
BottomNavItem.Artist to listOf(
AllArtistsScreenDestination.route,
ArtistInfoScreenDestination.route,
ViewAllScreenOnArtistDestination.route
),
BottomNavItem.Search to listOf(
SearchScreenDestination.route,
ViewAllSearchResultsDestination.route
)
)
SwingMusicTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
bottomBar = {
Column(
modifier = Modifier.fillMaxWidth()
) {
// Show mini player whenever bottom nav is visible
if (showBottomNav) {
MiniPlayer(
mediaControllerViewModel = mediaControllerViewModel,
onClickPlayerItem = {
navController.navigate(
NowPlayingScreenDestination.route
) {
launchSingleTop = true
restoreState = false
}
}
)
// Show spacer only when there's a playing track
playerState.value.nowPlayingTrack?.let {
Box(
modifier = Modifier
.height(12.dp)
.fillMaxWidth()
.background(MaterialTheme.colorScheme.inverseOnSurface)
)
}
}
if (showBottomNav) {
NavigationBar(
modifier = Modifier.fillMaxWidth(),
containerColor = MaterialTheme.colorScheme.inverseOnSurface
) {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
bottomNavItems.forEach { item ->
NavigationBarItem(
icon = {
Icon(
painter = painterResource(id = item.icon),
contentDescription = null
)
},
selected = navController.currentDestination?.route?.let { route ->
bottomNavRoutePrefixes[item]?.any { prefix ->
route.startsWith(prefix)
} == true
} == true,
alwaysShowLabel = false,
label = { Text(text = item.title) },
onClick = {
// Whatever you do, DON'T TOUCH this
if (navController.currentDestination?.route != item.destination.route) {
navController.navigate(item.destination.route) {
launchSingleTop = true
restoreState = false
popUpTo(navController.graph.startDestinationId) {
saveState = false
inclusive = false
}
}
}
// refresh folders starting from $home
if (item.destination.route == FoldersAndTracksScreenDestination.route) {
foldersViewModel.onFolderUiEvent(
FolderUiEvent.OnClickNavPath(
folder = foldersViewModel.homeDir
)
)
}
// refresh Search screen
if (item.destination.route == SearchScreenDestination.route) {
searchViewModel.onSearchUiEvent(
SearchUiEvent.OnClearSearchStates
)
}
}
)
}
}
}
}
}
}
) {
Surface(modifier = Modifier.fillMaxSize()) {
AnimatedVisibility(
visible = isUserLoggedIn == null,
enter = fadeIn(animationSpec = tween(durationMillis = 100))
) {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
CircularProgressIndicator()
}
}
isUserLoggedIn?.let { value ->
SwingMusicAppNavigation(
isUserLoggedIn = value,
navController = navController,
authViewModel = authViewModel,
mediaControllerViewModel = mediaControllerViewModel,
foldersViewModel = foldersViewModel,
artistInfoViewModel = artistInfoViewModel,
searchViewModel = searchViewModel
)
}
}
}
}
}
}
private fun initializeMediaController() {
if (
mediaControllerViewModel.getMediaController() == null ||
(this::controllerFuture.isInitialized).not()
) {
val sessionToken = SessionTokenManager.sessionToken
if (sessionToken != null) {
// Use the existing session token to build the MediaController
controllerFuture = MediaController.Builder(this, sessionToken).buildAsync()
controllerFuture.addListener(
{
val mediaController = controllerFuture.get()
mediaControllerViewModel.reconnectMediaController(mediaController)
}, MoreExecutors.directExecutor()
)
} else {
// Create a new session token if no existing token is found
val newSessionToken =
SessionToken(this, ComponentName(this, PlaybackService::class.java))
controllerFuture = MediaController.Builder(this, newSessionToken).buildAsync()
controllerFuture.addListener(
{
val mediaController = controllerFuture.get()
mediaControllerViewModel.setMediaController(mediaController)
}, MoreExecutors.directExecutor()
)
}
}
}
override fun onDestroy() {
super.onDestroy()
if (this::controllerFuture.isInitialized) {
mediaControllerViewModel.releaseMediaController(controllerFuture)
}
}
}
@OptIn(ExperimentalMaterialNavigationApi::class)
@ExperimentalAnimationApi
@Composable
internal fun SwingMusicAppNavigation(
isUserLoggedIn: Boolean,
navController: NavHostController,
authViewModel: AuthViewModel,
mediaControllerViewModel: MediaControllerViewModel,
foldersViewModel: FoldersViewModel,
artistInfoViewModel: ArtistInfoViewModel,
searchViewModel: SearchViewModel
) {
val navGraph = remember(isUserLoggedIn) { NavGraphs.root(isUserLoggedIn) }
val animatedNavHostEngine = rememberAnimatedNavHostEngine(
navHostContentAlignment = Alignment.TopCenter,
rootDefaultAnimations = RootNavGraphDefaultAnimations.ACCOMPANIST_FADING,
defaultAnimationsForNestedNavGraph = mapOf(
NavGraphs.root(isUserLoggedIn) to NestedNavGraphDefaultAnimations(
enterTransition = {
scaleInEnterTransition()
},
exitTransition = {
scaleOutExitTransition()
},
popEnterTransition = {
scaleInPopEnterTransition()
},
popExitTransition = {
scaleOutPopExitTransition()
}
)
)
)
DestinationsNavHost(
engine = animatedNavHostEngine,
navController = navController,
navGraph = navGraph,
dependenciesContainerBuilder = {
dependency(
CoreNavigator(navController = navController)
)
dependency(authViewModel)
dependency(foldersViewModel)
dependency(mediaControllerViewModel)
dependency(artistInfoViewModel)
dependency(searchViewModel)
}
)
}