From 9ec5f213e2717301c542d04423c6ca7545848e4f Mon Sep 17 00:00:00 2001 From: brahmkshatriya <69040506+brahmkshatriya@users.noreply.github.com> Date: Wed, 28 Feb 2024 20:28:54 +0530 Subject: [PATCH] Fix crashing of app when returning to app from long a period of time --- .../dev/brahmkshatriya/echo/player/Global.kt | 19 +++++++++--------- .../echo/player/ui/CreatePlayerUI.kt | 15 +++++++++----- .../echo/ui/adapters/PlaylistAdapter.kt | 20 +++++++++++++++++-- .../main/res/layout-land/bottom_player.xml | 11 ++++++---- app/src/main/res/layout/bottom_player.xml | 17 ++++++++++------ app/src/main/res/layout/bottom_playlist.xml | 20 ++++++++++++++----- 6 files changed, 71 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/dev/brahmkshatriya/echo/player/Global.kt b/app/src/main/java/dev/brahmkshatriya/echo/player/Global.kt index 2a645ae3..c4ed382e 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/player/Global.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/player/Global.kt @@ -10,13 +10,14 @@ import kotlinx.coroutines.launch import java.util.Collections object Global { - val queue = mutableListOf>() - fun getTrack(mediaId: String?) = queue.find { it.first == mediaId }?.second + private val _queue = mutableListOf>() + val queue get() = _queue.toList() + fun getTrack(mediaId: String?) = _queue.find { it.first == mediaId }?.second private val _clearQueue = MutableSharedFlow() val clearQueueFlow = _clearQueue.asSharedFlow() fun clearQueue(scope: CoroutineScope) { - queue.clear() + _queue.clear() scope.launch { _clearQueue.emit(Unit) } @@ -25,10 +26,10 @@ object Global { private val _removeTrack = MutableSharedFlow() val removeTrackFlow = _removeTrack.asSharedFlow() fun removeTrack(scope: CoroutineScope, index: Int) { - queue.removeAt(index) + _queue.removeAt(index) scope.launch { _removeTrack.emit(index) - if (queue.isEmpty()) _clearQueue.emit(Unit) + if (_queue.isEmpty()) _clearQueue.emit(Unit) } } @@ -36,12 +37,12 @@ object Global { val addTrackFlow = _addTrack.asSharedFlow() fun addTrack( scope: CoroutineScope, track: Track, stream: StreamableAudio, positionOffset: Int = 0 - ): Pair { + ): Pair { val item = PlayerHelper.mediaItemBuilder(track, stream) val mediaId = item.mediaId - val index = queue.size - positionOffset + val index = _queue.size - positionOffset - queue.add(index, mediaId to track) + _queue.add(index, mediaId to track) scope.launch { _addTrack.emit(index to item) } @@ -51,7 +52,7 @@ object Global { private val _moveTrack = MutableSharedFlow>() val moveTrackFlow = _moveTrack.asSharedFlow() fun moveTrack(scope: CoroutineScope, fromIndex: Int, toIndex: Int) { - Collections.swap(queue, fromIndex, toIndex) + Collections.swap(_queue, fromIndex, toIndex) scope.launch { _moveTrack.emit(fromIndex to toIndex) } diff --git a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt index c2f3c9d0..688f70f6 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt @@ -1,7 +1,6 @@ package dev.brahmkshatriya.echo.player.ui import android.animation.ObjectAnimator -import android.annotation.SuppressLint import android.graphics.drawable.Animatable import android.view.View import android.view.animation.LinearInterpolator @@ -43,7 +42,6 @@ import kotlin.math.max import kotlin.math.min -@SuppressLint("NotifyDataSetChanged") fun createPlayerUI( activity: MainActivity ) { @@ -85,6 +83,11 @@ fun createPlayerUI( playerBinding.expandedBackground.alpha = slideOffset } }) + playlistBinding.playlistTitleIcon.setOnClickListener { + bottomPlaylistBehavior.apply { + state = if (state == STATE_EXPANDED) STATE_COLLAPSED else STATE_EXPANDED + } + } val collapsedCoverSize = activity.resources.getDimension(R.dimen.collapsed_cover_size).toInt() bottomPlayerBehavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { @@ -367,21 +370,23 @@ fun createPlayerUI( } } observe(uiViewModel.playlist) { - adapter.setCurrent(it) + adapter.setCurrent(Global.queue, it) } observe(Global.addTrackFlow) { (index, _) -> - adapter.notifyItemInserted(index) + adapter.addItem(Global.queue, index) } observe(Global.clearQueueFlow) { + adapter.removeItems(Global.queue) + PlayerBackButtonHelper.playlistState.value = STATE_COLLAPSED + PlayerBackButtonHelper.playerSheetState.value = STATE_HIDDEN container.post { if (bottomPlayerBehavior.state != STATE_HIDDEN) { bottomPlayerBehavior.isHideable = true bottomPlayerBehavior.state = STATE_HIDDEN } } - adapter.notifyDataSetChanged() } } } \ No newline at end of file diff --git a/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt b/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt index 6fdfb066..582e5526 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt @@ -14,7 +14,7 @@ import dev.brahmkshatriya.echo.utils.loadInto class PlaylistAdapter( val callback: Callback, - val list: MutableList> = Global.queue, + var list: List> = Global.queue, ) : RecyclerView.Adapter() { open class Callback { @@ -56,11 +56,27 @@ class PlaylistAdapter( binding.playlistCurrentItem.isVisible = position == currentPosition } + private fun submitList(list: List>) { + this.list = list + } + private var currentPosition: Int? = null - fun setCurrent(position: Int?) { + fun setCurrent(list: List>, position: Int?) { + submitList(list) val old = currentPosition currentPosition = position old?.let { notifyItemChanged(it) } currentPosition?.let { notifyItemChanged(it) } } + + fun addItem(queue: List>, index: Int) { + submitList(queue) + notifyItemInserted(index) + } + + @SuppressLint("NotifyDataSetChanged") + fun removeItems(queue: List>) { + submitList(queue) + notifyDataSetChanged() + } } diff --git a/app/src/main/res/layout-land/bottom_player.xml b/app/src/main/res/layout-land/bottom_player.xml index 68b3a4c0..fb72747c 100644 --- a/app/src/main/res/layout-land/bottom_player.xml +++ b/app/src/main/res/layout-land/bottom_player.xml @@ -77,12 +77,14 @@ android:id="@+id/expandedTrackTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginStart="12dp" + android:layout_marginEnd="12sp" android:textAlignment="center" android:textSize="20sp" android:textStyle="bold" tools:text="Nice Track Title" /> - @@ -112,7 +115,7 @@ android:layout_height="wrap_content" android:layout_gravity="bottom|end" tools:text="00:00" /> - + - @@ -128,7 +131,7 @@ android:layout_height="wrap_content" android:layout_gravity="bottom|end" tools:text="00:00" /> - + + app:trackColor="?attr/colorSurfaceContainerHigh" + app:trackCornerRadius="4dp" + app:trackThickness="8dp" /> + app:trackHeight="8dp" /> diff --git a/app/src/main/res/layout/bottom_playlist.xml b/app/src/main/res/layout/bottom_playlist.xml index 969ea6b5..2aecf822 100644 --- a/app/src/main/res/layout/bottom_playlist.xml +++ b/app/src/main/res/layout/bottom_playlist.xml @@ -13,20 +13,30 @@ android:paddingBottom="8dp" android:gravity="center" android:orientation="horizontal" - android:paddingStart="24dp" + android:paddingStart="8dp" android:paddingEnd="8dp"> + +