From dc5752b2c6e5231788e3d73fd7d3ab5b41d1e5a7 Mon Sep 17 00:00:00 2001 From: Spark <2666652@gmail.com> Date: Mon, 11 Nov 2024 19:32:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E7=88=B6?= =?UTF-8?q?=E5=AD=90=E7=BB=84=E4=BB=B6=E5=88=87=E6=8D=A2=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/common/download_manager.dart | 12 +++++++++ lib/view/home_view.dart | 8 ++++++ lib/view/music_view.dart | 39 +++++++++++++++++++++-------- lib/view/user/my_download_view.dart | 30 ++++++++++++++++------ 4 files changed, 71 insertions(+), 18 deletions(-) diff --git a/lib/common/download_manager.dart b/lib/common/download_manager.dart index 18cec7a..1394b4c 100644 --- a/lib/common/download_manager.dart +++ b/lib/common/download_manager.dart @@ -185,6 +185,18 @@ class DownloadManager extends GetxController { await _saveDownloadsToPrefs(); } + bool updateSongInfo(int id, bool isCollected, bool isLiked) { + final downloadItem = _downloads[id.toString()]; + if (downloadItem != null) { + downloadItem.song.collection = isCollected; + downloadItem.song.likes = isLiked; + _downloads[id.toString()] = downloadItem; + _saveDownloadsToPrefs(); + return true; + } + return false; + } + String _getFileExtension(String url) { // Remove query parameters final urlWithoutQuery = url.split('?').first; diff --git a/lib/view/home_view.dart b/lib/view/home_view.dart index 84b6d3e..cf0db6e 100644 --- a/lib/view/home_view.dart +++ b/lib/view/home_view.dart @@ -288,6 +288,14 @@ class _HomeViewState extends State { builder: (context) => MusicView( songList: songs, initialSongIndex: index, + onSongStatusChanged: (index, isCollected, isLiked) { + setState(() { + // 更新父组件中的数据 + songs[index].collection = isCollected; + songs[index].likes = isLiked; + downloadManager.updateSongInfo(songs[index].id, isCollected, isLiked); + }); + }, ), ), ); diff --git a/lib/view/music_view.dart b/lib/view/music_view.dart index 84cd0ab..9916679 100644 --- a/lib/view/music_view.dart +++ b/lib/view/music_view.dart @@ -3,21 +3,23 @@ import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:just_audio/just_audio.dart'; import 'package:music_player_miao/common_widget/app_data.dart'; -import 'package:music_player_miao/models/universal_bean.dart'; import '../../view_model/home_view_model.dart'; import '../api/api_music_likes.dart'; import '../api/api_collection.dart'; import '../common/download_manager.dart'; import '../common_widget/Song_widegt.dart'; +import '../models/universal_bean.dart'; class MusicView extends StatefulWidget { final List songList; final int initialSongIndex; + final Function(int index, bool isCollected, bool isLiked)? onSongStatusChanged; const MusicView({ super.key, required this.songList, - required this.initialSongIndex + required this.initialSongIndex, + this.onSongStatusChanged, }); @override @@ -379,14 +381,23 @@ class _MusicViewState extends State { Row( children: [ IconButton( - // TODO 该处存在 BUG,影响功能:点赞 - // 原因:同收藏功能 onPressed: () async{ setState(() { likesnot = !likesnot; likes[currentSongIndex] = !likes[currentSongIndex]; }); - await LikesApiMusic().likesMusic(musicId: id[currentSongIndex], Authorization: AppData().currentToken); + + UniversalBean response = await LikesApiMusic().likesMusic(musicId: id[currentSongIndex], Authorization: AppData().currentToken); + if (response.code != 200) { + likesnot = !likesnot; + likes[currentSongIndex] = !likes[currentSongIndex]; + } + + widget.onSongStatusChanged?.call( + currentSongIndex, + collection[currentSongIndex], // 传递当前的收藏状态 + likes[currentSongIndex] + ); }, icon: Image.asset( likesnot @@ -397,17 +408,23 @@ class _MusicViewState extends State { ), ), IconButton( - // TODO 该处存在 BUG,影响功能:收藏 - // 原因:这个视图接收的数据从父组件传递过来 - // 在这更新数据不会影响父组件的数据,所以在父组件中的数据不会改变 - // 所以从父组件进入该组件,显示的点赞和收藏数据均是父组件初始化的时候从服务器获取的,而不是最新的 - // 同理,点赞逻辑也存在这个 BUG onPressed: () async{ setState(() { collectionsnot = !collectionsnot; collection[currentSongIndex] = !collection[currentSongIndex]; }); - await CollectionApiMusic().addCollection(musicId: id[currentSongIndex], Authorization: AppData().currentToken); + + UniversalBean response = await CollectionApiMusic().addCollection(musicId: id[currentSongIndex], Authorization: AppData().currentToken); + if (response.code != 200) { + collectionsnot = !collectionsnot; + collection[currentSongIndex] = !collection[currentSongIndex]; + } + + widget.onSongStatusChanged?.call( + currentSongIndex, + collection[currentSongIndex], + likes[currentSongIndex] // 传递当前的点赞状态 + ); }, icon: Image.asset( collectionsnot diff --git a/lib/view/user/my_download_view.dart b/lib/view/user/my_download_view.dart index 6eb6dc9..78451b5 100644 --- a/lib/view/user/my_download_view.dart +++ b/lib/view/user/my_download_view.dart @@ -38,6 +38,7 @@ class _MyDownloadViewState extends State { void _getSongs() async { setState(() { _songs = downloadManager.getLocalSongs(); + _selectedItems = List.generate(_songs.length, (index) => false); }); } @@ -238,6 +239,14 @@ class _MyDownloadViewState extends State { builder: (context) => MusicView( songList: _songs, initialSongIndex: 0, + onSongStatusChanged: (index, isCollected, isLiked) { + setState(() { + // 更新父组件中的数据 + _songs[index].collection = isCollected; + _songs[index].likes = isLiked; + downloadManager.updateSongInfo(_songs[index].id, isCollected, isLiked); + }); + }, ), ), ); @@ -311,13 +320,20 @@ class _MyDownloadViewState extends State { } : () { // 非选择模式下跳转到播放页面 - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => MusicView( - songList: _songs, - initialSongIndex: index, // 使用当前点击的歌曲索引 - ), + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => MusicView( + songList: _songs, + initialSongIndex: index, + onSongStatusChanged: (index, isCollected, isLiked) { + setState(() { + // 更新父组件中的数据 + _songs[index].collection = isCollected; + _songs[index].likes = isLiked; + }); + }, + ), ), ); },