sharee.bike-App/TINKLib/Model/Bikes/BikeCollectionMutable.cs

169 lines
5.8 KiB
C#
Raw Normal View History

2022-01-04 18:59:16 +01:00
using Serilog;
using System;
2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
2022-01-04 18:59:16 +01:00
using TINK.Model.Station;
2021-05-13 20:03:07 +02:00
using BikeInfo = TINK.Model.Bike.BC.BikeInfo;
using BikeInfoMutable = TINK.Model.Bike.BC.BikeInfoMutable;
namespace TINK.Model.Bike
{
/// <summary> Holds entity of bikes. </summary>
public class BikeCollectionMutable : ObservableCollection<BikeInfoMutable>, IBikeDictionaryMutable<BikeInfoMutable>
{
/// <summary> Constructs a mutable bike collection object. </summary>
public BikeCollectionMutable()
{
SelectedBike = null;
}
/// <summary>
/// Updates bikes dictionary from bikes response, i.e.
/// - removes bikes which are no more contained in bikes response
/// - updates state of all bikes from state contained in bikes response
/// </summary>
2022-01-04 18:59:16 +01:00
/// <param name="bikesAll"> Object holding bikes info from copri to update from. Holds station id but not station name.</param>
/// <param name="stations"> All stations to get station names from.</param>
2021-05-13 20:03:07 +02:00
/// <param name="p_oDateTimeProvider">Provices date time information.</param>
2022-01-04 18:59:16 +01:00
public void Update(IEnumerable<BikeInfo> bikesAll,
IEnumerable<IStation> stations)
2021-05-13 20:03:07 +02:00
{
// Get list of current bikes by state(s) to update.
// Needed to remove bikes which switched state and have to be removed from collection.
var bikesToBeRemoved = this.Select(x => x.Id).ToList();
foreach (var bikeInfo in bikesAll ?? new List<BikeInfo>())
2021-05-13 20:03:07 +02:00
{
2022-01-04 18:59:16 +01:00
// Get name of station form station id.
var stationName = stations?.FirstOrDefault(x => x.Id == bikeInfo.StationId)?.StationName ?? string.Empty;
if (string.IsNullOrEmpty(stationName))
Log.ForContext<BikeCollectionMutable>().Debug($"No name for station with id {bikeInfo.StationId} found.");
// Check if bike has to be added to list of existing station.
2021-05-13 20:03:07 +02:00
if (ContainsKey(bikeInfo.Id) == false)
{
2022-04-25 22:15:15 +02:00
var bikeInfoMutable = BikeInfoMutableFactory.Create(bikeInfo, stationName);
if (bikeInfoMutable != null)
{
// Bike does not yet exist in list of bikes.
Add(bikeInfoMutable);
}
2021-05-13 20:03:07 +02:00
continue;
}
// Update bike.
2022-01-04 18:59:16 +01:00
GetById(bikeInfo.Id).State.Load(bikeInfo.State);
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
if (bikesToBeRemoved.Contains<string>(bikeInfo.Id))
2021-05-13 20:03:07 +02:00
{
// Remove list from obsolete list.
bikesToBeRemoved.Remove(bikeInfo.Id);
}
}
// Remove obsolete bikes.
2022-01-04 18:59:16 +01:00
foreach (var stationId in bikesToBeRemoved)
2021-05-13 20:03:07 +02:00
{
2022-01-04 18:59:16 +01:00
RemoveById(stationId);
2021-05-13 20:03:07 +02:00
}
}
/// <summary>
/// Adds a new bike to collecion of bike.
/// </summary>
2022-01-04 18:59:16 +01:00
/// <param name="newBike">New bike to add.</param>
2021-05-13 20:03:07 +02:00
/// <exception cref="Exception">Thrown if bike is not unique.</exception>
2022-01-04 18:59:16 +01:00
public new void Add(BikeInfoMutable newBike)
2021-05-13 20:03:07 +02:00
{
// Ensure that bike id of new bike is is unique
2022-01-04 18:59:16 +01:00
foreach (BikeInfoMutable bike in Items)
2021-05-13 20:03:07 +02:00
{
2022-01-04 18:59:16 +01:00
if (bike.Id == newBike.Id)
2021-05-13 20:03:07 +02:00
{
2022-01-04 18:59:16 +01:00
throw new Exception(string.Format("Can not add bike with {0} to collection ob bike. Id is not unnique.", newBike));
2021-05-13 20:03:07 +02:00
}
}
2022-01-04 18:59:16 +01:00
base.Add(newBike);
2021-05-13 20:03:07 +02:00
}
/// <summary>
/// Bike selected by user for regerving or cancel reservation.
/// </summary>
public BikeInfoMutable SelectedBike
{
get;
private set;
}
2021-06-26 20:57:55 +02:00
public void SetSelectedBike(string id)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
SelectedBike = GetById(id);
2021-05-13 20:03:07 +02:00
}
/// <summary>
/// Gets a bike by its id.
/// </summary>
2021-06-26 20:57:55 +02:00
/// <param name="id"></param>
2021-05-13 20:03:07 +02:00
/// <returns></returns>
2021-06-26 20:57:55 +02:00
public BikeInfoMutable GetById(string id)
2021-05-13 20:03:07 +02:00
{
{
2021-06-26 20:57:55 +02:00
return this.FirstOrDefault(bike => bike.Id == id);
2021-05-13 20:03:07 +02:00
}
}
/// <summary>
/// Deteermines whether a bike by given key exists.
/// </summary>
/// <param name="p_strKey">Key to check.</param>
/// <returns>True if bike exists.</returns>
2021-06-26 20:57:55 +02:00
public bool ContainsKey(string id)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
return GetById(id) != null;
2021-05-13 20:03:07 +02:00
}
/// <summary>
/// Removes a bike by its id.
/// </summary>
2021-06-26 20:57:55 +02:00
/// <param name="id">Id of bike to be removed.</param>
public void RemoveById(string id)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
var l_oBike = GetById(id);
2021-05-13 20:03:07 +02:00
if (l_oBike == null)
{
// Nothing to do if bike does not exists.
return;
}
Remove(l_oBike);
}
/// <summary>
/// Create mutable objects from immutable objects.
/// </summary>
2022-04-25 22:15:15 +02:00
public static class BikeInfoMutableFactory
2021-05-13 20:03:07 +02:00
{
2022-04-25 22:15:15 +02:00
public static BikeInfoMutable Create(
BikeInfo bikeInfo,
string stationName)
2021-05-13 20:03:07 +02:00
{
2022-04-25 22:15:15 +02:00
if (bikeInfo is BluetoothLock.BikeInfo btBikeInfo)
{
return new BluetoothLock.BikeInfoMutable(btBikeInfo, stationName);
}
else if (bikeInfo is CopriLock.BikeInfo copriBikeInfo)
{
return new CopriLock.BikeInfoMutable(copriBikeInfo, stationName);
}
// Unsupported type detected.
return null;
2021-05-13 20:03:07 +02:00
}
}
}
}