forked from libre-tube/LibreTube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.kt
507 lines (439 loc) · 18.9 KB
/
MainActivity.kt
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package com.github.libretube.ui.activities
import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ScrollView
import androidx.activity.addCallback
import androidx.activity.viewModels
import androidx.appcompat.widget.SearchView
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.os.bundleOf
import androidx.core.view.allViews
import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.core.widget.NestedScrollView
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.NavDirections
import com.github.libretube.R
import com.github.libretube.compat.PictureInPictureCompat
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.ActivityMainBinding
import com.github.libretube.extensions.toID
import com.github.libretube.helpers.NavBarHelper
import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.NetworkHelper
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.helpers.ThemeHelper
import com.github.libretube.helpers.WindowHelper
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.ErrorDialog
import com.github.libretube.ui.fragments.AudioPlayerFragment
import com.github.libretube.ui.fragments.DownloadsFragment
import com.github.libretube.ui.fragments.PlayerFragment
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.models.SearchViewModel
import com.github.libretube.ui.models.SubscriptionsViewModel
class MainActivity : BaseActivity() {
lateinit var binding: ActivityMainBinding
lateinit var navController: NavController
lateinit var searchView: SearchView
private lateinit var searchItem: MenuItem
private var startFragmentId = R.id.homeFragment
private val playerViewModel: PlayerViewModel by viewModels()
private val searchViewModel: SearchViewModel by viewModels()
private val subscriptionsViewModel: SubscriptionsViewModel by viewModels()
private var savedSearchQuery: String? = null
private var shouldOpenSuggestions = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// enable auto rotation if turned on
requestOrientationChange()
// show noInternet Activity if no internet available on app startup
if (!NetworkHelper.isNetworkAvailable(this)) {
val noInternetIntent = Intent(this, NoInternetActivity::class.java)
startActivity(noInternetIntent)
finish()
return
} else if (PreferenceHelper.getString(PreferenceKeys.FETCH_INSTANCE, "").isEmpty()) {
val welcomeIntent = Intent(this, WelcomeActivity::class.java)
startActivity(welcomeIntent)
finish()
return
}
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// set the action bar for the activity
setSupportActionBar(binding.toolbar)
navController = findNavController(R.id.fragment)
binding.bottomNav.setupWithNavController(navController)
// save start tab fragment id and apply navbar style
startFragmentId = try {
NavBarHelper.applyNavBarStyle(binding.bottomNav)
} catch (e: Exception) {
R.id.homeFragment
}
// sets the navigation bar color to the previously calculated color
window.navigationBarColor = if (binding.bottomNav.menu.size() > 0) {
ThemeHelper.getThemeColor(this, com.google.android.material.R.attr.colorSurfaceContainer)
} else {
ThemeHelper.getThemeColor(this, android.R.attr.colorBackground)
}
// set default tab as start fragment
navController.graph = navController.navInflater.inflate(R.navigation.nav).also {
it.setStartDestination(startFragmentId)
}
binding.bottomNav.setOnApplyWindowInsetsListener(null)
// Prevent duplicate entries into backstack, if selected item and current
// visible fragment is different, then navigate to selected item.
binding.bottomNav.setOnItemReselectedListener {
if (it.itemId != navController.currentDestination?.id) {
navigateToBottomSelectedItem(it)
} else {
// get the host fragment containing the current fragment
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragment) as? NavHostFragment
// get the current fragment
val fragment = navHostFragment?.childFragmentManager?.fragments?.firstOrNull()
tryScrollToTop(fragment?.requireView())
}
}
binding.bottomNav.setOnItemSelectedListener {
navigateToBottomSelectedItem(it)
false
}
if (binding.bottomNav.menu.children.none { it.itemId == startFragmentId }) deselectBottomBarItems()
binding.toolbar.title = ThemeHelper.getStyledAppName(this)
// handle error logs
PreferenceHelper.getErrorLog().ifBlank { null }?.let {
ErrorDialog().show(supportFragmentManager, null)
}
setupSubscriptionsBadge()
// new way of handling back presses
onBackPressedDispatcher.addCallback {
if (playerViewModel.isFullscreen.value == true) {
supportFragmentManager.fragments.filterIsInstance<PlayerFragment>()
.firstOrNull()
?.let {
it.unsetFullscreen()
return@addCallback
}
}
if (binding.mainMotionLayout.progress == 0F) {
runCatching {
minimizePlayer()
return@addCallback
}
}
when (navController.currentDestination?.id) {
startFragmentId -> {
moveTaskToBack(true)
onUserLeaveHint()
}
R.id.searchResultFragment -> {
navController.popBackStack(R.id.searchFragment, true) ||
navController.popBackStack()
}
else -> {
navController.popBackStack()
}
}
}
loadIntentData()
}
/**
* Deselect all bottom bar items
*/
private fun deselectBottomBarItems() {
binding.bottomNav.menu.setGroupCheckable(0, true, false)
for (child in binding.bottomNav.menu.children) {
child.isChecked = false
}
binding.bottomNav.menu.setGroupCheckable(0, true, true)
}
/**
* Try to find a scroll or recycler view and scroll it back to the top
*/
private fun tryScrollToTop(view: View?) {
val scrollView = view?.allViews
?.firstOrNull { it is ScrollView || it is NestedScrollView || it is RecyclerView }
when (scrollView) {
is ScrollView -> scrollView.smoothScrollTo(0, 0)
is NestedScrollView -> scrollView.smoothScrollTo(0, 0)
is RecyclerView -> scrollView.smoothScrollToPosition(0)
}
}
/**
* Initialize the notification badge showing the amount of new videos
*/
private fun setupSubscriptionsBadge() {
if (!PreferenceHelper.getBoolean(
PreferenceKeys.NEW_VIDEOS_BADGE,
false
)
) {
return
}
subscriptionsViewModel.fetchSubscriptions(this)
subscriptionsViewModel.videoFeed.observe(this) { feed ->
val lastSeenVideoIndex = feed.orEmpty()
.indexOfFirst { PreferenceHelper.getLastSeenVideoId() == it.url?.toID() }
if (lastSeenVideoIndex < 1) return@observe
binding.bottomNav.getOrCreateBadge(R.id.subscriptionsFragment).apply {
number = lastSeenVideoIndex
backgroundColor = ThemeHelper.getThemeColor(
this@MainActivity,
androidx.appcompat.R.attr.colorPrimary
)
badgeTextColor = ThemeHelper.getThemeColor(
this@MainActivity,
com.google.android.material.R.attr.colorOnPrimary
)
}
}
}
/**
* Remove the focus of the search view in the toolbar
*/
private fun removeSearchFocus() {
searchView.setQuery("", false)
searchView.clearFocus()
searchView.isIconified = true
searchItem.collapseActionView()
searchView.onActionViewCollapsed()
}
private fun isSearchInProgress(): Boolean {
if (!::navController.isInitialized) return false
val id = navController.currentDestination?.id ?: return false
return id in listOf(R.id.searchFragment, R.id.searchResultFragment, R.id.channelFragment)
}
override fun invalidateMenu() {
// Don't invalidate menu when in search in progress
// this is a workaround as there is bug in android code
// details of bug: https://issuetracker.google.com/issues/244336571
if (isSearchInProgress()) {
return
}
super.invalidateMenu()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.action_bar, menu)
// stuff for the search in the topBar
val searchItem = menu.findItem(R.id.action_search)
this.searchItem = searchItem
searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
navController.navigate(NavDirections.showSearchResults(query))
searchView.clearFocus()
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
if (!shouldOpenSuggestions) return true
// Prevent navigation when search view is collapsed
if (searchView.isIconified ||
binding.bottomNav.menu.children.any {
it.itemId == navController.currentDestination?.id
}
) {
return true
}
// prevent malicious navigation when the search view is getting collapsed
val destIds = listOf(
R.id.searchResultFragment,
R.id.channelFragment,
R.id.playlistFragment
)
if (navController.currentDestination?.id in destIds && newText == null) {
return false
}
if (navController.currentDestination?.id != R.id.searchFragment) {
navController.navigate(
R.id.searchFragment,
bundleOf(IntentData.query to newText)
)
} else {
searchViewModel.setQuery(newText)
}
return true
}
})
searchItem.setOnActionExpandListener(object : MenuItem.OnActionExpandListener {
override fun onMenuItemActionExpand(item: MenuItem): Boolean {
if (navController.currentDestination?.id != R.id.searchResultFragment) {
searchViewModel.setQuery(null)
navController.navigate(R.id.searchFragment)
}
item.setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS or MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
)
return true
}
override fun onMenuItemActionCollapse(item: MenuItem): Boolean {
if (binding.mainMotionLayout.progress == 0F) {
runCatching {
minimizePlayer()
}
}
// Handover back press to `BackPressedDispatcher`
else if (binding.bottomNav.menu.children.none {
it.itemId == navController.currentDestination?.id
}
) {
this@MainActivity.onBackPressedDispatcher.onBackPressed()
}
// Suppress collapsing of search when search in progress.
return !isSearchInProgress()
}
})
// handle search queries passed by the intent
if (savedSearchQuery != null) {
searchItem.expandActionView()
searchView.setQuery(savedSearchQuery, true)
savedSearchQuery = null
}
return super.onCreateOptionsMenu(menu)
}
/**
* Update the query text in the search bar without opening the search suggestions
*/
fun setQuerySilent(query: String) {
if (!this::searchView.isInitialized) return
shouldOpenSuggestions = false
searchView.setQuery(query, false)
shouldOpenSuggestions = true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> {
val settingsIntent = Intent(this, SettingsActivity::class.java)
startActivity(settingsIntent)
true
}
R.id.action_about -> {
val aboutIntent = Intent(this, AboutActivity::class.java)
startActivity(aboutIntent)
true
}
R.id.action_help -> {
val helpIntent = Intent(this, HelpActivity::class.java)
startActivity(helpIntent)
true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun loadIntentData() {
// If activity is running in PiP mode, then start it in front.
if (PictureInPictureCompat.isInPictureInPictureMode(this)) {
val nIntent = Intent(this, MainActivity::class.java)
nIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(nIntent)
}
if (intent?.getBooleanExtra(IntentData.openAudioPlayer, false) == true) {
NavigationHelper.startAudioPlayer(this)
return
}
intent?.getStringExtra(IntentData.channelId)?.let {
navController.navigate(NavDirections.openChannel(channelId = it))
}
intent?.getStringExtra(IntentData.channelName)?.let {
navController.navigate(NavDirections.openChannel(channelName = it))
}
intent?.getStringExtra(IntentData.playlistId)?.let {
navController.navigate(NavDirections.openPlaylist(playlistId = it))
}
intent?.getStringExtra(IntentData.videoId)?.let {
NavigationHelper.navigateVideo(
context = this,
videoUrlOrId = it,
timestamp = intent.getLongExtra(IntentData.timeStamp, 0L)
)
}
intent?.getStringExtra(IntentData.query)?.let {
savedSearchQuery = it
}
intent?.getStringExtra("fragmentToOpen")?.let {
if (it != "downloads") { // Not a shortcut
ShortcutManagerCompat.reportShortcutUsed(this, it)
}
when (it) {
"home" -> navController.navigate(R.id.homeFragment)
"trends" -> navController.navigate(R.id.trendsFragment)
"subscriptions" -> navController.navigate(R.id.subscriptionsFragment)
"library" -> navController.navigate(R.id.libraryFragment)
"downloads" -> navController.navigate(R.id.downloadsFragment)
}
}
if (intent?.getBooleanExtra(IntentData.downloading, false) == true) {
(supportFragmentManager.fragments.find { it is NavHostFragment })
?.childFragmentManager?.fragments?.forEach { fragment ->
(fragment as? DownloadsFragment)?.bindDownloadService()
}
}
}
private fun minimizePlayer() {
binding.mainMotionLayout.transitionToEnd()
supportFragmentManager.fragments.forEach { fragment ->
(fragment as? PlayerFragment)?.binding?.apply {
mainContainer.isClickable = false
linLayout.isVisible = true
playerMotionLayout.setTransitionDuration(250)
playerMotionLayout.transitionToEnd()
playerMotionLayout.getConstraintSet(R.id.start).constrainHeight(R.id.player, 0)
playerMotionLayout.enableTransition(R.id.yt_transition, true)
}
(fragment as? AudioPlayerFragment)?.binding?.apply {
audioPlayerContainer.isClickable = false
playerMotionLayout.transitionToEnd()
}
}
playerViewModel.isFullscreen.value = false
requestOrientationChange()
}
@SuppressLint("SwitchIntDef")
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
when (newConfig.orientation) {
Configuration.ORIENTATION_PORTRAIT -> WindowHelper.toggleFullscreen(window, false)
Configuration.ORIENTATION_LANDSCAPE -> WindowHelper.toggleFullscreen(window, true)
}
}
private fun navigateToBottomSelectedItem(item: MenuItem) {
if (item.itemId == R.id.subscriptionsFragment) {
binding.bottomNav.removeBadge(R.id.subscriptionsFragment)
}
// navigate to the selected fragment, if the fragment already
// exists in backstack then pop up to that entry
if (!navController.popBackStack(item.itemId, false)) {
navController.navigate(item.itemId)
}
// Remove focus from search view when navigating to bottom view.
// Call only after navigate to destination, so it can be used in
// onMenuItemActionCollapse for backstack management
removeSearchFocus()
}
override fun onUserLeaveHint() {
super.onUserLeaveHint()
supportFragmentManager.fragments.forEach { fragment ->
(fragment as? PlayerFragment)?.onUserLeaveHint()
}
}
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent
loadIntentData()
}
}