mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-06-21 21:46:27 +02:00
Initial version.
This commit is contained in:
parent
193aaa1a56
commit
b72c67a53e
228 changed files with 25924 additions and 0 deletions
|
@ -0,0 +1,59 @@
|
|||
using System;
|
||||
using TINK.Model.State;
|
||||
using TINK.MultilingualResources;
|
||||
using TINK.ViewModel.Bikes.Bike;
|
||||
|
||||
namespace TINK.ViewModel
|
||||
{
|
||||
public class BikeAtStationInUseStateInfoProvider : IInUseStateInfoProvider
|
||||
{
|
||||
/// <summary> Gets reserved into display text. </summary>
|
||||
/// <todo>Log unexpeced states.</todo>
|
||||
/// <returns>Display text</returns>
|
||||
public string GetReservedInfo(
|
||||
TimeSpan? remainingTime,
|
||||
int? station = null,
|
||||
string code = null)
|
||||
{
|
||||
if (remainingTime == null)
|
||||
{
|
||||
// Remaining time not available.
|
||||
if (string.IsNullOrEmpty(code))
|
||||
{
|
||||
// Reservation code not available
|
||||
return string.Format(AppResources.StatusTextReservationExpiredMaximumReservationTime, StateRequestedInfo.MaximumReserveTime.Minutes);
|
||||
}
|
||||
|
||||
return string.Format(AppResources.StatusTextReservationExpiredCodeMaxReservationTime, code, StateRequestedInfo.MaximumReserveTime.Minutes);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(code))
|
||||
{
|
||||
return string.Format(AppResources.StatusTextReservationExpiredCodeRemaining, code, remainingTime.Value.Minutes);
|
||||
}
|
||||
|
||||
return string.Format(AppResources.StatusTextReservationExpiredRemaining, remainingTime.Value.Minutes);
|
||||
}
|
||||
|
||||
/// <summary> Gets booked into display text. </summary>
|
||||
/// <todo>Log unexpeced states.</todo>
|
||||
/// <returns>Display text</returns>
|
||||
public string GetBookedInfo(
|
||||
DateTime? from,
|
||||
int? station = null,
|
||||
string code = null)
|
||||
{
|
||||
if (from == null)
|
||||
{
|
||||
return AppResources.StatusTextBooked;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(code))
|
||||
{
|
||||
return string.Format(AppResources.StatusTextBookedCodeSince, code, from.Value.ToString(BikeViewModelBase.TIMEFORMAT));
|
||||
}
|
||||
|
||||
return string.Format(AppResources.StatusTextBookedSince, from.Value.ToString(BikeViewModelBase.TIMEFORMAT));
|
||||
}
|
||||
}
|
||||
}
|
380
TINKLib/ViewModel/BikesAtStation/BikesAtStationPageViewModel.cs
Normal file
380
TINKLib/ViewModel/BikesAtStation/BikesAtStationPageViewModel.cs
Normal file
|
@ -0,0 +1,380 @@
|
|||
using TINK.Model.Bike;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using TINK.Model.User;
|
||||
using System.Threading.Tasks;
|
||||
using TINK.Model.Connector;
|
||||
using TINK.Settings;
|
||||
using System;
|
||||
using Serilog;
|
||||
using System.Threading;
|
||||
using TINK.Model;
|
||||
using TINK.View;
|
||||
using Xamarin.Forms;
|
||||
using System.Linq;
|
||||
using TINK.Model.Bike.BluetoothLock;
|
||||
using System.Collections.Generic;
|
||||
using TINK.Services.BluetoothLock;
|
||||
using TINK.Model.Services.Geolocation;
|
||||
using TINK.ViewModel.Bikes;
|
||||
using TINK.Services.BluetoothLock.Tdo;
|
||||
using Plugin.Permissions.Abstractions;
|
||||
using Plugin.BLE.Abstractions.Contracts;
|
||||
using TINK.MultilingualResources;
|
||||
using Plugin.Permissions;
|
||||
|
||||
namespace TINK.ViewModel.BikesAtStation
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages one or more bikes which are located at a single station.
|
||||
/// </summary>
|
||||
public class BikesAtStationPageViewModel : BikesViewModel, INotifyCollectionChanged, INotifyPropertyChanged
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference on view servcie to show modal notifications and to perform navigation.
|
||||
/// </summary>
|
||||
private IViewService m_oViewService;
|
||||
|
||||
/// <summary>
|
||||
/// Holds the Id of the selected station;
|
||||
/// </summary>
|
||||
private readonly int? m_oStation;
|
||||
|
||||
/// <summary> Holds a reference to the external trigger service. </summary>
|
||||
private Action<string> OpenUrlInExternalBrowser { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs bike collection view model.
|
||||
/// </summary>
|
||||
/// <param name="user">Mail address of active user.</param>
|
||||
/// <param name="permissions">Holds object to query location permisions.</param>
|
||||
/// <param name="bluetoothLE">Holds object to query bluetooth state.</param>
|
||||
/// <param name="runtimPlatform">Specifies on which platform code is run.</param>
|
||||
/// <param name="isConnectedDelegate">Returns if mobile is connected to web or not.</param>
|
||||
/// <param name="p_oConnector">Connects system to copri.</param>
|
||||
/// <param name="lockService">Service to control lock retrieve info.</param>
|
||||
/// <param name="polling"> Holds whether to poll or not and the periode leght is polling is on. </param>
|
||||
/// <param name="l_oBikesAll">All bikes at given station.</param>
|
||||
/// <param name="openUrlInExternalBrowser">Action to open an external browser.</param>
|
||||
/// <param name="postAction">Executes actions on GUI thread.</param>
|
||||
/// <param name="viewService">Interface to actuate methodes on GUI.</param>
|
||||
public BikesAtStationPageViewModel(
|
||||
User user,
|
||||
IPermissions permissions,
|
||||
IBluetoothLE bluetoothLE,
|
||||
string runtimPlatform,
|
||||
int? selectedStation,
|
||||
Func<bool> isConnectedDelegate,
|
||||
Func<bool, IConnector> connectorFactory,
|
||||
IGeolocation geolocation,
|
||||
ILocksService lockService,
|
||||
PollingParameters polling,
|
||||
Action<string> openUrlInExternalBrowser,
|
||||
Action<SendOrPostCallback, object> postAction,
|
||||
IViewService viewService) : base(user, permissions, bluetoothLE, runtimPlatform, isConnectedDelegate, connectorFactory, geolocation, lockService, polling, postAction, viewService, () => new BikeAtStationInUseStateInfoProvider())
|
||||
{
|
||||
m_oViewService = viewService
|
||||
?? throw new ArgumentException("Can not instantiate bikes at station page view model- object. No view available.");
|
||||
|
||||
OpenUrlInExternalBrowser = openUrlInExternalBrowser
|
||||
?? throw new ArgumentException("Can not instantiate login page view model- object. No user external browse service available.");
|
||||
|
||||
m_oStation = selectedStation;
|
||||
|
||||
Title = string.Format(m_oStation != null
|
||||
? string.Format(AppResources.MarkingBikesAtStationTitle, m_oStation.ToString())
|
||||
: string.Empty);
|
||||
|
||||
CollectionChanged += (sender, eventargs) =>
|
||||
{
|
||||
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsNoBikesAtStationVisible)));
|
||||
OnPropertyChanged(new PropertyChangedEventArgs(nameof(NoBikesAtStationText)));
|
||||
OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsLoginRequiredHintVisible)));
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Name of the station which is displayed as title of the page.
|
||||
/// </summary>
|
||||
public string Title
|
||||
{
|
||||
get; private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Informs about need to log in before requesting an bike.
|
||||
/// </summary>
|
||||
public bool IsLoginRequiredHintVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return Count > 0
|
||||
&& !ActiveUser.IsLoggedIn;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Informs about need to log in before requesting an bike.
|
||||
/// </summary>
|
||||
public FormattedString LoginRequiredHintText
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ActiveUser.IsLoggedIn)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var l_oHint = new FormattedString();
|
||||
l_oHint.Spans.Add(new Span { Text = "Bitte Anmelden um Fahrräder zu reservieren! " });
|
||||
l_oHint.Spans.Add(new Span { Text = "Hier", ForegroundColor = ViewModelHelper.LINK_COLOR });
|
||||
l_oHint.Spans.Add(new Span { Text = " tippen um auf Anmeldeseite zu wechseln."});
|
||||
|
||||
return l_oHint;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Returns if info about the fact that user did not request or book any bikes is visible or not.<summary>
|
||||
/// Gets message that logged in user has not booked any bikes.
|
||||
/// </summary>
|
||||
public bool IsNoBikesAtStationVisible
|
||||
{
|
||||
get
|
||||
{
|
||||
return Count <= 0 && IsIdle == true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Info about the fact that user did not request or book any bikes. </summary>
|
||||
public string NoBikesAtStationText
|
||||
{
|
||||
get
|
||||
{
|
||||
return IsNoBikesAtStationVisible
|
||||
? $"Momentan sind keine Fahrräder an dieser Station verfügbar."
|
||||
: string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Commang object to bind login button to view model.</summary>
|
||||
public System.Windows.Input.ICommand LoginRequiredHintClickedCommand
|
||||
{
|
||||
get
|
||||
{
|
||||
return new Xamarin.Forms.Command(() => OpenLoginPage());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens login page.
|
||||
/// </summary>
|
||||
public void OpenLoginPage()
|
||||
{
|
||||
try
|
||||
{
|
||||
// Switch to map page
|
||||
m_oViewService.ShowPage(ViewTypes.LoginPage);
|
||||
}
|
||||
catch (Exception p_oException)
|
||||
{
|
||||
Log.Error("Ein unerwarteter Fehler ist auf der Seite Anmelden aufgetreten. Kontext: Klick auf Hinweistext auf Station N- seite ohne Anmeldung. {@Exception}", p_oException);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when page is shown.
|
||||
/// Starts update process.
|
||||
/// </summary>
|
||||
public async Task OnAppearing()
|
||||
{
|
||||
Log.ForContext<BikesAtStationPageViewModel>().Information($"Bikes at station {m_oStation} is appearing, either due to tap on a station or to app being shown again.");
|
||||
|
||||
ActionText = "Einen Moment bitte...";
|
||||
|
||||
// Stop polling before getting bikes info.
|
||||
await m_oViewUpdateManager.StopUpdatePeridically();
|
||||
|
||||
ActionText = AppResources.ActivityTextBikesAtStationGetBikes;
|
||||
|
||||
var bikesAll = await ConnectorFactory(IsConnected).Query.GetBikesAsync();
|
||||
|
||||
Exception = bikesAll.Exception; // Update communication error from query for bikes at station.
|
||||
|
||||
var bikesAtStation = bikesAll.Response.GetAtStation(m_oStation);
|
||||
var lockIdList = bikesAtStation
|
||||
.GetLockIt()
|
||||
.Cast<BikeInfo>()
|
||||
.Select(x => x.LockInfo)
|
||||
.ToList();
|
||||
|
||||
Title = string.Format(m_oStation != null
|
||||
? m_oStation.ToString()
|
||||
: string.Empty);
|
||||
|
||||
if (LockService is ILocksServiceFake serviceFake)
|
||||
{
|
||||
serviceFake.UpdateSimulation(bikesAtStation);
|
||||
}
|
||||
|
||||
ActionText = AppResources.ActivityTextSearchBikes;
|
||||
|
||||
// Check location permissions.
|
||||
if (bikesAtStation.GetLockIt().Count > 0
|
||||
&& RuntimePlatform == Device.Android)
|
||||
{
|
||||
var status = await Permissions.CheckPermissionStatusAsync<LocationPermission>();
|
||||
if (status != PermissionStatus.Granted)
|
||||
{
|
||||
var permissionResult = await Permissions.RequestPermissionAsync<LocationPermission>();
|
||||
|
||||
if (permissionResult != PermissionStatus.Granted)
|
||||
{
|
||||
var dialogResult = await m_oViewService.DisplayAlert(
|
||||
AppResources.MessageTitleHint,
|
||||
AppResources.MessageBikesManagementLocationPermissionOpenDialog,
|
||||
AppResources.MessageAnswerYes,
|
||||
AppResources.MessageAnswerNo);
|
||||
|
||||
if (!dialogResult)
|
||||
{
|
||||
// User decided not to give access to locations permissions.
|
||||
BikeCollection.Update(bikesAtStation);
|
||||
|
||||
await OnAppearing(() => UpdateTask());
|
||||
|
||||
ActionText = "";
|
||||
IsIdle = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Open permissions dialog.
|
||||
Permissions.OpenAppSettings();
|
||||
}
|
||||
}
|
||||
|
||||
if (Geolocation.IsGeolcationEnabled == false)
|
||||
{
|
||||
await m_oViewService.DisplayAlert(
|
||||
AppResources.MessageTitleHint,
|
||||
AppResources.MessageBikesManagementLocationActivation,
|
||||
AppResources.MessageAnswerOk);
|
||||
|
||||
BikeCollection.Update(bikesAtStation);
|
||||
|
||||
await OnAppearing(() => UpdateTask());
|
||||
|
||||
ActionText = "";
|
||||
IsIdle = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if bluetooth is activated.
|
||||
if (await BluetoothLE.GetBluetoothState() != BluetoothState.On)
|
||||
{
|
||||
await m_oViewService.DisplayAlert(
|
||||
AppResources.MessageTitleHint,
|
||||
AppResources.MessageBikesManagementBluetoothActivation,
|
||||
AppResources.MessageAnswerOk);
|
||||
|
||||
BikeCollection.Update(bikesAtStation);
|
||||
|
||||
await OnAppearing(() => UpdateTask());
|
||||
|
||||
ActionText = "";
|
||||
IsIdle = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Connect to bluetooth devices.
|
||||
ActionText = AppResources.ActivityTextSearchBikes;
|
||||
IEnumerable<LockInfoTdo> locksInfoTdo;
|
||||
try
|
||||
{
|
||||
locksInfoTdo = await LockService.GetLocksStateAsync(
|
||||
lockIdList.Select(x => x.ToLockInfoTdo()).ToList(),
|
||||
LockService.TimeOut.MultiConnect);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Log.ForContext<BikesAtStationPageViewModel>().Error("Getting bluetooth state failed. {Exception}", exception);
|
||||
locksInfoTdo = new List<LockInfoTdo>();
|
||||
}
|
||||
|
||||
var locksInfo = lockIdList.UpdateById(locksInfoTdo);
|
||||
|
||||
BikeCollection.Update(bikesAtStation.UpdateLockInfo(locksInfo));
|
||||
|
||||
// Backup GUI synchronization context.
|
||||
await OnAppearing(() => UpdateTask());
|
||||
|
||||
ActionText = "";
|
||||
IsIdle = true;
|
||||
}
|
||||
|
||||
/// <summary> Create task which updates my bike view model.</summary>
|
||||
private void UpdateTask()
|
||||
{
|
||||
PostAction(
|
||||
unused =>
|
||||
{
|
||||
ActionText = "Aktualisiere...";
|
||||
IsConnected = IsConnectedDelegate();
|
||||
},
|
||||
null);
|
||||
|
||||
var result = ConnectorFactory(IsConnected).Query.GetBikesAsync().Result;
|
||||
|
||||
BikeCollection bikes = result.Response.GetAtStation(m_oStation);
|
||||
|
||||
var exception = result.Exception;
|
||||
if (exception != null)
|
||||
{
|
||||
Log.ForContext<BikesAtStationPageViewModel>().Error("Getting all bikes bikes in polling context failed with exception {Exception}.", exception);
|
||||
}
|
||||
|
||||
PostAction(
|
||||
unused =>
|
||||
{
|
||||
BikeCollection.Update(bikes);
|
||||
Exception = result.Exception;
|
||||
ActionText = string.Empty;
|
||||
},
|
||||
null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// True if any action can be performed (request and cancel request)
|
||||
/// </summary>
|
||||
public override bool IsIdle
|
||||
{
|
||||
get => base.IsIdle;
|
||||
set
|
||||
{
|
||||
if (value == base.IsIdle)
|
||||
return;
|
||||
|
||||
Log.ForContext<BikesViewModel>().Debug($"Switch value of {nameof(IsIdle)} to {value}.");
|
||||
base.IsIdle = value;
|
||||
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(IsNoBikesAtStationVisible)));
|
||||
base.OnPropertyChanged(new PropertyChangedEventArgs(nameof(NoBikesAtStationText)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Opens login page.</summary>
|
||||
/// <param name="url">Url to open.</param>
|
||||
private void RegisterRequest(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
OpenUrlInExternalBrowser(url);
|
||||
}
|
||||
catch (Exception p_oException)
|
||||
{
|
||||
Log.Error("Ein unerwarteter Fehler ist auf der Login Seite beim Öffnen eines Browsers, Seite {url}, aufgetreten. {@Exception}", url, p_oException);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue