From 59db7d968a4550130c6f5c66b90bd23547525cf7 Mon Sep 17 00:00:00 2001 From: Lightczx <1686188646@qq.com> Date: Wed, 7 Feb 2024 13:34:55 +0800 Subject: [PATCH] clear multiple user selection & ignore same background --- .../BackgroundImage/BackgroundImageService.cs | 8 +++++++- .../BackgroundImage/IBackgroundImageService.cs | 3 +-- .../Snap.Hutao/Service/User/IUserDbService.cs | 1 + .../Service/User/UserCollectionService.cs | 13 +++++++++---- .../Snap.Hutao/Service/User/UserDbService.cs | 9 +++++++++ src/Snap.Hutao/Snap.Hutao/View/MainView.xaml.cs | 4 +++- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs index abfcdfcd..e6a04881 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/BackgroundImageService.cs @@ -26,7 +26,7 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService private HashSet backgroundPathSet; - public async ValueTask> GetNextBackgroundImageAsync() + public async ValueTask> GetNextBackgroundImageAsync(BackgroundImage? previous) { HashSet backgroundSet = await SkipOrInitBackgroundAsync().ConfigureAwait(false); @@ -37,6 +37,12 @@ internal sealed partial class BackgroundImageService : IBackgroundImageService string path = System.Random.Shared.GetItems(backgroundSet.ToArray(), 1)[0]; backgroundSet.Remove(path); + + if (string.Equals(path, previous?.ImageSource.UriSource.ToString(), StringComparison.OrdinalIgnoreCase)) + { + return new(false, default!); + } + using (FileStream fileStream = File.OpenRead(path)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream.AsRandomAccessStream()); diff --git a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/IBackgroundImageService.cs b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/IBackgroundImageService.cs index 36670b10..39bc6201 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/IBackgroundImageService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/BackgroundImage/IBackgroundImageService.cs @@ -1,10 +1,9 @@ // Copyright (c) DGP Studio. All rights reserved. // Licensed under the MIT license. - namespace Snap.Hutao.Service.BackgroundImage; internal interface IBackgroundImageService { - ValueTask> GetNextBackgroundImageAsync(); + ValueTask> GetNextBackgroundImageAsync(BackgroundImage? previous); } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/User/IUserDbService.cs b/src/Snap.Hutao/Snap.Hutao/Service/User/IUserDbService.cs index b0256188..68124f71 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/User/IUserDbService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/User/IUserDbService.cs @@ -14,4 +14,5 @@ internal interface IUserDbService ValueTask> GetUserListAsync(); ValueTask UpdateUserAsync(Model.Entity.User user); + ValueTask ClearUserSelectionAsync(); } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs b/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs index 23d07405..feaa5dfd 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/User/UserCollectionService.cs @@ -41,12 +41,12 @@ internal sealed partial class UserCollectionService : IUserCollectionService, ID public async ValueTask> GetUserCollectionAsync() { // Force run in background thread, otherwise will cause reentrance - await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding); + await taskContext.SwitchToBackgroundAsync(); using (await throttler.EnterAsync().ConfigureAwait(false)) { if (userCollection is null) { - List entities = await userDbService.GetUserListAsync().ConfigureAwait(false); + List entities = await userDbService.GetUserListAsync().ConfigureAwait(false); List users = await entities.SelectListAsync(userInitializationService.ResumeUserAsync).ConfigureAwait(false); midUserMap = []; @@ -70,9 +70,14 @@ internal sealed partial class UserCollectionService : IUserCollectionService, ID { CurrentUser = users.SelectedOrDefault(); } - catch (InvalidOperationException ex) + catch (InvalidOperationException) { - ThrowHelper.UserdataCorrupted(SH.ServiceUserCurrentMultiMatched, ex); + foreach (BindingUser user in users) + { + user.IsSelected = false; + } + + await userDbService.ClearUserSelectionAsync().ConfigureAwait(false); } } } diff --git a/src/Snap.Hutao/Snap.Hutao/Service/User/UserDbService.cs b/src/Snap.Hutao/Snap.Hutao/Service/User/UserDbService.cs index c3abc869..53e401fe 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/User/UserDbService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/User/UserDbService.cs @@ -57,4 +57,13 @@ internal sealed partial class UserDbService : IUserDbService await appDbContext.Users.ExecuteDeleteAsync().ConfigureAwait(false); } } + + public async ValueTask ClearUserSelectionAsync() + { + using (IServiceScope scope = serviceProvider.CreateScope()) + { + AppDbContext appDbContext = scope.ServiceProvider.GetRequiredService(); + await appDbContext.Users.ExecuteUpdateAsync(update => update.SetProperty(user => user.IsSelected, user => false)).ConfigureAwait(false); + } + } } \ No newline at end of file diff --git a/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml.cs b/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml.cs index 06a98e47..4b4a029f 100644 --- a/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml.cs +++ b/src/Snap.Hutao/Snap.Hutao/View/MainView.xaml.cs @@ -22,6 +22,7 @@ internal sealed partial class MainView : UserControl private readonly IBackgroundImageService backgroundImageService; private TaskCompletionSource acutalThemeChangedTaskCompletionSource = new(); private CancellationTokenSource periodicTimerCancellationTokenSource = new(); + private BackgroundImage? previousBackgroundImage; /// /// 构造一个新的主视图 @@ -51,10 +52,11 @@ internal sealed partial class MainView : UserControl { do { - (bool isOk, BackgroundImage backgroundImage) = await backgroundImageService.GetNextBackgroundImageAsync().ConfigureAwait(false); + (bool isOk, BackgroundImage backgroundImage) = await backgroundImageService.GetNextBackgroundImageAsync(previousBackgroundImage).ConfigureAwait(false); if (isOk) { + previousBackgroundImage = backgroundImage; await taskContext.SwitchToMainThreadAsync(); await AnimationBuilder