Andreas Fahrecker 08db53db20 Updated Flutter SDK for Null-Safety
Ported Progress Detail Screen to PlatformScaffold

Signed-off-by: Andreas Fahrecker <AndreasFahrecker@gmail.com>
2021-03-18 18:34:29 +01:00

60 lines
1.9 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:time_progress_tracker/models/app_settings.dart';
import 'package:time_progress_tracker/persistence/repository.dart';
import 'package:time_progress_tracker/utils/constants.dart';
class AppSettingsRepository extends Repository<AppSettingsEntity> {
static const String _key = "app_settings";
AppSettingsRepository(SharedPreferences prefs) : super(prefs);
@override
Future<AppSettingsEntity> load() {
final String? jsonString = this.prefs.getString(_key);
if (jsonString == null)
return Future<AppSettingsEntity>.value(AppSettingsEntity.defaults());
return Future<AppSettingsEntity>.value(
AppSettingsEntity.fromJson(codec.decode(jsonString)));
}
@override
Future<bool> save(AppSettingsEntity appSettings) =>
this.prefs.setString(_key, codec.encode(appSettings));
}
class AppSettingsEntity {
static const String _doneKey = "doneColorValue",
_leftKey = "leftColorValue",
_durationDaysKey = "durationDays";
final int doneColorValue, leftColorValue, durationDays;
AppSettingsEntity(
this.doneColorValue, this.leftColorValue, this.durationDays);
factory AppSettingsEntity.defaults() => defaultAppSettings.toEntity();
@override
int get hashCode => doneColorValue.hashCode ^ leftColorValue.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is AppSettingsEntity &&
runtimeType == other.runtimeType &&
doneColorValue == other.doneColorValue &&
leftColorValue == other.leftColorValue;
Map<String, Object> toJson() => {
_doneKey: doneColorValue,
_leftKey: leftColorValue,
_durationDaysKey: durationDays,
};
static AppSettingsEntity fromJson(Map<String, Object> json) =>
AppSettingsEntity(
json[_doneKey] as int,
json[_leftKey] as int,
json[_durationDaysKey] as int,
);
}