Skip to content

Commit

Permalink
added bindable property for IsVisible on Circular Button
Browse files Browse the repository at this point in the history
  • Loading branch information
Programming-With-Chris committed Aug 18, 2022
1 parent c916d69 commit 154e0ba
Show file tree
Hide file tree
Showing 11 changed files with 197 additions and 68 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*.user
*.userosscache
*.sln.docstates
*.DS_Store

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down
65 changes: 60 additions & 5 deletions ShoppingList/Controls/CircularButton.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
using System;
using IImage = Microsoft.Maui.Graphics.IImage;

namespace ShoppingList.Controls;

public class CircularButton : GraphicsView, IGraphicsView
public class CircularButton : GraphicsView
{
//public static new IDrawable Drawable { get; set; }

/// <summary>
/// The Color passed to the drawable for the "Fill Color" of the Fill Circle method
/// </summary>
public Color ButtonColor
{
get => (Color?)GetValue(ButtonColorProperty);
get => (Color)GetValue(ButtonColorProperty);
set => SetValue(ButtonColorProperty, value);
}

/// <summary>
/// A string containing the Image name (just the file name, not the directory)
/// <br/> Expects the file to be in Resources/Images/
/// </summary>
public string Image
{
get => (string)GetValue(ImageProperty);
set => SetValue(ImageProperty, value);

}

public new bool IsVisible
{
get => (bool)GetValue(IsVisibleProperty);
set => SetValue(IsVisibleProperty, value);
}

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

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

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

public CircularButton()
{
var drawable = new ShoppingList.Drawable.CircularButtonDrawable();
var drawable = new CircularButtonDrawable();
Drawable = drawable;
}

Expand All @@ -29,4 +55,33 @@ static void OnButtonColorChanged(BindableObject bindable, object oldValue, objec
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;
control.Invalidate();

}

static void OnIsVisibleChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (CircularButton)bindable;

//var newValueAsBool = (bool)newValue;

// if ( == true)
// {
// control.FadeTo(1, 2000);
// } else
// {
// control.FadeTo(0, 4000);
// }

var thisDrawable = control.Drawable as ShoppingList.Drawable.CircularButtonDrawable;
//thisDrawable.SetInvisible = (bool)newValue;
control.Invalidate();
}

}
34 changes: 23 additions & 11 deletions ShoppingList/Drawable/CircularButtonDrawable.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using Microsoft.Maui.Graphics.Platform;
using IImage = Microsoft.Maui.Graphics.IImage;

namespace ShoppingList.Drawable;

Expand All @@ -10,15 +11,23 @@ public class CircularButtonDrawable : IDrawable

public bool AreShadowsEnabled { get; set; } = true;

public Microsoft.Maui.Graphics.IImage Image { get; set; }
/// <summary>
/// A string containing the Image name.
/// </summary>
public string Image { get; set; }

public int Width { get; set; } = 0;
public int Height { get; set; } = 0;

public Color ButtonColor { get; set; } = Colors.White;
public Color ButtonColor { get; set; } = Colors.White;

public bool SetInvisible { get; set; } = false;

public void Draw(ICanvas canvas, RectF dirtyRect)
{
if (SetInvisible)
return;

canvas.StrokeColor = StrokeColor;

if (AreShadowsEnabled)
Expand All @@ -30,24 +39,27 @@ public void Draw(ICanvas canvas, RectF dirtyRect)
var limitingDim = width > height ? height : width;
PointF centerOfCircle = new PointF(width / 2, height / 2);

// canvas.BlendMode = BlendMode.SourceOut;
//canvas.FillColor = Colors.White;
//canvas.FillCircle(centerOfCircle, limitingDim / 2);

Microsoft.Maui.Graphics.IImage image;
#if !WINDOWS
IImage image;
Assembly assembly = GetType().GetTypeInfo().Assembly;
using (Stream stream = assembly.GetManifestResourceStream("ShoppingList.Resources.Images.plus_sign.png"))
using (Stream stream = assembly.GetManifestResourceStream("ShoppingList.Resources.Images." + Image))
{
image = PlatformImage.FromStream(stream);
Image = image;
if (image is null)
throw new FileNotFoundException("ShoppingList.Resources.Images." + Image);
}
if (Image != null)
if (image != null)
{
canvas.FillColor = this.ButtonColor;
canvas.FillCircle(centerOfCircle, limitingDim / 2);
canvas.DrawImage(Image, dirtyRect.X + dirtyRect.Width / 4, dirtyRect.Y + dirtyRect.Height / 4, dirtyRect.Width / 2, dirtyRect.Height / 2);
canvas.DrawImage(image, dirtyRect.X + dirtyRect.Width / 4, dirtyRect.Y + dirtyRect.Height / 4, dirtyRect.Width / 2, dirtyRect.Height / 2);
}
#else

canvas.FillColor = this.ButtonColor;
canvas.FillCircle(centerOfCircle, limitingDim / 2);

#endif
}
}

4 changes: 3 additions & 1 deletion ShoppingList/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public static MauiApp CreateMauiApp()
fonts.AddFont("Font Awesome 6 Free-Regular-400.otf");
});

