diff --git a/CastIt/ViewModels/MainViewModel.cs b/CastIt/ViewModels/MainViewModel.cs index 9f5e3877..f4346781 100644 --- a/CastIt/ViewModels/MainViewModel.cs +++ b/CastIt/ViewModels/MainViewModel.cs @@ -68,6 +68,7 @@ public class MainViewModel : BaseViewModel, IMainViewModel private readonly MvxInteraction _closeApp = new MvxInteraction(); private readonly MvxInteraction<(double, double)> _setWindowWidthAndHeight = new MvxInteraction<(double, double)>(); private readonly MvxInteraction _openSubTitleFileDialog = new MvxInteraction(); + private readonly MvxInteraction _beforeDeletingPlayList = new MvxInteraction(); #endregion #region Properties @@ -228,7 +229,7 @@ public int CurrentFileQuality public IMvxAsyncCommand SkipCommand { get; private set; } public IMvxCommand SwitchPlayListsCommand { get; private set; } public IMvxAsyncCommand AddNewPlayListCommand { get; private set; } - public IMvxAsyncCommand DeletePlayListCommand { get; private set; } + public MvxCommand DeletePlayListCommand { get; private set; } public IMvxAsyncCommand DeleteAllPlayListsExceptCommand { get; private set; } public IMvxCommand OpenSettingsCommand { get; private set; } public IMvxCommand OpenDevicesCommand { get; private set; } @@ -249,6 +250,8 @@ public IMvxInteraction CloseApp => _setWindowWidthAndHeight; public IMvxInteraction OpenSubTitleFileDialog => _openSubTitleFileDialog; + public IMvxInteraction BeforeDeletingPlayList + => _beforeDeletingPlayList; #endregion public MainViewModel( @@ -350,11 +353,11 @@ public override void SetCommands() SkipCommand = new MvxAsyncCommand(SkipSeconds); - SwitchPlayListsCommand = new MvxCommand(SwitchPlayLists); + SwitchPlayListsCommand = new MvxCommand(() => SwitchPlayLists()); AddNewPlayListCommand = new MvxAsyncCommand(AddNewPlayList); - DeletePlayListCommand = new MvxAsyncCommand(DeletePlayList); + DeletePlayListCommand = new MvxCommand(pl => _beforeDeletingPlayList.Raise(pl)); DeleteAllPlayListsExceptCommand = new MvxAsyncCommand(DeleteAllPlayLists); @@ -547,9 +550,12 @@ public void SetPlayListOptions(long id, bool loop, bool shuffle) public Task DeletePlayList(long id) { - var pl = PlayLists.FirstOrDefault(pl => pl.Id == id); - if (pl != null) - return DeletePlayList(pl); + var playlist = PlayLists.FirstOrDefault(pl => pl.Id == id); + if (playlist != null) + { + _beforeDeletingPlayList.Raise(playlist); + return Task.CompletedTask; + } Logger.Warn($"{nameof(DeletePlayList)}: Cant delete playlistId = {id} because it doesnt exists"); return ShowSnackbarMsg(GetText("PlayListDoesntExist")); } @@ -643,7 +649,7 @@ public Task RenamePlayList(long id, string newName) var playlist = PlayLists.FirstOrDefault(pl => pl.Id == id); if (playlist != null) return playlist.SavePlayList(newName); - Logger.Warn($"{nameof(DeletePlayList)}: Cant rename playlistId = {id} because it doesnt exists"); + Logger.Warn($"{nameof(RenamePlayList)}: Cant rename playlistId = {id} because it doesnt exists"); return ShowSnackbarMsg(GetText("PlayListDoesntExist")); } #endregion @@ -715,11 +721,16 @@ private async Task AddNewPlayList() _appWebServer.OnPlayListAdded?.Invoke(vm.Id); } - private async Task DeletePlayList(PlayListItemViewModel playlist) + public async Task DeletePlayList(int logicalIndex, PlayListItemViewModel playlist) { if (playlist == null) return; + + long index = PlayLists.IndexOf(playlist); long id = playlist.Id; + //Remember that if you move the tabs, the SelectedPlayListIndex is not updated + if (index == SelectedPlayListIndex) + SwitchPlayLists(false, logicalIndex); await _playListsService.DeletePlayList(id); playlist.CleanUp(); PlayLists.Remove(playlist); @@ -776,9 +787,14 @@ private async Task HandleCloseApp() _closeApp.Raise(); } - private void SwitchPlayLists() + private void SwitchPlayLists(bool forward = true, int? playlistIndex = null) { - int tentativeIndex = SelectedPlayListIndex + 1; + int increment = forward ? 1 : -1; + int tentativeIndex = SelectedPlayListIndex + increment; + if (playlistIndex.HasValue) + { + tentativeIndex = playlistIndex.Value + increment; + } SelectedPlayListIndex = PlayLists.ElementAtOrDefault(tentativeIndex) != null ? tentativeIndex : 0; } diff --git a/CastIt/Views/MainPage.xaml.cs b/CastIt/Views/MainPage.xaml.cs index 97bc750c..1fbb4da9 100644 --- a/CastIt/Views/MainPage.xaml.cs +++ b/CastIt/Views/MainPage.xaml.cs @@ -20,6 +20,7 @@ public partial class MainPage : MvxWpfView private IMvxInteraction _closeAppRequest; private IMvxInteraction<(double, double)> _setWindowWithAndHeightRequest; private IMvxInteraction _openSubTitleFileDialogRequest; + private IMvxInteraction _beforeDeletingPlayListRequest; public IMvxInteraction CloseAppRequest { @@ -63,6 +64,20 @@ public IMvxInteraction OpenSubTitleFileDialogRequest } } + public IMvxInteraction BeforeDeletingPlayListRequest + { + get => _beforeDeletingPlayListRequest; + set + { + if (_beforeDeletingPlayListRequest != null) + _beforeDeletingPlayListRequest.Requested -= BeforeDeletingPlayList; + + _beforeDeletingPlayListRequest = value; + if (value != null) + _beforeDeletingPlayListRequest.Requested += BeforeDeletingPlayList; + } + } + public MainPage() { InitializeComponent(); @@ -71,6 +86,7 @@ public MainPage() set.Bind(this).For(v => v.SetWindowWithAndHeightRequest).To(vm => vm.SetWindowWidthAndHeight).OneWay(); set.Bind(this).For(v => v.CloseAppRequest).To(vm => vm.CloseApp).OneWay(); set.Bind(this).For(v => v.OpenSubTitleFileDialogRequest).To(vm => vm.OpenSubTitleFileDialog).OneWay(); + set.Bind(this).For(v => v.BeforeDeletingPlayListRequest).To(vm => vm.BeforeDeletingPlayList).OneWay(); set.Apply(); } @@ -104,5 +120,14 @@ private void OpenSubtitleFileDialog(object sender, EventArgs e) } private void CloseAppHandler(object sender, EventArgs e) => WindowButtons.CloseApp(); + + private async void BeforeDeletingPlayList(object sender, MvxValueEventArgs e) + { + if (e.Value == null) + return; + var tabs = GetTabsPosition(); + var (playlist, logicalIndex) = tabs.FirstOrDefault(t => t.Key.Id == e.Value.Id); + await ViewModel.DeletePlayList(logicalIndex, playlist); + } } }