filters: simplified json conversion
This commit is contained in:
parent
d8d157a832
commit
15c50d5cef
9 changed files with 21 additions and 23 deletions
|
@ -18,14 +18,14 @@ class AlbumFilter extends CollectionFilter {
|
|||
|
||||
const AlbumFilter(this.album, this.uniqueName);
|
||||
|
||||
AlbumFilter.fromJson(Map<String, dynamic> json)
|
||||
AlbumFilter.fromMap(Map<String, dynamic> json)
|
||||
: this(
|
||||
json['album'],
|
||||
json['uniqueName'],
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
Map<String, dynamic> toMap() => {
|
||||
'type': type,
|
||||
'album': album,
|
||||
'uniqueName': uniqueName,
|
||||
|
|
|
@ -8,7 +8,7 @@ class FavouriteFilter extends CollectionFilter {
|
|||
static const type = 'favourite';
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
Map<String, dynamic> toMap() => {
|
||||
'type': type,
|
||||
};
|
||||
|
||||
|
|
|
@ -27,17 +27,17 @@ abstract class CollectionFilter implements Comparable<CollectionFilter> {
|
|||
final type = jsonMap['type'];
|
||||
switch (type) {
|
||||
case AlbumFilter.type:
|
||||
return AlbumFilter.fromJson(jsonMap);
|
||||
return AlbumFilter.fromMap(jsonMap);
|
||||
case FavouriteFilter.type:
|
||||
return FavouriteFilter();
|
||||
case LocationFilter.type:
|
||||
return LocationFilter.fromJson(jsonMap);
|
||||
return LocationFilter.fromMap(jsonMap);
|
||||
case MimeFilter.type:
|
||||
return MimeFilter.fromJson(jsonMap);
|
||||
return MimeFilter.fromMap(jsonMap);
|
||||
case QueryFilter.type:
|
||||
return QueryFilter.fromJson(jsonMap);
|
||||
return QueryFilter.fromMap(jsonMap);
|
||||
case TagFilter.type:
|
||||
return TagFilter.fromJson(jsonMap);
|
||||
return TagFilter.fromMap(jsonMap);
|
||||
}
|
||||
debugPrint('failed to parse filter from json=$jsonString');
|
||||
return null;
|
||||
|
@ -45,7 +45,9 @@ abstract class CollectionFilter implements Comparable<CollectionFilter> {
|
|||
|
||||
const CollectionFilter();
|
||||
|
||||
Map<String, dynamic> toJson();
|
||||
Map<String, dynamic> toMap();
|
||||
|
||||
String toJson() => jsonEncode(toMap());
|
||||
|
||||
bool filter(ImageEntry entry);
|
||||
|
||||
|
|
|
@ -17,14 +17,14 @@ class LocationFilter extends CollectionFilter {
|
|||
if (split.length > 1) _countryCode = split[1];
|
||||
}
|
||||
|
||||
LocationFilter.fromJson(Map<String, dynamic> json)
|
||||
LocationFilter.fromMap(Map<String, dynamic> json)
|
||||
: this(
|
||||
LocationLevel.values.firstWhere((v) => v.toString() == json['level'], orElse: () => null),
|
||||
json['location'],
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
Map<String, dynamic> toMap() => {
|
||||
'type': type,
|
||||
'level': level.toString(),
|
||||
'location': _countryCode != null ? '$_location$locationSeparator$_countryCode' : _location,
|
||||
|
|
|
@ -38,13 +38,13 @@ class MimeFilter extends CollectionFilter {
|
|||
_icon ??= AIcons.vector;
|
||||
}
|
||||
|
||||
MimeFilter.fromJson(Map<String, dynamic> json)
|
||||
MimeFilter.fromMap(Map<String, dynamic> json)
|
||||
: this(
|
||||
json['mime'],
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
Map<String, dynamic> toMap() => {
|
||||
'type': type,
|
||||
'mime': mime,
|
||||
};
|
||||
|
|
|
@ -32,13 +32,13 @@ class QueryFilter extends CollectionFilter {
|
|||
_filter = not ? (entry) => !entry.search(upQuery) : (entry) => entry.search(upQuery);
|
||||
}
|
||||
|
||||
QueryFilter.fromJson(Map<String, dynamic> json)
|
||||
QueryFilter.fromMap(Map<String, dynamic> json)
|
||||
: this(
|
||||
json['query'],
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
Map<String, dynamic> toMap() => {
|
||||
'type': type,
|
||||
'query': query,
|
||||
};
|
||||
|
|
|
@ -10,13 +10,13 @@ class TagFilter extends CollectionFilter {
|
|||
|
||||
const TagFilter(this.tag);
|
||||
|
||||
TagFilter.fromJson(Map<String, dynamic> json)
|
||||
TagFilter.fromMap(Map<String, dynamic> json)
|
||||
: this(
|
||||
json['tag'],
|
||||
);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() => {
|
||||
Map<String, dynamic> toMap() => {
|
||||
'type': type,
|
||||
'tag': tag,
|
||||
};
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:aves/model/filters/filters.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
@ -28,7 +26,7 @@ class AppShortcutService {
|
|||
try {
|
||||
await platform.invokeMethod('pin', <String, dynamic>{
|
||||
'label': label,
|
||||
'filters': filters.map((filter) => jsonEncode(filter.toJson())).toList(),
|
||||
'filters': filters.map((filter) => filter.toJson()).toList(),
|
||||
});
|
||||
} on PlatformException catch (e) {
|
||||
debugPrint('pin failed with code=${e.code}, exception=${e.message}, details=${e.details}');
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:aves/model/filters/album.dart';
|
||||
import 'package:aves/model/filters/favourite.dart';
|
||||
import 'package:aves/model/filters/filters.dart';
|
||||
|
@ -12,7 +10,7 @@ import 'package:test/test.dart';
|
|||
|
||||
void main() {
|
||||
test('Filter serialization', () {
|
||||
CollectionFilter jsonRoundTrip(filter) => CollectionFilter.fromJson(jsonEncode(filter.toJson()));
|
||||
CollectionFilter jsonRoundTrip(filter) => CollectionFilter.fromJson(filter.toJson());
|
||||
|
||||
final album = AlbumFilter('path/to/album', 'album');
|
||||
expect(album, jsonRoundTrip(album));
|
||||
|
|
Loading…
Reference in a new issue