-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,94 @@ | ||
using MusicX.Core.Models.Mix; | ||
using System; | ||
using MusicX.Core.Models.Mix; | ||
using MusicX.Core.Services; | ||
using System.Collections.ObjectModel; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text.Json.Nodes; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
using AsyncAwaitBestPractices; | ||
using Wpf.Ui.Common; | ||
|
||
namespace MusicX.ViewModels.Modals | ||
{ | ||
public class MixSettingsModalViewModel : BaseViewModel | ||
{ | ||
|
||
public string Title { get; set; } | ||
namespace MusicX.ViewModels.Modals; | ||
|
||
public string Subtitle { get; set; } | ||
public class MixSettingsModalViewModel(VkService vkService) : BaseViewModel | ||
{ | ||
public string Title { get; set; } = string.Empty; | ||
|
||
public bool IsLoading { get; set; } | ||
public string Subtitle { get; set; } = string.Empty; | ||
|
||
public bool IsLoading { get; set; } | ||
|
||
public ObservableCollection<MixSettingsCategoryViewModel> Categories { get; set; } = []; | ||
|
||
public ICommand? ApplyCommand { get; set; } | ||
public ICommand? ResetCommand { get; set; } | ||
|
||
public ObservableCollection<MixCategory> Categories { get; set; } | ||
public async Task LoadSettings(string mixId) | ||
{ | ||
IsLoading = true; | ||
|
||
var settings = await vkService.GetStreamMixSettings(mixId); | ||
|
||
private readonly VkService _vkService; | ||
IsLoading = false; | ||
Title = settings.Settings.Title; | ||
Subtitle = settings.Settings.Subtitle; | ||
Categories = new(settings.Settings.Categories.Select(b => new MixSettingsCategoryViewModel(b))); | ||
} | ||
} | ||
|
||
public MixSettingsModalViewModel(VkService vkService) | ||
{ | ||
_vkService = vkService; | ||
} | ||
public class MixSettingsCategoryViewModel : BaseViewModel | ||
{ | ||
public string Title { get; set; } | ||
public string Id { get; set; } | ||
public string Type { get; set; } | ||
public ObservableCollection<MixSettingsOptionViewModel> Options { get; set; } | ||
|
||
public MixSettingsCategoryViewModel(MixCategory category) | ||
{ | ||
Title = category.Title; | ||
Id = category.Id; | ||
Type = category.Type; | ||
Options = new(category.Options.Select(o => new MixSettingsOptionViewModel(o))); | ||
} | ||
} | ||
|
||
public class MixSettingsOptionViewModel : BaseViewModel | ||
{ | ||
public string Title { get; set; } | ||
public string Id { get; set; } | ||
|
||
public byte[]? Icon { get; set; } | ||
|
||
public bool Selected { get; set; } | ||
|
||
public MixSettingsOptionViewModel(MixOption option) | ||
{ | ||
Title = option.Title; | ||
Id = option.Id; | ||
if (!string.IsNullOrEmpty(option.IconUri)) | ||
Load(option.IconUri).SafeFireAndForget(); | ||
} | ||
|
||
public async Task LoadSettings(string mixId) | ||
private async Task Load(string uri) | ||
{ | ||
using var httpClient = new HttpClient(new HttpClientHandler | ||
{ | ||
IsLoading = true; | ||
OnPropertyChanged(nameof(IsLoading)); | ||
|
||
var settings = await _vkService.GetStreamMixSettings(mixId); | ||
|
||
IsLoading = false; | ||
OnPropertyChanged(nameof(IsLoading)); | ||
AutomaticDecompression = DecompressionMethods.All | ||
}); | ||
|
||
await using var stream = await httpClient.GetStreamAsync(uri); | ||
|
||
this.Title = settings.Settings.Title; | ||
OnPropertyChanged(nameof(Title)); | ||
var jsonObject = await JsonNode.ParseAsync(stream); | ||
|
||
if (jsonObject is null) return; | ||
|
||
this.Subtitle = settings.Settings.Subtitle; | ||
OnPropertyChanged(nameof(Subtitle)); | ||
var base64Image = jsonObject["assets"]![0]!["p"]!.AsValue().ToString(); | ||
|
||
this.Categories = new ObservableCollection<MixCategory>(settings.Settings.Categories); | ||
OnPropertyChanged(nameof(Subtitle)); | ||
|
||
} | ||
Icon = Convert.FromBase64String(base64Image["data:image/png;base64,".Length..]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters