Skip to content

Commit a911e1c

Browse files
committed
error message on save, also removed unreliable retry thing
1 parent 06a5980 commit a911e1c

5 files changed

Lines changed: 103 additions & 36 deletions

File tree

Models/Song.cs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,36 +79,23 @@ public string? Lyrics
7979
OnPropertyChanged();
8080
}
8181
}
82-
public async Task ApplyLyricsToFileAsync()
82+
public IOException? AttemptApplyLyricsToFile(string lyrics)
8383
{
84-
const int maxRetries = 20;
85-
const int delayOnRetry = 1000; // absolutely a hacky way to do this, however it works for virtual storage so :shrug:
86-
87-
for (int i = 0; i < maxRetries; i++)
84+
try
8885
{
89-
try
90-
{
91-
using (var file = TagLib.File.Create(FilePath))
92-
{
93-
file.Tag.Lyrics = Lyrics;
94-
file.Save();
95-
}
96-
97-
Debug.WriteLine("lyrics saved successfully.");
98-
return;
99-
}
100-
catch (IOException ex)
86+
using (var file = TagLib.File.Create(FilePath))
10187
{
102-
Debug.WriteLine($"attempt {i + 1} to save lyrics failed. retrying...");
103-
if (i < maxRetries - 1)
104-
{
105-
await Task.Delay(delayOnRetry);
106-
}
107-
else
108-
{
109-
Debug.WriteLine($"failed to save lyrics after {maxRetries} attempts: {ex.Message}");
110-
}
88+
file.Tag.Lyrics = lyrics;
89+
file.Save();
90+
Lyrics = lyrics;
11191
}
92+
93+
Debug.WriteLine("lyrics saved successfully.");
94+
return null;
95+
}
96+
catch (IOException ex)
97+
{
98+
return ex;
11299
}
113100
}
114101

Musium.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<None Remove="Popups\AddLyricsPopup.xaml" />
2929
<None Remove="Popups\ClearLyricsPopup.xaml" />
3030
<None Remove="Popups\ConfirmLyricsPopup.xaml" />
31+
<None Remove="Popups\LyricErrorPopup.xaml" />
3132
<None Remove="Styles\GridContentStyles.xaml" />
3233
</ItemGroup>
3334

@@ -68,6 +69,9 @@
6869
<None Update="Assets\Placeholder.png">
6970
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
7071
</None>
72+
<Page Update="Popups\LyricErrorPopup.xaml">
73+
<Generator>MSBuild:Compile</Generator>
74+
</Page>
7175
<Page Update="Popups\ClearLyricsPopup.xaml">
7276
<Generator>MSBuild:Compile</Generator>
7377
</Page>

Pages/Lyrics.xaml.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ private async void AddLyricsButton_Click(object sender, RoutedEventArgs e)
3737
await EditLyrics(true);
3838
}
3939

