-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShareniteSettings.cs
131 lines (120 loc) · 4.53 KB
/
ShareniteSettings.cs
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
using Playnite.SDK;
using Playnite.SDK.Data;
using Sharenite.Services;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sharenite
{
public class ShareniteSettings : ObservableObject
{
public bool keepInSync = true;
public bool showProgress = true;
public bool showErrors = true;
public bool KeepInSync { get => keepInSync; set => SetValue(ref keepInSync, value); }
public bool ShowProgress { get => showProgress; set => SetValue(ref showProgress, value); }
public bool ShowErrors { get => showErrors; set => SetValue(ref showErrors, value); }
// Playnite serializes settings object to a JSON object and saves it as text file.
// If you want to exclude some property from being saved then use `JsonDontSerialize` ignore attribute.
//[DontSerialize]
//public bool OptionThatWontBeSaved { get => optionThatWontBeSaved; set => SetValue(ref optionThatWontBeSaved, value); }
}
public class ShareniteSettingsViewModel : ObservableObject, ISettings
{
private static ILogger logger = LogManager.GetLogger();
private readonly Sharenite plugin;
private readonly IPlayniteAPI api;
private ShareniteSettings editingClone { get; set; }
private ShareniteSettings settings;
private ShareniteAccountClient clientApi;
public bool IsUserLoggedIn
{
get
{
try
{
clientApi.CheckAuthentication().GetAwaiter().GetResult();
return true;
}
catch
{
return false;
}
}
}
private void Login()
{
try
{
clientApi.Login();
OnPropertyChanged(nameof(IsUserLoggedIn));
}
catch (Exception e) when (!Debugger.IsAttached)
{
logger.Error(e, "Failed to authenticate user.");
}
}
public ShareniteSettings Settings
{
get => settings;
set
{
settings = value;
OnPropertyChanged();
}
}
public RelayCommand<object> LoginCommand
{
get => new RelayCommand<object>((a) =>
{
Login();
});
}
public ShareniteSettingsViewModel(Sharenite plugin, IPlayniteAPI api)
{
// Injecting your plugin instance is required for Save/Load method because Playnite saves data to a location based on what plugin requested the operation.
this.plugin = plugin;
this.api = api;
clientApi = new ShareniteAccountClient(plugin, api);
// Load saved settings.
var savedSettings = plugin.LoadPluginSettings<ShareniteSettings>();
// LoadPluginSettings returns null if not saved data is available.
if (savedSettings != null)
{
Settings = savedSettings;
}
else
{
Settings = new ShareniteSettings();
}
}
public void BeginEdit()
{
// Code executed when settings view is opened and user starts editing values.
editingClone = Serialization.GetClone(Settings);
}
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.
// This method should save settings made to Option1 and Option2.
plugin.SavePluginSettings(Settings);
}
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>();
return true;
}
}
}