Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] ListView ContextActions in modal page #26854

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Android.Widget;
using AndroidX.AppCompat.App;
using Microsoft.Maui.Controls.Platform;
using static Microsoft.Maui.Controls.Platform.ModalNavigationManager;
using AActionMode = global::AndroidX.AppCompat.View.ActionMode;
using AListView = Android.Widget.ListView;
using AMenu = Android.Views.IMenu;
Expand Down Expand Up @@ -226,6 +227,10 @@ bool HandleContextMode(AView view, int position)
if (cell == null)
return false;

var fragmentManager = view?.Context?.GetActivity()?.GetFragmentManager();
if (fragmentManager.Fragments.Count > 1 && fragmentManager.Fragments[^1] is ModalFragment modalFragment)
return modalFragment.HandleContextMode(cell);

if (_actionMode != null || _supportActionMode != null)
{
if (!cell.HasContextActions)
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/Menu/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public bool IsEnabled

protected virtual void OnClicked() => Clicked?.Invoke(this, EventArgs.Empty);

internal void RaiseClickedEvent() => OnClicked();

void IMenuItemController.Activate()
{
if (IsEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using AAnimation = Android.Views.Animations.Animation;
using AColor = Android.Graphics.Color;
using AView = Android.Views.View;
using AResource = Android.Resource;

namespace Microsoft.Maui.Controls.Platform
{
Expand Down Expand Up @@ -239,6 +240,29 @@ internal class ModalFragment : DialogFragment

public bool IsAnimated { get; internal set; }

internal bool HandleContextMode(Cell cell)
{
var contextActions = cell.ContextActions;
if (contextActions == null || contextActions.Count == 0)
return false;

var cancelText = _mauiWindowContext?.Context?.GetString(AResource.String.Cancel);
var actionItems = contextActions.ToDictionary(action => action.Text, action => action);
_modal.DisplayActionSheet(string.Empty, cancelText, null, contextActions.Where(a => a.IsEnabled).Select(a => a.Text).ToArray())
.ContinueWith(task =>
{
if (task.Result != null && actionItems.TryGetValue(task.Result, out var menuItem))
{
if (menuItem.Command is not null)
menuItem.Command.Execute(menuItem.CommandParameter);

menuItem.RaiseClickedEvent();
}
});

return true;
}

public ModalFragment(IMauiContext mauiContext, Page modal)
{
_modal = modal;
Expand Down
Loading