Cleaned up the repo
Singed-off-by: Andreas Fahrecker <AndreasFahrecker@gmail.com>
This commit is contained in:
@ -1,16 +1,14 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:time_progress_tracker/models/app_settings.dart';
|
||||
import 'package:time_progress_tracker/persistence/repository.dart';
|
||||
|
||||
class AppSettingsRepository {
|
||||
class AppSettingsRepository extends Repository<AppSettingsEntity> {
|
||||
static const String _key = "app_settings";
|
||||
final SharedPreferences prefs;
|
||||
final JsonCodec codec;
|
||||
|
||||
AppSettingsRepository(this.prefs, {this.codec = json});
|
||||
AppSettingsRepository(SharedPreferences prefs) : super(prefs);
|
||||
|
||||
Future<AppSettingsEntity> loadAppSettings() {
|
||||
@override
|
||||
Future<AppSettingsEntity> load() {
|
||||
final String jsonString = this.prefs.getString(_key);
|
||||
if (jsonString == null)
|
||||
return Future<AppSettingsEntity>.value(AppSettingsEntity.defaults());
|
||||
@ -18,7 +16,8 @@ class AppSettingsRepository {
|
||||
AppSettingsEntity.fromJson(codec.decode(jsonString)));
|
||||
}
|
||||
|
||||
Future<bool> saveAppSettings(AppSettingsEntity appSettings) =>
|
||||
@override
|
||||
Future<bool> save(AppSettingsEntity appSettings) =>
|
||||
this.prefs.setString(_key, codec.encode(appSettings));
|
||||
}
|
||||
|
||||
|
14
lib/persistence/repository.dart
Normal file
14
lib/persistence/repository.dart
Normal file
@ -0,0 +1,14 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
abstract class Repository<T> {
|
||||
final SharedPreferences prefs;
|
||||
final JsonCodec codec;
|
||||
|
||||
Repository(this.prefs, {this.codec = json});
|
||||
|
||||
Future<T> load();
|
||||
|
||||
Future<bool> save(T e);
|
||||
}
|
74
lib/persistence/time_progress.dart
Normal file
74
lib/persistence/time_progress.dart
Normal file
@ -0,0 +1,74 @@
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:time_progress_tracker/persistence/repository.dart';
|
||||
|
||||
class TimeProgressRepository extends Repository<List<TimeProgressEntity>> {
|
||||
static const String _key = "time_progress_repo";
|
||||
|
||||
TimeProgressRepository(SharedPreferences prefs) : super(prefs);
|
||||
|
||||
@override
|
||||
Future<List<TimeProgressEntity>> load() {
|
||||
final String jsonString = this.prefs.getString(_key);
|
||||
if (jsonString == null) {
|
||||
return Future<List<TimeProgressEntity>>.value([]);
|
||||
}
|
||||
return Future<List<TimeProgressEntity>>.value(codec
|
||||
.decode(jsonString)["timers"]
|
||||
.cast<Map<String, Object>>()
|
||||
.map<TimeProgressEntity>(TimeProgressEntity.fromJson)
|
||||
.toList(growable: false));
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> save(List<TimeProgressEntity> timeProgressList) {
|
||||
final String jsonString = codec.encode(
|
||||
{"timers": timeProgressList.map((timer) => timer.toJson()).toList()});
|
||||
return this.prefs.setString(_key, jsonString);
|
||||
}
|
||||
}
|
||||
|
||||
class TimeProgressEntity {
|
||||
static const String _idKey = "id",
|
||||
_nameKey = "name",
|
||||
_startTimeKey = "startTime",
|
||||
_endTimeKey = "endTime";
|
||||
final String id;
|
||||
final String name;
|
||||
final DateTime startTime;
|
||||
final DateTime endTime;
|
||||
|
||||
TimeProgressEntity(this.id, this.name, this.startTime, this.endTime);
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
id.hashCode ^ name.hashCode ^ startTime.hashCode ^ endTime.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is TimeProgressEntity &&
|
||||
runtimeType == other.runtimeType &&
|
||||
id == other.id &&
|
||||
name == other.name &&
|
||||
startTime == other.startTime &&
|
||||
endTime == other.endTime;
|
||||
|
||||
Map<String, Object> toJson() {
|
||||
return {
|
||||
_idKey: id,
|
||||
_nameKey: name,
|
||||
_startTimeKey: startTime.millisecondsSinceEpoch,
|
||||
_endTimeKey: endTime.millisecondsSinceEpoch
|
||||
};
|
||||
}
|
||||
|
||||
static TimeProgressEntity fromJson(Map<String, Object> json) {
|
||||
final String id = json[_idKey] as String;
|
||||
final String name = json[_nameKey] as String;
|
||||
final DateTime startTime =
|
||||
DateTime.fromMillisecondsSinceEpoch(json[_startTimeKey] as int);
|
||||
final DateTime endTime =
|
||||
DateTime.fromMillisecondsSinceEpoch(json[_endTimeKey] as int);
|
||||
return TimeProgressEntity(id, name, startTime, endTime);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
class TimeProgressEntity {
|
||||
final String id;
|
||||
final String name;
|
||||
final DateTime startTime;
|
||||
final DateTime endTime;
|
||||
|
||||
TimeProgressEntity(this.id, this.name, this.startTime, this.endTime);
|
||||
|
||||
@override
|
||||
int get hashCode =>
|
||||
id.hashCode ^ name.hashCode ^ startTime.hashCode ^ endTime.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is TimeProgressEntity &&
|
||||
runtimeType == other.runtimeType &&
|
||||
id == other.id &&
|
||||
name == other.name &&
|
||||
startTime == other.startTime &&
|
||||
endTime == other.endTime;
|
||||
|
||||
Map<String, Object> toJson() {
|
||||
return {
|
||||
"id": id,
|
||||
"name": name,
|
||||
"startTime": startTime.millisecondsSinceEpoch,
|
||||
"endTime": endTime.millisecondsSinceEpoch
|
||||
};
|
||||
}
|
||||
|
||||
static TimeProgressEntity fromJson(Map<String, Object> json) {
|
||||
final String id = json["id"] as String;
|
||||
final String name = json["name"] as String;
|
||||
final DateTime startTime =
|
||||
DateTime.fromMillisecondsSinceEpoch(json["startTime"] as int);
|
||||
final DateTime endTime =
|
||||
DateTime.fromMillisecondsSinceEpoch(json["endTime"] as int);
|
||||
return TimeProgressEntity(id, name, startTime, endTime);
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:time_progress_tracker/persistence/time_progress_entity.dart';
|
||||
|
||||
class TimeProgressRepository {
|
||||
static const String _key = "time_progress_repo";
|
||||
final SharedPreferences prefs;
|
||||
final JsonCodec codec;
|
||||
|
||||
TimeProgressRepository(this.prefs, {this.codec = json});
|
||||
|
||||
Future<List<TimeProgressEntity>> loadTimeProgressList() {
|
||||
final String jsonString = this.prefs.getString(_key);
|
||||
if (jsonString == null) {
|
||||
return Future<List<TimeProgressEntity>>.value([]);
|
||||
}
|
||||
return Future<List<TimeProgressEntity>>.value(codec
|
||||
.decode(jsonString)["timers"]
|
||||
.cast<Map<String, Object>>()
|
||||
.map<TimeProgressEntity>(TimeProgressEntity.fromJson)
|
||||
.toList(growable: false));
|
||||
}
|
||||
|
||||
Future<bool> saveTimeProgressList(List<TimeProgressEntity> timeProgressList) {
|
||||
final String jsonString = codec.encode(
|
||||
{"timers": timeProgressList.map((timer) => timer.toJson()).toList()});
|
||||
return this.prefs.setString(_key, jsonString);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user