Skip to content

Commit

Permalink
Fixes #236 (#237)
Browse files Browse the repository at this point in the history
* Fixes #236

* MCT v5
  • Loading branch information
VladislavAntonyuk authored Mar 1, 2023
1 parent 313780f commit 1a7d186
Show file tree
Hide file tree
Showing 30 changed files with 477 additions and 335 deletions.
2 changes: 1 addition & 1 deletion Auth/MauiAuth/MauiAuth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion Auth/MauiAuthBlazor/MauiAuthBlazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion DynamicFab/DynamicFab.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion IPTVPlayer/IPTVPlayer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui.MediaElement" Version="1.0.2" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion KanbanBoard/KanbanBoard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.3" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion MauiAnimation/MauiAnimation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion MauiBank/MauiBank.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Plugin.Fingerprint" Version="3.0.0-beta.1" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion MauiBluetooth/MauiBluetooth.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Plugin.BLE" Version="3.0.0-beta.2" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion MauiCaptcha/MauiCaptcha.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion MauiLocalization/MauiLocalization.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
</ItemGroup>

Expand Down
28 changes: 1 addition & 27 deletions MauiMaps/CustomPin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,11 @@
public class CustomPin : Pin
{
public static readonly BindableProperty ImageSourceProperty =
BindableProperty.Create(nameof(ImageSource), typeof(ImageSource), typeof(CustomPin),
propertyChanged: OnImageSourceChanged);
BindableProperty.Create(nameof(ImageSource), typeof(ImageSource), typeof(CustomPin));

public ImageSource? ImageSource
{
get => (ImageSource?)GetValue(ImageSourceProperty);
set => SetValue(ImageSourceProperty, value);
}

public Microsoft.Maui.Maps.IMap? Map { get; set; }

static async void OnImageSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (CustomPin)bindable;
if (control.Handler?.PlatformView is null)
{
// Workaround for when this executes the Handler and PlatformView is null
control.HandlerChanged += OnHandlerChanged;
return;
}

#if IOS || MACCATALYST
await control.AddAnnotation();
#else
await Task.CompletedTask;
#endif

void OnHandlerChanged(object? s, EventArgs e)
{
OnImageSourceChanged(control, oldValue, newValue);
control.HandlerChanged -= OnHandlerChanged;
}
}
}
49 changes: 32 additions & 17 deletions MauiMaps/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mauiMaps="clr-namespace:MauiMaps"
x:Class="MauiMaps.MainPage">
<ContentPage
x:Class="MauiMaps.MainPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:mauiMaps="clr-namespace:MauiMaps"
xmlns:models="clr-namespace:MauiMaps.Models"
x:DataType="mauiMaps:MainPageViewModel">

<Grid RowDefinitions="*,*">
<Map x:Name="MyMap" Grid.Row="0"/>
<Map x:Name="MyMap2" ItemsSource="{Binding Sites}" Grid.Row="1">
<Map.ItemTemplate>
<DataTemplate x:DataType="{x:Type mauiMaps:Site}">
<Grid>
<Map
x:Name="MyMap"
IsShowingUser="True"
ItemsSource="{Binding LocationPins}">
<Map.ItemTemplate>
<DataTemplate x:DataType="{x:Type models:LocationPin}">
<mauiMaps:CustomPin
Location="{Binding .Location}"
Address="{Binding Address}"
Label="{Binding Description}"
ImageSource="{Binding ImageSource}"
Type="Place"/>
Location="{Binding Location}"
Address="{Binding Address}"
Label="{Binding Description}"
ImageSource="{Binding ImageSource}"
Type="Place"/>
</DataTemplate>
</Map.ItemTemplate>
</Map>
</Grid>
</Map.ItemTemplate>
</Map>
<Button
Margin="0,0,0,10"
BackgroundColor="White"
Command="{Binding AddCommand}"
FontAttributes="Bold"
HeightRequest="40"
HorizontalOptions="Center"
Text="Add new pin"
TextColor="Red"
VerticalOptions="End" />
</Grid>

</ContentPage>
44 changes: 5 additions & 39 deletions MauiMaps/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
namespace MauiMaps;

using System.Collections.ObjectModel;
using CommunityToolkit.Maui.Alerts;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Maps;

