import 'dart:convert'; import 'package:OpenContacts/models/sem_ver.dart'; import 'package:OpenContacts/models/users/online_status.dart'; import 'package:flutter/material.dart'; import 'package:uuid/uuid.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": jsonEncode(value), "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 lastOnlineStatus; final SettingsEntry lastDismissedVersion; final SettingsEntry machineId; final SettingsEntry themeMode; final SettingsEntry sessionViewLastMinimumUsers; final SettingsEntry sessionViewLastIncludeEnded; final SettingsEntry sessionViewLastIncludeEmpty; final SettingsEntry sessionViewLastIncludeIncompatible; Settings({ SettingsEntry? notificationsDenied, SettingsEntry? lastOnlineStatus, SettingsEntry? themeMode, SettingsEntry? lastDismissedVersion, SettingsEntry? machineId, SettingsEntry? sessionViewLastMinimumUsers, SettingsEntry? sessionViewLastIncludeEnded, SettingsEntry? sessionViewLastIncludeEmpty, SettingsEntry? sessionViewLastIncludeIncompatible, }) : notificationsDenied = notificationsDenied ?? const SettingsEntry(deflt: false), lastOnlineStatus = lastOnlineStatus ?? SettingsEntry(deflt: OnlineStatus.online.index), themeMode = themeMode ?? SettingsEntry(deflt: ThemeMode.dark.index), lastDismissedVersion = lastDismissedVersion ?? SettingsEntry(deflt: SemVer.zero().toString()), machineId = machineId ?? SettingsEntry(deflt: const Uuid().v4()), sessionViewLastMinimumUsers = sessionViewLastMinimumUsers ?? const SettingsEntry(deflt: 0), sessionViewLastIncludeEnded = sessionViewLastIncludeEnded ?? const SettingsEntry(deflt: false), sessionViewLastIncludeEmpty = sessionViewLastIncludeEmpty ?? const SettingsEntry(deflt: true), sessionViewLastIncludeIncompatible = sessionViewLastIncludeIncompatible ?? const SettingsEntry(deflt: false); factory Settings.fromMap(Map map) { return Settings( notificationsDenied: getEntryOrNull(map["notificationsDenied"]), lastOnlineStatus: getEntryOrNull(map["lastOnlineStatus"]), themeMode: getEntryOrNull(map["themeMode"]), lastDismissedVersion: getEntryOrNull(map["lastDismissedVersion"]), machineId: getEntryOrNull(map["machineId"]), sessionViewLastMinimumUsers: getEntryOrNull(map["sessionViewLastMinimumUsers"]), sessionViewLastIncludeEnded: getEntryOrNull(map["sessionViewLastIncludeEnded"]), sessionViewLastIncludeEmpty: getEntryOrNull(map["sessionViewLastIncludeEmpty"]), sessionViewLastIncludeIncompatible: getEntryOrNull(map["sessionViewLastIncludeIncompatible"]), ); } static SettingsEntry? getEntryOrNull(Map? map) { if (map == null) return null; try { return SettingsEntry.fromMap(map); } catch (_) { return null; } } Map toMap() { return { "notificationsDenied": notificationsDenied.toMap(), "lastOnlineStatus": lastOnlineStatus.toMap(), "themeMode": themeMode.toMap(), "lastDismissedVersion": lastDismissedVersion.toMap(), "machineId": machineId.toMap(), "sessionViewLastMinimumUsers": sessionViewLastMinimumUsers.toMap(), "sessionViewLastIncludeEnded": sessionViewLastIncludeEnded.toMap(), "sessionViewLastIncludeEmpty": sessionViewLastIncludeEmpty.toMap(), "sessionViewLastIncludeIncompatible": sessionViewLastIncludeIncompatible.toMap(), }; } Settings copy() => copyWith(); Settings copyWith({ bool? notificationsDenied, int? lastOnlineStatus, int? themeMode, String? lastDismissedVersion, String? machineId, int? sessionViewLastMinimumUsers, bool? sessionViewLastIncludeEnded, bool? sessionViewLastIncludeEmpty, bool? sessionViewLastIncludeIncompatible, }) { return Settings( notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied), lastOnlineStatus: this.lastOnlineStatus.passThrough(lastOnlineStatus), themeMode: this.themeMode.passThrough(themeMode), lastDismissedVersion: this.lastDismissedVersion.passThrough(lastDismissedVersion), machineId: this.machineId.passThrough(machineId), sessionViewLastMinimumUsers: this.sessionViewLastMinimumUsers.passThrough(sessionViewLastMinimumUsers), sessionViewLastIncludeEnded: this.sessionViewLastIncludeEnded.passThrough(sessionViewLastIncludeEnded), sessionViewLastIncludeEmpty: this.sessionViewLastIncludeEmpty.passThrough(sessionViewLastIncludeEmpty), sessionViewLastIncludeIncompatible: this.sessionViewLastIncludeIncompatible.passThrough(sessionViewLastIncludeIncompatible), ); } }