Skip to content

Commit

Permalink
added handler implementation example
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Sep 3, 2022
1 parent f3c6fc6 commit 13d1621
Show file tree
Hide file tree
Showing 23 changed files with 100 additions and 57 deletions.
7 changes: 4 additions & 3 deletions ShoppingList/AppShell.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:ShoppingList"
xmlns:views="clr-namespace:ShoppingList"
xmlns:view="clr-namespace:ShoppingList.View"
Shell.BackgroundColor="{DynamicResource Primary}"
Shell.FlyoutBehavior="Flyout"
FlyoutBackgroundColor="{DynamicResource Secondary}"
Expand All @@ -14,7 +15,7 @@

<ShellContent
Title="Home"
ContentTemplate="{DataTemplate local:MainPage}"
ContentTemplate="{DataTemplate view:MainPage}"
Route="MainPage"
/>

Expand All @@ -23,11 +24,11 @@
>
<ShellContent Title="My Archived Lists"
Route="ArchivedListView"
ContentTemplate="{DataTemplate views:ArchivedListView}"/>
ContentTemplate="{DataTemplate view:ArchivedListView}"/>

<ShellContent Title="Settings"
Route="SettingsView"
ContentTemplate="{DataTemplate views:SettingsView}"/>
ContentTemplate="{DataTemplate view:SettingsView}"/>
<!-- Had to remove Icon, it was causing issues on IOS, Settings page wouldn't route-->

</FlyoutItem>
Expand Down
4 changes: 3 additions & 1 deletion ShoppingList/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace ShoppingList;
using ShoppingList.View;

namespace ShoppingList;

public partial class AppShell : Shell
{
Expand Down
30 changes: 19 additions & 11 deletions ShoppingList/Controls/CircularButton.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using ShoppingList.Handlers;
using IImage = Microsoft.Maui.Graphics.IImage;

namespace ShoppingList.Controls;
Expand Down Expand Up @@ -33,41 +32,50 @@ public string Image
}

public static BindableProperty ButtonColorProperty = BindableProperty.Create(
nameof(ButtonColor), typeof(Color), typeof(CircularButton), propertyChanged: OnButtonColorChanged);
nameof(ButtonColor), typeof(Color), typeof(CircularButton));
//propertyChanged: OnButtonColorChanged);

public static BindableProperty ImageProperty = BindableProperty.Create(
nameof(Image), typeof(string), typeof(CircularButton), propertyChanged: OnImageChanged);
public static BindableProperty ImageProperty = BindableProperty.Create(
nameof(Image), typeof(string), typeof(CircularButton));
//propertyChanged: OnImageChanged);

public static new BindableProperty IsVisibleProperty = BindableProperty.Create(
public new static BindableProperty IsVisibleProperty = BindableProperty.Create(
nameof(IsVisible), typeof(bool), typeof(CircularButton),
propertyChanged: OnIsVisibleChanged);
defaultValue: false);
//propertyChanged: OnIsVisibleChanged);

public CircularButton()
{
Handler = new CircularButtonHandler();
var drawable = new CircularButtonDrawable();
Drawable = drawable;
}


// Converted this logic over to the circular button handler
///<seealso cref = "CircularButtonHandler" />
static void OnButtonColorChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (CircularButton)bindable;
var buttonColor = control.ButtonColor;
var thisDrawable = control.Drawable as ShoppingList.Drawable.CircularButtonDrawable;
thisDrawable.ButtonColor = buttonColor;
if (control.Drawable is CircularButtonDrawable thisDrawable)
thisDrawable.ButtonColor = buttonColor;

control.Invalidate();
}

static void OnImageChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (CircularButton)bindable;
var image = control.Image;
var thisDrawable = control.Drawable as ShoppingList.Drawable.CircularButtonDrawable;
thisDrawable.Image = image;
if (control.Drawable is CircularButtonDrawable thisDrawable)
thisDrawable.Image = image;
control.Invalidate();

}

// Converted this logic over to the circular button handler
///<seealso cref = "CircularButtonHandler" />
static void OnIsVisibleChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (CircularButton)bindable;
Expand Down
38 changes: 38 additions & 0 deletions ShoppingList/Handlers/CircularButtonHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.Maui.Handlers;

namespace ShoppingList.Handlers;

