Skip to content

Commit

Permalink
Sharpnado Maui Tabs sample nearly done
Browse files Browse the repository at this point in the history
  • Loading branch information
roubachof committed Sep 29, 2022
1 parent d56462a commit d00ba4f
Show file tree
Hide file tree
Showing 134 changed files with 5,022 additions and 166 deletions.
206 changes: 206 additions & 0 deletions Maui.Tabs/BadgeView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;

using Microsoft.Maui.Controls.Shapes;

namespace Sharpnado.Tabs
{
public class BadgeView : Border
{
public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(
nameof(FontFamily),
typeof(string),
typeof(TabTextItem),
null,
BindingMode.OneWay);

public static readonly BindableProperty TextProperty = BindableProperty.Create(
nameof(Text),
typeof(string),
typeof(TabTextItem),
string.Empty);

public static readonly BindableProperty TextSizeProperty = BindableProperty.Create(
nameof(TextSize),
typeof(double),
typeof(TabTextItem),
10d);

public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
nameof(TextColor),
typeof(Color),
typeof(TabTextItem),
Colors.White);

public static readonly BindableProperty BadgePaddingProperty = BindableProperty.Create(
nameof(BadgePadding),
typeof(Thickness),
typeof(BadgeView),
defaultValueCreator: (b) => new Thickness(6, 2));

public static readonly BindableProperty ShowIndicatorProperty = BindableProperty.Create(
nameof(ShowIndicator),
typeof(bool),
typeof(BadgeView),
default(bool));

private bool _paddingInternalChange;

public BadgeView()
{
_paddingInternalChange = true;
Padding = new Thickness(6, 2);
Margin = new Thickness(0, 4, 0, 0);

BackgroundColor = Colors.Red;
HorizontalOptions = LayoutOptions.End;
VerticalOptions = LayoutOptions.Start;

var textLabel = new Label
{
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Margin = new Thickness(0, -1.5, 0, 0),
BindingContext = this,
};

Content = textLabel;
}

public string Text
{
get => (string)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}

public double TextSize
{
get => (double)GetValue(TextSizeProperty);
set => SetValue(TextSizeProperty, value);
}

public Color TextColor
{
get => (Color)GetValue(TextColorProperty);
set => SetValue(TextColorProperty, value);
}

public string FontFamily
{
get => (string)GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}

public Thickness BadgePadding
{
get => (Thickness)GetValue(BadgePaddingProperty);
set => SetValue(BadgePaddingProperty, value);
}

public bool ShowIndicator
{
get => (bool)GetValue(ShowIndicatorProperty);
set => SetValue(ShowIndicatorProperty, value);
}

private Label BadgeLabel => (Label)Content;

protected override void OnHandlerChanged()
{
base.OnHandlerChanged();
Update();
}

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
base.OnPropertyChanged(propertyName);

switch (propertyName)
{
case nameof(Padding):
if (!_paddingInternalChange && Padding != new Thickness(0))
{
throw new NotSupportedException("Use the BadgePadding property instead of Padding to set the text inner margin");
}

_paddingInternalChange = false;
break;

case nameof(BadgePadding):
case nameof(ShowIndicator):
case nameof(FontFamily):
case nameof(TextColor):
case nameof(TextSize):
case nameof(Text):
Update();
break;

case nameof(Height):
UpdateCornerRadius();
break;
}
}

private void UpdateCornerRadius()
{
if (Height < 1)
{
return;
}

double cornerRadius = Height / 2;
StrokeShape = new RoundRectangle { CornerRadius = new CornerRadius(cornerRadius) };
}

