AdvancedCollectionView background creation

This commit is contained in:
DismissedLight
2024-07-15 23:38:08 +08:00
parent a8065bf6e6
commit 04114fb170
14 changed files with 46 additions and 35 deletions

View File

@@ -31,9 +31,7 @@ internal sealed partial class AchievementService : IAchievementService
if (archives is null)
{
await taskContext.SwitchToBackgroundAsync();
ObservableCollection<AchievementArchive> source = achievementDbService.GetAchievementArchiveCollection();
await taskContext.SwitchToMainThreadAsync();
archives = new(source, serviceProvider);
archives = new(achievementDbService.GetAchievementArchiveCollection(), serviceProvider);
}
return archives;

View File

@@ -17,7 +17,6 @@ namespace Snap.Hutao.Service.AvatarInfo.Factory;
internal sealed partial class SummaryFactory : ISummaryFactory
{
private readonly IMetadataService metadataService;
private readonly ITaskContext taskContext;
/// <inheritdoc/>
public async ValueTask<Summary> CreateAsync(IEnumerable<Model.Entity.AvatarInfo> avatarInfos, CancellationToken token)
@@ -37,8 +36,6 @@ internal sealed partial class SummaryFactory : ISummaryFactory
IList<AvatarView> views = [.. avatars];
await taskContext.SwitchToMainThreadAsync();
return new()
{
Avatars = new(views),

View File

@@ -212,7 +212,7 @@ internal sealed partial class GachaStatisticsFactory : IGachaStatisticsFactory
return new()
{
// history
HistoryWishes = taskContext.InvokeOnMainThread(() => new AdvancedCollectionView<HistoryWish>(historyWishes)),
HistoryWishes = new(historyWishes),
// avatars
OrangeAvatars = orangeAvatarCounter.ToStatisticsList(),

View File

@@ -47,7 +47,6 @@ internal sealed partial class GachaLogService : IGachaLogService
if (await metadataService.InitializeAsync().ConfigureAwait(false))
{
context = await metadataService.GetContextAsync<GachaLogServiceMetadataContext>(token).ConfigureAwait(false);
await taskContext.SwitchToMainThreadAsync();
Archives = new(gachaLogDbService.GetGachaArchiveCollection(), serviceProvider);
return true;
}

View File

@@ -29,7 +29,7 @@ internal sealed partial class UserInitializationService : IUserInitializationSer
user.UserInfo = new() { Nickname = SH.ModelBindingUserInitializationFailed };
await taskContext.SwitchToMainThreadAsync();
user.UserGameRoles = [];
user.UserGameRoles = new([]);
}
return user;
@@ -213,7 +213,6 @@ internal sealed partial class UserInitializationService : IUserInitializationSer
if (userGameRolesResponse.IsOk())
{
await taskContext.SwitchToMainThreadAsync();
user.UserGameRoles = new(userGameRolesResponse.Data.List);
return user.UserGameRoles.Count > 0;
}

View File

@@ -28,11 +28,6 @@ internal class AdvancedCollectionView<T> : IAdvancedCollectionView<T>, INotifyPr
private int deferCounter;
private WeakEventListener<AdvancedCollectionView<T>, object?, NotifyCollectionChangedEventArgs>? sourceWeakEventListener;
public AdvancedCollectionView()
: this([])
{
}
public AdvancedCollectionView(IList<T> source)
{
view = [];
@@ -633,9 +628,8 @@ internal class AdvancedCollectionView<T> : IAdvancedCollectionView<T>, INotifyPr
return false;
}
CurrentChangingEventArgs e = new();
OnCurrentChanging(e);
if (e.Cancel)
OnCurrentChanging(out bool cancel);
if (cancel)
{
return false;
}
@@ -645,14 +639,17 @@ internal class AdvancedCollectionView<T> : IAdvancedCollectionView<T>, INotifyPr
return true;
}
private void OnCurrentChanging(CurrentChangingEventArgs e)
private void OnCurrentChanging(out bool cancel)
{
if (!created || deferCounter > 0)
{
cancel = false;
return;
}
CurrentChangingEventArgs e = new();
CurrentChanging?.Invoke(this, e);
cancel = e.Cancel;
}
private void OnCurrentChanged()

View File

@@ -1,6 +1,8 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using System.Runtime.CompilerServices;
namespace Snap.Hutao.UI.Xaml.Data;
internal static class AdvancedCollectionViewExtension
@@ -13,4 +15,11 @@ internal static class AdvancedCollectionViewExtension
view.MoveCurrentTo(default);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AdvancedCollectionView<T> ToAdvancedCollectionView<T>(this IList<T> source)
where T : class, IAdvancedCollectionViewItem
{
return new AdvancedCollectionView<T>(source);
}
}

View File

