sharee.bike-App/TINKLib/Model/State/StateInfoMutable.cs

298 lines
8.1 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System;
namespace TINK.Model.State
{
2022-09-06 16:08:19 +02:00
using System.ComponentModel;
using System.Runtime.Serialization;
/// <summary>
/// Manges the state of a bike.
/// </summary>
[DataContract]
public class StateInfoMutable : INotifyPropertyChanged, IStateInfoMutable
{
/// <summary>
/// Provider for current date time to calculate remainig time on demand for state of type reserved.
/// </summary>
private readonly Func<DateTime> _DateTimeNowProvider;
// Holds the current disposable state value
private StateInfo _StateInfo;
/// <summary>
/// Backs up remaining time of child object.
/// </summary>
private TimeSpan? _RemainingTime = null;
/// <summary> Notifies clients about state changes. </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Constructs a state object from source.
/// </summary>
/// <param name="state">State info to load from.</param>
public StateInfoMutable(
Func<DateTime> dateTimeNowProvider = null,
IStateInfo state = null)
{
// Back up date time provider to be able to pass this to requested- object if state changes to requested.
_DateTimeNowProvider = dateTimeNowProvider != null
? dateTimeNowProvider
: () => DateTime.Now;
_StateInfo = Create(state, dateTimeNowProvider);
}
/// <summary>
/// Loads state from immutable source.
/// </summary>
/// <param name="state">State to load from.</param>
public void Load(IStateInfo state)
{
if (state == null)
{
throw new ArgumentException("Can not load state info, object must not be null.");
}
// Back up last state value and remaining time value
// to be able to check whether an event has to be fired or not.
var l_oLastState = Value;
var l_oLastRemainingTime = _RemainingTime;
// Create new state info object from source.
_StateInfo = Create(state, _DateTimeNowProvider);
// Update remaining time value.
_StateInfo.GetIsStillReserved(out _RemainingTime);
if (l_oLastState == _StateInfo.Value
&& l_oLastRemainingTime == _RemainingTime)
{
return;
}
// State has changed, notify clients.
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
}
/// <summary>
/// Creates a state info object.
/// </summary>
/// <param name="state">State to load from.</param>
private static StateInfo Create(
IStateInfo state,
Func<DateTime> dateTimeNowProvider)
{
switch (state != null ? state.Value : InUseStateEnum.Disposable)
{
case InUseStateEnum.Disposable:
case InUseStateEnum.FeedbackPending:
return new StateInfo(state != null ? state.Value == InUseStateEnum.FeedbackPending : false);
case InUseStateEnum.Reserved:
// Todo: Handle p_oFrom == null here.
// Todo: Handle p_oDuration == null here.
return new StateInfo(
dateTimeNowProvider,
state.From.Value,
state.MailAddress,
state.Code);
case InUseStateEnum.Booked:
// Todo: Handle p_oFrom == null here.
// Todo: Clearify question: What to do if code changes form one value to another? This should never happen.
// Todo: Clearify question: What to do if from time changes form one value to another? This should never happen.
return new StateInfo(
state.From.Value,
state.MailAddress,
state.Code);
default:
// Todo: New state Busy has to be defined.
throw new Exception(string.Format("Can not create new state info object. Unknown state {0} detected.", state.Value));
}
}
/// <summary>
/// Gets the state value of object.
/// </summary>
public InUseStateEnum Value
=> _StateInfo.Value;
/// <summary>
/// Member for serialization purposes.
/// </summary>
[DataMember]
private BaseState StateInfoObject
{
get { return _StateInfo.StateInfoObject; }
set
{
var l_oStateOccupied = value as StateOccupiedInfo;
if (l_oStateOccupied != null)
{
_StateInfo = new StateInfo(l_oStateOccupied.From, l_oStateOccupied.MailAddress, l_oStateOccupied.Code);
return;
}
var l_oStateRequested = value as StateRequestedInfo;
if (l_oStateRequested != null)
{
_StateInfo = new StateInfo(_DateTimeNowProvider, l_oStateRequested.From, l_oStateRequested.MailAddress, l_oStateRequested.Code);
return;
}
_StateInfo = new StateInfo();
}
}
/// Transforms object to string.
/// </summary>
/// <returns></returns>
public new string ToString()
{
return _StateInfo.ToString();
}
/// <summary>
/// Checks and updates state if required.
/// </summary>
/// <returns>Value indicating wheter state has changed</returns>
public void UpdateOnTimeElapsed()
{
switch (_StateInfo.Value)
{
// State is disposable or booked. No need to update "OnTimeElapsed"
case InUseStateEnum.Disposable:
case InUseStateEnum.Booked:
return;
}
// Check if maximum reserved time has elapsed.
if (!_StateInfo.GetIsStillReserved(out _RemainingTime))
{
// Time has elapsed, switch state to disposable and notfiy client
_StateInfo = new StateInfo();
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
return;
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(RemainingTime)));
}
/// <summary> Updates state from webserver. </summary>
/// <param name="state">State of the bike.</param>
/// <param name="from">Date time when bike was reserved/ booked.</param>
/// <param name="mailAddress">Mailaddress of the one which reserved/ booked.</param>
/// <param name="code">Booking code if bike is booked or reserved.</param>
/// <param name="supressNotifyPropertyChanged">Controls whether notify property changed events are fired or not.</param>
public void Load(
InUseStateEnum state,
DateTime? from = null,
string mailAddress = null,
string code = null,
Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel notifyLevel = Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.All)
{
var lastState = _StateInfo.Value;
switch (state)
{
case InUseStateEnum.Disposable:
_StateInfo = new StateInfo();
// Set value to null. Otherwise potentially obsolete value will be taken remaining time.
_RemainingTime = null;
break;
case InUseStateEnum.Reserved:
// Todo: Handle p_oFrom == null here.
// Todo: Handle p_oDuration == null here.
_StateInfo = new StateInfo(
_DateTimeNowProvider,
from.Value,
mailAddress,
code);
// Set value to null. Otherwise potentially obsolete value will be taken remaining time.
_RemainingTime = null;
break;
case InUseStateEnum.Booked:
// Todo: Handle p_oFrom == null here.
// Todo: Clearify question: What to do if code changes form one value to another? This should never happen.
// Todo: Clearify question: What to do if from time changes form one value to another? This should never happen.
_StateInfo = new StateInfo(
from.Value,
mailAddress,
code);
// Set value to null. Otherwise potentially obsolete value will be taken remaining time.
_RemainingTime = null;
break;
default:
// Todo: New state Busy has to be defined.
break;
}
if (lastState != _StateInfo.Value
&& notifyLevel == Bikes.BikeInfoNS.BC.NotifyPropertyChangedLevel.All)
{
// State has changed, notify clients.
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
}
}
/// <summary>
/// If bike is reserved time raimaining while bike stays reserved, null otherwise.
/// </summary>
public TimeSpan? RemainingTime
{
get
{
switch (_StateInfo.Value)
{
// State is either available or occupied.
case InUseStateEnum.Disposable:
case InUseStateEnum.Booked:
return null;
}
if (_RemainingTime.HasValue == false)
{
// Value was not yet querried.
// Do querry before returning object.
_StateInfo.GetIsStillReserved(out _RemainingTime);
}
return _RemainingTime;
}
}
public DateTime? From
{
get
{
return _StateInfo.From;
}
}
public string MailAddress
{
get
{
return _StateInfo.MailAddress;
}
}
public string Code
{
get
{
return _StateInfo.Code;
}
}
}
2021-05-13 20:03:07 +02:00
}