Skip to content

Commit

Permalink
RealTimeLocationTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Apr 15, 2023
1 parent 5f7673c commit 29f653d
Show file tree
Hide file tree
Showing 37 changed files with 326 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Auth/AuthServices/AuthServices.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Identity.Client" Version="4.52.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.28.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.53.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.29.0" />
</ItemGroup>

</Project>
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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.0" />
<PackageReference Include="CommunityToolkit.Maui.MediaElement" Version="1.0.2" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions KanbanBoard/KanbanBoard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Maui" Version="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.5" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios' or $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) =='maccatalyst' ">
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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.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="5.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.1.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0" />
</ItemGroup>

Expand Down
6 changes: 6 additions & 0 deletions MauiMaps/Geolocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MauiMaps;

public static class Geolocator
{
public static IGeolocator Default = new GeolocatorImplementation();
}
6 changes: 6 additions & 0 deletions MauiMaps/IGeolocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace MauiMaps;

public interface IGeolocator
{
Task StartListening(IProgress<Location> positionChangedProgress, CancellationToken cancellationToken);
}
43 changes: 25 additions & 18 deletions MauiMaps/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,44 @@
xmlns:models="clr-namespace:MauiMaps.Models"
x:DataType="mauiMaps:MainPageViewModel">

<Grid>
<Map
<Grid>
<Map
x:Name="MyMap"
IsShowingUser="True"
ItemsSource="{Binding LocationPins}">
<Map.ItemTemplate>
<DataTemplate x:DataType="{x:Type models:LocationPin}">
<Map.ItemTemplate>
<DataTemplate x:DataType="{x:Type models:LocationPin}">
<mauiMaps:CustomPin
Location="{Binding Location}"
Location="{Binding Location}"
Address="{Binding Address}"
Label="{Binding Description}"
ImageSource="{Binding ImageSource}"
Type="Place"/>
</DataTemplate>
</Map.ItemTemplate>
</Map>
<HorizontalStackLayout
Margin="0,0,0,10"
BackgroundColor="White"
HorizontalOptions="Center"
VerticalOptions="End">
<Button
</Map.ItemTemplate>
</Map>
<ScrollView Orientation="Horizontal"
Margin="0,0,0,10"
BackgroundColor="White"
HorizontalOptions="Center"
VerticalOptions="End">
<HorizontalStackLayout>
<Button
Command="{Binding AddCommand}"
Text="Add new pin" />
<Button
<Button
Command="{Binding RemoveCommand}"
Text="Remove last pin" />
<Button
<Button
Command="{Binding RemoveAllCommand}"
Text="Clear all pins" />
</HorizontalStackLayout>
</Grid>
<Button
Command="{Binding RealTimeLocationTrackerCommand}"
Text="Start real time location tracking" />
<Button
Command="{Binding RealTimeLocationTrackerCancelCommand}"
Text="Stop real time location tracking" />
</HorizontalStackLayout>
</ScrollView>
</Grid>

</ContentPage>
26 changes: 26 additions & 0 deletions MauiMaps/MainPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
public partial class MainPageViewModel : ObservableObject
{
private readonly IFixture fixture;
private LocationPin? currentLocationPin;

public MainPageViewModel(IFixture fixture)
{
Expand Down Expand Up @@ -40,5 +41,30 @@ private void RemoveAll()
LocationPins.Clear();
}

[RelayCommand(IncludeCancelCommand = true, AllowConcurrentExecutions = false)]
private async Task RealTimeLocationTracker(CancellationToken cancellationToken)
{
var progress = new Progress<Location>(location =>
{
if (currentLocationPin is null)
{
currentLocationPin = new LocationPin
{
ImageSource = ImageSource.FromUri(new Uri($"https://picsum.photos/{Random.Shared.Next(40, 60)}")),
Location = location,
Description = "I am here!"
};
}
else
{
LocationPins.Remove(currentLocationPin);
currentLocationPin.Location = location;
}

LocationPins.Add(currentLocationPin);
});
await Geolocator.Default.StartListening(progress, cancellationToken);
}

public ObservableCollection<LocationPin> LocationPins { get; } = new();
}
4 changes: 2 additions & 2 deletions MauiMaps/MauiMaps.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

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

