Skip to content

Commit d6d05d1

Browse files
committed
✨ init service invoke, settings status save
1 parent bc8230a commit d6d05d1

9 files changed

+105
-22
lines changed

Biyori/Biyori.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<Compile Include="Lib\WPF\MarginSetter.cs" />
204204
<Compile Include="Lib\WPF\Spacing.cs" />
205205
<Compile Include="ServiceProvider.cs" />
206+
<Compile Include="Services\Sync\SyncService.cs" />
206207
<Compile Include="Settings\Frames\SettingsPage-Accounts.xaml.cs">
207208
<DependentUpon>SettingsPage-Accounts.xaml</DependentUpon>
208209
</Compile>

Biyori/Lib/Styles/Buttons.styles.xaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@
8484
<Trigger Property="IsFocused" Value="True">
8585
</Trigger>
8686
<Trigger Property="IsEnabled" Value="False">
87-
<Setter Property="Background" Value="#ccc" />
88-
<Setter Property="BorderBrush" Value="#aaa" />
87+
<Setter Property="Opacity" Value=".65" />
8988
</Trigger>
9089

9190
</ControlTemplate.Triggers>

Biyori/ServiceProvider.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Reflection;
56
using System.Text;
@@ -53,7 +54,8 @@ public void AddOrUpdate<T>(T serviceProvider) where T : ServiceProviderBase
5354
public void ScanCurrent() => ScanAssembly(Assembly.GetEntryAssembly());
5455
public void ScanAssembly(Assembly assembly)
5556
{
56-
var providers = assembly.GetTypes().Where(x => x.GetCustomAttributes<ServiceProviderParseAttribute>().Count() > 0);
57+
var providers = assembly.GetTypes().Where(x => x.GetCustomAttributes<ServiceProviderParseAttribute>().Count() > 0)
58+
.OrderByDescending(x => x.GetCustomAttribute<ServiceProviderParseAttribute>()?.PriotizeOrderNumber);
5759
var provderInstances = providers.Select(x =>
5860
{
5961
var attr = x.GetCustomAttribute<ServiceProviderParseAttribute>();
@@ -67,10 +69,12 @@ public void ScanAssembly(Assembly assembly)
6769
this.AddOrUpdateRange(provderInstances);
6870
}
6971
}
70-
public abstract class ServiceProviderBase
72+
public class ServiceProviderBase
7173
{
72-
private void OnInitialize()
74+
public virtual void OnInitialize()
7375
{
76+
Debug.WriteLine(String.Format("[{0}] Initializing, PrioID: {1}", this.GetType().Name,
77+
this.GetType().GetCustomAttribute<ServiceProviderParseAttribute>()?.PriotizeOrderNumber ?? 0));
7478
}
7579
}
7680
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
@@ -80,7 +84,7 @@ public ServiceProviderParseAttribute(string name)
8084
{
8185
Name = name;
8286
}
83-
87+
public int PriotizeOrderNumber { get; set; } = 0;
8488
public string Name { get; set; }
8589
public bool InitializeOnStartup { get; set; } = false;
8690
}

