sharee.bike-App/SharedBusinessLogic/Model/State/StateRequestedInfo.cs

120 lines
3.6 KiB
C#
Raw Normal View History

2023-06-06 12:00:24 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Runtime.Serialization;
2022-08-30 15:42:25 +02:00
using Newtonsoft.Json;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Model.State
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
/// <summary>
/// Manages state reserved.
/// </summary>
[DataContract]
public sealed class StateRequestedInfo : BaseState, IBaseState, INotAvailableState
{
2023-06-06 12:00:24 +02:00
/// <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;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
// Reference to date time provider.
2023-06-06 12:00:24 +02:00
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;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Prevents an invalid instance to be created.
/// Used by serializer only.
/// </summary>
2023-06-06 12:00:24 +02:00
internal StateRequestedInfo() : base(InUseStateEnum.Reserved)
2022-09-06 16:08:19 +02:00
{
// Is called in context of JSON deserialization.
_DateTimeNowProvider = () => DateTime.Now;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
2023-06-06 12:00:24 +02:00
/// Reservation performed with other device/ before start of app.
/// Date time info when bike was reserved has been received from web server.
2022-09-06 16:08:19 +02:00
/// </summary>
2023-06-06 12:00:24 +02:00
/// <param name="maxReservationTimeSpan">Time span for which a bike can be reserved.</param>
2022-09-06 16:08:19 +02:00
[JsonConstructor]
private StateRequestedInfo(
InUseStateEnum Value,
DateTime From,
2023-06-06 12:00:24 +02:00
TimeSpan maxReservationTimeSpan,
2022-09-06 16:08:19 +02:00
string MailAddress,
2023-06-06 12:00:24 +02:00
string Code) : this(() => DateTime.Now, From, maxReservationTimeSpan, MailAddress, Code)
2022-09-06 16:08:19 +02:00
{
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Reservation performed with other device/ before start of app.
2023-06-06 12:00:24 +02:00
/// Date time info when bike was reserved has been received from web server.
2022-09-06 16:08:19 +02:00
/// </summary>
/// <param name="dateTimeNowProvider">
2023-06-06 12:00:24 +02:00
/// 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.
2022-09-06 16:08:19 +02:00
/// </param>
2023-06-06 12:00:24 +02:00
/// <param name="maxReservationTimeSpan">Time span for which a bike can be reserved.</param>
2022-09-06 16:08:19 +02:00
public StateRequestedInfo(
Func<DateTime> dateTimeNowProvider,
DateTime from,
2023-06-06 12:00:24 +02:00
TimeSpan maxReservationTimeSpan,
2022-09-06 16:08:19 +02:00
string mailAddress,
string code) : base(InUseStateEnum.Reserved)
{
_DateTimeNowProvider = dateTimeNowProvider ?? (() => DateTime.Now);
2023-06-06 12:00:24 +02:00
MaxReservationTimeSpan = maxReservationTimeSpan;
2022-09-06 16:08:19 +02:00
From = from;
MailAddress = mailAddress;
Code = code;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Tries update
/// </summary>
2023-06-06 12:00:24 +02:00
/// <returns>True if reservation span has not exceeded and state remains reserved, false otherwise.</returns>
2022-09-06 16:08:19 +02:00
/// <todo>Implement logging of time stamps.</todo>
2023-06-06 12:00:24 +02:00
public bool GetIsStillReserved(out TimeSpan? remainingTime)
2022-09-06 16:08:19 +02:00
{
2023-06-06 12:00:24 +02:00
var timeReserved = _DateTimeNowProvider().Subtract(From);
if (timeReserved > MaxReservationTimeSpan)
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
{
2023-06-06 12:00:24 +02:00
// Reservation has elapsed. Do not update remaining time.
remainingTime = null;
2022-09-06 16:08:19 +02:00
return false;
}
2021-05-13 20:03:07 +02:00
2023-06-06 12:00:24 +02:00
remainingTime = MaxReservationTimeSpan - timeReserved;
2022-09-06 16:08:19 +02:00
return true;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
2023-06-06 12:00:24 +02:00
/// State reserved.
2022-09-06 16:08:19 +02:00
/// </summary>
2023-06-06 12:00:24 +02:00
public override InUseStateEnum Value =>
InUseStateEnum.Reserved;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Date time when bike was reserved.
/// </summary>
[DataMember]
public DateTime From { get; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Mail address of user who reserved the bike.
/// </summary>
[DataMember]
public string MailAddress { get; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Booking code.
/// </summary>
[DataMember]
public string Code { get; }
}
2021-05-13 20:03:07 +02:00
}