mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
119 lines
3.6 KiB
C#
119 lines
3.6 KiB
C#
using System;
|
|
using System.Runtime.Serialization;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace ShareeBike.Model.State
|
|
{
|
|
/// <summary>
|
|
/// Manages state reserved.
|
|
/// </summary>
|
|
[DataContract]
|
|
public sealed class StateRequestedInfo : BaseState, IBaseState, INotAvailableState
|
|
{
|
|
/// <summary>
|
|
/// Holds the value if time span is not known.
|
|
/// </summary>
|
|
/// <remarks> Default value see <see cref="TextToTypeHelper.DEFAULTMAXRESERVATIONTIMESPAN"/>. </remarks>
|
|
public static TimeSpan UNKNOWNMAXRESERVATIONTIMESPAN = TimeSpan.Zero;
|
|
|
|
// Reference to date time provider.
|
|
private readonly Func<DateTime> _DateTimeNowProvider;
|
|
|
|
/// <summary>
|
|
/// Time span for which a bike can be reserved. Default value is zero.
|
|
/// </summary>
|
|
public TimeSpan MaxReservationTimeSpan { get; private set; } = UNKNOWNMAXRESERVATIONTIMESPAN;
|
|
|
|
/// <summary>
|
|
/// Prevents an invalid instance to be created.
|
|
/// Used by serializer only.
|
|
/// </summary>
|
|
internal 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 web server.
|
|
/// </summary>
|
|
/// <param name="maxReservationTimeSpan">Time span for which a bike can be reserved.</param>
|
|
[JsonConstructor]
|
|
private StateRequestedInfo(
|
|
InUseStateEnum Value,
|
|
DateTime From,
|
|
TimeSpan maxReservationTimeSpan,
|
|
string MailAddress,
|
|
string Code) : this(() => DateTime.Now, From, maxReservationTimeSpan, MailAddress, Code)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reservation performed with other device/ before start of app.
|
|
/// Date time info when bike was reserved has been received from web server.
|
|
/// </summary>
|
|
/// <param name="dateTimeNowProvider">
|
|
/// Used to provide current date time information for potential calls of <seealso cref="GetIsStillReserved"/>.
|
|
/// Not used to calculate remaining time because this duration would always be shorter as the one received from web server.
|
|
/// </param>
|
|
/// <param name="maxReservationTimeSpan">Time span for which a bike can be reserved.</param>
|
|
public StateRequestedInfo(
|
|
Func<DateTime> dateTimeNowProvider,
|
|
DateTime from,
|
|
TimeSpan maxReservationTimeSpan,
|
|
string mailAddress,
|
|
string code) : base(InUseStateEnum.Reserved)
|
|
{
|
|
_DateTimeNowProvider = dateTimeNowProvider ?? (() => DateTime.Now);
|
|
MaxReservationTimeSpan = maxReservationTimeSpan;
|
|
From = from;
|
|
MailAddress = mailAddress;
|
|
Code = code;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tries update
|
|
/// </summary>
|
|
/// <returns>True if reservation span has not exceeded and state remains reserved, false otherwise.</returns>
|
|
/// <todo>Implement logging of time stamps.</todo>
|
|
public bool GetIsStillReserved(out TimeSpan? remainingTime)
|
|
{
|
|
var timeReserved = _DateTimeNowProvider().Subtract(From);
|
|
if (timeReserved > MaxReservationTimeSpan)
|
|
|
|
{
|
|
// Reservation has elapsed. Do not update remaining time.
|
|
remainingTime = null;
|
|
return false;
|
|
}
|
|
|
|
remainingTime = MaxReservationTimeSpan - timeReserved;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// State reserved.
|
|
/// </summary>
|
|
public override InUseStateEnum Value =>
|
|
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; }
|
|
}
|
|
}
|