@@ -126,7 +126,7 @@ internal sealed partial class AchievementViewModel : Abstraction.ViewModel, INav
return false;
}
List<AchievementGoalView> sortedGoals;
AdvancedCollectionView<AchievementGoalView> sortedGoals;
using (await EnterCriticalSectionAsync().ConfigureAwait(false))
{
@@ -134,13 +134,13 @@ internal sealed partial class AchievementViewModel : Abstraction.ViewModel, INav
.GetAchievementGoalListAsync(CancellationToken)
.ConfigureAwait(false);
sortedGoals = goals.SortBy(goal => goal.Order).SelectList(AchievementGoalView.From);
sortedGoals = goals.SortBy(goal => goal.Order).SelectList(AchievementGoalView.From).ToAdvancedCollectionView();
}
IAdvancedDbCollectionView<EntityArchive> archives = await scopeContext.AchievementService.GetArchivesAsync(CancellationToken).ConfigureAwait(false);
await scopeContext.TaskContext.SwitchToMainThreadAsync();
AchievementGoals = new(sortedGoals);
AchievementGoals = sortedGoals;
Archives = archives;
Archives.MoveCurrentTo(Archives.SourceCollection.SelectedOrDefault());
return true;
@@ -298,8 +298,10 @@ internal sealed partial class AchievementViewModel : Abstraction.ViewModel, INav
return;
}
AdvancedCollectionView<AchievementView> achievements = new(combined);
await scopeContext.TaskContext.SwitchToMainThreadAsync();
Achievements = new(combined);
Achievements = achievements;
AchievementFinishPercent.Update(this);
UpdateAchievementsFilterByGoal(AchievementGoals?.CurrentItem);
UpdateAchievementsSort();

View File

@@ -326,12 +326,10 @@ internal sealed partial class LaunchGameViewModel : Abstraction.ViewModel, IView
{
gameAccountFilter = new(SelectedScheme?.GetSchemeType());
ObservableReorderableDbCollection<GameAccount> accounts = gameService.GameAccountCollection;
AdvancedCollectionView<GameAccount> accountsView = new(accounts) { Filter = gameAccountFilter.Filter };
await taskContext.SwitchToMainThreadAsync();
GameAccountsView = new(accounts)
{
Filter = gameAccountFilter.Filter,
};
GameAccountsView = accountsView;
}
}
}

View File

@@ -59,12 +59,10 @@ internal sealed partial class LaunchGameViewModelSlim : Abstraction.ViewModelSli
}
gameAccountFilter = new(scheme?.GetSchemeType());
AdvancedCollectionView<GameAccount> accountsView = new(accounts) { Filter = gameAccountFilter.Filter };
await taskContext.SwitchToMainThreadAsync();
GameAccountsView = new(accounts)
{
Filter = gameAccountFilter.Filter,
};
GameAccountsView = accountsView;
}
[Command("LaunchCommand")]

View File

@@ -85,8 +85,10 @@ internal sealed partial class SpiralAbyssRecordViewModel : Abstraction.ViewModel
.ConfigureAwait(false);
}
AdvancedCollectionView<SpiralAbyssView> spiralAbyssEntries = new(collection);
await taskContext.SwitchToMainThreadAsync();
SpiralAbyssEntries = new(collection);
SpiralAbyssEntries = spiralAbyssEntries;
SpiralAbyssEntries.MoveCurrentTo(SpiralAbyssEntries.SourceCollection.FirstOrDefault(s => s.Engaged));
}
catch (OperationCanceledException)

View File

@@ -109,8 +109,12 @@ internal sealed partial class WikiAvatarViewModel : Abstraction.ViewModel
using (await EnterCriticalSectionAsync().ConfigureAwait(false))
{
AdvancedCollectionView<Avatar> avatarsView = new(list);
await taskContext.SwitchToMainThreadAsync();
Avatars = new(list);
Avatars = avatarsView;
// TODO: use CurrentItem
Selected = Avatars.View.ElementAtOrDefault(0);
}

View File

@@ -68,8 +68,12 @@ internal sealed partial class WikiMonsterViewModel : Abstraction.ViewModel
using (await EnterCriticalSectionAsync().ConfigureAwait(false))
{
AdvancedCollectionView<Monster> monstersView = new(ordered);
await taskContext.SwitchToMainThreadAsync();
Monsters = new(ordered);
Monsters = monstersView;
// TODO: use CurrentItem
Selected = Monsters.View.ElementAtOrDefault(0);
}

View File

@@ -108,9 +108,13 @@ internal sealed partial class WikiWeaponViewModel : Abstraction.ViewModel
using (await EnterCriticalSectionAsync().ConfigureAwait(false))
{
AdvancedCollectionView<Weapon> weaponsView = new(list);
await taskContext.SwitchToMainThreadAsync();
Weapons = new(list);
Weapons = weaponsView;
// TODO: use CurrentItem
Selected = Weapons.View.ElementAtOrDefault(0);
}