using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.Tasks; using gehGassiApp.Domain.Common; namespace gehGassiApp.Domain.Pushnotifications { /// /// Hilfsklasse zum abspeichern der referenz-Info für Server PushNotifications /// public class ServerPushNotificationInfo { /// /// Title der Nachricht /// [JsonPropertyName("title")] public string Title { get; set; } /// /// Inhalt der Nachricht /// [JsonPropertyName("body")] public string Body { get; set; } /// /// Daten der Nachricht /// [JsonPropertyName("data")] public string Data { get; set; } /// /// Typ des Appusers damit man bei jenen die beides sind auch unterscheiden kann /// [JsonPropertyName("appMode")] [JsonConverter(typeof(AppModeConverter))] public AppMode? AppMode { get; set; } /// /// Id des verknüpften Objektes /// [JsonPropertyName("key")] public string Key { get; set; } /// /// Typ des Objektes das verbunden wurde /// [JsonPropertyName("table")] public string Table { get; set; } /// /// Typ der Systemnachricht /// [JsonPropertyName("type")] [JsonConverter(typeof(SystemMessageTypeConverter))] public SystemMessageType? Type { get; set; } } public class SystemMessageTypeConverter : JsonConverter { public override SystemMessageType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { var value = reader.GetInt32(); return (SystemMessageType)value; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } try { var stringVal = reader.GetString(); if (string.IsNullOrEmpty(stringVal)) { return null; } var intVal = int.Parse(stringVal); return (SystemMessageType)intVal; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return null; } public override void Write(Utf8JsonWriter writer, SystemMessageType? value, JsonSerializerOptions options) { if (value == null) { writer.WriteNullValue(); return; } var intVal = (int)value; writer.WriteNumberValue(intVal); } } public class AppModeConverter : JsonConverter { public override AppMode? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { try { var value = reader.GetInt32(); return (AppMode)value; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } try { var stringVal = reader.GetString(); if (string.IsNullOrEmpty(stringVal)) { return null; } var intVal = int.Parse(stringVal); return (AppMode)intVal; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return null; } public override void Write(Utf8JsonWriter writer, AppMode? value, JsonSerializerOptions options) { if (value == null) { writer.WriteNullValue(); return; } var intVal = (int)value; writer.WriteNumberValue(intVal); } } }