builder.Services.AddSingleton<UserListService>();
builder.Services.AddSingleton<ItemService>();
builder.Services.AddSingleton<UserListService>();
builder.Services.AddSingleton<KrogerAPIService>();
builder.Services.AddSingleton<UserListViewModel>();
builder.Services.AddTransient<UserListDetailViewModel>();
builder.Services.AddTransient<UserListDataInputViewModel>();
Expand Down
Binary file added ShoppingList/Resources/Images/undo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions ShoppingList/ShoppingList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@

<ItemGroup>
<EmbeddedResource Include="Resources\Images\plus_sign.png" />
<EmbeddedResource Include="Resources\Images\undo.png" />
</ItemGroup>

<!--<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-android'">
Expand All @@ -150,6 +151,9 @@
</PackageReference>
</ItemGroup>-->

<ItemGroup>
<MauiImage Remove="Resources\Images\undo.png" />
</ItemGroup>
<ProjectExtensions><VisualStudio><UserProperties XamarinHotReloadDebuggerTimeoutExceptionShoppingListHideInfoBar="True" /></VisualStudio></ProjectExtensions>

<!-- <PropertyGroup Condition="$(TargetFramework.Contains('-ios'))">
Expand Down
30 changes: 26 additions & 4 deletions ShoppingList/View/UserListDetails.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:model="clr-namespace:ShoppingList.Model"
xmlns:viewmodel="clr-namespace:ShoppingList.ViewModels"
xmlns:drawables="clr-namespace:ShoppingList.Drawable"
xmlns:controls="clr-namespace:ShoppingList.Controls"
x:DataType="viewmodel:UserListDetailViewModel"
x:Class="ShoppingList.UserListDetails"
Expand Down Expand Up @@ -81,15 +80,17 @@
</CollectionView.ItemTemplate>
</CollectionView>
<!-- </RefreshView>-->
<controls:CircularButton

<controls:CircularButton
Grid.Row="0"
Grid.Column="2"
x:Name="CircularButton"
x:Name="NewItemButton"
WidthRequest="75"
HeightRequest="75"
Margin="0,0,40,20"
ButtonColor="{StaticResource Secondary}"
ButtonColor="{StaticResource Primary}"
BackgroundColor="Transparent"
Image="plus_sign.png"
VerticalOptions="End"
HorizontalOptions="End"
>
Expand All @@ -99,6 +100,27 @@
</GraphicsView.GestureRecognizers>
</controls:CircularButton>


<controls:CircularButton
Grid.Row="0"
Grid.Column="0"
x:Name="UndoButton"
WidthRequest="75"
HeightRequest="75"
Margin="20,40,0,0"
ButtonColor="{StaticResource Primary}"
BackgroundColor="Transparent"
Image="undo.png"
VerticalOptions="End"
HorizontalOptions="Start"
IsEnabled="{Binding HasUndo}"
IsVisible="{Binding HasUndo}"
>
<GraphicsView.GestureRecognizers>
<TapGestureRecognizer Command="{Binding UndoButtonPressedCommand}"/>

</GraphicsView.GestureRecognizers>
</controls:CircularButton>
<!--<skiasharp:SKLottieView x:Name="CartLottie"
WidthRequest="100"
HeightRequest="100"
Expand Down
24 changes: 0 additions & 24 deletions ShoppingList/View/UserListDetails.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@ public UserListDetails(UserListDetailViewModel userListDetailViewModel)
BindingContext = userListDetailViewModel;
_ulvm = userListDetailViewModel;

var cartLottie = this.FindByName("SKLottieViewControlTemplate");
//var canvas = cartLottie.OnPaintSurface();


//var circularButtonGV = this.FindByName("CircularButton") as GraphicsView;
//var circularButton = circularButtonGV.Drawable as CircularButton;
//circularButton.StrokeColor = Colors.White;

//circularButton.AreShadowsEnabled = true;
//circularButton.Width = 75;
//circularButton.Height = 75;

//IImage image;
//Assembly assembly = GetType().GetTypeInfo().Assembly;
//using (Stream stream = assembly.GetManifestResourceStream("ShoppingList.Resources.Images.plus_solid.svg"))
//{
// image = PlatformImage.FromStream(stream);
//}
//circularButton.Image = image;
//circularButtonGV.Invalidate();


}

public void OnCheckboxClicked(object sender, CheckedChangedEventArgs e)
Expand All @@ -59,10 +37,8 @@ public void OnCheckboxClicked(object sender, CheckedChangedEventArgs e)
else
currentFrame.FadeTo(1, 1000);

//thisCheckbox.CheckedChanged -= OnCheckboxClicked;
itemThatWasClicked.IsCompleted = thisCheckbox.IsChecked;
_ulvm.ItemWasChecked(itemThatWasClicked);
//thisCheckbox.CheckedChanged += OnCheckboxClicked;

}

Expand Down
4 changes: 2 additions & 2 deletions ShoppingList/ViewModel/StoreFinderViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public partial class StoreFinderViewModel : BaseViewModel
string zipSearched;


public StoreFinderViewModel()
public StoreFinderViewModel(KrogerAPIService krogerAPIService)
{
_kapis = new KrogerAPIService();
_kapis = krogerAPIService;
}

[RelayCommand]
Expand Down
Loading

0 comments on commit 154e0ba

Please sign in to comment.