mirror of
https://jihulab.com/DGP-Studio/Snap.Hutao.git
synced 2025-11-19 21:02:53 +08:00
22
src/Snap.Hutao/Snap.Hutao/Core/Threading/VolatileWrite.cs
Normal file
22
src/Snap.Hutao/Snap.Hutao/Core/Threading/VolatileWrite.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (c) DGP Studio. All rights reserved.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
namespace Snap.Hutao.Core.Threading;
|
||||
|
||||
internal readonly ref struct VolatileWrite
|
||||
{
|
||||
private readonly ref bool reference;
|
||||
private readonly bool initialState;
|
||||
|
||||
public VolatileWrite(ref bool reference, bool initialState)
|
||||
{
|
||||
this.reference = ref reference;
|
||||
this.initialState = initialState;
|
||||
Volatile.Write(ref this.reference, initialState);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Volatile.Write(ref reference, !initialState);
|
||||
}
|
||||
}
|
||||
@@ -109,6 +109,7 @@ internal sealed partial class GachaLogService : IGachaLogService
|
||||
|
||||
if (target is not null && Archives is not null)
|
||||
{
|
||||
await taskContext.SwitchToMainThreadAsync();
|
||||
Archives.CurrentItem = target;
|
||||
}
|
||||
|
||||
|
||||
@@ -83,6 +83,8 @@ internal sealed partial class UIGFImportService : IUIGFImportService
|
||||
}
|
||||
|
||||
gachaLogDbService.AddGachaItemRange(fullItems);
|
||||
|
||||
await taskContext.SwitchToMainThreadAsync();
|
||||
archives.MoveCurrentTo(archive);
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ internal sealed partial class GachaLogViewModel : Abstraction.ViewModel
|
||||
private AdvancedDbCollectionView<GachaArchive>? archives;
|
||||
private GachaStatistics? statistics;
|
||||
private bool isAggressiveRefresh;
|
||||
private bool suppressCurrentItemChangedHandling;
|
||||
|
||||
public AdvancedDbCollectionView<GachaArchive>? Archives
|
||||
{
|
||||
@@ -119,25 +120,30 @@ internal sealed partial class GachaLogViewModel : Abstraction.ViewModel
|
||||
|
||||
private void OnCurrentArchiveChanged(object? sender, object? e)
|
||||
{
|
||||
if (suppressCurrentItemChangedHandling)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateStatisticsAsync(Archives?.CurrentItem).SafeForget(logger);
|
||||
}
|
||||
|
||||
[Command("RefreshByWebCacheCommand")]
|
||||
private Task RefreshByWebCacheAsync()
|
||||
private async Task RefreshByWebCacheAsync()
|
||||
{
|
||||
return RefreshCoreAsync(RefreshOption.WebCache).AsTask();
|
||||
await RefreshCoreAsync(RefreshOption.WebCache).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Command("RefreshBySTokenCommand")]
|
||||
private Task RefreshBySTokenAsync()
|
||||
private async Task RefreshBySTokenAsync()
|
||||
{
|
||||
return RefreshCoreAsync(RefreshOption.SToken).AsTask();
|
||||
await RefreshCoreAsync(RefreshOption.SToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[Command("RefreshByManualInputCommand")]
|
||||
private Task RefreshByManualInputAsync()
|
||||
private async Task RefreshByManualInputAsync()
|
||||
{
|
||||
return RefreshCoreAsync(RefreshOption.ManualInput).AsTask();
|
||||
await RefreshCoreAsync(RefreshOption.ManualInput).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async ValueTask RefreshCoreAsync(RefreshOption option)
|
||||
@@ -184,7 +190,16 @@ internal sealed partial class GachaLogViewModel : Abstraction.ViewModel
|
||||
{
|
||||
try
|
||||
{
|
||||
authkeyValid = await gachaLogService.RefreshGachaLogAsync(query, strategy, progress, CancellationToken).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
suppressCurrentItemChangedHandling = true;
|
||||
authkeyValid = await gachaLogService.RefreshGachaLogAsync(query, strategy, progress, CancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
suppressCurrentItemChangedHandling = false;
|
||||
await UpdateStatisticsAsync(Archives?.CurrentItem).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (HutaoException ex)
|
||||
{
|
||||
@@ -339,7 +354,7 @@ internal sealed partial class GachaLogViewModel : Abstraction.ViewModel
|
||||
|
||||
private async ValueTask<bool> TryImportUIGFInternalAsync(UIGF uigf)
|
||||
{
|
||||
if (!uigf.IsCurrentVersionSupported(out UIGFVersion version))
|
||||
if (!uigf.IsCurrentVersionSupported(out _))
|
||||
{
|
||||
infoBarService.Warning(SH.ViewModelGachaLogImportWarningTitle, SH.ViewModelGachaLogImportWarningMessage);
|
||||
return false;
|
||||
@@ -357,7 +372,16 @@ internal sealed partial class GachaLogViewModel : Abstraction.ViewModel
|
||||
{
|
||||
using (await dialog.BlockAsync(taskContext).ConfigureAwait(false))
|
||||
{
|
||||
await gachaLogService.ImportFromUIGFAsync(uigf).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
suppressCurrentItemChangedHandling = true;
|
||||
await gachaLogService.ImportFromUIGFAsync(uigf).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
suppressCurrentItemChangedHandling = false;
|
||||
await UpdateStatisticsAsync(Archives?.CurrentItem).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
|
||||
Reference in New Issue
Block a user