Skip to content

Commit a4174c7

Browse files
committed
Added the option to reorder vaults in MAUI
1 parent 8c1b3c4 commit a4174c7

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

src/Platforms/SecureFolderFS.Maui/Views/MainPage.xaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@
5555
<CollectionView
5656
Grid.Row="1"
5757
BackgroundColor="Transparent"
58+
CanReorderItems="True"
5859
IsVisible="{Binding ViewModel.VaultListViewModel.HasVaults, Mode=OneWay}"
5960
ItemsSource="{Binding ViewModel.VaultListViewModel.Items, Mode=OneWay}"
61+
ReorderCompleted="ItemsCollection_ReorderCompleted"
6062
SelectionMode="None">
6163
<CollectionView.ItemTemplate>
6264
<DataTemplate x:DataType="vm:VaultListItemViewModel">

src/Platforms/SecureFolderFS.Maui/Views/MainPage.xaml.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ protected override void OnAppearing()
4545

4646
base.OnAppearing();
4747
}
48+
49+
private async void ItemsCollection_ReorderCompleted(object? sender, EventArgs e)
50+
{
51+
if (ViewModel is null)
52+
return;
53+
54+
await ViewModel.VaultListViewModel.SyncOrderAsync();
55+
}
4856

4957
private async Task ItemTappedAsync(VaultListItemViewModel itemViewModel, View? view)
5058
{

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/VaultList/VaultListItemViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private async Task MoveItemAsync(string? direction, CancellationToken cancellati
7676
UpdateCanMove();
7777

7878
// Save after move
79-
await _vaultCollectionModel.SaveAsync(cancellationToken);
79+
await _vaultCollectionModel.TrySaveAsync(cancellationToken);
8080
}
8181

8282
[RelayCommand]

src/Sdk/SecureFolderFS.Sdk/ViewModels/Controls/VaultList/VaultListViewModel.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace SecureFolderFS.Sdk.ViewModels.Controls.VaultList
2828
public sealed partial class VaultListViewModel : ObservableObject, IAsyncInitialize
2929
{
3030
private readonly IVaultCollectionModel _vaultCollectionModel;
31+
private bool _ignoreCollectionChanged;
3132

3233
[ObservableProperty] private bool _HasVaults;
3334
[ObservableProperty] private VaultListItemViewModel? _SelectedItem;
@@ -63,6 +64,33 @@ public Task InitAsync(CancellationToken cancellationToken = default)
6364
return Task.CompletedTask;
6465
}
6566

67+
/// <summary>
68+
/// Syncs the order of the vaults with the internal model.
69+
/// </summary>
70+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that cancels this action.</param>
71+
/// <returns>A <see cref="Task"/> that represents the asynchronous operation.</returns>
72+
public async Task SyncOrderAsync(CancellationToken cancellationToken = default)
73+
{
74+
try
75+
{
76+
_ignoreCollectionChanged = true;
77+
foreach (var item in Items)
78+
{
79+
var currentIndex = _vaultCollectionModel.IndexOf(item.VaultViewModel.VaultModel);
80+
var targetIndex = Items.IndexOf(item);
81+
82+
if (currentIndex != targetIndex)
83+
_vaultCollectionModel.Move(currentIndex, targetIndex);
84+
}
85+
86+
await _vaultCollectionModel.TrySaveAsync(cancellationToken);
87+
}
88+
finally
89+
{
90+
_ignoreCollectionChanged = false;
91+
}
92+
}
93+
6694
[RelayCommand(AllowConcurrentExecutions = true)]
6795
private async Task AddNewVaultAsync(IFolder? folder, CancellationToken cancellationToken)
6896
{
@@ -135,6 +163,9 @@ private void RemoveVault(IVaultModel vaultModel)
135163

136164
private void VaultCollectionModel_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
137165
{
166+
if (_ignoreCollectionChanged)
167+
return;
168+
138169
switch (e.Action)
139170
{
140171
case NotifyCollectionChangedAction.Add when e.NewItems is not null && e.NewItems[0] is IVaultModel vaultModel:

0 commit comments

Comments
 (0)