public partial class CircularButtonHandler : GraphicsViewHandler
{
static IPropertyMapper<CircularButton, CircularButtonHandler> PropertyMapper = new PropertyMapper<CircularButton, CircularButtonHandler>(GraphicsViewHandler.Mapper)
{
[nameof(CircularButton.ButtonColor)] = MapButtonColor,
[nameof(CircularButton.Image)] = MapImage,
[nameof(CircularButton.IsVisible)] = MapIsVisible
};

public CircularButtonHandler() : base(PropertyMapper)
{
}
static void MapButtonColor(CircularButtonHandler handler, CircularButton button)
{
if (button.Drawable is CircularButtonDrawable drawable)
drawable.ButtonColor = button.ButtonColor;
button.Invalidate();
}

static void MapImage(CircularButtonHandler handler, CircularButton button)
{
if (button.Drawable is CircularButtonDrawable drawable)
drawable.Image = button.Image;
button.Invalidate();
}
static void MapIsVisible(CircularButtonHandler handler, CircularButton button)
{
if (button.IsVisible)
button.FadeTo(1, 500);
else
button.FadeTo(0, 500);
}

}
3 changes: 2 additions & 1 deletion ShoppingList/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ShoppingList.ViewModels;
using ShoppingList.View;
using ShoppingList.ViewModels;
using SkiaSharp.Views.Maui.Controls.Hosting;

