Skip to content

Commit

Permalink
Add visual indicator when opening song's album (#297)
Browse files Browse the repository at this point in the history
* re-add modifications

* add hiighlight when other album is displayed

* refactoring

* remove useless line

* refactoring
  • Loading branch information
Drhaal authored May 19, 2021
1 parent a30f411 commit 5718b33
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.afollestad.recyclical.datasource.dataSourceOf
import com.afollestad.recyclical.datasource.dataSourceTypedOf
import com.afollestad.recyclical.setup
import com.afollestad.recyclical.withItem
import com.google.android.material.card.MaterialCardView
Expand Down Expand Up @@ -58,8 +58,8 @@ class DetailsFragment : Fragment(R.layout.fragment_details), SearchView.OnQueryT
private lateinit var mArtistDetailsAnimator: Animator
private lateinit var mAlbumsRecyclerViewLayoutManager: LinearLayoutManager

private val mSelectedAlbumsDataSource = dataSourceOf()
private val mSongsDataSource = dataSourceOf()
private val mSelectedAlbumsDataSource = dataSourceTypedOf<Album>()
private val mSongsDataSource = dataSourceTypedOf<Music>()

private var mLaunchedBy = GoConstants.ARTIST_VIEW

Expand Down Expand Up @@ -426,15 +426,59 @@ class DetailsFragment : Fragment(R.layout.fragment_details), SearchView.OnQueryT
}).attachToRecyclerView(rv)
}

if (goPreferences.isAnimations) {
view.afterMeasured {
view.afterMeasured {
if (goPreferences.isAnimations) {
_detailsFragmentBinding?.root?.run {
mArtistDetailsAnimator = createCircularReveal(
isErrorFragment = false,
show = true
)
}
}
highlightSong(arguments?.getLong(TAG_HIGHLIGHTED_SONG_ID))
}
}

fun highlightSong(songId: Long?) {
if(songId == null) return

val selectedPos = mSongsDataSource.indexOfFirst {
it.id == songId
}

if (selectedPos > -1) {
var songView: View? = _detailsFragmentBinding?.songsRv?.layoutManager?.findViewByPosition(selectedPos)
if (songView == null) {
//Wait for song to appear on screen
_detailsFragmentBinding?.songsRv?.addOnScrollListener(object :
RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
songView = _detailsFragmentBinding?.songsRv?.layoutManager?.findViewByPosition(selectedPos)
animateHighlightedSong(songView)
_detailsFragmentBinding?.songsRv?.clearOnScrollListeners()
}
}
})
_detailsFragmentBinding?.songsRv?.smoothScrollToPosition(selectedPos)
} else {
animateHighlightedSong(songView)
}
}
}

private fun animateHighlightedSong(songView: View?){
songView?.let {
val unpressRunnable = java.lang.Runnable {
songView.isPressed = false
}

val pressRunnable = java.lang.Runnable {
songView.isPressed = true
songView.postOnAnimationDelayed(unpressRunnable, 1000)
}
pressRunnable.run()
}
}

Expand Down Expand Up @@ -733,8 +777,17 @@ class DetailsFragment : Fragment(R.layout.fragment_details), SearchView.OnQueryT
return sOpenNewDetailsFragment
}

