sharee.bike-App/SharedBusinessLogic/Model/WhatsNewMessages.cs

105 lines
3.3 KiB
C#
Raw Normal View History

2022-08-30 15:42:25 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Essentials;
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Model
2022-08-30 15:42:25 +02:00
{
2022-09-06 16:08:19 +02:00
/// <summary>
/// Single whats new entry.
/// </summary>
public struct WhatsNewEntry
{
public readonly Version Version;
public readonly string Message;
public readonly IEnumerable<AppFlavor> Flavors;
public readonly IEnumerable<DevicePlatform> Platforms;
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
public WhatsNewEntry(
string message,
Version version,
IEnumerable<AppFlavor> concernedFlavors,
IEnumerable<DevicePlatform> concernedPlatforms)
{
Version = version;
Message = message;
Flavors = concernedFlavors;
Platforms = concernedPlatforms;
}
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Manges whats new messages.
/// </summary>
public class WhatsNewMessages : List<WhatsNewEntry>, IEnumerable<WhatsNewEntry>
{
/// <summary>
/// Add a new entry to whats new messages
/// </summary>
/// <param name="version">When change was introduced.</param>
/// <param name="message">Text describing </param>
/// <param name="concernedFlavors">List of apps to which the message appiles. Null or empty list if message applies to all flavors.</param>
/// <param name="concernedPlatforms">List of platforms to which the message applies. Null or empty list if message applies to all platorms.</param>
public void Add(
Version version,
string message,
IEnumerable<AppFlavor> concernedFlavors = null,
IEnumerable<DevicePlatform> concernedPlatforms = null)
{
if (string.IsNullOrEmpty(message?.Trim()))
{
// Igore enties which hold not text.
return;
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
if (version == null || version == new Version())
{
// Igore enties which hold an invalid version.
return;
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
Add(new WhatsNewEntry(
message,
version,
concernedFlavors ?? new List<AppFlavor>(),
concernedPlatforms ?? new List<DevicePlatform>()));
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>
/// Filters messges for a given version of the app, a flavor of app and a given environment from object which holds all messages.
/// </summary>
/// <remarks>
/// A log message might apply to all app flavors (sharee.bike, Lastenrad Bayern, ...) all OS and ís assigned to a version.
/// </remarks>
/// <param name="flavor">Flavor to filter for.</param>
/// <param name="platform">Platform to filter for.</param>
/// <returns>Filtered messages.</returns>
public Dictionary<Version, string> GetFilteredMessages(
AppFlavor flavor,
DevicePlatform platform,
Version shownInVersionOrLastVersion,
Version currentVersion)
{
if (shownInVersionOrLastVersion == null
|| shownInVersionOrLastVersion == new Version())
{
// Initial install detected.
return new Dictionary<Version, string>();
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
if (currentVersion == null || currentVersion == new Version())
{
// Invalid state detected.
return new Dictionary<Version, string>();
}
2022-08-30 15:42:25 +02:00
2022-09-06 16:08:19 +02:00
return this.Where(element =>
shownInVersionOrLastVersion < element.Version /* do not show messages for old version */
&& element.Version <= currentVersion /* do not show message entered for future versions */
&& (element.Flavors.Count() == 0 || element.Flavors.Contains(flavor)) /* filter by app flavor */
&& (element.Platforms.Count() == 0 || element.Platforms.Contains(platform))/* filter by platorm */ )
.ToDictionary(element => element.Version, element => element.Message);
}
}
2022-08-30 15:42:25 +02:00
}