Skip to content

Commit

Permalink
增加收藏歌曲到歌单实现(But,没有收藏歌曲接口😞)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangchenyan committed Mar 20, 2024
1 parent de5323d commit 84c087b
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package me.wcy.music.common.dialog.songmenu.items

import android.view.View
import androidx.fragment.app.FragmentActivity
import kotlinx.coroutines.CoroutineScope
import me.wcy.music.common.bean.SongData
import me.wcy.music.common.dialog.songmenu.MenuItem
import top.wangchenyan.common.ext.toast
import me.wcy.music.mine.collect.CollectSongFragment
import top.wangchenyan.common.ext.findActivity

/**
* Created by wangchenyan.top on 2023/10/11.
Expand All @@ -17,6 +19,10 @@ class CollectMenuItem(
get() = "收藏到歌单"

override fun onClick(view: View) {
toast("敬请期待")
val activity = view.context.findActivity()
if (activity is FragmentActivity) {
CollectSongFragment.newInstance(songData.id)
.show(activity.supportFragmentManager, CollectSongFragment.TAG)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package me.wcy.music.discover.playlist.square.item
import androidx.core.view.isVisible
import com.blankj.utilcode.util.SizeUtils
import me.wcy.music.common.bean.PlaylistData
import me.wcy.music.databinding.ItemPlaylistBinding
import me.wcy.music.databinding.ItemDiscoverPlaylistBinding
import me.wcy.music.utils.ConvertUtils
import me.wcy.music.utils.ImageUtils.loadCover
import me.wcy.radapter3.RItemBinder
Expand All @@ -15,10 +15,10 @@ class PlaylistItemBinder(
private val itemWidth: Int,
private val showPlayButton: Boolean,
private val listener: OnItemClickListener
) : RItemBinder<ItemPlaylistBinding, PlaylistData>() {
) : RItemBinder<ItemDiscoverPlaylistBinding, PlaylistData>() {

override fun onBind(
viewBinding: ItemPlaylistBinding,
viewBinding: ItemDiscoverPlaylistBinding,
item: PlaylistData,
position: Int
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import top.wangchenyan.common.ext.getColorEx
import top.wangchenyan.common.ext.showConfirmDialog
import top.wangchenyan.common.ext.viewBindings
import top.wangchenyan.common.widget.CustomSpan.appendStyle
import me.wcy.music.R
import me.wcy.music.databinding.FragmentCurrentPlaylistBinding
import me.wcy.music.main.playing.PlayingActivity
import me.wcy.music.service.AudioPlayer
import me.wcy.music.service.PlayMode
import me.wcy.music.storage.db.entity.SongEntity
import me.wcy.radapter3.RAdapter
import top.wangchenyan.common.ext.getColorEx
import top.wangchenyan.common.ext.showConfirmDialog
import top.wangchenyan.common.ext.viewBindings
import top.wangchenyan.common.widget.CustomSpan.appendStyle
import javax.inject.Inject

/**
Expand Down Expand Up @@ -59,9 +59,6 @@ class CurrentPlaylistFragment : BottomSheetDialogFragment() {
}

private fun initView() {
viewBinding.root.setOnClickListener {
dismissAllowingStateLoss()
}
viewBinding.llPlayMode.setOnClickListener {
switchPlayMode()
}
Expand Down
104 changes: 104 additions & 0 deletions app/src/main/java/me/wcy/music/mine/collect/CollectSongFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package me.wcy.music.mine.collect

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.SizeUtils
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import me.wcy.music.R
import me.wcy.music.common.bean.PlaylistData
import me.wcy.music.databinding.FragmentCollectSongBinding
import me.wcy.music.mine.playlist.UserPlaylistItemBinder
import me.wcy.radapter3.RAdapter
import top.wangchenyan.common.ext.toast
import top.wangchenyan.common.ext.viewBindings
import top.wangchenyan.common.widget.decoration.SpacingDecoration

/**
* Created by wangchenyan.top on 2024/3/20.
*/
@AndroidEntryPoint
class CollectSongFragment : BottomSheetDialogFragment() {
private val viewBinding by viewBindings<FragmentCollectSongBinding>()
private val viewModel: CollectSongViewModel by viewModels()
private val adapter by lazy { RAdapter<PlaylistData>() }

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(STYLE_NORMAL, R.style.BottomSheetDialogTheme)
}

