import 'dart:convert'; import 'package:contacts_plus_plus/models/friend.dart'; class SettingsEntry { final T? value; final T deflt; const SettingsEntry({this.value, required this.deflt}); factory SettingsEntry.fromMap(Map map) { return SettingsEntry( value: jsonDecode(map["value"]) as T?, deflt: map["default"], ); } Map toMap() { return { "value": value.toString(), "default": deflt, }; } T get valueOrDefault => value ?? deflt; SettingsEntry withValue({required T newValue}) => SettingsEntry(value: newValue, deflt: deflt); SettingsEntry passThrough(T? newValue) { return newValue == null ? this : this.withValue(newValue: newValue); } } class Settings { final SettingsEntry notificationsDenied; final SettingsEntry unreadCheckIntervalMinutes; final SettingsEntry lastOnlineStatus; Settings({ SettingsEntry? notificationsDenied, SettingsEntry? unreadCheckIntervalMinutes, SettingsEntry? lastOnlineStatus, }) : notificationsDenied = notificationsDenied ?? const SettingsEntry(deflt: false), unreadCheckIntervalMinutes = unreadCheckIntervalMinutes ?? const SettingsEntry(deflt: 60), lastOnlineStatus = lastOnlineStatus ?? SettingsEntry(deflt: OnlineStatus.online.index); factory Settings.fromMap(Map map) { return Settings( notificationsDenied: retrieveEntryOrNull(map["notificationsDenied"]), unreadCheckIntervalMinutes: retrieveEntryOrNull(map["unreadCheckIntervalMinutes"]), lastOnlineStatus: retrieveEntryOrNull(map["lastOnlineStatus"]), ); } static SettingsEntry? retrieveEntryOrNull(Map? map) { if (map == null) return null; return SettingsEntry.fromMap(map); } Map toMap() { return { "notificationsDenied": notificationsDenied.toMap(), "unreadCheckIntervalMinutes": unreadCheckIntervalMinutes.toMap(), "lastOnlineStatus": lastOnlineStatus.toMap(), }; } Settings copy() => copyWith(); Settings copyWith({bool? notificationsDenied, int? unreadCheckIntervalMinutes, int? lastOnlineStatus}) { return Settings( notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied), unreadCheckIntervalMinutes: this.unreadCheckIntervalMinutes.passThrough(unreadCheckIntervalMinutes), lastOnlineStatus: this.lastOnlineStatus.passThrough(lastOnlineStatus), ); } }