private void Update()
{
InternalLogger.Debug($"Update text: {BadgeLabel.Text}, showIndicator: {ShowIndicator}");

bool isInt = int.TryParse(Text, out var count);
bool isEmpty = string.IsNullOrEmpty(Text);
IsVisible = ShowIndicator || (isInt && count > 0) || (!isInt && !isEmpty);

BadgeLabel.FontFamily = FontFamily;
BadgeLabel.Text = Text;
BadgeLabel.TextColor = TextColor;
BadgeLabel.FontSize = TextSize;

BadgeLabel.IsVisible = !ShowIndicator;

if (ShowIndicator)
{
double margin = (TextSize + BadgePadding.VerticalThickness) / 2;

TranslationX = HorizontalOptions.Alignment switch
{
LayoutAlignment.Start => margin,
LayoutAlignment.End => -margin,
_ => 0,
};

TranslationY = VerticalOptions.Alignment switch
{
LayoutAlignment.Start => margin,
LayoutAlignment.End => -margin,
_ => 0,
};

HeightRequest = 10;
WidthRequest = 10;
StrokeShape = new RoundRectangle { CornerRadius = new CornerRadius(5) };
return;
}
else
{
TranslationX = 0;
TranslationY = 0;
WidthRequest = -1;
HeightRequest = -1;
UpdateCornerRadius();
}

_paddingInternalChange = true;
Padding = BadgePadding;
}
}
}
4 changes: 2 additions & 2 deletions Maui.Tabs/BottomTabItem.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
RowSpacing="0">

<Grid.RowDefinitions>
<RowDefinition Height="8*" />
<RowDefinition x:Name="IconRowDefinition" Height="8*" />
<RowDefinition x:Name="TextRowDefinition" Height="5*" />
</Grid.RowDefinitions>

Expand All @@ -52,4 +52,4 @@
Text="{Binding Source={x:Reference RootLayout}, Path=Label}" />
</Grid>
</ContentView.Content>
</tabs:TabTextItem>
</tabs:TabTextItem>
9 changes: 4 additions & 5 deletions Maui.Tabs/Effects/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Windows.Input;
using Xamarin.Forms;

