Skip to content

Commit

Permalink
fix #1791 #1794
Browse files Browse the repository at this point in the history
  • Loading branch information
Lightczx committed Jul 6, 2024
1 parent 497a5fb commit 72a1ec2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
22 changes: 22 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Core/Threading/VolatileWrite.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public async ValueTask<bool> RefreshGachaLogAsync(GachaLogQuery query, RefreshSt

if (target is not null && Archives is not null)
{
await taskContext.SwitchToMainThreadAsync();
Archives.CurrentItem = target;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public async ValueTask ImportAsync(GachaLogServiceMetadataContext context, UIGF
}

gachaLogDbService.AddGachaItemRange(fullItems);

await taskContext.SwitchToMainThreadAsync();
archives.MoveCurrentTo(archive);
}

Expand Down
42 changes: 33 additions & 9 deletions src/Snap.Hutao/Snap.Hutao/ViewModel/GachaLog/GachaLogViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -119,25 +120,30 @@ protected override void UninitializeOverride()

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)
Expand Down Expand Up @@ -184,7 +190,16 @@ private async ValueTask RefreshCoreAsync(RefreshOption option)
{
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)
{
Expand Down Expand Up @@ -339,7 +354,7 @@ private async ValueTask UpdateStatisticsAsync(GachaArchive? archive)

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;
Expand All @@ -357,7 +372,16 @@ private async ValueTask<bool> TryImportUIGFInternalAsync(UIGF uigf)
{
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)
Expand Down

0 comments on commit 72a1ec2

Please sign in to comment.