import 'dart:convert'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:time_progress_tracker/models/app_settings.dart'; class AppSettingsRepository { static const String _key = "app_settings"; final SharedPreferences prefs; final JsonCodec codec; AppSettingsRepository(this.prefs, {this.codec = json}); Future loadAppSettings() { final String jsonString = this.prefs.getString(_key); if (jsonString == null) return Future.value(AppSettingsEntity.defaults()); return Future.value( AppSettingsEntity.fromJson(codec.decode(jsonString))); } Future saveAppSettings(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() => AppSettings.defaults().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 toJson() => { _doneKey: doneColorValue, _leftKey: leftColorValue, _durationDaysKey: durationDays, }; static AppSettingsEntity fromJson(Map json) => AppSettingsEntity( json[_doneKey], json[_leftKey], json[_durationDaysKey], ); }