namespace XamEffects {
namespace Sharpnado.Tabs.Effects {
public static class Commands {
[Obsolete("Not need with usual Linking")]
public static void Init() {
Expand Down Expand Up @@ -103,8 +103,7 @@ static void PropertyChanged(BindableObject bindable, object oldValue, object new
}
}

public class CommandsRoutingEffect : RoutingEffect {
public CommandsRoutingEffect() : base("XamEffects." + nameof(Commands)) {
}
public class CommandsRoutingEffect : RoutingEffect
{
}
}
}
2 changes: 1 addition & 1 deletion Maui.Tabs/Effects/EffectsConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Xamarin.Forms;

namespace XamEffects {
namespace Sharpnado.Tabs.Effects {
public static class EffectsConfig {
[Obsolete("Not need with usual Linking")]
public static void Init() {
Expand Down
13 changes: 4 additions & 9 deletions Maui.Tabs/Effects/TouchEffect.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Linq;
using Xamarin.Forms;

namespace XamEffects {
namespace Sharpnado.Tabs.Effects {
public static class TouchEffect {

public static readonly BindableProperty ColorProperty =
Expand Down Expand Up @@ -48,8 +44,7 @@ static void PropertyChanged(BindableObject bindable, object oldValue, object new
}
}

public class TouchRoutingEffect : RoutingEffect {
public TouchRoutingEffect() : base("XamEffects." + nameof(TouchEffect)) {
}
public class TouchRoutingEffect : RoutingEffect
{
}
}
}
2 changes: 1 addition & 1 deletion Maui.Tabs/Maui.Tabs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@

<ItemGroup>
<Compile Include="..\Tabs\Tabs\AssemblyConfiguration.cs" Link="AssemblyConfiguration.cs" />
<Compile Include="..\Tabs\Tabs\BadgeView.cs" Link="BadgeView.cs" />
<Compile Include="..\Tabs\Tabs\BottomTabItem.xaml.cs" Link="BottomTabItem.xaml.cs" />
<Compile Include="..\Tabs\Tabs\DelayedView.cs" Link="DelayedView.cs" />
<Compile Include="..\Tabs\Tabs\Effects\ImageEffect.cs" Link="Effects\ImageEffect.cs" />
<Compile Include="..\Tabs\Tabs\IAnimatableReveal.cs" Link="IAnimatableReveal.cs" />
<Compile Include="..\Tabs\Tabs\IconOptions.cs" Link="IconOptions.cs" />
Expand Down
24 changes: 20 additions & 4 deletions Maui.Tabs/MauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Sharpnado.Tabs;
using Sharpnado.Tabs.Effects;


namespace Sharpnado.Tabs;

public static class MauiAppBuilderExtensions
{
Expand All @@ -10,9 +13,22 @@ public static MauiAppBuilder UseSharpnadoTabs(
InternalLogger.EnableDebug = debugLogEnable;
InternalLogger.EnableLogging = loggerEnable;

#if __IOS__
XamEffects.iOS.CommandsPlatform.Init();
XamEffects.iOS.TouchEffectPlatform.Init();
builder.ConfigureEffects(x =>
{
#if ANDROID
x.Add<CommandsRoutingEffect, Sharpnado.Tabs.Effects.Droid.CommandsPlatform>();
x.Add<TouchRoutingEffect, Sharpnado.Tabs.Effects.Droid.TouchEffectPlatform>();
x.Add<TintableImageEffect, Sharpnado.Tabs.Droid.AndroidTintableImageEffect>();
#elif IOS
x.Add<CommandsRoutingEffect, Sharpnado.Tabs.Effects.iOS.CommandsPlatform>();
x.Add<TouchRoutingEffect, Sharpnado.Tabs.Effects.iOS.TouchEffectPlatform>();
x.Add<TintableImageEffect, Sharpnado.Tabs.iOS.iOSTintableImageEffect>();
#endif
});

#if IOS
Sharpnado.Tabs.Effects.iOS.CommandsPlatform.Init();
Sharpnado.Tabs.Effects.iOS.TouchEffectPlatform.Init();
Sharpnado.Tabs.iOS.iOSTintableImageEffect.Init();
#endif

Expand Down
18 changes: 5 additions & 13 deletions Maui.Tabs/Platforms/Android/CommandsPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,18 @@
// This will exclude this file from stylecop analysis
// <auto-generated/>

using System;
using Android.Graphics;
using Android.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using XamEffects;
using XamEffects.Droid;

using View = Android.Views.View;
using System.Threading;
using XamEffects.Droid.GestureCollectors;
using Microsoft.Maui.Controls.Compatibility.Platform.Android;
using System.ComponentModel;

using Microsoft.Maui.Controls.Platform;

using Rect = Android.Graphics.Rect;
using Sharpnado.Tabs.Effects.Droid.GestureCollectors;

[assembly: ExportEffect(typeof(CommandsPlatform), nameof(Commands))]
using Rect = Android.Graphics.Rect;

namespace XamEffects.Droid {
namespace Sharpnado.Tabs.Effects.Droid {
public class CommandsPlatform : PlatformEffect {
public View View => Control ?? Container;
public bool IsDisposed => (Container as IVisualElementRenderer)?.Element == null;
Expand Down Expand Up @@ -94,4 +86,4 @@ protected override void OnDetached() {
TouchCollector.Delete(View, OnTouch);
}
}
}
}
7 changes: 2 additions & 5 deletions Maui.Tabs/Platforms/Android/TintableImageEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
using Sharpnado.Tabs.Droid;
using Sharpnado.Tabs.Effects;

[assembly: ResolutionGroupName("Sharpnado")]
[assembly: ExportEffect(typeof(AndroidTintableImageEffect), nameof(TintableImageEffect))]

namespace Sharpnado.Tabs.Droid
{
public class AndroidTintableImageEffect : PlatformEffect
Expand Down Expand Up @@ -36,7 +33,7 @@ private void UpdateColor()
{
var effect =
(TintableImageEffect)Element.Effects.FirstOrDefault(x => x is TintableImageEffect);
var color = effect?.TintColor.ToAndroid();
var color = effect?.TintColor?.ToAndroid();

if (Control is ImageView imageView && imageView.Handle != IntPtr.Zero && color.HasValue)
{
Expand All @@ -46,4 +43,4 @@ private void UpdateColor()
}
}
}
}
}
Loading

0 comments on commit d00ba4f

Please sign in to comment.