Skip to content

Commit

Permalink
Allow user to choose fetching strategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
angelinn committed May 10, 2024
1 parent 0388fa8 commit 29e0905
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BruTile.Web;
using Mapsui.Tiling.Fetcher;
using Mapsui.Tiling.Layers;
using Mapsui.Tiling.Rendering;
using System;
using System.Text;
using TramlineFive.Common.Services;
Expand All @@ -13,9 +14,9 @@ public static class TileServerFactory
private static readonly BruTile.Attribution OpenStreetMap = new BruTile.Attribution(
"© OpenStreetMap contributors", "http://www.openstreetmap.org/copyright");

public static TileLayer CreateTileLayer(string name)
public static TileLayer CreateTileLayer(string name, IDataFetchStrategy dataFetchStrategy)
{
return new TileLayer(CreateTileSource(name), dataFetchStrategy: new DataFetchStrategy()) { Name = name };
return new TileLayer(CreateTileSource(name), dataFetchStrategy: dataFetchStrategy, renderFetchStrategy: new MinimalRenderFetchStrategy()) { Name = name };
}

private static HttpTileSource CreateTileSource(string name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public LineMapService(PublicTransport publicTransport)

public async Task SetupMapAsync(LineRoute route, TransportType transportType, string tileServer = null)

Check warning on line 39 in src/TramlineFive/TramlineFive.Common/Services/LineMapService.cs

View workflow job for this annotation

GitHub Actions / Android Build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
Map.Layers.Add(TileServerFactory.CreateTileLayer(tileServer ?? "carto-light"));
Map.Layers.Add(TileServerFactory.CreateTileLayer(tileServer ?? "carto-light", null));
Map.Home = (h) => ZoomToBox(h, routeBox);
LoadPinStyles();

Expand Down
18 changes: 14 additions & 4 deletions src/TramlineFive/TramlineFive.Common/Services/MapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using SkgtService.Models.Json;
using SkgtService.Models;
using CommunityToolkit.Mvvm.Messaging;
using Mapsui.Tiling.Fetcher;

namespace TramlineFive.Common.Services;

Expand Down Expand Up @@ -51,17 +52,18 @@ public MapService(LocationService locationService, PublicTransport publicTranspo
this.publicTransport = publicTransport;
}

public async Task Initialize(Map map, Navigator navigator, string tileServer)
public async Task Initialize(Map map, Navigator navigator, string tileServer, string fetchStrategy)
{
this.map = map;
this.navigator = navigator;

await TileServerSettings.LoadTileServersAsync();

await SetupMapAsync(tileServer);

await SetupMapAsync(tileServer, fetchStrategy);
}

public async Task SetupMapAsync(string tileServer)
public async Task SetupMapAsync(string tileServer, string fetchingStrategy)
{
MPoint point = SphericalMercator.FromLonLat(CENTER_OF_SOFIA);
map.Home = n =>
Expand All @@ -73,7 +75,15 @@ public async Task SetupMapAsync(string tileServer)
WeakReferenceMessenger.Default.Send(new MapLoadedMessage());
};

map.Layers.Add(TileServerFactory.CreateTileLayer(tileServer ?? "carto-light"));
IDataFetchStrategy fetchStrategy = fetchingStrategy switch
{
"None" => null,
"Minimal" => new MinimalDataFetchStrategy(),
"Full" => new DataFetchStrategy(),
_ => new DataFetchStrategy()
};

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

LoadPinStyles();
ILayer stopsLayer = await LoadStops();
Expand Down
1 change: 1 addition & 0 deletions src/TramlineFive/TramlineFive.Common/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public static class Settings
public const string StopsUpdated = "StopsUpdated";
public const string SelectedTileServer = "SelectedTileServer";
public const string Theme = "Theme";
public const string FetchingStrategy = "FetchingStrategy";
}
10 changes: 6 additions & 4 deletions src/TramlineFive/TramlineFive.Common/ViewModels/MapViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task Initialize(Map nativeMap, Navigator navigator)
try
{
await mapService.Initialize(nativeMap, navigator,
ApplicationService.GetStringSetting(Settings.SelectedTileServer, null));
ApplicationService.GetStringSetting(Settings.SelectedTileServer, null), ApplicationService.GetStringSetting(Settings.FetchingStrategy, null));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -242,19 +242,21 @@ private async Task OnIntSettingChangedAsync(SettingChanged<int> m)
if (m.Name == Settings.MaxTextZoom && m.Value > 0)
{
mapService.MaxTextZoom = m.Value;
await mapService.SetupMapAsync(ApplicationService.GetStringSetting(Settings.SelectedTileServer, null));
await mapService.SetupMapAsync(ApplicationService.GetStringSetting(Settings.SelectedTileServer, null), ApplicationService.GetStringSetting(Settings.FetchingStrategy, null));
}
else if (m.Name == Settings.MaxPinsZoom && m.Value > 0)
{
mapService.MaxPinsZoom = m.Value;
await mapService.SetupMapAsync(ApplicationService.GetStringSetting(Settings.SelectedTileServer, null));
await mapService.SetupMapAsync(ApplicationService.GetStringSetting(Settings.SelectedTileServer, null), ApplicationService.GetStringSetting(Settings.FetchingStrategy, null));
}
}

