sharee.bike-App/TINKLib/Model/WhatsNewMessages.cs
Anja Müller-Meißner 0468955d49 Version 3.0.338
2022-09-08 09:55:14 +02:00

105 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Essentials;
namespace TINK.Model
{
/// <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;
public WhatsNewEntry(
string message,
Version version,
IEnumerable<AppFlavor> concernedFlavors,
IEnumerable<DevicePlatform> concernedPlatforms)
{
Version = version;
Message = message;
Flavors = concernedFlavors;
Platforms = concernedPlatforms;
}
}
/// <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;
}
if (version == null || version == new Version())
{
// Igore enties which hold an invalid version.
return;
}
Add(new WhatsNewEntry(
message,
version,
concernedFlavors ?? new List<AppFlavor>(),
concernedPlatforms ?? new List<DevicePlatform>()));
}
/// <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>();
}
if (currentVersion == null || currentVersion == new Version())
{
// Invalid state detected.
return new Dictionary<Version, string>();
}
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);
}
}
}