Skip to content

Commit

Permalink
Fix crashing of app when returning to app from long a period of time
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Feb 28, 2024
1 parent 6219dc2 commit 9ec5f21
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
19 changes: 10 additions & 9 deletions app/src/main/java/dev/brahmkshatriya/echo/player/Global.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import kotlinx.coroutines.launch
import java.util.Collections

object Global {
val queue = mutableListOf<Pair<String, Track>>()
fun getTrack(mediaId: String?) = queue.find { it.first == mediaId }?.second
private val _queue = mutableListOf<Pair<String, Track>>()
val queue get() = _queue.toList()
fun getTrack(mediaId: String?) = _queue.find { it.first == mediaId }?.second

private val _clearQueue = MutableSharedFlow<Unit>()
val clearQueueFlow = _clearQueue.asSharedFlow()
fun clearQueue(scope: CoroutineScope) {
queue.clear()
_queue.clear()
scope.launch {
_clearQueue.emit(Unit)
}
Expand All @@ -25,23 +26,23 @@ object Global {
private val _removeTrack = MutableSharedFlow<Int>()
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)
}
}

private val _addTrack = MutableSharedFlow<Pair<Int, MediaItem>>()
val addTrackFlow = _addTrack.asSharedFlow()
fun addTrack(
scope: CoroutineScope, track: Track, stream: StreamableAudio, positionOffset: Int = 0
): Pair<Int,MediaItem> {
): Pair<Int, MediaItem> {
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)
}
Expand All @@ -51,7 +52,7 @@ object Global {
private val _moveTrack = MutableSharedFlow<Pair<Int, Int>>()
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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -43,7 +42,6 @@ import kotlin.math.max
import kotlin.math.min


@SuppressLint("NotifyDataSetChanged")
fun createPlayerUI(
activity: MainActivity
) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import dev.brahmkshatriya.echo.utils.loadInto

class PlaylistAdapter(
val callback: Callback,
val list: MutableList<Pair<String, Track>> = Global.queue,
var list: List<Pair<String, Track>> = Global.queue,
) : RecyclerView.Adapter<PlaylistAdapter.ViewHolder>() {

open class Callback {
Expand Down Expand Up @@ -56,11 +56,27 @@ class PlaylistAdapter(
binding.playlistCurrentItem.isVisible = position == currentPosition
}

private fun submitList(list: List<Pair<String, Track>>) {
this.list = list
}

private var currentPosition: Int? = null
fun setCurrent(position: Int?) {
fun setCurrent(list: List<Pair<String, Track>>, position: Int?) {
submitList(list)
val old = currentPosition
currentPosition = position
old?.let { notifyItemChanged(it) }
currentPosition?.let { notifyItemChanged(it) }
}

fun addItem(queue: List<Pair<String, Track>>, index: Int) {
submitList(queue)
notifyItemInserted(index)
}

@SuppressLint("NotifyDataSetChanged")
fun removeItems(queue: List<Pair<String, Track>>) {
submitList(queue)
notifyDataSetChanged()
}
}
11 changes: 7 additions & 4 deletions app/src/main/res/layout-land/bottom_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
Expand All @@ -98,10 +100,11 @@

<TextView
android:id="@+id/expandedTrackAuthor"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="12dp"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:textSize="16sp"
tools:text="Author" />
Expand All @@ -112,7 +115,7 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
tools:text="00:00" />
</FrameLayout>
</LinearLayout>

<FrameLayout
android:layout_width="wrap_content"
Expand Down
17 changes: 11 additions & 6 deletions app/src/main/res/layout/bottom_player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,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" />

<FrameLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
Expand All @@ -114,10 +116,11 @@

<TextView
android:id="@+id/expandedTrackAuthor"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="12dp"
android:textAlignment="center"
android:textColor="?attr/colorPrimary"
android:textSize="16sp"
tools:text="Author" />
Expand All @@ -128,7 +131,7 @@
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
tools:text="00:00" />
</FrameLayout>
</LinearLayout>

<FrameLayout
android:layout_width="wrap_content"
Expand All @@ -144,7 +147,9 @@
android:progress="40"
app:indicatorColor="?attr/colorTertiaryContainer"
app:indicatorTrackGapSize="0dp"
app:trackColor="?attr/colorSurfaceContainerHigh" />
app:trackColor="?attr/colorSurfaceContainerHigh"
app:trackCornerRadius="4dp"
app:trackThickness="8dp" />

<com.google.android.material.slider.Slider
android:id="@+id/expandedSeekBar"
Expand All @@ -160,7 +165,7 @@
app:thumbWidth="16dp"
app:trackColorActive="?attr/colorTertiary"
app:trackColorInactive="@android:color/transparent"
app:trackHeight="4dp" />
app:trackHeight="8dp" />

</FrameLayout>

Expand Down
20 changes: 15 additions & 5 deletions app/src/main/res/layout/bottom_playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,30 @@
android:paddingBottom="8dp"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="24dp"
android:paddingStart="8dp"
android:paddingEnd="8dp">

<com.google.android.material.button.MaterialButton
android:id="@+id/playlistTitleIcon"
style="?attr/materialIconButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/playlist"
android:padding="8dp"
app:icon="@drawable/ic_queue_music"
app:iconSize="24dp"
app:iconTint="@color/button_player"
app:strokeWidth="0dp" />

<TextView
android:id="@+id/playlistTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:layout_weight="1"
android:drawablePadding="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="@string/up_next"
android:textSize="18sp"
app:drawableStartCompat="@drawable/ic_queue_music"
app:drawableTint="@color/button_player" />

<com.google.android.material.button.MaterialButton
Expand Down

0 comments on commit 9ec5f21

Please sign in to comment.