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

Implement ListView and CollectionView comparison #28

Open
wants to merge 1 commit into
base: master
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 @@ -16,7 +16,7 @@
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>false</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v8.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<AndroidEnableSGenConcurrent>true</AndroidEnableSGenConcurrent>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
<NuGetPackageImportStamp>
Expand All @@ -31,6 +31,9 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<AotAssemblies>false</AotAssemblies>
<EnableLLVM>false</EnableLLVM>
<BundleAssemblies>false</BundleAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -52,6 +55,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.FFImageLoading">
<Version>2.4.5.909-pre</Version>
</PackageReference>
<PackageReference Include="Xamarin.FFImageLoading.Forms">
<Version>2.4.5.909-pre</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Android.Support.ViewPager" Version="28.0.0.1" />
<PackageReference Include="Xamarin.Android.Support.Design" Version="28.0.0.1" />
Expand All @@ -64,7 +73,9 @@
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="Helpers\ConversionHelpers.cs" />
<Compile Include="MainActivity.cs" />
<Compile Include="Renderers\LabelControlRenderer.cs" />
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Android.App;

namespace CollectionViewChallenge.Droid.Helpers
{
public static class ConversionHelper
{
public static int ConvertDPToPixels(int dp)
{
float scale = Application.Context.Resources.DisplayMetrics.Density;

return (int)(dp * scale + 0.5);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;

using Android.App;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Runtime;
using FFImageLoading.Forms.Platform;

namespace CollectionViewChallenge.Droid
{
Expand All @@ -22,8 +19,12 @@ protected override void OnCreate(Bundle savedInstanceState)
global::Xamarin.Forms.Forms.SetFlags("Shell_Experimental", "Visual_Experimental", "CollectionView_Experimental", "FastRenderers_Experimental");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

LoadApplication(new App());

CachedImageRenderer.Init(false);
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.CollectionViewChallenge">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<application android:label="CollectionViewChallenge.Android"></application>
</manifest>
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<application android:label="CollectionViewChallenge.Android"></application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Android.Content;
using Android.Graphics.Drawables;
using CollectionViewChallenge.Controls;
using CollectionViewChallenge.Droid.Helpers;
using CollectionViewChallenge.Droid.Renderers;
using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(LabelControl), typeof(LabelControlRenderer))]

namespace CollectionViewChallenge.Droid.Renderers
{
public class LabelControlRenderer : LabelRenderer
{
private GradientDrawable m_background;

public LabelControlRenderer(Context context)
: base(context)
{
m_background = new GradientDrawable();
m_background.SetShape(ShapeType.Rectangle);
}

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
base.OnElementChanged(e);

if (Control != null)
{
UpdatePadding();
}
}

protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);

LabelControl label = Element as LabelControl;
if (label == null)
return;

m_background.SetColor(label.BackgroundColor.ToAndroid());

int cornerRadiusPx = ConversionHelper.ConvertDPToPixels(label.CornerRadius);
m_background.SetCornerRadius(cornerRadiusPx);

int borderWidthPx = ConversionHelper.ConvertDPToPixels(label.BorderWidth);
m_background.SetStroke(borderWidthPx, label.BorderColour.ToAndroid());

Background = m_background;
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
Invalidate();
else if (e.PropertyName == LabelControl.CornerRadiusProperty.PropertyName)
Invalidate();
else if (e.PropertyName == LabelControl.BorderWidthProperty.PropertyName)
Invalidate();
else if (e.PropertyName == LabelControl.BorderColourProperty.PropertyName)
Invalidate();
else if (e.PropertyName == LabelControl.PaddingProperty.PropertyName)
UpdatePadding();
}

private void UpdatePadding()
{
LabelControl label = Element as LabelControl;
if (label == null)
return;

int left = ConversionHelper.ConvertDPToPixels((int)label.Padding.Left + label.BorderWidth);
int top = ConversionHelper.ConvertDPToPixels((int)label.Padding.Top + label.BorderWidth);
int right = ConversionHelper.ConvertDPToPixels((int)label.Padding.Right + label.BorderWidth);
int bottom = ConversionHelper.ConvertDPToPixels((int)label.Padding.Bottom + label.BorderWidth);

Control.SetPadding(left, top, right, bottom);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;

using FFImageLoading.Forms.Platform;
using Foundation;
using UIKit;

Expand All @@ -24,8 +21,11 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.SetFlags("Shell_Experimental", "Visual_Experimental", "CollectionView_Experimental", "FastRenderers_Experimental");
global::Xamarin.Forms.Forms.Init();

LoadApplication(new App());

CachedImageRenderer.Init();

return base.FinishedLaunching(app, options);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
</PropertyGroup>
<ItemGroup>
<Compile Include="Controls\UILabelControl.cs" />
<Compile Include="Main.cs" />
<Compile Include="AppDelegate.cs" />
<Compile Include="Renderers\LabelControlRenderer.cs" />
<None Include="Entitlements.plist" />
<None Include="Info.plist" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down Expand Up @@ -133,6 +135,12 @@
<Reference Include="System.Numerics.Vectors" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Xamarin.FFImageLoading">
<Version>2.4.5.909-pre</Version>
</PackageReference>
<PackageReference Include="Xamarin.FFImageLoading.Forms">
<Version>2.4.5.909-pre</Version>
</PackageReference>
<PackageReference Include="Xamarin.Forms" Version="4.0.0.346134-pre9" />
<PackageReference Include="Xamarin.Essentials" Version="1.1.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CoreGraphics;
using System;
using UIKit;

namespace CollectionViewChallenge.iOS.Controls
{
/// <summary>
/// Custom iOS UILabel that allows padding of content
/// </summary>
internal class UILabelControl : UILabel
{
private UIEdgeInsets m_padding = UIEdgeInsets.Zero;
public UIEdgeInsets Padding
{
get { return m_padding; }
set
{
m_padding = value;
InvalidateIntrinsicContentSize();
}
}

public override CGRect TextRectForBounds(CGRect bounds, nint numberOfLines)
{
CGRect rect = base.TextRectForBounds(Padding.InsetRect(bounds), numberOfLines);

return new CGRect(rect.X - Padding.Left,
rect.Y - Padding.Top,
rect.Width + Padding.Left + Padding.Right,
rect.Height + Padding.Top + Padding.Bottom);
}

public override void DrawText(CGRect rect)
{
base.DrawText(Padding.InsetRect(rect));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using CollectionViewChallenge.Controls;
using CollectionViewChallenge.iOS.Controls;
using CollectionViewChallenge.iOS.Renderers;
using System;
using System.ComponentModel;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(LabelControl), typeof(LabelControlRenderer))]

namespace CollectionViewChallenge.iOS.Renderers
{
public class LabelControlRenderer : LabelRenderer
{
private UIEdgeInsets m_padding;

public LabelControlRenderer()
{
m_padding = new UIEdgeInsets();
}

protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
{
if (Control == null)
SetNativeControl(new UILabelControl());

base.OnElementChanged(e);

if (Control != null)
{
UpdateCornerRadius();
UpdateBorderWidth();
UpdateBorderColour();
UpdatePadding();
}
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName == LabelControl.CornerRadiusProperty.PropertyName)
UpdateCornerRadius();
else if (e.PropertyName == LabelControl.BorderWidthProperty.PropertyName)
UpdateBorderWidth();
else if (e.PropertyName == LabelControl.BorderColourProperty.PropertyName)
UpdateBorderColour();
else if (e.PropertyName == LabelControl.PaddingProperty.PropertyName)
UpdatePadding();
}

private void UpdateCornerRadius()
{
LabelControl label = Element as LabelControl;
if (label == null)
return;

Layer.CornerRadius = label.CornerRadius;
}

private void UpdateBorderWidth()
{
LabelControl label = Element as LabelControl;
if (label == null)
return;

Layer.BorderWidth = label.BorderWidth;
}

private void UpdateBorderColour()
{
LabelControl label = Element as LabelControl;
if (label == null)
return;

Layer.BorderColor = label.BorderColour.ToCGColor();
}

private void UpdatePadding()
{
LabelControl label = Element as LabelControl;
if (label == null)
return;

m_padding.Top = (nfloat)label.Padding.Top;
m_padding.Left = (nfloat)label.Padding.Left;
m_padding.Bottom = (nfloat)label.Padding.Bottom;
m_padding.Right = (nfloat)label.Padding.Right;

(Control as UILabelControl).Padding = m_padding;
}
}
}
Loading