Skip to content

Commit

Permalink
最近播放(播放列表上方的下拉列表“更多”对话框)中除了第一项其余都允许移除,移除文件夹和播放列表的逻辑为将最后播放时间时间清空
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Jul 30, 2024
1 parent 63b9cb7 commit 76e8cb1
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 35 deletions.
29 changes: 14 additions & 15 deletions MusicPlayer2/MoreRecentItemDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,8 @@ afx_msg LRESULT CMoreRecentItemDlg::OnListboxSelChanged(WPARAM wParam, LPARAM lP
auto& data_list{ m_searched ? m_search_result : CRecentFolderAndPlaylist::Instance().GetItemList() };
if (index >= 0 && index < static_cast<int>(data_list.size()))
{
const auto& selected_item = data_list[index];
//此界面仅允许删除媒体库项目
if (selected_item.IsMedialib())
delete_enable = true;
select_valid = true;
delete_enable = index > 0; //除了第一个都可以删除
}

EnableDlgCtrl(IDC_DELETE_BUTTON, delete_enable);
Expand All @@ -202,19 +199,21 @@ void CMoreRecentItemDlg::OnBnClickedDeleteButton()
if (sel_index >= 0 && sel_index < static_cast<int>(data_list.size()))
{
const auto& selected_item = data_list[sel_index];
if (selected_item.IsMedialib() && selected_item.medialib_info != nullptr)
CString item_str;
std::wstring type_name;
if (selected_item.IsMedialib())
type_name = CMediaLibPlaylistMgr::GetTypeName(selected_item.medialib_info->medialib_type);
else if (selected_item.IsFolder())
type_name = theApp.m_str_table.LoadText(L"TXT_FOLDER");
else if (selected_item.IsPlaylist())
type_name = theApp.m_str_table.LoadText(L"TXT_PLAYLIST");
item_str.Format(_T("%s: %s"), type_name.c_str(), m_list_ctrl.GetItemText(sel_index).GetString());
std::wstring messagebox_info = theApp.m_str_table.LoadTextFormat(L"MSG_DELETE_RECENTPLAYED_ITEM_INQUIRY", { item_str });
if (MessageBox(messagebox_info.c_str(), nullptr, MB_ICONQUESTION | MB_YESNO) == IDYES)
{
CString item_str;
std::wstring type_name = CMediaLibPlaylistMgr::GetTypeName(selected_item.medialib_info->medialib_type);
item_str.Format(_T("%s: %s"), type_name.c_str(), m_list_ctrl.GetItemText(sel_index).GetString());
std::wstring messagebox_info = theApp.m_str_table.LoadTextFormat(L"MSG_DELETE_RECENTPLAYED_ITEM_INQUIRY", { item_str });
if (MessageBox(messagebox_info.c_str(), nullptr, MB_ICONQUESTION | MB_YESNO) == IDYES)
if (CRecentFolderAndPlaylist::Instance().RemoveItem(selected_item))
{
if (CMediaLibPlaylistMgr::Instance().DeleteItem(selected_item.medialib_info))
{
m_list_ctrl.DeleteItem(sel_index);
CRecentFolderAndPlaylist::Instance().Init();
}
m_list_ctrl.DeleteItem(sel_index);
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions MusicPlayer2/PlaylistMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,36 @@ std::wstring CPlaylistMgr::GetPlaylistDisplayName(const std::wstring path)
playlist_name = path_helper.GetFileNameWithoutExtension();
return playlist_name;
}

bool CPlaylistMgr::ResetLastPlayedTime(const wstring& path)
{
std::shared_lock<std::shared_mutex> lock(m_shared_mutex);
if (path == m_default_playlist.path)
{
m_default_playlist.last_played_time = 0;
return true;
}
if (path == m_favourite_playlist.path)
{
m_favourite_playlist.last_played_time = 0;
return true;
}
if (path == m_temp_playlist.path)
{
m_temp_playlist.last_played_time = 0;
return true;
}
auto iter = std::find_if(m_recent_playlists.begin(), m_recent_playlists.end(), [path](const PlaylistInfo& item) {
return item.path == path;
});

if (iter != m_recent_playlists.end())
{
iter->last_played_time = 0;
return true;
}
else
{
return false;
}
}
2 changes: 2 additions & 0 deletions MusicPlayer2/PlaylistMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class CPlaylistMgr

static std::wstring GetPlaylistDisplayName(const std::wstring path);

bool ResetLastPlayedTime(const wstring& path); //将上次播放时间清空,使它从“最近播放”中移除

private:
CPlaylistMgr();
const PlaylistInfo& GetPlaylistInfo(int index);
Expand Down
29 changes: 27 additions & 2 deletions MusicPlayer2/RecentFolderAndPlaylist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ void CRecentFolderAndPlaylist::Init()

//添加最近播放媒体库项目
CMediaLibPlaylistMgr::Instance().IterateItems([&](const MediaLibPlaylistInfo& medialib_item_info) {
m_list.emplace_back(&medialib_item_info);
if (medialib_item_info.last_played_time > 0)
m_list.emplace_back(&medialib_item_info);
});

//添加最近播放文件夹
CRecentFolderMgr::Instance().IteratePathInfo([&](const PathInfo& path_info) {
m_list.emplace_back(&path_info);
if (path_info.last_played_time > 0)
m_list.emplace_back(&path_info);
});

//按最近播放时间排序
Expand Down Expand Up @@ -70,6 +72,29 @@ bool CRecentFolderAndPlaylist::GetItem(int index, std::function<void(const Item&
return false;
}

bool CRecentFolderAndPlaylist::RemoveItem(const Item& item)
{
bool is_removed{};
if (item.IsMedialib())
{
is_removed = CMediaLibPlaylistMgr::Instance().DeleteItem(item.medialib_info);
CMediaLibPlaylistMgr::Instance().SavePlaylistData();
}
else if (item.IsFolder() && item.folder_info != nullptr)
{
is_removed = CRecentFolderMgr::Instance().ResetLastPlayedTime(item.folder_info->path);
CRecentFolderMgr::Instance().SaveData();
}
else if (item.IsPlaylist() && item.playlist_info != nullptr)
{
is_removed = CPlaylistMgr::Instance().ResetLastPlayedTime(item.playlist_info->path);
CPlaylistMgr::Instance().SavePlaylistData();
}
if (is_removed)
Init();
return is_removed;
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
bool CRecentFolderAndPlaylist::Item::IsPlaylist() const
Expand Down
2 changes: 2 additions & 0 deletions MusicPlayer2/RecentFolderAndPlaylist.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class CRecentFolderAndPlaylist
int GetSize() const;
bool GetItem(int index, std::function<void(const Item&)> func);

bool RemoveItem(const Item& item);

private:
CRecentFolderAndPlaylist();
std::vector<Item> m_list;
Expand Down
14 changes: 14 additions & 0 deletions MusicPlayer2/RecentFolderMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ int CRecentFolderMgr::DeleteInvalidItems()
return cleard_cnt;
}

bool CRecentFolderMgr::ResetLastPlayedTime(const std::wstring& path)
{
std::shared_lock<std::shared_mutex> lock(m_shared_mutex);
auto iter = std::find_if(m_recent_path.begin(), m_recent_path.end(), [&](const PathInfo& path_info) {
return path_info.path == path;
});
if (iter != m_recent_path.end())
{
iter->last_played_time = 0;
return true;
}
return false;
}

bool CRecentFolderMgr::LoadData()
{
// 打开文件
Expand Down
1 change: 1 addition & 0 deletions MusicPlayer2/RecentFolderMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CRecentFolderMgr
void GetItem(int index, std::function<void(const PathInfo&)> func);
bool DeleteItem(const std::wstring& path);
int DeleteInvalidItems();
bool ResetLastPlayedTime(const std::wstring& path); //将上次播放时间清空,使它从“最近播放”中移除

//从文件读取数据
bool LoadData();
Expand Down
30 changes: 13 additions & 17 deletions MusicPlayer2/UIWindowCmdHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,19 @@ void CUIWindowCmdHelper::OnRecentPlayedListCommand(UiElement::RecentPlayedList*
//移除
else if (command == ID_RECENT_PLAYED_REMOVE)
{
//仅支持移除媒体库项目
CString item_str;
std::wstring type_name;
if (item.IsMedialib())
type_name = CMediaLibPlaylistMgr::GetTypeName(item.medialib_info->medialib_type);
else if (item.IsFolder())
type_name = theApp.m_str_table.LoadText(L"TXT_FOLDER");
else if (item.IsPlaylist())
type_name = theApp.m_str_table.LoadText(L"TXT_PLAYLIST");
item_str.Format(_T("%s: %s"), type_name.c_str(), item.GetName().c_str());
std::wstring messagebox_info = theApp.m_str_table.LoadTextFormat(L"MSG_DELETE_RECENTPLAYED_ITEM_INQUIRY", { item_str });
if (AfxMessageBox(messagebox_info.c_str(), MB_ICONQUESTION | MB_YESNO) == IDYES)
{
CString item_str;
std::wstring type_name = CMediaLibPlaylistMgr::GetTypeName(item.medialib_info->medialib_type);
item_str.Format(_T("%s: %s"), type_name.c_str(), item.GetName().c_str());
std::wstring messagebox_info = theApp.m_str_table.LoadTextFormat(L"MSG_DELETE_RECENTPLAYED_ITEM_INQUIRY", { item_str });
if (AfxMessageBox(messagebox_info.c_str(), MB_ICONQUESTION | MB_YESNO) == IDYES)
{
if (CMediaLibPlaylistMgr::Instance().DeleteItem(item.medialib_info))
{
CRecentFolderAndPlaylist::Instance().Init();
}
}
CRecentFolderAndPlaylist::Instance().RemoveItem(item);
}
}
//复制文本
Expand Down Expand Up @@ -511,11 +510,8 @@ void CUIWindowCmdHelper::SetRecentPlayedListMenuState(CMenu* pMenu)
if (recent_played != nullptr)
{
int item_selected{ recent_played->GetItemSelected() };
if (item_selected >= 0 && item_selected < static_cast<int>(CRecentFolderAndPlaylist::Instance().GetItemList().size()))
{
const CRecentFolderAndPlaylist::Item& item{ CRecentFolderAndPlaylist::Instance().GetItemList()[item_selected] };
pMenu->EnableMenuItem(ID_RECENT_PLAYED_REMOVE, MF_BYCOMMAND | (item.IsMedialib() ? MF_ENABLED : MF_GRAYED));
}
//最近播放中排在第一个的项目为正在播放的项目,不允许移除
pMenu->EnableMenuItem(ID_RECENT_PLAYED_REMOVE, MF_BYCOMMAND | (item_selected > 0 ? MF_ENABLED : MF_GRAYED));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion MusicPlayer2/language/Simplified_Chinese.ini
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ MSG_PLAYLIST_FIX_ERROR_PATH_INQUIRY = "此功能将会检查播放列表中不
MSG_PLAYLIST_FIX_ERROR_PATH_COMPLETE = "完成,已修复<%1%>个错误的路径。"
MSG_HIDE_MENU_BAR_INFO = "提示:隐藏菜单栏后可以右击窗口标题栏,或者点击右上角的主菜单“≡”按钮,在弹出的菜单中选择“视图”——“显示菜单栏”以重新显示菜单栏。\r\n点击“取消”不再提示。"
MSG_CANNOT_FIND_IN_MEDIA_LIB_WARNING = "无法在媒体库中找到 <%1%>“<%2%>”!"
MSG_DELETE_RECENTPLAYED_ITEM_INQUIRY = "确实要从最近播放项目列表中删除该项目吗?\r\n<%1%>"
MSG_DELETE_RECENTPLAYED_ITEM_INQUIRY = "确实要从最近播放项目列表中移除该项目吗?\r\n<%1%>"
MSG_REMOVE_FROM_PLAYLIST_INQUIRY = "确实要从播放列表“<%1%>”中移除选中的<%2%>个项目吗?"
MSG_FORCE_UPDATE_MEDIA_LIB_INQUIRY = "将会在后台强制更新整个媒体库中所有曲目的信息,此操作可能会比较耗时,要继续吗?"
Expand Down

0 comments on commit 76e8cb1

Please sign in to comment.