Skip to content

Commit

Permalink
Merge pull request #415 from Fooxboy/90
Browse files Browse the repository at this point in the history
Поддержка эвентов для блоков
  • Loading branch information
Fooxboy authored Aug 30, 2024
2 parents 1c194a7 + 9cfbd7d commit 579b2bc
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 14 deletions.
13 changes: 10 additions & 3 deletions MusicX/Controls/NavigationBar.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
Expand All @@ -7,7 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using MusicX.Services;
using NLog;
using Wpf.Ui.Extensions;

namespace MusicX.Controls;

public class NavigationBar : Control
Expand Down Expand Up @@ -91,9 +92,15 @@ private void FrameOnNavigated(object sender, NavigationEventArgs e)
CurrentItem = Items.FirstOrDefault(b => b.Tag == menuView.MenuTag);
else
CurrentItem = null;

if (CurrentItem != null)
Frame.CleanNavigation();
{
while (Frame.CanGoBack)
{
if (Frame.RemoveBackEntry().CustomContentState is IDisposable disposable)
disposable.Dispose();
}
}

Log.Info("Navigated to {Content}", e.Content.ToString());
}
Expand Down
3 changes: 3 additions & 0 deletions MusicX/Controls/PlayerControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -424,15 +424,18 @@ private async void LikeTrack_Click(object sender, RoutedEventArgs e)
var vkService = StaticService.Container.GetRequiredService<VkService>();
var boomService = StaticService.Container.GetRequiredService<BoomService>();
var snackbarService = StaticService.Container.GetRequiredService<ISnackbarService>();
var eventService = StaticService.Container.GetRequiredService<SectionEventService>();

