Skip to content

Commit

Permalink
[CastIt] The selected tab was not being correctly selected after dele…
Browse files Browse the repository at this point in the history
…ting a playlist
  • Loading branch information
Wolfteam committed Nov 3, 2020
1 parent b8883cf commit b244487
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
36 changes: 26 additions & 10 deletions CastIt/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PlayListItemViewModel> _beforeDeletingPlayList = new MvxInteraction<PlayListItemViewModel>();
#endregion

#region Properties
Expand Down Expand Up @@ -228,7 +229,7 @@ public int CurrentFileQuality
public IMvxAsyncCommand<int> SkipCommand { get; private set; }
public IMvxCommand SwitchPlayListsCommand { get; private set; }
public IMvxAsyncCommand AddNewPlayListCommand { get; private set; }
public IMvxAsyncCommand<PlayListItemViewModel> DeletePlayListCommand { get; private set; }
public MvxCommand<PlayListItemViewModel> DeletePlayListCommand { get; private set; }
public IMvxAsyncCommand<PlayListItemViewModel> DeleteAllPlayListsExceptCommand { get; private set; }
public IMvxCommand OpenSettingsCommand { get; private set; }
public IMvxCommand OpenDevicesCommand { get; private set; }
Expand All @@ -249,6 +250,8 @@ public IMvxInteraction CloseApp
=> _setWindowWidthAndHeight;
public IMvxInteraction OpenSubTitleFileDialog
=> _openSubTitleFileDialog;
public IMvxInteraction<PlayListItemViewModel> BeforeDeletingPlayList
=> _beforeDeletingPlayList;
#endregion

public MainViewModel(
Expand Down Expand Up @@ -350,11 +353,11 @@ public override void SetCommands()

SkipCommand = new MvxAsyncCommand<int>(SkipSeconds);

SwitchPlayListsCommand = new MvxCommand(SwitchPlayLists);
SwitchPlayListsCommand = new MvxCommand(() => SwitchPlayLists());

AddNewPlayListCommand = new MvxAsyncCommand(AddNewPlayList);

DeletePlayListCommand = new MvxAsyncCommand<PlayListItemViewModel>(DeletePlayList);
DeletePlayListCommand = new MvxCommand<PlayListItemViewModel>(pl => _beforeDeletingPlayList.Raise(pl));

DeleteAllPlayListsExceptCommand = new MvxAsyncCommand<PlayListItemViewModel>(DeleteAllPlayLists);

Expand Down Expand Up @@ -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"));
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down
25 changes: 25 additions & 0 deletions CastIt/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class MainPage : MvxWpfView<MainViewModel>
private IMvxInteraction _closeAppRequest;
private IMvxInteraction<(double, double)> _setWindowWithAndHeightRequest;
private IMvxInteraction _openSubTitleFileDialogRequest;
private IMvxInteraction<PlayListItemViewModel> _beforeDeletingPlayListRequest;

public IMvxInteraction CloseAppRequest
{
Expand Down Expand Up @@ -63,6 +64,20 @@ public IMvxInteraction OpenSubTitleFileDialogRequest
}
}

public IMvxInteraction<PlayListItemViewModel> BeforeDeletingPlayListRequest
{
get => _beforeDeletingPlayListRequest;
set
{
if (_beforeDeletingPlayListRequest != null)
_beforeDeletingPlayListRequest.Requested -= BeforeDeletingPlayList;

_beforeDeletingPlayListRequest = value;
if (value != null)
_beforeDeletingPlayListRequest.Requested += BeforeDeletingPlayList;
}
}

public MainPage()
{
InitializeComponent();
Expand All @@ -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();
}

Expand Down Expand Up @@ -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<PlayListItemViewModel> 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);
}
}
}

0 comments on commit b244487

Please sign in to comment.