Skip to content

Commit 149d0c7

Browse files
authored
update members on CV implementation to be public (#9)
* update members on CV implementation to be public and overriden * unsafe for windows
1 parent 792b519 commit 149d0c7

File tree

5 files changed

+80
-49
lines changed

5 files changed

+80
-49
lines changed

Directory.Build.props

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<Project>
2-
<PropertyGroup>
3-
<NetVersion>net9.0</NetVersion>
4-
<MauiPackageVersion>9.0.100</MauiPackageVersion>
5-
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
<LangVersion>preview</LangVersion>
8-
<UseMaui>true</UseMaui>
9-
<SingleProject>true</SingleProject>
2+
<PropertyGroup>
3+
<NetVersion>net9.0</NetVersion>
4+
<MauiPackageVersion>9.0.100</MauiPackageVersion>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<LangVersion>preview</LangVersion>
8+
<UseMaui>true</UseMaui>
9+
<SingleProject>true</SingleProject>
1010

11-
<IsAndroid>false</IsAndroid>
12-
<IsIos>false</IsIos>
13-
<IsWindows>false</IsWindows>
11+
<IsAndroid>false</IsAndroid>
12+
<IsIos>false</IsIos>
13+
<IsWindows>false</IsWindows>
1414

15-
<IsAndroid Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">true</IsAndroid>
16-
<IsIos Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">true</IsIos>
17-
<IsWindows Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">true</IsWindows>
15+
<IsAndroid Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">true</IsAndroid>
16+
<IsIos Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">true</IsIos>
17+
<IsWindows Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">true</IsWindows>
1818

19-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
20-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
21-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
22-
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
23-
</PropertyGroup>
19+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">15.0</SupportedOSPlatformVersion>
20+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
21+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
22+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
23+
</PropertyGroup>
2424
</Project>

src/PJ.ContextActions.Maui/CollectionView/PJCollectionViewHandler.android.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,20 @@ protected override ReorderableItemsViewAdapter<ReorderableItemsView, IGroupableI
1414
}
1515

1616

17-
sealed class PJViewAdapter : ReorderableItemsViewAdapter<ReorderableItemsView, IGroupableItemsViewSource>
17+
/// <summary>
18+
/// Custom adapter for CollectionView that enables context actions (menu items) on long press.
19+
/// </summary>
20+
/// <remarks>
21+
/// This adapter extends the standard ReorderableItemsViewAdapter to provide context menu
22+
/// functionality for items in a CollectionView. It manages the creation and binding of
23+
/// menu items to collection view items, allowing for contextual actions when a user
24+
/// performs a long press on an item.
25+
/// </remarks>
26+
public class PJViewAdapter : ReorderableItemsViewAdapter<ReorderableItemsView, IGroupableItemsViewSource>
1827
{
1928
IGroupableItemsViewSource itemsSource = default!;
20-
ReorderableItemsView collectionView;
21-
internal static MenuItem[]? MenuItems;
29+
protected ReorderableItemsView collectionView;
30+
protected MenuItem[]? menuItems;
2231

2332
public PJViewAdapter(ReorderableItemsView reorderableItemsView, Func<Microsoft.Maui.Controls.View, Context, ItemContentView>? createView = null) : base(reorderableItemsView, createView)
2433
{
@@ -46,14 +55,14 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
4655
return;
4756
}
4857

49-
if (MenuItems is null)
58+
if (menuItems is null)
5059
{
51-
MenuItems = new MenuItem[contextActions.Count];
60+
menuItems = new MenuItem[contextActions.Count];
5261

5362
foreach (var (index, item) in contextActions.Index())
5463
{
5564
item.BindingContext = cv.BindingContext;
56-
MenuItems[index] = item;
65+
menuItems[index] = item;
5766
}
5867
}
5968

@@ -64,9 +73,8 @@ public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int positi
6473
if (position >= 0 && position < size)
6574
{
6675
var element = itemsSource.GetItem(position + 1);
67-
var contextMenuListener = new ItemContextMenuListener(element);
76+
var contextMenuListener = new ItemContextMenuListener(element, menuItems);
6877
holder.ItemView.SetOnCreateContextMenuListener(contextMenuListener);
69-
7078
}
7179
}
7280

@@ -81,13 +89,24 @@ public override void OnViewRecycled(Java.Lang.Object holder)
8189
}
8290
}
8391

