Skip to content

Commit

Permalink
播放按钮支持loading状态
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchenyan committed Apr 3, 2024
1 parent eb2ae6d commit c4eb106
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 31 deletions.
45 changes: 29 additions & 16 deletions app/src/main/java/me/wcy/music/main/playing/PlayingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import me.wcy.music.discover.DiscoverApi
import me.wcy.music.ext.registerReceiverCompat
import me.wcy.music.main.playlist.CurrentPlaylistFragment
import me.wcy.music.service.PlayMode
import me.wcy.music.service.PlayState
import me.wcy.music.service.PlayerController
import me.wcy.music.service.likesong.LikeSongProcessor
import me.wcy.music.storage.LrcCache
Expand Down Expand Up @@ -180,7 +181,7 @@ class PlayingActivity : BaseMusicActivity() {
viewBinding.ivMode.setOnClickListener {
switchPlayMode()
}
viewBinding.ivPlay.setOnClickListener {
viewBinding.flPlay.setOnClickListener {
playerController.playPause()
}
viewBinding.ivPrev.setOnClickListener {
Expand Down Expand Up @@ -253,14 +254,7 @@ class PlayingActivity : BaseMusicActivity() {
updateCover(song)
updateLrc(song)
viewBinding.albumCoverView.reset()
val playState = playerController.playState.value
if (playState.isPlaying || playState.isPreparing) {
viewBinding.ivPlay.isSelected = true
viewBinding.albumCoverView.start()
} else {
viewBinding.ivPlay.isSelected = false
viewBinding.albumCoverView.pause()
}
updatePlayState(playerController.playState.value)
updateOnlineActionsState(song)
} else {
finish()
Expand All @@ -269,13 +263,7 @@ class PlayingActivity : BaseMusicActivity() {

lifecycleScope.launch {
playerController.playState.collectLatest { playState ->
if (playState.isPlaying) {
viewBinding.ivPlay.isSelected = true
viewBinding.albumCoverView.start()
} else {
viewBinding.ivPlay.isSelected = false
viewBinding.albumCoverView.pause()
}
updatePlayState(playState)
}
}

Expand Down Expand Up @@ -366,6 +354,31 @@ class PlayingActivity : BaseMusicActivity() {
playerController.setPlayMode(mode)
}

private fun updatePlayState(playState: PlayState) {
when (playState) {
PlayState.Preparing -> {
viewBinding.flPlay.isEnabled = false
viewBinding.ivPlay.isSelected = false
viewBinding.loadingProgress.isVisible = true
viewBinding.albumCoverView.pause()
}

PlayState.Playing -> {
viewBinding.flPlay.isEnabled = true
viewBinding.ivPlay.isSelected = true
viewBinding.loadingProgress.isVisible = false
viewBinding.albumCoverView.start()
}

else -> {
viewBinding.flPlay.isEnabled = true
viewBinding.ivPlay.isSelected = false
viewBinding.loadingProgress.isVisible = false
viewBinding.albumCoverView.pause()
}
}
}

private fun updateOnlineActionsState(song: MediaItem) {
viewBinding.llActions.isVisible = song.isLocal().not()
viewBinding.ivLike.isSelected = likeSongProcessor.isLiked(song.getSongId())
Expand Down
27 changes: 23 additions & 4 deletions app/src/main/java/me/wcy/music/widget/PlayBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import me.wcy.music.consts.RoutePath
import me.wcy.music.databinding.LayoutPlayBarBinding
import me.wcy.music.main.playlist.CurrentPlaylistFragment
import me.wcy.music.service.PlayServiceModule.playerController
import me.wcy.music.service.PlayState
import me.wcy.music.utils.getDuration
import me.wcy.music.utils.getSmallCover
import me.wcy.router.CRouter
Expand Down Expand Up @@ -62,7 +63,7 @@ class PlayBar @JvmOverloads constructor(
viewBinding.root.setOnClickListener {
CRouter.with(context).url(RoutePath.PLAYING).start()
}
viewBinding.ivPlay.setOnClickListener {
viewBinding.flPlay.setOnClickListener {
playerController.playPause()
}
viewBinding.ivNext.setOnClickListener {
Expand Down Expand Up @@ -98,9 +99,27 @@ class PlayBar @JvmOverloads constructor(

lifecycleOwner.lifecycleScope.launch {
playerController.playState.collectLatest { playState ->
val isPlaying = playState.isPreparing || playState.isPlaying
viewBinding.ivPlay.isSelected = isPlaying
if (isPlaying) {
when (playState) {
PlayState.Preparing -> {
viewBinding.flPlay.isEnabled = false
viewBinding.ivPlay.isSelected = false
viewBinding.loadingProgress.isVisible = true
}

PlayState.Playing -> {
viewBinding.flPlay.isEnabled = true
viewBinding.ivPlay.isSelected = true
viewBinding.loadingProgress.isVisible = false
}

else -> {
viewBinding.flPlay.isEnabled = true
viewBinding.ivPlay.isSelected = false
viewBinding.loadingProgress.isVisible = false
}
}

if (playState.isPlaying) {
if (rotateAnimator.isPaused) {
rotateAnimator.resume()
} else if (rotateAnimator.isStarted.not()) {
Expand Down
31 changes: 21 additions & 10 deletions app/src/main/res/layout/activity_playing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@
android:id="@+id/ivMode"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingVertical="8dp"
android:src="@drawable/ic_play_mode_level_list"
Expand All @@ -281,26 +280,39 @@
android:id="@+id/ivPrev"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingVertical="16dp"
android:src="@drawable/ic_previous"
app:tint="@color/translucent_white_p80" />

<ImageView
android:id="@+id/ivPlay"
<FrameLayout
android:id="@+id/flPlay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/ic_playing_play_pause_selector"
app:tint="@color/translucent_white_p80" />
android:layout_weight="1">

<ImageView
android:id="@+id/ivPlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_playing_play_pause_selector"
app:tint="@color/translucent_white_p80" />

<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/loadingProgress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:indeterminate="true"
app:indicatorColor="@color/common_theme_color"
app:indicatorSize="48dp"
app:trackThickness="2dp" />
</FrameLayout>

<ImageView
android:id="@+id/ivNext"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingVertical="16dp"
android:src="@drawable/ic_next"
Expand All @@ -310,7 +322,6 @@
android:id="@+id/ivPlaylist"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingVertical="16dp"
android:src="@drawable/ic_playlist"
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/layout/layout_play_bar.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</LinearLayout>

<FrameLayout
android:id="@+id/ivPlay"
android:id="@+id/flPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

Expand All @@ -59,12 +59,23 @@
app:trackThickness="2dp" />

<ImageView
android:id="@+id/ivPlay"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_gravity="center"
android:contentDescription="@null"
android:src="@drawable/ic_play_bar_play_pause_selector"
app:tint="@color/common_text_h1_color" />

<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/loadingProgress"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="center"
android:indeterminate="true"
app:indicatorColor="@color/common_theme_color"
app:indicatorSize="22dp"
app:trackThickness="1.5dp" />
</FrameLayout>

<ImageView
Expand Down

0 comments on commit c4eb106

Please sign in to comment.