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

Fix Expander in CollectionView on Apple #751

Merged
merged 28 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9729ec6
Fix Expander in CollectionView on Apple
VladislavAntonyuk Nov 10, 2022
66f3fd5
Merge branch 'main' into expander-collectionview
brminnick Nov 15, 2022
e7a436d
Merge branch 'main' into expander-collectionview
brminnick Nov 23, 2022
dd2d892
Merge branch 'main' into expander-collectionview
VladislavAntonyuk Dec 1, 2022
e8093d8
Merge branch 'main' into expander-collectionview
VladislavAntonyuk Dec 1, 2022
9878ae6
Update Windows CollectionView
VladislavAntonyuk Dec 4, 2022
0540f50
Merge branch 'main' into expander-collectionview
VladislavAntonyuk Dec 4, 2022
15bbbaa
Merge branch 'main' into expander-collectionview
jfversluis Dec 19, 2022
00b56d0
Merge branch 'main' into expander-collectionview
VladislavAntonyuk Dec 21, 2022
d01fde4
Remove Task Delay, that didnt always work
VladislavAntonyuk Dec 26, 2022
6aef561
Extract platform code
VladislavAntonyuk Dec 26, 2022
f30368a
Merge branch 'main' into expander-collectionview
pictos Jan 11, 2023
962af1d
Alternative to #751 (#890)
pictos Jan 12, 2023
6518fb3
Merge branch 'main' into expander-collectionview
VladislavAntonyuk Jan 12, 2023
2612191
Replace `public static Action<T>` with `public virtual void OnHeaderT…
brminnick Jan 12, 2023
736da1d
Revert `OnHeaderTapped` to `private`, Add `Action<TappedEventArgs>`
brminnick Jan 13, 2023
44f8367
Merge branch 'main' into expander-collectionview
brminnick Jan 13, 2023
1eb1e7a
Add `HeaderTapGestureRecognizer` + `HandleHeaderTapped ` Unit Tests
brminnick Jan 13, 2023
dc4d115
`dotnet format`
brminnick Jan 13, 2023
7c412fc
Improve Expander Sample Page Formatting
brminnick Jan 14, 2023
3348750
Merge branch 'main' into expander-collectionview
brminnick Jan 14, 2023
54195c0
Update src/CommunityToolkit.Maui.UnitTests/Views/Expander/ExpanderTes…
VladislavAntonyuk Jan 14, 2023
c963b40
Merge branch 'main' into expander-collectionview
brminnick Jan 18, 2023
0a66e54
Merge branch 'main' into expander-collectionview
brminnick Jan 29, 2023
c686a3a
Update Expander to avoid reflection
VladislavAntonyuk Feb 1, 2023
1cca8a4
Merge branch 'main' into expander-collectionview
brminnick Feb 1, 2023
0c2524f
Fix Windows Expander in CollectionView
VladislavAntonyuk Feb 1, 2023
ecfe517
Merge branch 'expander-collectionview' of https://github.com/Communit…
VladislavAntonyuk Feb 1, 2023
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
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ variables:
PathToCommunityToolkitAnalyzersCodeFixCsproj: 'src/CommunityToolkit.Maui.Analyzers.CodeFixes/CommunityToolkit.Maui.Analyzers.CodeFixes.csproj'
PathToCommunityToolkitMediaElementAnalyzersCodeFixCsproj: 'src/CommunityToolkit.Maui.MediaElement.Analyzers.CodeFixes/CommunityToolkit.Maui.MediaElement.Analyzers.CodeFixes.csproj'
PathToCommunityToolkitAnalyzersUnitTestCsproj: 'src/CommunityToolkit.Maui.Analyzers.UnitTests/CommunityToolkit.Maui.Analyzers.UnitTests.csproj'
DotNetMauiRollbackFile: 'https://maui.blob.core.windows.net/metadata/rollbacks/7.0.49.json'
DotNetMauiRollbackFile: 'https://maui.blob.core.windows.net/metadata/rollbacks/7.0.59.json'
CommunityToolkitSampleApp_Xcode_Version: '14.2.0'
CommunityToolkitLibrary_Xcode_Version: '14.0.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
xmlns:viewModels="clr-namespace:CommunityToolkit.Maui.Sample.ViewModels.Views"
x:TypeArguments="viewModels:ExpanderViewModel"
x:DataType="viewModels:ExpanderViewModel"
Padding="12,0"
Title="Expander">

<ScrollView>
Expand All @@ -21,7 +22,7 @@
</mct:Expander.Header>

<mct:Expander.Content>
<VerticalStackLayout BackgroundColor="LightGray">
<VerticalStackLayout>
<Label Text="Item 1"/>
<Label Text="Item 2"/>
</VerticalStackLayout>
Expand All @@ -45,6 +46,55 @@
</mct:Expander>
</mct:Expander.Content>
</mct:Expander>

<Label Text="Expander in ListView" FontSize="24" FontAttributes="Bold"/>

<ListView ItemsSource="{Binding ContentCreators}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<mct:Expander x:DataType="viewModels:ContentCreator"
ExpandedChanged="Expander_ExpandedChanged">
<mct:Expander.Header>
<Label Text="{Binding Name}"/>
</mct:Expander.Header>
<mct:Expander.Content>
<VerticalStackLayout>
<Label Text="{Binding Resource}"/>
<Image Source="{Binding Image}"
WidthRequest="100"
HeightRequest="100"/>
</VerticalStackLayout>
</mct:Expander.Content>
</mct:Expander>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>

<Label Text="Expander in CollectionView" FontSize="24" FontAttributes="Bold"/>

<CollectionView ItemsSource="{Binding ContentCreators}">
<CollectionView.ItemTemplate>
<DataTemplate>
<mct:Expander x:DataType="viewModels:ContentCreator"
ExpandedChanged="Expander_ExpandedChanged">
<mct:Expander.Header>
<Label Text="{Binding Name}"/>
</mct:Expander.Header>
<mct:Expander.Content>
<VerticalStackLayout>
<Label Text="{Binding Resource}" HorizontalOptions="Center"/>
<Image Source="{Binding Image}"
WidthRequest="100"
HeightRequest="100"/>
</VerticalStackLayout>
</mct:Expander.Content>
</mct:Expander>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</VerticalStackLayout>
</ScrollView>
</pages:BasePage>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ExpanderPageCS()
new Label()
.Text("Expander C# Sample")
.Font(bold: true, size: 24)
.CenterHorizontal(),
.CenterHorizontal().TextCenter(),

new Picker() { ItemsSource = Enum.GetValues<ExpandDirection>(), Title = "Direction" }
.CenterHorizontal().TextCenter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public ExpanderViewModel()
{
ContentCreators.Add(new ContentCreator("Brandon Minnick", "https://codetraveler.io/", "https://avatars.githubusercontent.com/u/13558917"));
ContentCreators.Add(new ContentCreator("Gerald Versluis", "https://blog.verslu.is/", "https://avatars.githubusercontent.com/u/939291"));
ContentCreators.Add(new ContentCreator("Jay Cho", "https://github.com/JoonghyunCho", "https://avatars.githubusercontent.com/u/14328614"));
ContentCreators.Add(new ContentCreator("Kym Phillpotts", "https://kymphillpotts.com", "https://avatars.githubusercontent.com/u/1327346"));
ContentCreators.Add(new ContentCreator("Pedro Jesus", "https://github.com/pictos", "https://avatars.githubusercontent.com/u/20712372"));
ContentCreators.Add(new ContentCreator("Shaun Lawrence", "https://github.com/bijington", "https://avatars.githubusercontent.com/u/17139988"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace CommunityToolkit.Maui.UnitTests.ImageSources.GravatarImageSource;

using CommunityToolkit.Maui.ImageSources;
using CommunityToolkit.Maui.ImageSources;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.ImageSources;

public class GravatarImageSourceTests : BaseHandlerTest
{
readonly TimeSpan cacheValidity = new(1, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Maui.Controls.Shapes;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.AvatarView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class AvatarViewImageTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.AvatarView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class AvatarViewInterfaceTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Maui.Controls.Shapes;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.AvatarView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class AvatarViewTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class DrawingLineAdapterTests : BaseTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class DrawingLineCompletedEventArgsTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class DrawingLineTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Xunit;
using Xunit.Abstractions;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class DrawingViewTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class MauiDrawingLineCompletedEventArgsTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class MauiDrawingLineTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class MauiDrawingViewExtensionsTests : BaseTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.DrawingView;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class MauiDrawingViewTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.Expander;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class ExpandedChangedEventArgsTests : BaseHandlerTest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.ComponentModel;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.UnitTests.Mocks;
using FluentAssertions;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.Expander;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class ExpanderTests : BaseHandlerTest
{
Expand All @@ -13,7 +12,7 @@ public class ExpanderTests : BaseHandlerTest
[Fact]
public void ExpanderShouldBeAssignedToIExpander()
{
new Maui.Views.Expander().Should().BeAssignableTo<IExpander>();
expander.Should().BeAssignableTo<IExpander>();
}

[Fact]
Expand Down Expand Up @@ -62,4 +61,53 @@ public void ExpanderDirectionThrowsInvalidEnumArgumentException(ExpandDirection
{
Assert.Throws<InvalidEnumArgumentException>(() => expander.Direction = direction);
}

[Fact]
public void EnsureExpandedChanged()
{
var isExpanded_Initial = expander.IsExpanded;

var header = new View();
expander.Header = header;

expander.HeaderTapGestureRecognizer.SendTapped(header);
var isExpanded_Final = expander.IsExpanded;

Assert.True(isExpanded_Final);
Assert.False(isExpanded_Initial);
Assert.NotEqual(isExpanded_Initial, isExpanded_Final);

expander.HeaderTapGestureRecognizer.SendTapped(header);

Assert.False(expander.IsExpanded);
}

[Fact]
public void EnsureHandleHeaderTappedExecutesWhenHeaderTapped()
{
int handleHeaderTappedCount = 0;
bool didHandleHeaderTappedExecute = false;

var header = new View();
expander.Header = new View();
expander.HandleHeaderTapped = HandleHeaderTapped;

expander.HeaderTapGestureRecognizer.SendTapped(header);

Assert.True(didHandleHeaderTappedExecute);
Assert.Equal(1, handleHeaderTappedCount);

expander.HandleHeaderTapped = null;

expander.HeaderTapGestureRecognizer.SendTapped(header);

Assert.True(didHandleHeaderTappedExecute);
Assert.Equal(1, handleHeaderTappedCount);

void HandleHeaderTapped(TappedEventArgs tappedEventArgs)
{
handleHeaderTappedCount++;
didHandleHeaderTappedExecute = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using CommunityToolkit.Maui.Views;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests.Views.Popup;
namespace CommunityToolkit.Maui.UnitTests.Views;

public class PopupTests : BaseHandlerTest
{
Expand Down
55 changes: 55 additions & 0 deletions src/CommunityToolkit.Maui/Views/Expander/Expander.macios.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using CoreGraphics;
using UIKit;

namespace CommunityToolkit.Maui.Views;

public partial class Expander
{
static void ForceUpdateCellSize(CollectionView collectionView, Size size, Point? tapLocation)
{
if (tapLocation is null)
{
return;
}

var controller = GetController(collectionView);

if (controller?.CollectionView.CollectionViewLayout is UICollectionViewFlowLayout layout)
{
var cells = layout.CollectionView.VisibleCells.OrderBy(x => x.Frame.Y).ToArray();
var clickedCell = GetCellByPoint(cells, new CGPoint(tapLocation.Value.X, tapLocation.Value.Y));
if (clickedCell is null)
{
return;
}

for (int i = 0; i < cells.Length; i++)
{
var cell = cells[i];

if (i > 0)
pictos marked this conversation as resolved.
Show resolved Hide resolved
{
var prevCellFrame = cells[i - 1].Frame;
cell.Frame = new CGRect(cell.Frame.X, prevCellFrame.Y + prevCellFrame.Height, cell.Frame.Width, cell.Frame.Height);
}

if (cell.Equals(clickedCell))
{
cell.Frame = new CGRect(cell.Frame.X, cell.Frame.Y, cell.Frame.Width, size.Height);
}
}
}
}

static UICollectionViewController? GetController(CollectionView collectionView)
{
var handler = collectionView.Handler as Microsoft.Maui.Controls.Handlers.Items.CollectionViewHandler;
return handler?.Controller;
}

static UICollectionViewCell? GetCellByPoint(UICollectionViewCell[] cells, CGPoint point)
{
return cells.FirstOrDefault(cell => cell.Frame.Contains(point));
}
}
Loading