public partial class MainPage : ContentPage
{
public ObservableCollection<Site> Sites { get; } = new();

public MainPage()
public MainPage(MainPageViewModel viewModel)
{
BindingContext = viewModel;
InitializeComponent();
InitMap1();
InitMapMvvm();
}

void InitMap1()
Expand All @@ -22,16 +20,14 @@ void InitMap1()
Label = "From Uri",
Location = new Location(10, 10),
Address = "Address",
ImageSource = ImageSource.FromUri(new Uri("https://picsum.photos/50")),
Map = MyMap
ImageSource = ImageSource.FromUri(new Uri("https://picsum.photos/50"))
};
var customPinFromResource = new CustomPin()
{
Label = "From Resource",
Location = new Location(12, 12),
Address = "Address3",
ImageSource = ImageSource.FromResource("MauiMaps.Resources.EmbeddedImages.icon.jpeg"),
Map = MyMap
ImageSource = ImageSource.FromResource("MauiMaps.Resources.EmbeddedImages.icon.jpeg")
};
MyMap.Pins.Add(customPinFromUri);
MyMap.Pins.Add(customPinFromResource);
Expand All @@ -45,34 +41,4 @@ void InitMap1()
};
MyMap.MoveToRegion(new MapSpan(new Location(10, 10), 10, 10));
}

void InitMapMvvm()
{
BindingContext = this;
var site1 = new Site()
{
Location = new Location(10, 10),
Description = "From Uri",
Address = "Address",
ImageSource = ImageSource.FromUri(new Uri("https://picsum.photos/50")),
};
var site2 = new Site()
{
Description = "From Resource",
Location = new Location(12, 12),
Address = "Address3",
ImageSource = ImageSource.FromResource("MauiMaps.Resources.EmbeddedImages.icon.jpeg")
};
Sites.Add(site1);
Sites.Add(site2);
MyMap2.MoveToRegion(new MapSpan(new Location(10, 10), 10, 10));
}
}

public class Site
{
public string? Description { get; set; }
public string? Address { get; set; }
public Location? Location { get; set; }
public ImageSource? ImageSource { get; set; }
}
29 changes: 29 additions & 0 deletions MauiMaps/MainPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace MauiMaps;

using System.Collections.ObjectModel;
using AutoFixture;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Models;

public partial class MainPageViewModel : ObservableObject
{
private readonly IFixture fixture;

public MainPageViewModel(IFixture fixture)
{
this.fixture = fixture;
}

[RelayCommand]
private void Add()
{
var newLocation = fixture.Build<LocationPin>()
.With(x => x.ImageSource, ImageSource.FromUri(new Uri($"https://picsum.photos/{Random.Shared.Next(40, 60)}")))
.With(x => x.Location, new Location(Random.Shared.Next(5, 15), Random.Shared.Next(5, 15)))
.Create();
LocationPins.Add(newLocation);
}

public ObservableCollection<LocationPin> LocationPins { get; } = new();
}
6 changes: 4 additions & 2 deletions MauiMaps/MauiMaps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
<!-- Images -->
<EmbeddedResource Include="Resources\EmbeddedImages\*" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="4.0.0" />
<PackageReference Include="AutoFixture" Version="4.18.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Microsoft.Maui.Controls.Maps" Version="7.0.59" />
</ItemGroup>

Expand Down
27 changes: 26 additions & 1 deletion MauiMaps/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace MauiMaps;

using AutoFixture;
using CommunityToolkit.Maui;
using Microsoft.Maui.Platform;

public static class MauiProgram
{
Expand All @@ -9,13 +11,36 @@ public static MauiApp CreateMauiApp()
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>().UseMauiMaps()
.UseMauiCommunityToolkit();
builder.RegisterAppServices()
.RegisterViewModels()
.RegisterViews();
builder.ConfigureMauiHandlers(handlers =>
{
#if ANDROID
#if ANDROID || IOS || MACCATALYST
handlers.AddHandler<Microsoft.Maui.Controls.Maps.Map, CustomMapHandler>();
#endif
});

return builder.Build();
}

public static MauiAppBuilder RegisterAppServices(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddSingleton<IFixture, Fixture>();
return mauiAppBuilder;
}

public static MauiAppBuilder RegisterViewModels(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddSingleton<MainPageViewModel>();

return mauiAppBuilder;
}

public static MauiAppBuilder RegisterViews(this MauiAppBuilder mauiAppBuilder)
{
mauiAppBuilder.Services.AddSingleton<MainPage>();

return mauiAppBuilder;
}
}
9 changes: 9 additions & 0 deletions MauiMaps/Models/LocationPin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MauiMaps.Models;

public class LocationPin
{
public string? Description { get; set; }
public string? Address { get; set; }
public Location? Location { get; set; }
public ImageSource? ImageSource { get; set; }
}
Loading

0 comments on commit 1a7d186

Please sign in to comment.