forked from ashpynov/PlayniteSound
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayniteSoundsSettingsViewModel.cs
More file actions
158 lines (137 loc) · 5.35 KB
/
PlayniteSoundsSettingsViewModel.cs
File metadata and controls
158 lines (137 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using Playnite.SDK;
using Playnite.SDK.Data;
using PlayniteSounds.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace PlayniteSounds
{
public class PlayniteSoundsSettingsViewModel : ObservableObject, ISettings
{
private PlayniteSoundsSettings _settings;
public PlayniteSoundsSettings Settings
{
get => _settings;
set
{
_settings = value;
OnPropertyChanged();
}
}
public RelayCommand<object> BrowseForFFmpegFile
{
get => new RelayCommand<object>((a) =>
{
var filePath = _plugin.PlayniteApi.Dialogs.SelectFile("ffmpeg|ffmpeg.exe");
if (!string.IsNullOrWhiteSpace(filePath))
{
Settings.FFmpegPath = filePath;
}
});
}
public RelayCommand<object> BrowseForFFmpegNormalizeFile
{
get => new RelayCommand<object>((a) =>
{
var filePath = _plugin.PlayniteApi.Dialogs.SelectFile(string.Empty);
if (!string.IsNullOrWhiteSpace(filePath))
{
Settings.FFmpegNormalizePath = filePath;
}
});
}
public RelayCommand<object> NavigateUrlCommand
{
get => new RelayCommand<object>((url) =>
{
_plugin.Try(() => Process.Start(new Uri(url.ToString()).AbsoluteUri));
});
}
public RelayCommand<object> BrowseForYtDlpFile
{
get => new RelayCommand<object>((a) =>
{
var filePath = _plugin.PlayniteApi.Dialogs.SelectFile("yt-dlp|yt-dlp.exe");
if (!string.IsNullOrWhiteSpace(filePath))
{
Settings.YtDlpPath = filePath;
}
});
}
private PlayniteSoundsSettings EditingClone { get; set; }
private readonly PlayniteSounds _plugin;
public PlayniteSoundsSettingsViewModel(PlayniteSounds plugin)
{
try
{
// Injecting your plugin instance is required for Save/Load method because Playnite saves data to a location based on what plugin requested the operation.
_plugin = plugin;
// Load saved settings.
var savedSettings = plugin.LoadPluginSettings<PlayniteSoundsSettings>();
// LoadPluginSettings returns null if no saved data is available.
Settings = savedSettings ?? new PlayniteSoundsSettings();
}
catch (Exception e)
{
PlayniteSounds.HandleException(e);
}
}
public void BeginEdit()
{
// Code executed when settings view is opened and user starts editing values.
try
{
EditingClone = Serialization.GetClone(Settings);
}
catch (Exception e)
{
PlayniteSounds.HandleException(e);
}
}
public void CancelEdit()
{
// Code executed when user decides to cancel any changes made since BeginEdit was called.
// This method should revert any changes made to Option1 and Option2.
Settings = EditingClone;
}
public void EndEdit()
{
// Code executed when user decides to confirm changes made since BeginEdit was called.
try
{
_plugin.SavePluginSettings(Settings);
var musicTypeChanged = Settings.MusicType != EditingClone.MusicType;
var musicStateChanged = Settings.MusicState != EditingClone.MusicState;
var DetailsMusicType = Settings.DetailsMusicType != EditingClone.DetailsMusicType;
_plugin.ReloadMusic = _plugin.ReloadMusic || musicTypeChanged || musicStateChanged || DetailsMusicType;
_plugin.UpdateDownloadManager(Settings);
_plugin.ReplayMusic();
_plugin.ResetMusicVolume();
}
catch (Exception e)
{
PlayniteSounds.HandleException(e);
}
}
public bool VerifySettings(out List<string> errors)
{
// Code execute when user decides to confirm changes made since BeginEdit was called.
// Executed before EndEdit is called and EndEdit is not called if false is returned.
// List of errors is presented to user if verification fails.
errors = new List<string>();
var outcome = true;
if (!string.IsNullOrEmpty(Settings.FFmpegPath) && !File.Exists(Settings.FFmpegPath))
{
errors.Add($"The path to FFmpeg '{Settings.FFmpegPath}' is invalid");
outcome = false;
}
if (!string.IsNullOrEmpty(Settings.YtDlpPath) && !File.Exists(Settings.YtDlpPath))
{
errors.Add($"The path to Yt-Dlp '{Settings.YtDlpPath}' is invalid");
outcome = false;
}
return outcome;
}
}
}