Skip to content

Commit

Permalink
fix: music categories navigation to albums/playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
zznty committed Oct 13, 2024
1 parent d568084 commit 18aa282
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 93 deletions.
12 changes: 2 additions & 10 deletions MusicX/Controls/Blocks/MusicCategoryBlockControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows.Media;
using MusicX.Helpers;
using MusicX.ViewModels;
using NLog;
using Wpf.Ui.Appearance;
Expand Down Expand Up @@ -69,16 +70,7 @@ private async Task OpenPage(Link link)
{
try
{
if (link.Meta?.ContentType is "custom")
{
navigationService.OpenSection(link.Meta.TrackCode);
return;
}

var music = await vkService.GetAudioCatalogAsync(link.Url);
navigationService.OpenSection(music.Catalog.DefaultSection);

return;
await navigationService.OpenLinkAsync(link);
}
catch(Exception ex)
{
Expand Down
85 changes: 2 additions & 83 deletions MusicX/Controls/LinkControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection;
using MusicX.Core.Models;
using MusicX.Core.Services;
using MusicX.Helpers;
using MusicX.Services;
using MusicX.ViewModels;
using MusicX.Views;
Expand Down Expand Up @@ -105,95 +106,13 @@ private async void CardAction_Click(object sender, RoutedEventArgs e)
{
try
{
if (Link.Meta.ContentType == null)
{
var match = Regex.Match(Link.Url, "https://vk.com/podcasts\\?category=[0-9]+$");

if (match.Success)
{
//var podcasts = await vkService.GetPodcastsAsync(Link.Url);
//await navigationService.OpenSection(podcasts.Catalog.DefaultSection, true);

return;

}
var music = await vkService.GetAudioCatalogAsync(Link.Url);
navigationService.OpenSection(music.Catalog.DefaultSection);

return;
}

if (Link.Meta.ContentType == "artist")
{
var url = new Uri(Link.Url);

navigationService.OpenSection(url.Segments.LastOrDefault(), SectionType.Artist);
}

if (Link.Meta.ContentType is "group" or "user" or "chat")
{
if (CustomSectionsService.CustomLinkRegex().IsMatch(Link.Id))
{
navigationService.OpenSection(Link.Id);
return;
}

var match = UserProfileRegex().Match(Link.Url);
if(match.Success)
{
var music = await vkService.GetAudioCatalogAsync(Link.Url);

navigationService.OpenSection(music.Catalog.DefaultSection);

return;
}


Process.Start(new ProcessStartInfo
{
FileName = Link.Url,
UseShellExecute = true
});
}

if (Link.Meta.ContentType == "curator")
{

var curator = await vkService.GetAudioCuratorAsync(Link.Meta.TrackCode, Link.Url);

navigationService.OpenSection(curator.Catalog.DefaultSection);

}

if (Link.Meta.ContentType == "audio_playlists")
{
const string playlistUrl = "https://vk.com/music/playlist/";
if (Link.Url.StartsWith(playlistUrl))
{
var (playlistId, ownerId, accessKey, _) = PlaylistData.Parse(Link.Url[playlistUrl.Length..]);
navigationService.OpenExternalPage(new PlaylistView(playlistId, ownerId, accessKey));
return;
}

if (Link.Url == "https://vk.com/audio?catalog=my_audios")
{
navigationService.OpenMenuSection("Музыка");
return;
}

var catalog = await vkService.GetAudioCatalogAsync(Link.Url);

navigationService.OpenSection(catalog.Catalog.DefaultSection);
}
await navigationService.OpenLinkAsync(Link);
}
catch(Exception ex)
{
logger.Error(ex, "Failed click action in link control");
}

}

[GeneratedRegex("https://vk.com/audios\\-?[0-9]+$")]
private static partial Regex UserProfileRegex();
}
}
116 changes: 116 additions & 0 deletions MusicX/Helpers/NavigationServiceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using MusicX.Core.Models;
using MusicX.Core.Services;
using MusicX.Services;
using MusicX.ViewModels;
using MusicX.Views;

namespace MusicX.Helpers;

public static partial class NavigationServiceExtensions
{
public static async Task OpenLinkAsync(this NavigationService navigationService, Link link)
{
var vkService = StaticService.Container.GetRequiredService<VkService>();

switch (link.Meta.ContentType)
{
case "" or null:
{
var match = PodcastsCategoryUrl().Match(link.Url);

if (match.Success)
{
//var podcasts = await vkService.GetPodcastsAsync(Link.Url);
//await navigationService.OpenSection(podcasts.Catalog.DefaultSection, true);

return;
}

var music = await vkService.GetAudioCatalogAsync(link.Url);
navigationService.OpenSection(music.Catalog.DefaultSection);

return;
}
case "artist":
{
var url = new Uri(link.Url);

navigationService.OpenSection(url.Segments.Last(), SectionType.Artist);
break;
}
case "group" or "user" or "chat" when CustomSectionsService.CustomLinkRegex().IsMatch(link.Id):
{
navigationService.OpenSection(link.Id);
return;
}
case "group" or "user" or "chat":
{
var match = UserProfileRegex().Match(link.Url);
if (match.Success)
{
var music = await vkService.GetAudioCatalogAsync(link.Url);

navigationService.OpenSection(music.Catalog.DefaultSection);

return;
}


Process.Start(new ProcessStartInfo
{
FileName = link.Url,
UseShellExecute = true
});
break;
}
case "curator":
{
var curator = await vkService.GetAudioCuratorAsync(link.Meta.TrackCode, link.Url);

navigationService.OpenSection(curator.Catalog.DefaultSection);
break;
}
case "audio_playlists" or "audio_albums":
{
var match = PlaylistOrAlbumUrl().Match(link.Url);
if (match.Success)
{
var (playlistId, ownerId, accessKey, _) = PlaylistData.Parse(match.Groups["id"].Value);
navigationService.OpenExternalPage(new PlaylistView(playlistId, ownerId, accessKey));
return;
}

if (link.Url == "https://vk.com/audio?catalog=my_audios")
{
navigationService.OpenMenuSection("Музыка");
return;
}

var catalog = await vkService.GetAudioCatalogAsync(link.Url);

navigationService.OpenSection(catalog.Catalog.DefaultSection);
break;
}
case "custom":
{
navigationService.OpenSection(link.Meta.TrackCode);
break;
}
}
}

[GeneratedRegex(@"https://vk\.com/podcasts\?category=[0-9]+$")]
private static partial Regex PodcastsCategoryUrl();

[GeneratedRegex(@"https://vk\.com/audios\-?[0-9]+$")]
private static partial Regex UserProfileRegex();

[GeneratedRegex(@"https://vk\.com/music/(?:album|playlist)/(?<id>[\-\w]+)$")]
private static partial Regex PlaylistOrAlbumUrl();
}

0 comments on commit 18aa282

Please sign in to comment.