40+
private async void DisplayError(IOException ex)
41+
{
42+
ContentDialog dialog = new ContentDialog();
43+
44+
dialog.XamlRoot = XamlRoot;
45+
dialog.Title = "An error has occurred.";
46+
dialog.CloseButtonText = "Sad";
47+
dialog.DefaultButton = ContentDialogButton.Close;
48+
dialog.Content = new LyricErrorPopup();
49+
LyricErrorPopup content = (LyricErrorPopup)dialog.Content;
50+
content.SetText(ex.Message);
51+
52+
await dialog.ShowAsync();
53+
}
4054
private async Task EditLyrics(bool adding = false)
4155
{
4256
if (Audio.CurrentSongPlaying == null) return;
@@ -100,8 +114,14 @@ private async Task EditLyrics(bool adding = false)
100114
if (Audio.CurrentSongPlaying == null) return;
101115
if (dialog.Content is AddLyricsPopup popup)
102116
{
103-
Audio.CurrentSongPlaying.Lyrics = lyrics;
104-
if (!popup.SessionChecked) await Audio.CurrentSongPlaying.ApplyLyricsToFileAsync();
117+
if (!popup.SessionChecked)
118+
{
119+
var ex = Audio.CurrentSongPlaying.AttemptApplyLyricsToFile(lyrics);
120+
if (ex != null) DisplayError(ex);
121+
} else
122+
{
123+
Audio.CurrentSongPlaying.Lyrics = lyrics;
124+
}
105125
}
106126
break;
107127
case ContentDialogResult.Secondary:
@@ -139,30 +159,37 @@ private async Task EditLyrics(bool adding = false)
139159
{
140160
if (Audio.CurrentSongPlaying == null) return;
141161
var text = await package.GetTextAsync();
142-
Audio.CurrentSongPlaying.Lyrics = text;
143162
if (dialog.Content is AddLyricsPopup popup)
144163
{
145-
if (!popup.SessionChecked) await Audio.CurrentSongPlaying.ApplyLyricsToFileAsync();
164+
if (!popup.SessionChecked)
165+
{
166+
var ex = Audio.CurrentSongPlaying.AttemptApplyLyricsToFile(text);
167+
if (ex != null) DisplayError(ex);
168+
}
169+
else
170+
{
171+
Audio.CurrentSongPlaying.Lyrics = text;
172+
};
146173
}
147174
}
148175
break;
149176
default:
150177
break;
151178
}
152179
}
153-
private async void ClearLyrics()
180+
private void ClearLyrics()
154181
{
155182
Song currentSong = Audio.CurrentSongPlaying;
156183
if (currentSong == null) return;
157-
currentSong.Lyrics = "";
158-
await currentSong.ApplyLyricsToFileAsync();
184+
var ex = currentSong.AttemptApplyLyricsToFile("");
185+
if (ex != null) DisplayError(ex);
159186
}
160-
private async void MarkAsInstrumental()
187+
private void MarkAsInstrumental()
161188
{
162189
Song currentSong = Audio.CurrentSongPlaying;
163190
if (currentSong == null) return;
164-
currentSong.Lyrics = "[INSTRUMENTAL]";
165-
await currentSong.ApplyLyricsToFileAsync();
191+
var ex = currentSong.AttemptApplyLyricsToFile("[INSTRUMENTAL]");
192+
if (ex != null) DisplayError(ex);
166193
}
167194
private void MarkInstrumentalButton_Click(object sender, RoutedEventArgs e)
168195
{

Popups/LyricErrorPopup.xaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Page
3+
x:Class="Musium.Popups.LyricErrorPopup"
4+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:local="using:Musium.Popups"
7+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
mc:Ignorable="d">
10+
11+
<Grid>
12+
<TextBlock x:Name="Text" TextWrapping="Wrap"/>
13+
</Grid>
14+
</Page>

Popups/LyricErrorPopup.xaml.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.UI.Xaml;
2+
using Microsoft.UI.Xaml.Controls;
3+
using Microsoft.UI.Xaml.Controls.Primitives;
4+
using Microsoft.UI.Xaml.Data;
5+
using Microsoft.UI.Xaml.Input;
6+
using Microsoft.UI.Xaml.Media;
7+
using Microsoft.UI.Xaml.Navigation;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.IO;
11+
using System.Linq;
12+
using System.Runtime.InteropServices.WindowsRuntime;
13+
using Windows.Foundation;
14+
using Windows.Foundation.Collections;
15+
16+
// To learn more about WinUI, the WinUI project structure,
17+
// and more about our project templates, see: http://aka.ms/winui-project-info.
18+
19+
namespace Musium.Popups
20+
{
21+
/// <summary>
22+
/// An empty page that can be used on its own or navigated to within a Frame.
23+
/// </summary>
24+
public sealed partial class LyricErrorPopup : Page
25+
{
26+
public LyricErrorPopup()
27+
{
28+
InitializeComponent();
29+
}
30+
public void SetText(string text)
31+
{
32+
Text.Text = text;
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)