92+
/// <summary>
93+
/// Handles the creation of context menus for items in a CollectionView on Android.
94+
/// </summary>
95+
/// <remarks>
96+
/// This class implements the Android-specific interface for creating context menus
97+
/// when a user performs a long-press on an item in the CollectionView. It associates
98+
/// the menu items with the data element that was pressed, allowing context-specific
99+
/// actions to be performed.
100+
/// </remarks>
84101
sealed class ItemContextMenuListener : Java.Lang.Object, global::Android.Views.View.IOnCreateContextMenuListener
85102
{
86103
readonly object element;
104+
readonly MenuItem[] items;
87105

88-
public ItemContextMenuListener(object element)
106+
public ItemContextMenuListener(object element, MenuItem[] items)
89107
{
90108
this.element = element;
109+
this.items = items;
91110
}
92111

93112
public void OnCreateContextMenu(IContextMenu? menu, Android.Views.View? v, IContextMenuContextMenuInfo? menuInfo)
@@ -97,14 +116,7 @@ public void OnCreateContextMenu(IContextMenu? menu, Android.Views.View? v, ICont
97116
return;
98117
}
99118

100-
if (PJViewAdapter.MenuItems is null)
101-
{
102-
return;
103-
}
104-
105-
var menuItems = PJViewAdapter.MenuItems;
106-
107-
foreach (var (index, item) in menuItems.Index())
119+
foreach (var (index, item) in items.Index())
108120
{
109121
var mItem = menu.Add(0, index + 1, index, item.Text);
110122
Assert(mItem is not null);

src/PJ.ContextActions.Maui/CollectionView/PJCollectionViewHandler.ios.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ protected override ItemsViewController<ReorderableItemsView> CreateController(Re
1515

1616
sealed class PJReorderableItemsViewController : ReorderableItemsViewController<ReorderableItemsView>
1717
{
18-
public ReorderableItemsView ReorderableItemsView { get; }
19-
2018
public PJReorderableItemsViewController(ReorderableItemsView reorderableItemsView, ItemsViewLayout layout) : base(reorderableItemsView, layout)
2119
{
22-
ReorderableItemsView = reorderableItemsView;
20+
2321
}
2422

2523
protected override UICollectionViewDelegateFlowLayout CreateDelegator()
@@ -28,10 +26,18 @@ protected override UICollectionViewDelegateFlowLayout CreateDelegator()
2826
}
2927
}
3028

31-
sealed class PJDelegator : ReorderableItemsViewDelegator<ReorderableItemsView,
32-
ReorderableItemsViewController<ReorderableItemsView>>
29+
/// <summary>
30+
/// Custom delegator for handling context menu interactions in CollectionView on iOS.
31+
/// Extends the ReorderableItemsViewDelegator to provide context menu functionality.
32+
/// </summary>
33+
/// <remarks>
34+
/// This class intercepts tap and hold gestures on collection items and displays
35+
/// a context menu with actions defined in the CollectionView's ContextActions property.
36+
/// </remarks>
37+
public class PJDelegator : ReorderableItemsViewDelegator<ReorderableItemsView, ReorderableItemsViewController<ReorderableItemsView>>
3338
{
34-
readonly ReorderableItemsViewController<ReorderableItemsView> itemsViewController;
39+
protected ReorderableItemsViewController<ReorderableItemsView> itemsViewController;
40+
List<MenuItem>? items;
3541

3642
public PJDelegator(ItemsViewLayout itemsViewLayout, ReorderableItemsViewController<ReorderableItemsView> itemsViewController) : base(itemsViewLayout, itemsViewController)
3743
{
@@ -50,7 +56,7 @@ public PJDelegator(ItemsViewLayout itemsViewLayout, ReorderableItemsViewControll
5056
var element = vc.ItemsSource[indexPath];
5157
var cv = (CollectionView)vc.ItemsView;
5258

53-
var items = ContextActions.GetContextActions(cv);
59+
items ??= ContextActions.GetContextActions(cv);
5460

5561
if (items.Count is 0)
5662
{

src/PJ.ContextActions.Maui/CollectionView/PJCollectionViewHandler.windows.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
using Microsoft.Maui.Controls.Handlers.Items;
22
using Microsoft.UI.Xaml.Controls;
3-
using Microsoft.UI.Xaml.Media.Imaging;
43
using WMenuFlyout = Microsoft.UI.Xaml.Controls.MenuFlyout;
54
using WMenuFlyoutItem = Microsoft.UI.Xaml.Controls.MenuFlyoutItem;
65

76
namespace PJ.ContextActions.Maui;
87

9-
sealed class PJCollectionViewHandler : CollectionViewHandler
8+
public class PJCollectionViewHandler : CollectionViewHandler
109
{
11-
MenuItem[]? menuItems;
10+
protected MenuItem[]? menuItems;
1211
Command<CommandBag>? mauiCommand;
1312

1413
protected override ListViewBase CreatePlatformView()
1514
{
1615
var listView = base.CreatePlatformView();
1716

18-
listView.ContainerContentChanging += OnContainerContentChanging;
17+
listView.ContainerContentChanging += OnContainerContentChangingDelegate;
1918

2019
return listView;
2120
}
2221

2322
protected override void DisconnectHandler(ListViewBase platformView)
2423
{
25-
platformView.ContainerContentChanging -= OnContainerContentChanging;
24+
platformView.ContainerContentChanging -= OnContainerContentChangingDelegate;
2625
base.DisconnectHandler(platformView);
2726
}
2827

29-
void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
28+
/// <summary>
29+
/// Handles the ContainerContentChanging event for the ListView, setting up context menus for each item.
30+
/// </summary>
31+
/// <param name="sender">The ListViewBase that triggered the event.</param>
32+
/// <param name="args">Event arguments containing information about the changed container content.</param>
33+
/// <remarks>
34+
/// This method configures a context menu (MenuFlyout) for each item in the collection view.
35+
/// It initializes menu items from the CollectionView's context actions, binds commands,
36+
/// and sets up icons if specified. The context menu appears when the user right-clicks on an item.
37+
/// </remarks>
38+
protected virtual void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
3039
{
3140
if (args.InRecycleQueue || args.ItemContainer is null)
3241
return;
@@ -74,4 +83,7 @@ void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEve
7483

7584
args.ItemContainer.ContextFlyout = menuFlyout;
7685
}
86+
87+
void OnContainerContentChangingDelegate(ListViewBase sender, ContainerContentChangingEventArgs args) =>
88+
OnContainerContentChanging(sender, args);
7789
}

src/PJ.ContextActions.Maui/PJ.ContextActions.Maui.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
88
<!-- <TargetFrameworks>$(TargetFrameworks);net9.0-tizen</TargetFrameworks> -->
99

10+
<AllowUnsafeBlocks Condition="$(IsWindows)">true</AllowUnsafeBlocks>
1011
</PropertyGroup>
1112

1213
<PropertyGroup>

0 commit comments

Comments
 (0)