mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-12 13:36:26 +01:00
113 lines
3.2 KiB
C#
113 lines
3.2 KiB
C#
using System;
|
|
using System.Runtime.Serialization;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace TINK.Model.State
|
|
{
|
|
/// <summary>
|
|
/// Manages state reserved.
|
|
/// </summary>
|
|
[DataContract]
|
|
public sealed class StateRequestedInfo : BaseState, IBaseState, INotAvailableState
|
|
{
|
|
// Maximum time while reserving request is kept.
|
|
public static readonly TimeSpan MaximumReserveTime = new TimeSpan(0, 15, 0); // 15 mins
|
|
|
|
// Reference to date time provider.
|
|
private Func<DateTime> _DateTimeNowProvider;
|
|
|
|
/// <summary>
|
|
/// Prevents an invalid instance to be created.
|
|
/// Used by serializer only.
|
|
/// </summary>
|
|
private StateRequestedInfo() : base(InUseStateEnum.Reserved)
|
|
{
|
|
// Is called in context of JSON deserialization.
|
|
_DateTimeNowProvider = () => DateTime.Now;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reservation performed with other device/ before start of app.
|
|
/// Date time info when bike was reserved has been received from webserver.
|
|
/// </summary>
|
|
/// <param name="p_oRemainingTime">Time span which holds duration how long bike still will be reserved.</param>
|
|
[JsonConstructor]
|
|
private StateRequestedInfo(
|
|
InUseStateEnum Value,
|
|
DateTime From,
|
|
string MailAddress,
|
|
string Code) : this(() => DateTime.Now, From, MailAddress, Code)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reservation performed with other device/ before start of app.
|
|
/// Date time info when bike was reserved has been received from webserver.
|
|
/// </summary>
|
|
/// <param name="dateTimeNowProvider">
|
|
/// Used to to provide current date time information for potential calls of <seealso cref="GetIsStillReserved"/>.
|
|
/// Not used to calculate remaining time because this duration whould always be shorter as the one received from webserver.
|
|
/// </param>
|
|
public StateRequestedInfo(
|
|
Func<DateTime> dateTimeNowProvider,
|
|
DateTime from,
|
|
string mailAddress,
|
|
string code) : base(InUseStateEnum.Reserved)
|
|
{
|
|
_DateTimeNowProvider = dateTimeNowProvider ?? (() => DateTime.Now);
|
|
From = from;
|
|
MailAddress = mailAddress;
|
|
Code = code;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries update
|
|
/// </summary>
|
|
/// <returns>True if reservation span has not exeeded and state remains reserved, false otherwise.</returns>
|
|
/// <todo>Implement logging of time stamps.</todo>
|
|
public bool GetIsStillReserved(out TimeSpan? p_oRemainingTime)
|
|
{
|
|
var l_oTimeReserved = _DateTimeNowProvider().Subtract(From);
|
|
if (l_oTimeReserved > MaximumReserveTime)
|
|
|
|
{
|
|
// Reservation has elapsed. To not update remaining time.
|
|
p_oRemainingTime = null;
|
|
return false;
|
|
}
|
|
|
|
p_oRemainingTime = MaximumReserveTime - l_oTimeReserved;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// State reserved.
|
|
/// Setter exists only for serialization purposes.
|
|
/// </summary>
|
|
public override InUseStateEnum Value
|
|
{
|
|
get
|
|
{
|
|
return InUseStateEnum.Reserved;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Date time when bike was reserved.
|
|
/// </summary>
|
|
[DataMember]
|
|
public DateTime From { get; }
|
|
|
|
/// <summary>
|
|
/// Mail address of user who reserved the bike.
|
|
/// </summary>
|
|
[DataMember]
|
|
public string MailAddress { get; }
|
|
|
|
/// <summary>
|
|
/// Booking code.
|
|
/// </summary>
|
|
[DataMember]
|
|
public string Code { get; }
|
|
}
|
|
}
|