Added EF and DB to the core, crude demo in the UI

This commit is contained in:
2026-08-23 20:42:42 +03:00 Unverified
parent db368674d4
commit fb8427bd0e
20 changed files with 285 additions and 36 deletions
+25
View File
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using WinWARP.Core.Models.Entities;
namespace WinWARP.Core.Data;
public class AppDbContext : DbContext
{
public DbSet<Subscription> Subscriptions => Set<Subscription>();
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Subscription>()
.HasKey(s => s.Id);
}
}
+22
View File
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace WinWARP.Core.Models.Entities;
public class Server
{
[Key]
public int Id
{
get; set;
}
public int SubscriptionId
{
get; set;
}
}
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace WinWARP.Core.Models.Entities;
public class Subscription
{
[Key]
public int Id
{
get; set;
}
[Required]
[StringLength(100)]
public string Link
{
get; set;
}
[StringLength(100)]
public string WebPage
{
get; set;
} = string.Empty;
[StringLength(100)]
public string DefaultTitle
{
get; set;
} = string.Empty;
public int DefaultUpdateInterval
{
get; set;
} = 1;
[StringLength(100)]
public string Title
{
get; set;
} = string.Empty;
public int UpdateInterval
{
get; set;
} = 1;
[StringLength(100)]
public string SupportURL
{
get; set;
} = string.Empty;
}
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using WinWARP.Core.Data;
using WinWARP.Core.Models.Entities;
using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Core.Services.Implementations;
public class SubscriptionDbService : ISubscriptionDbService
{
private IDbContextFactory<AppDbContext> _dbContextFactory;
public SubscriptionDbService(IDbContextFactory<AppDbContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
public async Task<List<Subscription>> GetSubscriptionsAsync()
{
using var dbContext = await _dbContextFactory.CreateDbContextAsync();
return await dbContext.Subscriptions.ToListAsync();
}
public async Task AddSubscriptionAsync(Subscription subscription)
{
using var dbContext = await _dbContextFactory.CreateDbContextAsync();
dbContext.Subscriptions.Add(subscription);
await dbContext.SaveChangesAsync();
}
}
@@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
namespace WinWARP.Core.Services; namespace WinWARP.Core.Services.Interfaces;
public interface ILocalizationService public interface ILocalizationService
{ {
@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Text; using System.Text;
namespace WinWARP.Core.Services; namespace WinWARP.Core.Services.Interfaces;
public interface INavigationService public interface INavigationService
{ {
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using WinWARP.Core.Models.Entities;
namespace WinWARP.Core.Services.Interfaces;
public interface ISubscriptionDbService
{
public Task<List<Subscription>> GetSubscriptionsAsync();
public Task AddSubscriptionAsync(Subscription subscription);
}
+1 -1
View File
@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using WinWARP.Core.Services; using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Core.ViewModels; namespace WinWARP.Core.ViewModels;
+1 -1
View File
@@ -4,7 +4,7 @@ using System.ComponentModel;
using System.Text; using System.Text;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using WinWARP.Core.Services; using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Core.ViewModels; namespace WinWARP.Core.ViewModels;
+1 -1
View File
@@ -4,7 +4,7 @@ using System.Collections.ObjectModel;
using System.Text; using System.Text;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using WinWARP.Core.Services; using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Core.ViewModels; namespace WinWARP.Core.ViewModels;
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using WinWARP.Core.Models.Entities;
using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Core.ViewModels;
public partial class SubsSettingsViewModel(ISubscriptionDbService dbService) : ObservableObject
{
private readonly ISubscriptionDbService _dbService = dbService;
public ObservableCollection<Subscription> Subscriptions { get; } = [];
public async Task LoadSubscriptionsAsync()
{
Subscriptions.Clear();
var data = await _dbService.GetSubscriptionsAsync();
foreach (var subscription in data)
{
Subscriptions.Add(subscription);
}
}
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(AddSubscriptionCommand))]
public partial string NewSubscriptionLink { get; set; } = string.Empty;
[RelayCommand(CanExecute = nameof(CanAddSubscription))]
public async Task AddSubscriptionAsync()
{
if (!string.IsNullOrWhiteSpace(NewSubscriptionLink))
{
var subscription = new Subscription
{
Link = NewSubscriptionLink,
Title = "New Subscription",
UpdateInterval = 60 // Default update interval
};
await _dbService.AddSubscriptionAsync(subscription);
Subscriptions.Add(subscription);
NewSubscriptionLink = string.Empty;
}
}
private bool CanAddSubscription()
{
return !string.IsNullOrWhiteSpace(NewSubscriptionLink);
}
}
@@ -3,8 +3,9 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
namespace WinWARP.Core.ViewModels; namespace WinWARP.Core.ViewModels.Wrappers;
public class SubsSettingsViewModel : ObservableObject public partial class SubscriptionViewModel : ObservableObject
{ {
} }
+5
View File
@@ -8,6 +8,11 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SingBoxLib" Version="1.2.1" /> <PackageReference Include="SingBoxLib" Version="1.2.1" />
</ItemGroup> </ItemGroup>
+43 -20
View File
@@ -4,6 +4,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
@@ -12,20 +13,22 @@ using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation; using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using Windows.ApplicationModel; using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation; using Windows.ApplicationModel.Activation;
using Windows.Foundation; using Windows.Foundation;
using Windows.Foundation.Collections; using Windows.Foundation.Collections;
using Windows.UI.ApplicationSettings; using Windows.UI.ApplicationSettings;
using WinUI3Localizer; using WinUI3Localizer;
using WinWARP.Core.Services;
using WinWARP.Core.ViewModels; using WinWARP.Core.ViewModels;
using WinWARP.Core.ViewModels.ConfigSettings; using WinWARP.Core.ViewModels.ConfigSettings;
using WinWARP.Core.ViewModels.Settings; using WinWARP.Core.ViewModels.Settings;
using WinWARP.Core.Data;
using WinWARP.Ui.Services; using WinWARP.Ui.Services;
using WinWARP.Ui.Views; using WinWARP.Ui.Views;
using WinWARP.Ui.Views.Settings; using WinWARP.Ui.Views.Settings;
using WinWARP.Core.Services.Implementations;
using WinWARP.Core.Services.Interfaces;
using System.Diagnostics;
// To learn more about WinUI, the WinUI project structure, // To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info. // and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -52,34 +55,34 @@ public partial class App : Application
{ {
InitializeComponent(); InitializeComponent();
Services = ConfigureServices(); Services = ConfigureServices();
using var context = Services.GetRequiredService<IDbContextFactory<AppDbContext>>().CreateDbContext();
context.Database.EnsureCreated();
} }
/// <summary> /// <summary>
/// Configures the services for dependency injection. /// Configures the services for dependency injection.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private static IServiceProvider ConfigureServices() private static ServiceProvider ConfigureServices()
{ {
var services = new ServiceCollection(); var services = new ServiceCollection();
var navigationService = new NavigationService(); // DB
navigationService.Configure(typeof(HomeViewModel), typeof(HomePage)); Debug.WriteLine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WinWARP", "app.db"));
navigationService.Configure(typeof(SettingsViewModel), typeof(SettingsPage)); services.AddDbContextFactory<AppDbContext>(options =>
navigationService.Configure(typeof(SubsSettingsViewModel), typeof(SubsSettingsPage)); {
navigationService.Configure(typeof(ConfigSettingsViewModel), typeof(ConfigSettingsPage)); var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WinWARP");
navigationService.Configure(typeof(GeneralSettingsViewModel), typeof(GeneralSettingsPage)); if (!Directory.Exists(folder))
navigationService.Configure(typeof(UpdateSettingsViewModel), typeof(UpdateSettingsPage)); {
Directory.CreateDirectory(folder);
}
navigationService.Configure(typeof(InboundSettingsViewModel), typeof(InboundSettingsPage)); var dbPath = Path.Combine(folder, "app.db");
navigationService.Configure(typeof(RoutingSettingsViewModel), typeof(RoutingSettingsPage)); options.UseSqlite($"Data Source={dbPath}");
navigationService.Configure(typeof(DnsSettingsViewModel), typeof(DnsSettingsPage)); });
navigationService.Configure(typeof(TlsSettingsViewModel), typeof(TlsSettingsPage)); services.AddTransient<ISubscriptionDbService, SubscriptionDbService>();
navigationService.Configure(typeof(WarpSettingsViewModel), typeof(WarpSettingsPage));
services.AddSingleton<ILocalizationService, LocalizationService>();
services.AddSingleton<INavigationService>(navigationService);
// ViewModels // ViewModels
services.AddTransient<HomeViewModel>(); services.AddTransient<HomeViewModel>();
@@ -98,6 +101,26 @@ public partial class App : Application
// Views // Views
services.AddTransient<MainWindow>(); services.AddTransient<MainWindow>();
// UI services
var navigationService = new NavigationService();
navigationService.Configure(typeof(HomeViewModel), typeof(HomePage));
navigationService.Configure(typeof(SettingsViewModel), typeof(SettingsPage));
navigationService.Configure(typeof(SubsSettingsViewModel), typeof(SubsSettingsPage));
navigationService.Configure(typeof(ConfigSettingsViewModel), typeof(ConfigSettingsPage));
navigationService.Configure(typeof(GeneralSettingsViewModel), typeof(GeneralSettingsPage));
navigationService.Configure(typeof(UpdateSettingsViewModel), typeof(UpdateSettingsPage));
navigationService.Configure(typeof(InboundSettingsViewModel), typeof(InboundSettingsPage));
navigationService.Configure(typeof(RoutingSettingsViewModel), typeof(RoutingSettingsPage));
navigationService.Configure(typeof(DnsSettingsViewModel), typeof(DnsSettingsPage));
navigationService.Configure(typeof(TlsSettingsViewModel), typeof(TlsSettingsPage));
navigationService.Configure(typeof(WarpSettingsViewModel), typeof(WarpSettingsPage));
services.AddSingleton<INavigationService>(navigationService);
services.AddSingleton<ILocalizationService, LocalizationService>();
return services.BuildServiceProvider(); return services.BuildServiceProvider();
} }
@@ -109,7 +132,7 @@ public partial class App : Application
{ {
var stringsFolderPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Strings"); var stringsFolderPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Strings");
ILocalizer localizer = await new LocalizerBuilder() var localizer = await new LocalizerBuilder()
.AddStringResourcesFolderForLanguageDictionaries(stringsFolderPath) .AddStringResourcesFolderForLanguageDictionaries(stringsFolderPath)
.SetOptions(options => .SetOptions(options =>
{ {
+1 -1
View File
@@ -14,7 +14,7 @@ using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation; using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation; using Windows.Foundation;
using Windows.Foundation.Collections; using Windows.Foundation.Collections;
using WinWARP.Core.Services; using WinWARP.Core.Services.Interfaces;
using WinWARP.Core.ViewModels; using WinWARP.Core.ViewModels;
using WinWARP.Ui.Services; using WinWARP.Ui.Services;
+1 -1
View File
@@ -3,7 +3,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using WinUI3Localizer; using WinUI3Localizer;
using WinWARP.Core.Services; using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Ui.Services; namespace WinWARP.Ui.Services;
+1 -1
View File
@@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation; using Microsoft.UI.Xaml.Navigation;
using WinWARP.Core.Services; using WinWARP.Core.Services.Interfaces;
namespace WinWARP.Ui.Services; namespace WinWARP.Ui.Services;
+15 -1
View File
@@ -6,9 +6,23 @@
xmlns:local="using:WinWARP.Ui.Views" xmlns:local="using:WinWARP.Ui.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="using:WinWARP.Core.Models.Entities"
mc:Ignorable="d"> mc:Ignorable="d">
<Grid> <Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Command="{x:Bind ViewModel.AddSubscriptionCommand}" Content="Add Subscription" />
<TextBox Grid.Row="1" Text="{x:Bind ViewModel.NewSubscriptionLink, Mode=TwoWay}" />
<ListBox Grid.Row="2" ItemsSource="{x:Bind ViewModel.Subscriptions}">
<ListBox.ItemTemplate>
<DataTemplate x:DataType="m:Subscription">
<TextBlock Text="{x:Bind Link}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid> </Grid>
</Page> </Page>
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime; using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Controls.Primitives;
@@ -12,6 +13,7 @@ using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation; using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation; using Windows.Foundation;
using Windows.Foundation.Collections; using Windows.Foundation.Collections;
using WinWARP.Core.ViewModels;
// To learn more about WinUI, the WinUI project structure, // To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info. // and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -22,8 +24,14 @@ namespace WinWARP.Ui.Views;
/// </summary> /// </summary>
public sealed partial class SubsSettingsPage : Page public sealed partial class SubsSettingsPage : Page
{ {
public SubsSettingsViewModel ViewModel { get; }
public SubsSettingsPage() public SubsSettingsPage()
{ {
InitializeComponent(); InitializeComponent();
ViewModel = App.Services.GetRequiredService<SubsSettingsViewModel>();
DataContext = ViewModel;
ViewModel.LoadSubscriptionsAsync().ConfigureAwait(false);
} }
} }
+3 -5
View File
@@ -14,6 +14,7 @@
<WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize> <WindowsAppSdkBootstrapInitialize>true</WindowsAppSdkBootstrapInitialize>
<EnableMsixTooling>true</EnableMsixTooling> <EnableMsixTooling>true</EnableMsixTooling>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Remove="Views\HomePage.xaml" /> <None Remove="Views\HomePage.xaml" />
@@ -55,12 +56,9 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.2.251219" /> <PackageReference Include="CommunityToolkit.WinUI.Behaviors" Version="8.2.251219" />
<PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.2.251219" /> <PackageReference Include="CommunityToolkit.WinUI.Controls.SettingsControls" Version="8.2.251219" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.10" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.10" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.10" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.2526" /> <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.28000.2526" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.3.1" /> <PackageReference Include="Microsoft.WindowsAppSDK" Version="2.4.0" />
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="3.53.3" />
<PackageReference Include="WinUI3Localizer" Version="2.3.0" /> <PackageReference Include="WinUI3Localizer" Version="2.3.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>