using System; using System.Collections.Generic; using System.Linq; using Xamarin.Essentials; namespace TINK.Model { /// /// Single whats new entry. /// public struct WhatsNewEntry { public readonly Version Version; public readonly string Message; public readonly IEnumerable Flavors; public readonly IEnumerable Platforms; public WhatsNewEntry( string message, Version version, IEnumerable concernedFlavors, IEnumerable concernedPlatforms) { Version = version; Message = message; Flavors = concernedFlavors; Platforms = concernedPlatforms; } } /// /// Manges whats new messages. /// public class WhatsNewMessages : List, IEnumerable { /// /// Add a new entry to whats new messages /// /// When change was introduced. /// Text describing /// List of apps to which the message appiles. Null or empty list if message applies to all flavors. /// List of platforms to which the message applies. Null or empty list if message applies to all platorms. public void Add( Version version, string message, IEnumerable concernedFlavors = null, IEnumerable 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(), concernedPlatforms ?? new List())); } /// /// Filters messges for a given version of the app, a flavor of app and a given environment from object which holds all messages. /// /// /// A log message might apply to all app flavors (sharee.bike, Lastenrad Bayern, ...) all OS and ís assigned to a version. /// /// Flavor to filter for. /// Platform to filter for. /// Filtered messages. public Dictionary GetFilteredMessages( AppFlavor flavor, DevicePlatform platform, Version shownInVersionOrLastVersion, Version currentVersion) { if (shownInVersionOrLastVersion == null || shownInVersionOrLastVersion == new Version()) { // Initial install detected. return new Dictionary(); } if (currentVersion == null || currentVersion == new Version()) { // Invalid state detected. return new Dictionary(); } 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); } } }