Skip to content

Commit

Permalink
Improve loading times.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelinn committed Jun 13, 2024
1 parent 1bd0adf commit 45fe9c8
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 24 deletions.
15 changes: 14 additions & 1 deletion src/TramlineFive/TramlineFive.Common/Services/MapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using SkgtService.Models;
using CommunityToolkit.Mvvm.Messaging;
using Mapsui.Tiling.Fetcher;
using Mapsui.Extensions;

namespace TramlineFive.Common.Services;

Expand Down Expand Up @@ -70,7 +71,7 @@ public async Task SetupMapAsync(string tileServer, string fetchingStrategy)
{
n.CenterOn(point);
n.ZoomTo(map.Navigator.Resolutions[17]);
ShowNearbyStops(point);
_ = ShowNearbyStops(point);
WeakReferenceMessenger.Default.Send(new MapLoadedMessage());
};
Expand All @@ -85,10 +86,22 @@ public async Task SetupMapAsync(string tileServer, string fetchingStrategy)

map.Layers.Add(TileServerFactory.CreateTileLayer(tileServer ?? "carto-light", fetchStrategy));

await Task.Delay(100);

LoadPinStyles();
ILayer stopsLayer = await LoadStops();
map.Layers.Add(stopsLayer);

MyLocationLayer locationLayer = new MyLocationLayer(map);
locationLayer.Enabled = true;
map.Layers.Add(locationLayer);

WeakReferenceMessenger.Default.Register<UpdateLocationMessage>(this, (r, m) =>
{
var point = SphericalMercator.FromLonLat(m.Position.Longitude, m.Position.Latitude).ToMPoint();
locationLayer.UpdateMyLocation(point);
});

navigator.ViewportChanged += (sender, args) => SendMapRefreshMessage();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Fizzler;

using Mapsui;
using Newtonsoft.Json.Linq;
Expand Down
13 changes: 6 additions & 7 deletions src/TramlineFive/TramlineFive.Maui/Pages/MapPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
</ResourceDictionary>
</ContentPage.Resources>

<AbsoluteLayout>
<mapsui:MapView x:Name="map" VerticalOptions="StartAndExpand" HorizontalOptions="Fill" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="1,1,1,1"
IsNorthingButtonVisible="False" IsMyLocationButtonVisible="False" IsZoomButtonVisible="False" MyLocationEnabled="False">
<mapsui:MapView.Behaviors>
<AbsoluteLayout x:Name="absLayout">
<mapsui:MapControl x:Name="map" VerticalOptions="StartAndExpand" HorizontalOptions="Fill" AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="1,1,1,1">
<mapsui:MapControl.Behaviors>
<mct:EventToCommandBehavior EventName="Info" Command="{Binding MapInfoCommand}" x:TypeArguments="mapsuipl:MapInfoEventArgs" />
</mapsui:MapView.Behaviors>
</mapsui:MapView>

</mapsui:MapControl.Behaviors>
</mapsui:MapControl>
<ActivityIndicator AbsoluteLayout.LayoutFlags="All" AbsoluteLayout.LayoutBounds="1,1,1,1" BindingContext="{Binding VirtualTablesViewModel, Source={StaticResource Locator}}" IsRunning="{Binding IsLoading}" HeightRequest="64" Color="DodgerBlue" Margin="0,100,0,0" />

<Grid x:Name="grid" AbsoluteLayout.LayoutBounds="1,1,1,1" AbsoluteLayout.LayoutFlags="All">
Expand Down
63 changes: 48 additions & 15 deletions src/TramlineFive/TramlineFive.Maui/Pages/MapPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Maui.Behaviors;
using CommunityToolkit.Mvvm.Messaging;
using Mapsui;
using Mapsui.Extensions;
using Mapsui.Layers;
using Mapsui.Projections;
using Mapsui.UI.Maui;
using Mapsui.Widgets;
using Mapsui.Widgets.ScaleBar;
Expand All @@ -26,19 +30,49 @@ public partial class MapPage : ContentPage
//MapView map;
//View slideMenu;

//private MapView map;

public MapPage()
{
InitializeComponent();

Loaded += OnLoaded;

WeakReferenceMessenger.Default.Register<ShowMapMessage>(this, async (r, m) => await ToggleMap(m));
//Messenger.Default.Register<RefreshMapMessage>(this, m => map.Refresh());
WeakReferenceMessenger.Default.Register<UpdateLocationMessage>(this, (r, m) => map.MyLocationLayer.UpdateMyLocation(new Position(m.Position.Latitude, m.Position.Longitude)));
}

private async void OnLoaded(object sender, EventArgs e)
private async Task LoadDataAsync()
{
_ = LoadMapAsync();

await ServiceContainer.ServiceProvider.GetService<HistoryViewModel>().LoadHistoryAsync();
await ServiceContainer.ServiceProvider.GetService<FavouritesViewModel>().LoadFavouritesAsync();
}

protected override async void OnAppearing()
{
if (initialized)
return;

//map = new MapView();
//map.IsNorthingButtonVisible = false;
//map.IsMyLocationButtonVisible = false;
//map.IsZoomButtonVisible = false;
//map.MyLocationEnabled = false;

//map.VerticalOptions = LayoutOptions.StartAndExpand;
//map.HorizontalOptions = LayoutOptions.Fill;

//absLayout.SetLayoutBounds(map, new Rect(1, 1, 1, 1));
//absLayout.SetLayoutFlags(map, AbsoluteLayoutFlags.All);
//absLayout.Insert(0, map);

//await Task.Delay(100);

initialized = true;

_ = LoadDataAsync();

DateTime versionCheckTime = Preferences.Get("VersionCheckDate", DateTime.MinValue);
if (DateTime.Now - versionCheckTime < TimeSpan.FromDays(1))
return;
Expand All @@ -55,6 +89,7 @@ private async void OnLoaded(object sender, EventArgs e)
}

Preferences.Set("VersionCheckDate", DateTime.Now);

}

private async void OnMapTouchAction(object sender, SkiaSharp.Views.Maui.SKTouchEventArgs e)
Expand Down Expand Up @@ -108,7 +143,7 @@ private async Task ToggleMap(ShowMapMessage message)
await Task.Delay((int)difference);
}


Dispatcher.Dispatch(async () =>
{
isOpened = message.Show;
Expand All @@ -120,26 +155,22 @@ private async Task ToggleMap(ShowMapMessage message)
});
}

protected override void OnAppearing()
private async Task LoadMapAsync()
{
if (initialized)
return;

initialized = true;
Task task = (BindingContext as MapViewModel).LoadAsync();

nativeMap = new Map
{
BackColor = Mapsui.Styles.Color.White,
CRS = "EPSG:3857"
};

mapService = ServiceContainer.ServiceProvider.GetService<MapService>();
Task _ = (BindingContext as MapViewModel).Initialize(nativeMap, nativeMap.Navigator);
await (BindingContext as MapViewModel).Initialize(nativeMap, nativeMap.Navigator);

map.Map = nativeMap;
map.TouchAction += OnMapTouchAction;

await (BindingContext as MapViewModel).LoadAsync();

if (VersionTracking.IsFirstLaunchEver)
WeakReferenceMessenger.Default.Send(new ChangePageMessage("//Location"));
}
Expand All @@ -148,7 +179,9 @@ protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height);
slideMenu.TranslationY = isOpened ? 0 : Height;
map.HeightRequest = Height;
}

if (map != null)
map.HeightRequest = Height;
}
}
}

0 comments on commit 45fe9c8

Please sign in to comment.