private async Task OnStringSettingChangedAsync(SettingChanged<string> m)
{
if (m.Name == Settings.SelectedTileServer)
await mapService.SetupMapAsync(m.Value);
await mapService.SetupMapAsync(m.Value, ApplicationService.GetStringSetting(Settings.FetchingStrategy, "Minimal"));
else if (m.Name == Settings.FetchingStrategy)
await mapService.SetupMapAsync(ApplicationService.GetStringSetting(Settings.SelectedTileServer, "carto-light"), m.Value);
}

private void OnStopSelectedMessageReceived(StopSelectedMessage message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
Expand All @@ -22,6 +23,13 @@ public partial class SettingsViewModel : BaseViewModel
public DateTime Updated { get; private set; }

public List<string> TileServers => TileServerSettings.TileServers.Keys.ToList();
public List<string> FetchingStrategies => new List<string>()
{
"None",
"Minimal",
"Full"
};

public List<Theme> Themes => new() { new Theme("Светла", Names.LightTheme), new Theme("Тъмна", Names.DarkTheme) };

private Func<string, string, string, string[], Task<string>> displayActionSheet;
Expand All @@ -34,6 +42,7 @@ public SettingsViewModel()
MaxTextZoom = ApplicationService.GetIntSetting(Settings.MaxTextZoom, 0);
MaxPinsZoom = ApplicationService.GetIntSetting(Settings.MaxPinsZoom, 0);
SelectedTileServer = ApplicationService.GetStringSetting(Settings.SelectedTileServer, TileServers.First());
SelectedFetchingStrategy = ApplicationService.GetStringSetting(Settings.FetchingStrategy, "Full");

string theme = ApplicationService.GetStringSetting(Settings.Theme, Names.LightTheme);
SelectedTheme = theme == Names.LightTheme ? Themes[0] : Themes[1];
Expand All @@ -52,6 +61,15 @@ private async Task ChooseTileServer()
SelectedTileServer = result;
}


[RelayCommand]
private async Task ChooseFetchingStrategy()
{
string result = await displayActionSheet("Избор на стратегия", String.Empty, String.Empty, FetchingStrategies.ToArray());
if (!String.IsNullOrEmpty(result))
SelectedFetchingStrategy = result;
}

[RelayCommand]
private async Task ChooseTheme()
{
Expand Down Expand Up @@ -131,7 +149,7 @@ private void RefreshStopsUpdatedTime()
private bool showNearestStop;
partial void OnShowNearestStopChanged(bool value)
{
ApplicationService.SetBoolSetting(Settings.ShowStopOnLaunch, showNearestStop);
ApplicationService.SetBoolSetting(Settings.ShowStopOnLaunch, ShowNearestStop);
}

[ObservableProperty]
Expand All @@ -144,6 +162,14 @@ partial void OnSelectedThemeChanged(Theme oldValue, Theme newValue)
Messenger.Send(new ChangeThemeMessage(newValue.Value));
}
}

[ObservableProperty]
private string selectedFetchingStrategy;
partial void OnSelectedFetchingStrategyChanged(string value)
{
ApplicationService.SetStringSetting(Settings.FetchingStrategy, value);
Messenger.Send(new SettingChanged<string>(Settings.FetchingStrategy, value));
}
}

public record Theme(string Name, string Value);
Expand Down
18 changes: 18 additions & 0 deletions src/TramlineFive/TramlineFive.Maui/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@
</StackLayout>
</Grid>


<Grid Margin="0,20,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="55" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ChooseFetchingStrategyCommand}" />
</Grid.GestureRecognizers>

<Label FontFamily="MaterialIconsOutlinedRegular.otf" Text="map" TextColor="{DynamicResource IconsColor}" FontSize="26" VerticalOptions="Center" Margin="10,0,0,0" />
<StackLayout Grid.Column="1" Spacing="3">
<Label Text="Начин на зареждане" FontSize="16" VerticalTextAlignment="Center" />
<Label Text="{Binding SelectedFetchingStrategy}" FontSize="Small" VerticalTextAlignment="Center" TextColor="{DynamicResource DetailsTextColor}"/>
</StackLayout>
</Grid>

<!--<Label Text="Max zoom for text" Margin="0,20,0,0" FontSize="Medium" />
<Entry Text="{Binding MaxTextZoom}" />
Expand Down

0 comments on commit 29e0905

Please sign in to comment.