namespace ShoppingList;
Expand Down
4 changes: 0 additions & 4 deletions ShoppingList/Services/DatabaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ public class DatabaseHandler
/// </summary>
public DatabaseHandler()
{
//#if ios
_pathToDb = Path.Combine (
Environment.GetFolderPath (Environment.SpecialFolder.Personal),
"database.db3");
//#else
//_pathToDb = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "shoppinglist_sqlite.db");
//#endif

_db = new SQLiteConnection(_pathToDb);
_db.CreateTable<Item>();
Expand Down
2 changes: 1 addition & 1 deletion ShoppingList/View/ArchivedListView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Title="{Binding Title}"
x:Name="CurrentPage"
BackgroundColor="{DynamicResource Secondary}"
x:Class="ShoppingList.ArchivedListView">
x:Class="ShoppingList.View.ArchivedListView">

<Grid ColumnDefinitions="*,*"
ColumnSpacing="5"
Expand Down
5 changes: 1 addition & 4 deletions ShoppingList/View/ArchivedListView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using ShoppingList.ViewModels;
using ShoppingList.Controls;

namespace ShoppingList;
namespace ShoppingList.View;

public partial class ArchivedListView : ContentPage
{
Expand Down
3 changes: 2 additions & 1 deletion ShoppingList/View/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Title="{Binding Title}"
x:Name="CurrentPage"
BackgroundColor="{DynamicResource Secondary}"
x:Class="ShoppingList.MainPage">
x:Class="ShoppingList.View.MainPage">

<Grid ColumnDefinitions="*,*"
ColumnSpacing="5"
Expand Down Expand Up @@ -91,6 +91,7 @@
Image="plus_sign.png"
VerticalOptions="End"
HorizontalOptions="End"
IsVisible="True"
>
<GraphicsView.GestureRecognizers>
<TapGestureRecognizer Tapped="NewListButtonPressed"/>
Expand Down
6 changes: 2 additions & 4 deletions ShoppingList/View/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using ShoppingList.ViewModels;
using ShoppingList.Controls;
using SkiaSharp.Extended.UI.Controls;
using SkiaSharp.Extended.UI.Controls;

namespace ShoppingList;
namespace ShoppingList.View;

public partial class MainPage : ContentPage
{
Expand Down
8 changes: 7 additions & 1 deletion ShoppingList/View/SettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
xmlns:control="clr-namespace:ShoppingList.Controls"
x:DataType="viewmodel:SettingsViewModel"
x:Class="ShoppingList.SettingsView"
x:Class="ShoppingList.View.SettingsView"
BackgroundColor="{DynamicResource Secondary}"
Shell.PresentationMode="Animated"
>
Expand Down Expand Up @@ -97,6 +97,7 @@
<control:CircularButton ButtonColor="{StaticResource NeonPrimary}"
WidthRequest="30"
HeightRequest="30"
IsVisible="True"
Margin="25,0,0,25"
BackgroundColor="Transparent"
x:Name="Neon">
Expand All @@ -111,6 +112,7 @@
<control:CircularButton ButtonColor="{StaticResource StrikingPrimary}"
WidthRequest="30"
HeightRequest="30"
IsVisible="True"
Margin="25,0,0,25"
BackgroundColor="Transparent"
x:Name="Striking">
Expand All @@ -124,6 +126,7 @@
<control:CircularButton ButtonColor="{StaticResource MutedPrimary}"
WidthRequest="30"
HeightRequest="30"
IsVisible="True"
Margin="25,0,0,25"
BackgroundColor="Transparent"
x:Name="Muted">
Expand All @@ -137,6 +140,7 @@
<control:CircularButton ButtonColor="{StaticResource PrettyPrimary}"
WidthRequest="30"
HeightRequest="30"
IsVisible="True"
Margin="25,0,0,25"
BackgroundColor="Transparent"
x:Name="Pretty">
Expand Down Expand Up @@ -175,6 +179,7 @@
<control:CircularButton ButtonColor="{StaticResource DeepRedPrimary}"
WidthRequest="30"
HeightRequest="30"
IsVisible="True"
Margin="25,0,0,25"
BackgroundColor="Transparent"
x:Name="DeepRed">
Expand All @@ -188,6 +193,7 @@
<control:CircularButton ButtonColor="{StaticResource DepressionPrimary}"
WidthRequest="30"
HeightRequest="30"
IsVisible="True"
Margin="25,0,0,25"
BackgroundColor="Transparent"
x:Name="Depression">
Expand Down
4 changes: 1 addition & 3 deletions ShoppingList/View/SettingsView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ShoppingList.ViewModels;

namespace ShoppingList;
namespace ShoppingList.View;

public partial class SettingsView : ContentPage
{
Expand Down
2 changes: 1 addition & 1 deletion ShoppingList/View/StoreFinder.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:model="clr-namespace:ShoppingList.Model"
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
x:DataType="viewmodel:StoreFinderViewModel"
x:Class="ShoppingList.StoreFinder"
x:Class="ShoppingList.View.StoreFinder"
BackgroundColor="{DynamicResource Secondary}"
Shell.PresentationMode="ModalAnimated"
>
Expand Down
4 changes: 1 addition & 3 deletions ShoppingList/View/StoreFinder.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ShoppingList.ViewModels;

namespace ShoppingList;
namespace ShoppingList.View;

public partial class StoreFinder : ContentPage
{
Expand Down
2 changes: 1 addition & 1 deletion ShoppingList/View/UserListDataInput.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
xmlns:controls="clr-namespace:ShoppingList.Controls"
x:DataType="viewmodel:UserListDataInputViewModel"
x:Class="ShoppingList.UserListDataInput"
x:Class="ShoppingList.View.UserListDataInput"
BackgroundColor="{DynamicResource Secondary}"
Shell.PresentationMode="Animated"
>
Expand Down
4 changes: 1 addition & 3 deletions ShoppingList/View/UserListDataInput.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ShoppingList.ViewModels;

namespace ShoppingList;
namespace ShoppingList.View;

public partial class UserListDataInput : ContentPage
{
Expand Down
6 changes: 3 additions & 3 deletions ShoppingList/View/UserListDetails.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
xmlns:controls="clr-namespace:ShoppingList.Controls"
x:DataType="viewmodel:UserListDetailViewModel"
x:Class="ShoppingList.UserListDetails"
x:Class="ShoppingList.View.UserListDetails"
BackgroundColor="{DynamicResource Secondary}"
Title="{Binding Title}"
>
Title="{Binding Title}">


<Shell.BackButtonBehavior>
Expand Down Expand Up @@ -129,6 +128,7 @@
Image="plus_sign.png"
VerticalOptions="End"
HorizontalOptions="End"
IsVisible="True"
>
<controls:CircularButton.GestureRecognizers>
<TapGestureRecognizer Tapped="NewItemButtonPressed"/>
Expand Down
12 changes: 3 additions & 9 deletions ShoppingList/View/UserListDetails.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
//using Microsoft.Maui.Graphics.Platform;
using System.Reflection;
using ShoppingList.ViewModels;
using IImage = Microsoft.Maui.Graphics.IImage;
using SkiaSharp.Extended.UI.Controls;
using SkiaSharp.Views.Maui.Controls;
using ShoppingList.Controls;

namespace ShoppingList;
namespace ShoppingList.View;

public partial class UserListDetails : ContentPage
{
Expand All @@ -26,7 +20,7 @@ protected override void OnAppearing()
{
base.OnAppearing();
var undoButton = this.FindByName("UndoButton") as CircularButton;
undoButton.FadeTo(0, 500);
undoButton?.FadeTo(0, 500);

}

Expand Down Expand Up @@ -54,7 +48,7 @@ private async void NewItemButtonPressed(object sender, EventArgs e)
{

var circularButton = sender as CircularButton;
await circularButton.BounceOnPressAsync();
await circularButton?.BounceOnPressAsync();
_ulvm.NewItemDialog();
}

Expand Down
1 change: 1 addition & 0 deletions ShoppingList/ViewModel/ArchivedListViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using ShoppingList.Model.Api;
using ShoppingList.View;

namespace ShoppingList.ViewModels;

Expand Down
1 change: 1 addition & 0 deletions ShoppingList/ViewModel/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using ShoppingList.View;

namespace ShoppingList.ViewModels;

Expand Down
1 change: 1 addition & 0 deletions ShoppingList/ViewModel/StoreFinderViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ShoppingList.Model.Api;
using System.Collections.ObjectModel;
using System.Diagnostics;
using ShoppingList.View;

namespace ShoppingList.ViewModels;

Expand Down
Loading

0 comments on commit 13d1621

Please sign in to comment.