mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 07:06:33 +01:00
313 lines
12 KiB
C#
313 lines
12 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.State;
|
|
using TINK.Model.User;
|
|
using TINK.MultilingualResources;
|
|
using TINK.View;
|
|
using Xamarin.Forms;
|
|
|
|
using BikeInfoMutable = TINK.Model.Bike.BC.BikeInfoMutable;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike
|
|
{
|
|
/// <summary>
|
|
/// Defines the type of BikesViewModel child items, i.e. BikesViewModel derives from ObservableCollection<BikeViewModelBase>.
|
|
/// Holds references to
|
|
/// - connection state services
|
|
/// - copri service
|
|
/// - view service
|
|
/// </summary>
|
|
public abstract class BikeViewModelBase
|
|
{
|
|
/// <summary>
|
|
/// Time format for text "Gebucht seit".
|
|
/// </summary>
|
|
public const string TIMEFORMAT = "dd. MMMM HH:mm";
|
|
|
|
/// <summary> Provides info about the smart device (phone, tablet, ...).</summary>
|
|
protected ISmartDevice SmartDevice;
|
|
|
|
/// <summary>
|
|
/// Reference on view servcie to show modal notifications and to perform navigation.
|
|
/// </summary>
|
|
protected IViewService ViewService { get; }
|
|
|
|
/// <summary> Provides an connector object.</summary>
|
|
protected Func<bool, IConnector> ConnectorFactory { get; }
|
|
|
|
/// <summary> Delegate to retrieve connected state. </summary>
|
|
protected Func<bool> IsConnectedDelegate { get; }
|
|
|
|
/// <summary> Removes bike from bikes view model. </summary>
|
|
protected Action<string> BikeRemoveDelegate { get; }
|
|
|
|
/// <summary> Object to manage update of view model objects from Copri.</summary>
|
|
public Func<IPollingUpdateTaskManager> ViewUpdateManager { get; }
|
|
|
|
/// <summary>
|
|
/// Holds the bike to display.
|
|
/// </summary>
|
|
protected BikeInfoMutable bike;
|
|
|
|
/// <summary> Reference on the user </summary>
|
|
protected IUser ActiveUser { get; }
|
|
|
|
/// <summary>
|
|
/// Provides context related info.
|
|
/// </summary>
|
|
private IInUseStateInfoProvider StateInfoProvider { get; }
|
|
|
|
/// <summary>View model to be used for progress report and unlocking/ locking view.</summary>
|
|
protected IBikesViewModel BikesViewModel { get; }
|
|
|
|
/// <summary>
|
|
/// Notifies GUI about changes.
|
|
/// </summary>
|
|
public abstract event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
/// <summary>
|
|
/// Notfies childs about changed bike state.
|
|
/// </summary>
|
|
public abstract void OnSelectedBikeStateChanged();
|
|
|
|
/// <summary> Raises events in order to update GUI.</summary>
|
|
public abstract void RaisePropertyChanged(object sender, PropertyChangedEventArgs eventArgs);
|
|
|
|
/// <summary>
|
|
/// Constructs a bike view model object.
|
|
/// </summary>
|
|
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...).</param>
|
|
/// <param name="selectedBike">Bike to be displayed.</param>
|
|
/// <param name="activeUser">Object holding logged in user or an empty user object.</param>
|
|
/// <param name="stateInfoProvider">Provides in use state information.</param>
|
|
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
|
public BikeViewModelBase(
|
|
Func<bool> isConnectedDelegate,
|
|
Func<bool, IConnector> connectorFactory,
|
|
Action<string> bikeRemoveDelegate,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
BikeInfoMutable selectedBike,
|
|
IUser activeUser,
|
|
IInUseStateInfoProvider stateInfoProvider,
|
|
IBikesViewModel bikesViewModel)
|
|
{
|
|
IsConnectedDelegate = isConnectedDelegate;
|
|
|
|
ConnectorFactory = connectorFactory;
|
|
|
|
BikeRemoveDelegate = bikeRemoveDelegate;
|
|
|
|
ViewUpdateManager = viewUpdateManager;
|
|
|
|
SmartDevice = smartDevice;
|
|
|
|
ViewService = viewService;
|
|
|
|
bike = selectedBike
|
|
?? throw new ArgumentException(string.Format("Can not construct {0}- object, bike object is null.", typeof(BikeViewModelBase)));
|
|
|
|
ActiveUser = activeUser
|
|
?? throw new ArgumentException(string.Format("Can not construct {0}- object, user object is null.", typeof(BikeViewModelBase)));
|
|
|
|
StateInfoProvider = stateInfoProvider
|
|
?? throw new ArgumentException(string.Format("Can not construct {0}- object, user object is null.", typeof(IInUseStateInfoProvider)));
|
|
|
|
selectedBike.PropertyChanged +=
|
|
(sender, eventargs) => OnSelectedBikePropertyChanged(eventargs.PropertyName);
|
|
|
|
BikesViewModel = bikesViewModel
|
|
?? throw new ArgumentException($"Can not construct {GetType().Name}-object. {nameof(bikesViewModel)} must not be null.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handles BikeInfoMutable events.
|
|
/// Helper member to raise events. Maps model event change notification to view model events.
|
|
/// </summary>
|
|
/// <param name="p_strNameOfProp"></param>
|
|
private void OnSelectedBikePropertyChanged(string p_strNameOfProp)
|
|
{
|
|
if (p_strNameOfProp == nameof(State))
|
|
{
|
|
OnSelectedBikeStateChanged(); // Notify derived class about change of state.
|
|
}
|
|
|
|
var state = State;
|
|
if (LastState != state)
|
|
{
|
|
RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(State)));
|
|
LastState = state;
|
|
}
|
|
|
|
var stateText = StateText;
|
|
if (LastStateText != stateText)
|
|
{
|
|
RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(StateText)));
|
|
LastStateText = stateText;
|
|
}
|
|
|
|
var stateColor = StateColor;
|
|
if (LastStateColor != stateColor)
|
|
{
|
|
RaisePropertyChanged(this, new PropertyChangedEventArgs(nameof(StateColor)));
|
|
LastStateColor = stateColor;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the display name of the bike containing of bike id and type of bike..
|
|
/// </summary>
|
|
public string Name => bike.GetDisplayName();
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the unique Id of bike or an empty string, if no name is defined to avoid duplicate display of id.
|
|
/// </summary>
|
|
public string DisplayId => bike.GetDisplayId();
|
|
|
|
/// <summary>
|
|
/// Gets the unique Id of bike used by derived model to determine which bike to remove.
|
|
/// </summary>
|
|
public string Id=> bike.Id;
|
|
|
|
/// <summary>
|
|
/// Returns status of a bike as text.
|
|
/// </summary>
|
|
/// <todo> Log invalid states for diagnose purposes.</todo>
|
|
public string StateText
|
|
{
|
|
get
|
|
{
|
|
switch (bike.State.Value)
|
|
{
|
|
case InUseStateEnum.Disposable:
|
|
return AppResources.StatusTextAvailable;
|
|
}
|
|
|
|
if (!ActiveUser.IsLoggedIn)
|
|
{
|
|
// Nobody is logged in.
|
|
switch (bike.State.Value)
|
|
{
|
|
case InUseStateEnum.Reserved:
|
|
return GetReservedInfo(
|
|
bike.State.RemainingTime,
|
|
bike.CurrentStation,
|
|
null); // Hide reservation code because no one but active user should see code
|
|
|
|
case InUseStateEnum.Booked:
|
|
return GetBookedInfo(
|
|
bike.State.From,
|
|
bike.CurrentStation,
|
|
null); // Hide reservation code because no one but active user should see code
|
|
|
|
default:
|
|
return string.Format("Unbekannter status {0}.", bike.State.Value);
|
|
}
|
|
}
|
|
|
|
switch (bike.State.Value)
|
|
{
|
|
case InUseStateEnum.Reserved:
|
|
return bike.State.MailAddress == ActiveUser.Mail
|
|
? GetReservedInfo(
|
|
bike.State.RemainingTime,
|
|
bike.CurrentStation,
|
|
bike.State.Code)
|
|
: "Fahrrad bereits reserviert durch anderen Nutzer.";
|
|
|
|
case InUseStateEnum.Booked:
|
|
return bike.State.MailAddress == ActiveUser.Mail
|
|
? GetBookedInfo(
|
|
bike.State.From,
|
|
bike.CurrentStation,
|
|
bike.State.Code)
|
|
: "Fahrrad bereits gebucht durch anderen Nutzer.";
|
|
|
|
default:
|
|
return string.Format("Unbekannter status {0}.", bike.State.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary> Gets the value of property <see cref="StateColor"/> when PropertyChanged was fired. </summary>
|
|
private string LastStateText { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets reserved into display text.
|
|
/// </summary>
|
|
/// <todo>Log unexpeced states.</todo>
|
|
/// <param name="p_oInUseState"></param>
|
|
/// <returns>Display text</returns>
|
|
private string GetReservedInfo(
|
|
TimeSpan? p_oRemainingTime,
|
|
string p_strStation = null,
|
|
string p_strCode = null)
|
|
{
|
|
return StateInfoProvider.GetReservedInfo(p_oRemainingTime, p_strStation, p_strCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets booked into display text.
|
|
/// </summary>
|
|
/// <todo>Log unexpeced states.</todo>
|
|
/// <param name="p_oInUseState"></param>
|
|
/// <returns>Display text</returns>
|
|
private string GetBookedInfo(
|
|
DateTime? p_oFrom,
|
|
string p_strStation = null,
|
|
string p_strCode = null)
|
|
{
|
|
return StateInfoProvider.GetBookedInfo(p_oFrom, p_strStation, p_strCode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Exposes the bike state.
|
|
/// </summary>
|
|
public InUseStateEnum State => bike.State.Value;
|
|
|
|
/// <summary> Gets the value of property <see cref="State"/> when PropertyChanged was fired. </summary>
|
|
public InUseStateEnum LastState { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets the color which visualizes the state of bike in relation to logged in user.
|
|
/// </summary>
|
|
public Color StateColor
|
|
{
|
|
get
|
|
{
|
|
if (!ActiveUser.IsLoggedIn)
|
|
{
|
|
return Color.Default;
|
|
}
|
|
|
|
var l_oSelectedBikeState = bike.State;
|
|
switch (l_oSelectedBikeState.Value)
|
|
{
|
|
case InUseStateEnum.Reserved:
|
|
return l_oSelectedBikeState.MailAddress == ActiveUser.Mail
|
|
? InUseStateEnum.Reserved.GetColor()
|
|
: Color.Red; // Bike is reserved by someone else
|
|
|
|
case InUseStateEnum.Booked:
|
|
return l_oSelectedBikeState.MailAddress == ActiveUser.Mail
|
|
? InUseStateEnum.Booked.GetColor()
|
|
: Color.Red; // Bike is booked by someone else
|
|
|
|
default:
|
|
return Color.Default;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary> Holds description about the tarif. </summary>
|
|
public TariffDescriptionViewModel TariffDescription => new TariffDescriptionViewModel(bike.TariffDescription);
|
|
|
|
/// <summary> Gets the value of property <see cref="StateColor"/> when PropertyChanged was fired. </summary>
|
|
public Color LastStateColor { get; set; }
|
|
|
|
}
|
|
}
|