switch (PlayerService.CurrentTrack?.Data)
{
case VkTrackData vkData when LikeIcon.Filled:
await vkService.Dislike(vkData.Info.Id, vkData.Info.OwnerId);
await vkService.AudioDeleteAsync(vkData.Info.Id, vkData.Info.OwnerId);
eventService.Dispatch(this, SectionEvent.AudiosRemove);
break;
case VkTrackData vkData:
await vkService.AudioAddAsync(vkData.Info.Id, vkData.Info.OwnerId);
eventService.Dispatch(this, SectionEvent.AudiosAdd);
break;
case BoomTrackData boomData when LikeIcon.Filled:
await boomService.UnLike(boomData.Id);
Expand Down
3 changes: 3 additions & 0 deletions MusicX/Controls/PlaylistControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,15 @@ private void cardAction_MouseLeave(object sender, MouseEventArgs e)
private async void AddToLibrary_MouseDown(object sender, MouseButtonEventArgs e)
{
var snackbarService = StaticService.Container.GetRequiredService<ISnackbarService>();
var eventService = StaticService.Container.GetRequiredService<SectionEventService>();

try
{
var vkService = StaticService.Container.GetRequiredService<VkService>();

await vkService.AddPlaylistAsync(Playlist.Id, Playlist.OwnerId, Playlist.AccessKey);

eventService.Dispatch(this, SectionEvent.PlaylistsFollow);

snackbarService.Show("Плейлист добавлен", "Плейлист теперь находится в Вашей библиотеке", ControlAppearance.Success);
}
Expand Down
5 changes: 3 additions & 2 deletions MusicX/Controls/TrackControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,18 +481,19 @@ private async void AddRemove_MouseDown(object sender, MouseButtonEventArgs e)
{
var configService = StaticService.Container.GetRequiredService<ConfigService>();
var vkService = StaticService.Container.GetRequiredService<VkService>();

var eventService = StaticService.Container.GetRequiredService<SectionEventService>();

var config = await configService.GetConfig();

if (Audio.OwnerId == config.UserId)
{
await vkService.AudioDeleteAsync(Audio.Id, Audio.OwnerId);
eventService.Dispatch(this, SectionEvent.AudiosRemove);
}
else
{
await vkService.AudioAddAsync(Audio.Id, Audio.OwnerId);

eventService.Dispatch(this, SectionEvent.AudiosAdd);
}
}catch(Exception ex)
{
Expand Down
3 changes: 2 additions & 1 deletion MusicX/RootWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ private void NavigationServiceOnBackRequested(object? sender, EventArgs e)
return;

RootFrame.GoBack();
RootFrame.RemoveBackEntry();
if (RootFrame.RemoveBackEntry().CustomContentState is IDisposable disposable)
disposable.Dispose();
}
private void NavigationServiceOnExternalSectionOpened(object? sender, SectionViewModel e)
{
Expand Down
28 changes: 28 additions & 0 deletions MusicX/Services/SectionEventService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace MusicX.Services;

public class SectionEventService
{
public event EventHandler<string>? Event;

public void Dispatch(object? sender, string name)
{
Event?.Invoke(sender, name);
}
}

public static class SectionEvent
{
public const string AudiosAdd = "music_audios_add";
public const string AudiosRemove = "music_audios_remove";

public const string PlaylistsAdd = "music_playlists_add";
public const string PlaylistsRemove = "music_playlists_remove";

public const string PlaylistsFollow = "music_playlists_follow";
public const string PlaylistsUnfollow = "music_playlists_unfollow";

public const string ArtistSubscribe = "artist_subscribe";
public const string ArtistUnsubscribe = "artist_unsubscribe";
}
7 changes: 6 additions & 1 deletion MusicX/ViewModels/BlockViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Specialized;
using System.Collections.Immutable;
using System.Collections.Specialized;
using MusicX.Core.Models;
using MusicX.Helpers;
using MusicX.Shared.ListenTogether.Radio;
Expand Down Expand Up @@ -49,6 +50,8 @@ public class BlockViewModel : BaseViewModel

public ObservableRangeCollection<Longread> Longreads { get; }

public ImmutableHashSet<string> ListenEvents { get; private set; }

public BlockViewModel(Block block)
{
Id = block.Id;
Expand All @@ -74,6 +77,7 @@ public BlockViewModel(Block block)
Stations = new(block.Stations);
Curators = new(block.Curators);
Longreads = new(block.Longreads);
ListenEvents = [..block.ListenEvents];
}

public void MergeBlock(Block block)
Expand All @@ -98,5 +102,6 @@ public void MergeBlock(Block block)
Stations.AddRange(block.Stations, action);
Curators.AddRange(block.Curators, action);
Longreads.AddRange(block.Longreads, action);
ListenEvents = ListenEvents.Union(block.ListenEvents);
}
}
5 changes: 4 additions & 1 deletion MusicX/ViewModels/Controls/BlockButtonViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,16 @@ private async void Invoke()
case "toggle_artist_subscription" when Artist is not null && ParentBlock is not null:
{
var vkService = StaticService.Container.GetRequiredService<VkService>();
var eventService = StaticService.Container.GetRequiredService<SectionEventService>();

if (Artist.IsFollowed)
await vkService.UnfollowArtist(Action.ArtistId, ParentBlock.Id);
else
await vkService.FollowArtist(Action.ArtistId, ParentBlock.Id);

Artist.IsFollowed = !Artist.IsFollowed;
Artist.IsFollowed = !Artist.IsFollowed;

eventService.Dispatch(this, Artist.IsFollowed ? SectionEvent.ArtistSubscribe : SectionEvent.ArtistUnsubscribe);
Refresh();
break;
}
Expand Down
6 changes: 4 additions & 2 deletions MusicX/ViewModels/Modals/CreatePlaylistModalViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class CreatePlaylistModalViewModel : BaseViewModel
private readonly TracksSelectorModalViewModel selectorViewModel;
private readonly ConfigService configService;
private readonly ISnackbarService _snackbarService;
private readonly SectionEventService _eventService;

private bool isEdit;
public bool IsEdit
Expand All @@ -67,13 +68,14 @@ public bool IsEdit

public CreatePlaylistModalViewModel(NavigationService navigationService, VkService vkService,
TracksSelectorModalViewModel selectorViewModel, ConfigService configService,
ISnackbarService snackbarService)
ISnackbarService snackbarService, SectionEventService eventService)
{
this.navigationService = navigationService;
this.vkService = vkService;
this.selectorViewModel = selectorViewModel;
this.configService = configService;
_snackbarService = snackbarService;
_eventService = eventService;

this.AddTracksCommand = new RelayCommand(AddTracks);
this.CreateCommand = new AsyncCommand(Create);
Expand Down Expand Up @@ -145,7 +147,7 @@ private async Task Create()
}else
{
playlistId = await vkService.CreatePlaylistAsync(config.UserId, this.Title, this.Description, Tracks);

_eventService.Dispatch(this, SectionEvent.PlaylistsAdd);
}

if(!this.CoverPath.StartsWith("http"))
Expand Down
9 changes: 8 additions & 1 deletion MusicX/ViewModels/PlaylistViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,19 @@ public class PlaylistViewModel:BaseViewModel
private readonly VkService vkService;
private readonly Logger logger;
private readonly ISnackbarService _snackbarService;
private readonly SectionEventService _eventService;

public ConfigService ConfigService { get; set; }

public PlaylistViewModel(VkService vkService, Logger logger, ConfigService configService,
ISnackbarService snackbarService)
ISnackbarService snackbarService, SectionEventService eventService)
{
this.vkService = vkService;
this.ConfigService = configService;
this.logger = logger;

_snackbarService = snackbarService;
_eventService = eventService;
}
public async ValueTask LoadMore()
{
Expand Down Expand Up @@ -270,6 +272,7 @@ public async Task<bool> AddPlaylist()
try
{
await vkService.AddPlaylistAsync(Playlist.Id, Playlist.OwnerId, Playlist.AccessKey);
_eventService.Dispatch(this, SectionEvent.PlaylistsFollow);
return true;
}catch(Exception ex)
{
Expand All @@ -296,6 +299,10 @@ public async Task<bool> RemovePlaylist()
try
{
await vkService.DeletePlaylistAsync(Playlist.Id, Playlist.OwnerId);
_eventService.Dispatch(this,
Playlist.OwnerId == ConfigService.Config.UserId
? SectionEvent.PlaylistsRemove
: SectionEvent.PlaylistsUnfollow);
return true;
}
catch (Exception ex)
Expand Down
37 changes: 35 additions & 2 deletions MusicX/ViewModels/SectionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public enum ContentState
Loaded
}

public class SectionViewModel : BaseViewModel, INotifyOnActivated
public class SectionViewModel : BaseViewModel, INotifyOnActivated, IDisposable
{
private readonly VkService vkService;
private readonly Logger logger;
private readonly ISnackbarService _snackbarService;
private readonly ConfigService configService;
private readonly SectionEventService _eventService;

public ContentState ContentState { get; set; }
public bool IsLoadingMore { get; set; }
Expand All @@ -41,12 +42,37 @@ public class SectionViewModel : BaseViewModel, INotifyOnActivated
public ObservableRangeCollection<BlockViewModel> Blocks { get; } = [];

public SectionViewModel(VkService vkService, Logger logger, ISnackbarService snackbarService,
ConfigService configService)
ConfigService configService, SectionEventService eventService)
{
this.vkService = vkService;
this.logger = logger;
_snackbarService = snackbarService;
this.configService = configService;
_eventService = eventService;

_eventService.Event += EventServiceOnEvent;
}

private async void EventServiceOnEvent(object? sender, string e)
{
var changed = false;
for (var i = 0; i < Blocks.Count; i++)
{
var block = Blocks[i];

if (!block.ListenEvents.Contains(e))
continue;

changed = true;

// не работает хз
// var response = await vkService.GetBlockItems(block.Id);
//
// Blocks[i] = new BlockViewModel(response.Block);
}

if (changed)
await LoadAsync();
}

public async Task LoadAsync()
Expand Down Expand Up @@ -327,6 +353,13 @@ public void OnActivated()
{
if (Section is null)
LoadAsync().SafeFireAndForget();
else
_eventService.Event += EventServiceOnEvent;
}

public void Dispose()
{
_eventService.Event -= EventServiceOnEvent;
}
}
}
7 changes: 6 additions & 1 deletion MusicX/Views/SectionView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private async void SectionScrollViewer_ScrollChanged(object sender, ScrollChange
}

[Serializable]
private class SectionState : CustomContentState, ISerializable
private class SectionState : CustomContentState, ISerializable, IDisposable
{
private const string TypeKey = "SectionType";
private const string IdKey = "SectionId";
Expand Down Expand Up @@ -77,6 +77,11 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
info.AddValue(TypeKey, (int)_viewModel.SectionType);
info.AddValue(IdKey, _viewModel.Section.Id);
}

public void Dispose()
{
_viewModel.Dispose();
}
}
public override string ToString()
{
Expand Down
1 change: 1 addition & 0 deletions MusicX/Views/StartingWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ await Task.Run(async () =>
collection.AddSingleton<IScrobbler, MemoryScrobbler>();
collection.AddSingleton<ITrackApi, TrackApi>();
collection.AddSingleton<WindowThemeService>();
collection.AddSingleton<SectionEventService>();

var container = StaticService.Container = collection.BuildServiceProvider();

Expand Down

0 comments on commit 579b2bc

Please sign in to comment.