Biyori/Services/Sync/SyncService.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Biyori.Settings.Frames;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace Biyori.Services.Sync
12+
{
13+
[ServiceProviderParse("animeSync", InitializeOnStartup = true)]
14+
public class SyncProviderService : ServiceProviderBase
15+
{
16+
private List<Thread> _threads = new List<Thread>();
17+
private string dataPath { get => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data"); }
18+
private string userPath { get => Path.Combine(this.dataPath, "user"); }
19+
private string profilePath { get => Path.Combine(this.userPath, App.ServiceProvider.GetProvider<Settings.SettingsProviderService>()?
20+
.GetConfig<AccountSettings>()?.CurrentAccount?.ProfileHash ?? "default"); }
21+
private string animeImagePath { get => Path.Combine(this.dataPath, "images"); }
22+
private string animeCoverPath { get => Path.Combine(this.dataPath, "covers"); }
23+
private string animeDbPath { get => Path.Combine(this.dataPath, "anime.db"); }
24+
public override void OnInitialize()
25+
{
26+
base.OnInitialize();
27+
Debug.WriteLine(this.profilePath);
28+
}
29+
}
30+
}

Biyori/Settings/Frames/SettingsPage-Accounts.xaml

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
Title="SettingsPage_Accounts">
1212

1313
<Grid Margin="10" DataContext="{Binding ElementName=root}">
14+
<Grid.RowDefinitions>
15+
<RowDefinition Height="*"/>
16+
<RowDefinition Height="50"/>
17+
</Grid.RowDefinitions>
1418
<TabControl>
1519
<TabItem Header="General" IsSelected="True">
1620
<Grid Margin="10">
@@ -82,5 +86,8 @@
8286
</Grid>
8387
</TabItem>
8488
</TabControl>
89+
<Grid Grid.Row="1">
90+
<Button Style="{StaticResource NavButtons}" IsEnabled="{Binding Path=DataChanged, Mode=OneWay}" Click="AccountSelection_Save" HorizontalAlignment="Right" VerticalAlignment="Center" Content="Apply Changes"></Button>
91+
</Grid>
8592
</Grid>
8693
</Page>

Biyori/Settings/Frames/SettingsPage-Accounts.xaml.cs

+15-7
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,20 @@ namespace Biyori.Settings.Frames
2626
/// </summary>
2727
public partial class SettingsPage_Accounts : Page
2828
{
29-
private SettingsProvider settingsProvider { get => App.ServiceProvider.GetProvider<SettingsProvider>(); }
29+
private SettingsProviderService settingsProvider { get => App.ServiceProvider.GetProvider<SettingsProviderService>(); }
3030
private AccountSettings accountSettings { get => this.settingsProvider.GetConfig<AccountSettings>(); }
3131
private List<AccountInfo> _accountInfo { get => this.settingsProvider.GetConfig<AccountSettings>()?.Accounts; }
3232
public List<AccountInfo> accountInfo { get; set; } = new List<AccountInfo>();
33-
[AlsoNotifyFor("selectedAccountShow")]
34-
public AccountInfo selectedAccount { get => this.accountSettings.CurrentAccount; set {
35-
var s = this.accountSettings;
36-
s.CurrentAccount = value;
37-
this.settingsProvider.UpdateConfig<AccountSettings>(s);
38-
} }
33+
[AlsoNotifyFor("selectedAccountShow", "DataChanged")]
34+
public AccountInfo selectedAccount { get; set; }
3935
public Visibility selectedAccountShow { get => this.selectedAccount != null ? Visibility.Visible : Visibility.Hidden; }
36+
public bool DataChanged { get => this.selectedAccount?.ProfileHash != this.accountSettings.CurrentAccount?.ProfileHash; }
4037
public SettingsPage_Accounts()
4138
{
4239
InitializeComponent();
4340
this.accountInfo.Clear();
4441
this.accountInfo = _accountInfo;
42+
this.selectedAccount = this.accountSettings.CurrentAccount;
4543
}
4644

4745
private void AccountSelection_Click(object sender, RoutedEventArgs e)
@@ -51,6 +49,14 @@ private void AccountSelection_Click(object sender, RoutedEventArgs e)
5149
this.selectedAccount = (sender as Button)?.DataContext as AccountInfo;
5250
}));
5351
}
52+
private void AccountSelection_Save(object sender, RoutedEventArgs e)
53+
{
54+
Dispatcher.BeginInvoke((Action)(() =>
55+
{
56+
this.accountSettings.CurrentAccount = this.selectedAccount;
57+
this.settingsProvider.UpdateConfig(this.accountSettings);
58+
}));
59+
}
5460
}
5561
[SettingsSection("account", true)]
5662
public class AccountSettings : SettingsBase
@@ -74,6 +80,8 @@ public class AccountInfo
7480
public AccountEndpoints Type { get; set; } = AccountEndpoints.Kitsu;
7581
[JsonIgnore]
7682
public string TypeString { get => this.Type.ToString(); }
83+
[JsonIgnore]
84+
public string ProfileHash { get => this.Username?.GetHashCode().ToString("X8"); }
7785
}
7886
public enum AccountEndpoints
7987
{

Biyori/Settings/SettingsProvider.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
7+
using System.Diagnostics;
78
using System.IO;
89
using System.Linq;
910
using System.Reflection;
@@ -13,14 +14,14 @@
1314
namespace Biyori.Settings
1415
{
1516
[AddINotifyPropertyChangedInterface]
16-
[ServiceProviderParse("settings", InitializeOnStartup = true)]
17-
public class SettingsProvider : ServiceProviderBase
17+
[ServiceProviderParse("settings", InitializeOnStartup = true, PriotizeOrderNumber = 999)]
18+
public class SettingsProviderService : ServiceProviderBase
1819
{
1920
private string configPath { get => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config"); }
2021
private string settingsPath { get => Path.Combine(configPath, "settings.json"); }
2122
private ConcurrentDictionary<string, SettingsBase> Settings { get; set; } = new ConcurrentDictionary<string, SettingsBase>();
2223

23-
public SettingsProvider()
24+
public SettingsProviderService()
2425
{
2526
bool initialConfig = false;
2627
if (!Directory.Exists(this.configPath))
@@ -110,6 +111,11 @@ public async Task SaveSettings()
110111
}
111112
}
112113
}
114+
115+
public override void OnInitialize()
116+
{
117+
base.OnInitialize();
118+
}
113119
}
114120
public abstract class SettingsBase { }
115121
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]