override fun getTheme(): Int {
return R.style.BottomSheetDialogTheme
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return viewBinding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val songId = arguments?.getLong("song_id") ?: 0
if (songId <= 0) {
toast("参数错误")
dismissAllowingStateLoss()
return
}

viewModel.songId = songId

initView()
initData()
lifecycleScope.launch {
viewModel.getMyPlayList()
}
}

private fun initView() {
adapter.register(
UserPlaylistItemBinder(
true,
object : UserPlaylistItemBinder.OnItemClickListener {
override fun onItemClick(item: PlaylistData) {
toast("接口走丢了…")
dismissAllowingStateLoss()
}

override fun onMoreClick(item: PlaylistData) {
}
})
)
val spacingDecoration = SpacingDecoration(SizeUtils.dp2px(10f))
viewBinding.recyclerView.addItemDecoration(spacingDecoration)
viewBinding.recyclerView.adapter = adapter
}

private fun initData() {
lifecycleScope.launch {
viewModel.myPlaylists.collectLatest {
adapter.refresh(it)
}
}
}

companion object {
const val TAG = "CollectSongFragment"

fun newInstance(songId: Long): CollectSongFragment {
return CollectSongFragment().apply {
arguments = bundleOf("song_id" to songId)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.wcy.music.mine.collect

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import me.wcy.music.account.service.UserService
import me.wcy.music.common.bean.PlaylistData
import me.wcy.music.mine.MineApi
import top.wangchenyan.common.model.CommonResult
import javax.inject.Inject

/**
* Created by wangchenyan.top on 2024/3/20.
*/
@HiltViewModel
class CollectSongViewModel @Inject constructor() : ViewModel() {
private val _myPlaylists = MutableStateFlow<List<PlaylistData>>(emptyList())
val myPlaylists = _myPlaylists

var songId: Long = 0

@Inject
lateinit var userService: UserService

suspend fun getMyPlayList(): CommonResult<List<PlaylistData>> {
val uid = userService.profile.value?.userId ?: 0
val res = kotlin.runCatching {
MineApi.get().getUserPlaylist(uid)
}
val playlistData = res.getOrNull()
return if (playlistData?.code == 200) {
val list = playlistData.playlists.filter { it.userId == uid }
_myPlaylists.value = list
CommonResult.success(list)
} else {
CommonResult.fail(playlistData?.code ?: -1)
}
}
}
37 changes: 37 additions & 0 deletions app/src/main/res/layout/fragment_collect_song.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<com.hjq.shape.layout.ShapeLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:minHeight="300dp"
android:orientation="vertical"
app:shape_solidColor="@color/card_bg"
app:shape_topLeftRadius="16dp"
app:shape_topRightRadius="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="16dp"
android:paddingVertical="16dp"
android:text="收藏到歌单"
android:textColor="@color/common_text_h1_color"
android:textSize="18dp"
android:textStyle="bold" />

<View
android:layout_width="match_parent"
android:layout_height="@dimen/common_divider_size"
android:layout_marginHorizontal="16dp"
android:background="@color/common_divider" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginVertical="16dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_user_playlist" />
</com.hjq.shape.layout.ShapeLinearLayout>
5 changes: 3 additions & 2 deletions app/src/main/res/layout/fragment_current_playlist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:minHeight="300dp"
android:orientation="vertical"
app:shape_solidColor="@color/card_bg"
app:shape_topLeftRadius="16dp"
Expand Down Expand Up @@ -72,8 +73,8 @@
<View
android:layout_width="match_parent"
android:layout_height="@dimen/common_divider_size"
android:background="@color/common_divider"
android:paddingHorizontal="16dp" />
android:layout_marginHorizontal="16dp"
android:background="@color/common_divider" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_discover.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
android:clipToPadding="false"
android:paddingHorizontal="16dp"
tools:itemCount="1"
tools:listitem="@layout/item_playlist" />
tools:listitem="@layout/item_discover_playlist" />

<TextView
android:id="@+id/tvRankingList"
Expand Down
File renamed without changes.

0 comments on commit 84c087b

Please sign in to comment.