</Project>
6 changes: 6 additions & 0 deletions MauiMaps/Platforms/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-feature android:name="android.hardware.location" android:required="false" />
<uses-feature android:name="android.hardware.location.gps" android:required="false" />
<uses-feature android:name="android.hardware.location.network" android:required="false" />
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
Expand Down
77 changes: 77 additions & 0 deletions MauiMaps/Platforms/Android/GeolocatorImplementation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Android.Locations;
using Android.OS;
using Android.Runtime;
using CommunityToolkit.Maui.Alerts;
using Location = Android.Locations.Location;
using Permissions = Microsoft.Maui.ApplicationModel.Permissions;

namespace MauiMaps;

public class GeolocatorImplementation : IGeolocator
{
GeolocationContinuousListener? locator;

public async Task StartListening(IProgress<Microsoft.Maui.Devices.Sensors.Location> positionChangedProgress, CancellationToken cancellationToken)
{
var permission = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
if (permission != PermissionStatus.Granted)
{
permission = await Permissions.RequestAsync<Permissions.LocationAlways>();
if (permission != PermissionStatus.Granted)
{
await Toast.Make("No permission").Show(CancellationToken.None);
return;
}
}

locator = new GeolocationContinuousListener();
var taskCompletionSource = new TaskCompletionSource();
cancellationToken.Register(() =>
{
locator.Dispose();
locator = null;
taskCompletionSource.TrySetResult();
});
locator.OnLocationChangedAction = location =>
positionChangedProgress.Report(
new Microsoft.Maui.Devices.Sensors.Location(location.Latitude, location.Longitude));
await taskCompletionSource.Task;
}
}

internal class GeolocationContinuousListener : Java.Lang.Object, ILocationListener
{
public Action<Location>? OnLocationChangedAction { get; set; }

LocationManager? locationManager;

public GeolocationContinuousListener()
{
locationManager = (LocationManager?)Android.App.Application.Context.GetSystemService(Android.Content.Context.LocationService);
locationManager?.RequestLocationUpdates(LocationManager.GpsProvider, 1000, 0, this);
}

public void OnLocationChanged(Location location)
{
OnLocationChangedAction?.Invoke(location);
}

public void OnProviderDisabled(string provider)
{
}

public void OnProviderEnabled(string provider)
{
}

public void OnStatusChanged(string? provider, [GeneratedEnum] Availability status, Bundle? extras)
{
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
locationManager?.RemoveUpdates(this);
locationManager?.Dispose();
}
}
41 changes: 41 additions & 0 deletions MauiMaps/Platforms/MacCatalyst/GeolocatorImplementation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace MauiMaps;

using CommunityToolkit.Maui.Alerts;
using CoreLocation;

public class GeolocatorImplementation : IGeolocator
{
readonly CLLocationManager manager = new();

public async Task StartListening(IProgress<Location> positionChangedProgress, CancellationToken cancellationToken)
{
var permission = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
if (permission != PermissionStatus.Granted)
{
permission = await Permissions.RequestAsync<Permissions.LocationAlways>();
if (permission != PermissionStatus.Granted)
{
await Toast.Make("No permission").Show(CancellationToken.None);
return;
}
}
var taskCompletionSource = new TaskCompletionSource();
cancellationToken.Register(() =>
{
manager.LocationsUpdated -= PositionChanged;
taskCompletionSource.TrySetResult();
});
manager.LocationsUpdated += PositionChanged;

void PositionChanged(object? sender, CLLocationsUpdatedEventArgs args)
{
if (args.Locations.Length > 0)
{
var coordinate = args.Locations[^1].Coordinate;
positionChangedProgress.Report(new Location(coordinate.Latitude, coordinate.Longitude));
}
}

await taskCompletionSource.Task;
}
}
10 changes: 10 additions & 0 deletions MauiMaps/Platforms/MacCatalyst/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,15 @@
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/appicon.appiconset</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>
<key>UIBackgroundModes</key>
<array>
<string>location</string>
</array>
</dict>
</plist>
Loading

0 comments on commit 29f653d

Please sign in to comment.