forked from wangchenyan/ponymusic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de5323d
commit 84c087b
Showing
9 changed files
with
199 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
app/src/main/java/me/wcy/music/mine/collect/CollectSongFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/me/wcy/music/mine/collect/CollectSongViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.