mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-03-13 18:58:53 +01:00
Refactor: simplify constructing album upgrader
This commit is contained in:
parent
0eef1a88cc
commit
b6a9ff2d1d
4 changed files with 81 additions and 61 deletions
|
@ -143,11 +143,10 @@ class AppDbAlbumEntry {
|
|||
json["index"],
|
||||
Album.fromJson(
|
||||
json["album"].cast<String, dynamic>(),
|
||||
upgraderV1: AlbumUpgraderV1(logFilePath: json["path"]),
|
||||
upgraderV2: AlbumUpgraderV2(logFilePath: json["path"]),
|
||||
upgraderV3: AlbumUpgraderV3(logFilePath: json["path"]),
|
||||
upgraderV4: AlbumUpgraderV4(logFilePath: json["path"]),
|
||||
upgraderV5: AlbumUpgraderV5(account, logFilePath: json["path"]),
|
||||
upgraderFactory: DefaultAlbumUpgraderFactory(
|
||||
account: account,
|
||||
logFilePath: json["path"],
|
||||
),
|
||||
)!,
|
||||
);
|
||||
}
|
||||
|
|
|
@ -40,44 +40,40 @@ class Album with EquatableMixin {
|
|||
|
||||
static Album? fromJson(
|
||||
JsonObj json, {
|
||||
required AlbumUpgraderV1? upgraderV1,
|
||||
required AlbumUpgraderV2? upgraderV2,
|
||||
required AlbumUpgraderV3? upgraderV3,
|
||||
required AlbumUpgraderV4? upgraderV4,
|
||||
required AlbumUpgraderV5? upgraderV5,
|
||||
required AlbumUpgraderFactory? upgraderFactory,
|
||||
}) {
|
||||
final jsonVersion = json["version"];
|
||||
JsonObj? result = json;
|
||||
if (jsonVersion < 2) {
|
||||
result = upgraderV1?.call(result);
|
||||
result = upgraderFactory?.buildV1()?.call(result);
|
||||
if (result == null) {
|
||||
_log.info("[fromJson] Version $jsonVersion not compatible");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (jsonVersion < 3) {
|
||||
result = upgraderV2?.call(result);
|
||||
result = upgraderFactory?.buildV2()?.call(result);
|
||||
if (result == null) {
|
||||
_log.info("[fromJson] Version $jsonVersion not compatible");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (jsonVersion < 4) {
|
||||
result = upgraderV3?.call(result);
|
||||
result = upgraderFactory?.buildV3()?.call(result);
|
||||
if (result == null) {
|
||||
_log.info("[fromJson] Version $jsonVersion not compatible");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (jsonVersion < 5) {
|
||||
result = upgraderV4?.call(result);
|
||||
result = upgraderFactory?.buildV4()?.call(result);
|
||||
if (result == null) {
|
||||
_log.info("[fromJson] Version $jsonVersion not compatible");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (jsonVersion < 6) {
|
||||
result = upgraderV5?.call(result);
|
||||
result = upgraderFactory?.buildV5()?.call(result);
|
||||
if (result == null) {
|
||||
_log.info("[fromJson] Version $jsonVersion not compatible");
|
||||
return null;
|
||||
|
@ -284,12 +280,11 @@ class AlbumRemoteDataSource implements AlbumDataSource {
|
|||
try {
|
||||
return Album.fromJson(
|
||||
jsonDecode(utf8.decode(data)),
|
||||
upgraderV1: AlbumUpgraderV1(logFilePath: albumFile.path),
|
||||
upgraderV2: AlbumUpgraderV2(logFilePath: albumFile.path),
|
||||
upgraderV3: AlbumUpgraderV3(logFilePath: albumFile.path),
|
||||
upgraderV4: AlbumUpgraderV4(logFilePath: albumFile.path),
|
||||
upgraderV5: AlbumUpgraderV5(account,
|
||||
albumFile: albumFile, logFilePath: albumFile.path),
|
||||
upgraderFactory: DefaultAlbumUpgraderFactory(
|
||||
account: account,
|
||||
albumFile: albumFile,
|
||||
logFilePath: albumFile.path,
|
||||
),
|
||||
)!
|
||||
.copyWith(
|
||||
lastUpdated: OrNull(null),
|
||||
|
|
|
@ -205,3 +205,46 @@ class AlbumUpgraderV5 implements AlbumUpgrader {
|
|||
|
||||
static final _log = Logger("entity.album.upgrader.AlbumUpgraderV5");
|
||||
}
|
||||
|
||||
abstract class AlbumUpgraderFactory {
|
||||
const AlbumUpgraderFactory();
|
||||
|
||||
AlbumUpgraderV1? buildV1();
|
||||
AlbumUpgraderV2? buildV2();
|
||||
AlbumUpgraderV3? buildV3();
|
||||
AlbumUpgraderV4? buildV4();
|
||||
AlbumUpgraderV5? buildV5();
|
||||
}
|
||||
|
||||
class DefaultAlbumUpgraderFactory extends AlbumUpgraderFactory {
|
||||
const DefaultAlbumUpgraderFactory({
|
||||
required this.account,
|
||||
this.albumFile,
|
||||
this.logFilePath,
|
||||
});
|
||||
|
||||
@override
|
||||
buildV1() => AlbumUpgraderV1(logFilePath: logFilePath);
|
||||
|
||||
@override
|
||||
buildV2() => AlbumUpgraderV2(logFilePath: logFilePath);
|
||||
|
||||
@override
|
||||
buildV3() => AlbumUpgraderV3(logFilePath: logFilePath);
|
||||
|
||||
@override
|
||||
buildV4() => AlbumUpgraderV4(logFilePath: logFilePath);
|
||||
|
||||
@override
|
||||
buildV5() => AlbumUpgraderV5(
|
||||
account,
|
||||
albumFile: albumFile,
|
||||
logFilePath: logFilePath,
|
||||
);
|
||||
|
||||
final Account account;
|
||||
final File? albumFile;
|
||||
|
||||
/// File path for logging only
|
||||
final String? logFilePath;
|
||||
}
|
||||
|
|
|
@ -38,11 +38,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -78,11 +74,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -140,11 +132,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -200,11 +188,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -251,11 +235,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -297,11 +277,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -344,11 +320,7 @@ void main() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -1705,11 +1677,7 @@ void _fromJsonShares() {
|
|||
expect(
|
||||
Album.fromJson(
|
||||
json,
|
||||
upgraderV1: null,
|
||||
upgraderV2: null,
|
||||
upgraderV3: null,
|
||||
upgraderV4: null,
|
||||
upgraderV5: null,
|
||||
upgraderFactory: const _NullAlbumUpgraderFactory(),
|
||||
),
|
||||
Album(
|
||||
lastUpdated: DateTime.utc(2020, 1, 2, 3, 4, 5, 678, 901),
|
||||
|
@ -1796,3 +1764,18 @@ void _toAppDbJsonShares() {
|
|||
],
|
||||
});
|
||||
}
|
||||
|
||||
class _NullAlbumUpgraderFactory extends AlbumUpgraderFactory {
|
||||
const _NullAlbumUpgraderFactory();
|
||||
|
||||
@override
|
||||
buildV1() => null;
|
||||
@override
|
||||
buildV2() => null;
|
||||
@override
|
||||
buildV3() => null;
|
||||
@override
|
||||
buildV4() => null;
|
||||
@override
|
||||
buildV5() => null;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue