Compare commits

...

1 Commits

Author SHA1 Message Date
qhy040404
ceb26bb669 impl #1334 2024-02-01 22:19:35 +08:00
10 changed files with 72 additions and 1 deletions

View File

@@ -14,7 +14,7 @@ namespace Snap.Hutao.Model.Entity;
/// </summary>
[HighQuality]
[Table("game_accounts")]
internal sealed class GameAccount : ObservableObject, IMappingFrom<GameAccount, string, string, SchemeType>
internal sealed class GameAccount : ObservableObject, IMappingFrom<GameAccount, string, string, SchemeType>, ICloneable<GameAccount>
{
/// <summary>
/// 内部Id
@@ -54,6 +54,17 @@ internal sealed class GameAccount : ObservableObject, IMappingFrom<GameAccount,
};
}
public GameAccount Clone()
{
return new()
{
AttachUid = AttachUid,
Name = Name,
MihoyoSDK = MihoyoSDK,
Type = Type,
};
}
/// <summary>
/// 更新绑定的Uid
/// </summary>

View File

@@ -120,6 +120,11 @@ internal sealed partial class GameAccountService : IGameAccountService
await gameDbService.RemoveGameAccountByIdAsync(gameAccount.InnerId).ConfigureAwait(false);
}
public void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts)
{
gameDbService.ReorderGameAccounts(reorderedGameAccounts);
}
private static GameAccount? SingleGameAccountOrDefault(ObservableCollection<GameAccount> gameAccounts, string registrySdk)
{
try

View File

@@ -22,4 +22,6 @@ internal interface IGameAccountService
ValueTask RemoveGameAccountAsync(GameAccount gameAccount);
bool SetGameAccount(GameAccount account);
void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts);
}

View File

@@ -59,4 +59,19 @@ internal sealed partial class GameDbService : IGameDbService
await appDbContext.GameAccounts.ExecuteDeleteWhereAsync(a => a.InnerId == id).ConfigureAwait(false);
}
}
public void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts)
{
using (IServiceScope scope = serviceProvider.CreateScope())
{
AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
appDbContext.GameAccounts.ExecuteDelete();
// Use foreach to avoid ORDER BY InnerId
foreach (GameAccount gameAccount in reorderedGameAccounts)
{
appDbContext.GameAccounts.AddAndSave(gameAccount);
}
}
}
}

View File

@@ -76,4 +76,9 @@ internal sealed partial class GameServiceFacade : IGameServiceFacade
{
return LaunchExecutionEnsureGameNotRunningHandler.IsGameRunning(out _);
}
public void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts)
{
gameAccountService.ReorderGameAccounts(reorderedGameAccounts);
}
}

View File

@@ -17,4 +17,6 @@ internal interface IGameDbService
void UpdateGameAccount(GameAccount gameAccount);
ValueTask UpdateGameAccountAsync(GameAccount gameAccount);
void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts);
}

View File

@@ -62,4 +62,6 @@ internal interface IGameServiceFacade
ValueTask RemoveGameAccountAsync(GameAccount gameAccount);
GameAccount? DetectCurrentGameAccount(SchemeType scheme);
void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts);
}

View File

@@ -202,6 +202,10 @@
IsClickEnabled="True"/>
<Border Padding="0,1" Style="{StaticResource BorderCardStyle}">
<ListView
AllowDrop="True"
CanDragItems="True"
CanReorderItems="True"
DragItemsCompleted="OnAccountListViewReordered"
ItemTemplate="{StaticResource GameAccountListTemplate}"
ItemsSource="{Binding GameAccountsView}"
SelectedItem="{Binding SelectedGameAccount, Mode=TwoWay}"/>

View File

@@ -1,8 +1,12 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using CommunityToolkit.WinUI.Collections;
using Microsoft.UI.Xaml.Controls;
using Snap.Hutao.Control;
using Snap.Hutao.Model.Entity;
using Snap.Hutao.ViewModel.Game;
using System.Collections.ObjectModel;
namespace Snap.Hutao.View.Page;
@@ -20,4 +24,20 @@ internal sealed partial class LaunchGamePage : ScopedPage
InitializeWith<LaunchGameViewModel>();
InitializeComponent();
}
public void OnAccountListViewReordered(object sender, DragItemsCompletedEventArgs e)
{
ListView listView = (ListView)sender;
AdvancedCollectionView advancedCollectionView = (AdvancedCollectionView)listView.ItemsSource;
ObservableCollection<GameAccount> gameAccounts = (ObservableCollection<GameAccount>)advancedCollectionView.Source;
List<GameAccount> newGameAccounts = [];
foreach (GameAccount gameAccount in gameAccounts)
{
newGameAccounts.Add(gameAccount.Clone());
}
((LaunchGameViewModel)DataContext).ReorderGameAccounts(newGameAccounts);
}
}

View File

@@ -172,6 +172,11 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView
SelectedGamePathEntry = selectedEntry;
}
public void ReorderGameAccounts(IEnumerable<GameAccount> reorderedGameAccounts)
{
gameService.ReorderGameAccounts(reorderedGameAccounts);
}
protected override ValueTask<bool> InitializeUIAsync()
{
ImmutableList<GamePathEntry> gamePathEntries = launchOptions.GetGamePathEntries(out GamePathEntry? entry);