fun tryToSnapToAlbumPosition(snapPosition: Int) {
fun tryToSnapToAlbumPosition(snapPosition: Int, highlightedSongId: Long?) {
sPlayFirstSong = false
highlightedSongId?.let {
_detailsFragmentBinding?.songsRv?.addOnLayoutChangeListener(object: View.OnLayoutChangeListener {
override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
highlightSong(highlightedSongId)
_detailsFragmentBinding?.songsRv?.removeOnLayoutChangeListener(this)
}
})
}

if (sLaunchedByArtistView && snapPosition != -1) {
_detailsFragmentBinding?.albumsRv?.smoothSnapToPosition(
snapPosition
Expand Down Expand Up @@ -786,6 +839,7 @@ class DetailsFragment : Fragment(R.layout.fragment_details), SearchView.OnQueryT
}
setSongsDataSource(songs, true)
_detailsFragmentBinding?.songsRv?.scrollToPosition(0)

}

companion object {
Expand All @@ -795,6 +849,7 @@ class DetailsFragment : Fragment(R.layout.fragment_details), SearchView.OnQueryT
private const val TAG_SELECTED_ALBUM_POSITION = "SELECTED_ALBUM_POSITION"
private const val TAG_IS_SHUFFLING = "IS_SHUFFLING"
private const val TAG_SHUFFLED_ALBUM = "SHUFFLED_ALBUM"
private const val TAG_HIGHLIGHTED_SONG_ID = "HIGHLIGHTED_SONG_ID"

/**
* Use this factory method to create a new instance of
Expand All @@ -808,14 +863,16 @@ class DetailsFragment : Fragment(R.layout.fragment_details), SearchView.OnQueryT
launchedBy: String,
playedAlbumPosition: Int,
isShuffleMode: Pair<Boolean, String?>,
highlightedSongId: Long?
) =
DetailsFragment().apply {
arguments = bundleOf(
TAG_ARTIST_FOLDER to selectedArtistOrFolder,
TAG_IS_FOLDER to launchedBy,
TAG_SELECTED_ALBUM_POSITION to playedAlbumPosition,
TAG_IS_SHUFFLING to isShuffleMode.first,
TAG_SHUFFLED_ALBUM to isShuffleMode.second
TAG_SHUFFLED_ALBUM to isShuffleMode.second,
TAG_HIGHLIGHTED_SONG_ID to highlightedSongId
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ class MainActivity : AppCompatActivity(), UIControlInterface, MediaControlInterf
private fun openDetailsFragment(
selectedArtistOrFolder: String?,
launchedBy: String,
isShuffleMode: Pair<Boolean, String?>
isShuffleMode: Pair<Boolean, String?>,
highlightedSongId: Long?
) {
if (!sDetailsFragmentExpanded) {
mDetailsFragment =
Expand All @@ -455,7 +456,8 @@ class MainActivity : AppCompatActivity(), UIControlInterface, MediaControlInterf
mMediaPlayerHolder,
mMusicViewModel.deviceAlbumsByArtist
),
isShuffleMode
isShuffleMode,
highlightedSongId
)
sCloseDetailsFragment = true
if (sAllowCommit) {
Expand Down Expand Up @@ -1154,17 +1156,20 @@ class MainActivity : AppCompatActivity(), UIControlInterface, MediaControlInterf
selectedArtistOrFolder,
mMediaPlayerHolder,
mMusicViewModel.deviceAlbumsByArtist,
)
),
mMediaPlayerHolder.currentSong.first?.id
)
}
mDetailsFragment?.highlightSong(mMediaPlayerHolder.currentSong.first?.id)
} else {
openDetailsFragment(
selectedArtistOrFolder,
mMediaPlayerHolder.launchedBy,
Pair(
checkIsPlayer(false) && mMediaPlayerHolder.isShuffledSongsQueued.first && mMediaPlayerHolder.currentSong.first?.artist == selectedArtistOrFolder && mMediaPlayerHolder.launchedBy != GoConstants.ALBUM_VIEW && mMediaPlayerHolder.launchedBy != GoConstants.FOLDER_VIEW,
mMediaPlayerHolder.isShuffledSongsQueued.second
)
),
mMediaPlayerHolder.currentSong.first?.id
)
}

Expand Down Expand Up @@ -1216,7 +1221,8 @@ class MainActivity : AppCompatActivity(), UIControlInterface, MediaControlInterf
Pair(
checkIsPlayer(false) && mMediaPlayerHolder.isShuffledSongsQueued.first && mMediaPlayerHolder.currentSong.first?.artist == this && mMediaPlayerHolder.launchedBy != GoConstants.ALBUM_VIEW && mMediaPlayerHolder.launchedBy != GoConstants.FOLDER_VIEW,
mMediaPlayerHolder.isShuffledSongsQueued.second
)
),
null
)
}
}
Expand All @@ -1228,7 +1234,8 @@ class MainActivity : AppCompatActivity(), UIControlInterface, MediaControlInterf
Pair(
checkIsPlayer(false) && mMediaPlayerHolder.isShuffledSongsQueued.first && mMediaPlayerHolder.currentSong.first?.artist == artistOrFolder && mMediaPlayerHolder.launchedBy != GoConstants.ALBUM_VIEW && mMediaPlayerHolder.launchedBy != GoConstants.FOLDER_VIEW,
mMediaPlayerHolder.isShuffledSongsQueued.second
)
),
null
)
}

Expand Down

0 comments on commit 5718b33

Please sign in to comment.