2023-05-04 07:13:24 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-05-07 09:01:01 -04:00
|
|
|
import 'package:contacts_plus_plus/models/sem_ver.dart';
|
2023-05-29 14:16:23 -04:00
|
|
|
import 'package:contacts_plus_plus/models/users/online_status.dart';
|
2023-05-25 13:49:09 -04:00
|
|
|
import 'package:flutter/material.dart';
|
2023-05-17 02:07:10 -04:00
|
|
|
import 'package:uuid/uuid.dart';
|
2023-05-05 10:39:40 -04:00
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
class SettingsEntry<T> {
|
|
|
|
final T? value;
|
|
|
|
final T deflt;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
const SettingsEntry({this.value, required this.deflt});
|
|
|
|
|
|
|
|
factory SettingsEntry.fromMap(Map map) {
|
|
|
|
return SettingsEntry<T>(
|
2023-05-04 07:13:24 -04:00
|
|
|
value: jsonDecode(map["value"]) as T?,
|
2023-05-03 17:24:27 -04:00
|
|
|
deflt: map["default"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map toMap() {
|
|
|
|
return {
|
2023-05-07 09:01:01 -04:00
|
|
|
"value": jsonEncode(value),
|
2023-05-03 17:24:27 -04:00
|
|
|
"default": deflt,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
T get valueOrDefault => value ?? deflt;
|
|
|
|
|
|
|
|
SettingsEntry<T> withValue({required T newValue}) => SettingsEntry(value: newValue, deflt: deflt);
|
|
|
|
|
|
|
|
SettingsEntry<T> passThrough(T? newValue) {
|
|
|
|
return newValue == null ? this : this.withValue(newValue: newValue);
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 11:51:18 -04:00
|
|
|
|
2023-05-03 17:24:27 -04:00
|
|
|
class Settings {
|
|
|
|
final SettingsEntry<bool> notificationsDenied;
|
2023-05-05 10:39:40 -04:00
|
|
|
final SettingsEntry<int> lastOnlineStatus;
|
2023-05-07 09:01:01 -04:00
|
|
|
final SettingsEntry<String> lastDismissedVersion;
|
2023-05-17 02:07:10 -04:00
|
|
|
final SettingsEntry<String> machineId;
|
2023-05-25 13:49:09 -04:00
|
|
|
final SettingsEntry<int> themeMode;
|
2023-07-11 12:44:20 -04:00
|
|
|
final SettingsEntry<int> sessionViewLastMinimumUsers;
|
|
|
|
final SettingsEntry<bool> sessionViewLastIncludeEnded;
|
|
|
|
final SettingsEntry<bool> sessionViewLastIncludeEmpty;
|
|
|
|
final SettingsEntry<bool> sessionViewLastIncludeIncompatible;
|
2023-05-03 15:55:34 -04:00
|
|
|
|
2023-05-05 10:39:40 -04:00
|
|
|
Settings({
|
|
|
|
SettingsEntry<bool>? notificationsDenied,
|
|
|
|
SettingsEntry<int>? lastOnlineStatus,
|
2023-05-25 13:49:09 -04:00
|
|
|
SettingsEntry<int>? themeMode,
|
2023-05-17 02:07:10 -04:00
|
|
|
SettingsEntry<String>? lastDismissedVersion,
|
2023-07-11 12:44:20 -04:00
|
|
|
SettingsEntry<String>? machineId,
|
|
|
|
SettingsEntry<int>? sessionViewLastMinimumUsers,
|
|
|
|
SettingsEntry<bool>? sessionViewLastIncludeEnded,
|
|
|
|
SettingsEntry<bool>? sessionViewLastIncludeEmpty,
|
|
|
|
SettingsEntry<bool>? sessionViewLastIncludeIncompatible,
|
|
|
|
}) : notificationsDenied = notificationsDenied ?? const SettingsEntry<bool>(deflt: false),
|
2023-05-07 09:01:01 -04:00
|
|
|
lastOnlineStatus = lastOnlineStatus ?? SettingsEntry<int>(deflt: OnlineStatus.online.index),
|
2023-05-25 13:49:09 -04:00
|
|
|
themeMode = themeMode ?? SettingsEntry<int>(deflt: ThemeMode.dark.index),
|
2023-05-17 02:07:10 -04:00
|
|
|
lastDismissedVersion = lastDismissedVersion ?? SettingsEntry<String>(deflt: SemVer.zero().toString()),
|
2023-07-11 12:44:20 -04:00
|
|
|
machineId = machineId ?? SettingsEntry<String>(deflt: const Uuid().v4()),
|
|
|
|
sessionViewLastMinimumUsers = sessionViewLastMinimumUsers ?? const SettingsEntry<int>(deflt: 0),
|
|
|
|
sessionViewLastIncludeEnded = sessionViewLastIncludeEnded ?? const SettingsEntry<bool>(deflt: false),
|
|
|
|
sessionViewLastIncludeEmpty = sessionViewLastIncludeEmpty ?? const SettingsEntry<bool>(deflt: true),
|
|
|
|
sessionViewLastIncludeIncompatible =
|
|
|
|
sessionViewLastIncludeIncompatible ?? const SettingsEntry<bool>(deflt: false);
|
2023-05-03 15:55:34 -04:00
|
|
|
|
|
|
|
factory Settings.fromMap(Map map) {
|
|
|
|
return Settings(
|
2023-07-11 12:44:20 -04:00
|
|
|
notificationsDenied: getEntryOrNull<bool>(map["notificationsDenied"]),
|
|
|
|
lastOnlineStatus: getEntryOrNull<int>(map["lastOnlineStatus"]),
|
|
|
|
themeMode: getEntryOrNull<int>(map["themeMode"]),
|
|
|
|
lastDismissedVersion: getEntryOrNull<String>(map["lastDismissedVersion"]),
|
|
|
|
machineId: getEntryOrNull<String>(map["machineId"]),
|
|
|
|
sessionViewLastMinimumUsers: getEntryOrNull<int>(map["sessionViewLastMinimumUsers"]),
|
|
|
|
sessionViewLastIncludeEnded: getEntryOrNull<bool>(map["sessionViewLastIncludeEnded"]),
|
|
|
|
sessionViewLastIncludeEmpty: getEntryOrNull<bool>(map["sessionViewLastIncludeEmpty"]),
|
|
|
|
sessionViewLastIncludeIncompatible: getEntryOrNull<bool>(map["sessionViewLastIncludeIncompatible"]),
|
2023-05-03 15:55:34 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-11 12:44:20 -04:00
|
|
|
static SettingsEntry<T>? getEntryOrNull<T>(Map? map) {
|
2023-05-05 10:39:40 -04:00
|
|
|
if (map == null) return null;
|
2023-05-07 09:01:01 -04:00
|
|
|
try {
|
|
|
|
return SettingsEntry<T>.fromMap(map);
|
|
|
|
} catch (_) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-05 10:39:40 -04:00
|
|
|
}
|
|
|
|
|
2023-05-03 15:55:34 -04:00
|
|
|
Map toMap() {
|
|
|
|
return {
|
2023-05-03 17:24:27 -04:00
|
|
|
"notificationsDenied": notificationsDenied.toMap(),
|
2023-05-05 10:39:40 -04:00
|
|
|
"lastOnlineStatus": lastOnlineStatus.toMap(),
|
2023-05-25 13:49:09 -04:00
|
|
|
"themeMode": themeMode.toMap(),
|
2023-05-07 09:01:01 -04:00
|
|
|
"lastDismissedVersion": lastDismissedVersion.toMap(),
|
2023-05-17 02:07:10 -04:00
|
|
|
"machineId": machineId.toMap(),
|
2023-07-11 12:44:20 -04:00
|
|
|
"sessionViewLastMinimumUsers": sessionViewLastMinimumUsers.toMap(),
|
|
|
|
"sessionViewLastIncludeEnded": sessionViewLastIncludeEnded.toMap(),
|
|
|
|
"sessionViewLastIncludeEmpty": sessionViewLastIncludeEmpty.toMap(),
|
|
|
|
"sessionViewLastIncludeIncompatible": sessionViewLastIncludeIncompatible.toMap(),
|
2023-05-03 15:55:34 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings copy() => copyWith();
|
|
|
|
|
2023-05-07 09:01:01 -04:00
|
|
|
Settings copyWith({
|
|
|
|
bool? notificationsDenied,
|
|
|
|
int? lastOnlineStatus,
|
2023-05-25 13:49:09 -04:00
|
|
|
int? themeMode,
|
2023-05-07 09:01:01 -04:00
|
|
|
String? lastDismissedVersion,
|
2023-05-17 02:07:10 -04:00
|
|
|
String? machineId,
|
2023-07-11 12:44:20 -04:00
|
|
|
int? sessionViewLastMinimumUsers,
|
|
|
|
bool? sessionViewLastIncludeEnded,
|
|
|
|
bool? sessionViewLastIncludeEmpty,
|
|
|
|
bool? sessionViewLastIncludeIncompatible,
|
2023-05-07 09:01:01 -04:00
|
|
|
}) {
|
2023-05-03 15:55:34 -04:00
|
|
|
return Settings(
|
2023-05-03 17:24:27 -04:00
|
|
|
notificationsDenied: this.notificationsDenied.passThrough(notificationsDenied),
|
2023-05-05 10:39:40 -04:00
|
|
|
lastOnlineStatus: this.lastOnlineStatus.passThrough(lastOnlineStatus),
|
2023-05-25 13:49:09 -04:00
|
|
|
themeMode: this.themeMode.passThrough(themeMode),
|
2023-05-07 09:01:01 -04:00
|
|
|
lastDismissedVersion: this.lastDismissedVersion.passThrough(lastDismissedVersion),
|
2023-05-17 02:07:10 -04:00
|
|
|
machineId: this.machineId.passThrough(machineId),
|
2023-07-11 12:44:20 -04:00
|
|
|
sessionViewLastMinimumUsers: this.sessionViewLastMinimumUsers.passThrough(sessionViewLastMinimumUsers),
|
|
|
|
sessionViewLastIncludeEnded: this.sessionViewLastIncludeEnded.passThrough(sessionViewLastIncludeEnded),
|
|
|
|
sessionViewLastIncludeEmpty: this.sessionViewLastIncludeEmpty.passThrough(sessionViewLastIncludeEmpty),
|
|
|
|
sessionViewLastIncludeIncompatible:
|
|
|
|
this.sessionViewLastIncludeIncompatible.passThrough(sessionViewLastIncludeIncompatible),
|
2023-05-03 15:55:34 -04:00
|
|
|
);
|
|
|
|
}
|
2023-05-03 11:51:18 -04:00
|
|
|
}
|