Biyori/Settings/SettingsWindow.xaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
</controls:CardControl>
4848
</Grid>
4949
<Grid Grid.Column="1" Grid.Row="2">
50-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" wpf:MarginSetter.Margin="8, 0" Margin="8, 0">
51-
<Button Style="{StaticResource NavButtons}" Click="SaveChange_Click" HorizontalAlignment="Right" VerticalAlignment="Center" Content="Save Changes"></Button>
50+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" IsEnabled="{Binding Path=ApplyChangeStatus, Mode=OneWay}" wpf:MarginSetter.Margin="0,0,4,0" Margin="8, 0">
51+
<Button x:Name="saveChange" Padding="16,6" Style="{StaticResource NavButtons}" Click="SaveChange_Click" HorizontalAlignment="Right" VerticalAlignment="Center" Content="OK"></Button>
52+
<Button x:Name="closeWindow" Padding="16,6" Style="{StaticResource NavButtons}" Click="CloseWindow_Click" HorizontalAlignment="Right" VerticalAlignment="Center" Content="Cancel"></Button>
53+
<Button x:Name="applyChange" Padding="16,6" Style="{StaticResource NavButtons}" Click="ApplyChange_Click" HorizontalAlignment="Right" VerticalAlignment="Center" Content="Apply"></Button>
5254
</StackPanel>
5355
</Grid>
5456
</Grid>

Biyori/Settings/SettingsWindow.xaml.cs

+30-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Biyori.Settings
2424
public partial class SettingsWindow : Window
2525
{
2626
public IEnumerable<SettingsRouteAttribute> SettingRoutes { get => Assembly.GetEntryAssembly().GetTypes().Where(x => x.GetCustomAttributes<SettingsRouteAttribute>().Count() > 0).Select(x => x.GetCustomAttribute<SettingsRouteAttribute>()); }
27-
public SettingsProvider settingsProvider { get; private set; }
27+
public SettingsProviderService settingsProvider { get; private set; }
2828
public SettingsWindow()
2929
{
3030
InitializeComponent();
@@ -40,7 +40,7 @@ public SettingsWindow()
4040
protected override void OnInitialized(EventArgs e)
4141
{
4242
base.OnInitialized(e);
43-
this.settingsProvider = App.ServiceProvider.GetProvider<SettingsProvider>();
43+
this.settingsProvider = App.ServiceProvider.GetProvider<SettingsProviderService>();
4444
var accountConfig = this.settingsProvider.GetConfig<AccountSettings>();
4545
if (Debugger.IsAttached && accountConfig.Accounts.Count == 0)
4646
{
@@ -89,10 +89,36 @@ private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
8989
}
9090
}
9191

92+
public bool ApplyChangeStatus { get; set; } = true;
9293
private void SaveChange_Click(object sender, RoutedEventArgs e)
9394
{
94-
this.settingsProvider.SaveSettings().Wait();
95-
this.Close();
95+
Dispatcher.BeginInvoke((Action)(() =>
96+
{
97+
this.ApplyChangeStatus = false;
98+
this.settingsProvider.SaveSettings().Wait();
99+
this.ApplyChangeStatus = true;
100+
this.Close();
101+
}));
102+
}
103+
104+
private void CloseWindow_Click(object sender, RoutedEventArgs e)
105+
{
106+
Dispatcher.BeginInvoke((Action)(() =>
107+
{
108+
this.ApplyChangeStatus = false;
109+
this.settingsProvider.LoadSettings().Wait();
110+
this.ApplyChangeStatus = true;
111+
this.Close();
112+
}));
113+
}
114+
private void ApplyChange_Click(object sender, RoutedEventArgs e)
115+
{
116+
Dispatcher.BeginInvoke((Action)(() =>
117+
{
118+
this.ApplyChangeStatus = false;
119+
this.settingsProvider.SaveSettings().Wait();
120+
this.ApplyChangeStatus = true;
121+
}));
96122
}
97123
}
98124

0 commit comments

Comments
 (0)