filters: simplified json conversion

This commit is contained in:
Thibault Deckers 2020-09-20 15:43:15 +09:00
parent d8d157a832
commit 15c50d5cef
9 changed files with 21 additions and 23 deletions

View file

@ -18,14 +18,14 @@ class AlbumFilter extends CollectionFilter {
const AlbumFilter(this.album, this.uniqueName); const AlbumFilter(this.album, this.uniqueName);
AlbumFilter.fromJson(Map<String, dynamic> json) AlbumFilter.fromMap(Map<String, dynamic> json)
: this( : this(
json['album'], json['album'],
json['uniqueName'], json['uniqueName'],
); );
@override @override
Map<String, dynamic> toJson() => { Map<String, dynamic> toMap() => {
'type': type, 'type': type,
'album': album, 'album': album,
'uniqueName': uniqueName, 'uniqueName': uniqueName,

View file

@ -8,7 +8,7 @@ class FavouriteFilter extends CollectionFilter {
static const type = 'favourite'; static const type = 'favourite';
@override @override
Map<String, dynamic> toJson() => { Map<String, dynamic> toMap() => {
'type': type, 'type': type,
}; };

View file

@ -27,17 +27,17 @@ abstract class CollectionFilter implements Comparable<CollectionFilter> {
final type = jsonMap['type']; final type = jsonMap['type'];
switch (type) { switch (type) {
case AlbumFilter.type: case AlbumFilter.type:
return AlbumFilter.fromJson(jsonMap); return AlbumFilter.fromMap(jsonMap);
case FavouriteFilter.type: case FavouriteFilter.type:
return FavouriteFilter(); return FavouriteFilter();
case LocationFilter.type: case LocationFilter.type:
return LocationFilter.fromJson(jsonMap); return LocationFilter.fromMap(jsonMap);
case MimeFilter.type: case MimeFilter.type:
return MimeFilter.fromJson(jsonMap); return MimeFilter.fromMap(jsonMap);
case QueryFilter.type: case QueryFilter.type:
return QueryFilter.fromJson(jsonMap); return QueryFilter.fromMap(jsonMap);
case TagFilter.type: case TagFilter.type:
return TagFilter.fromJson(jsonMap); return TagFilter.fromMap(jsonMap);
} }
debugPrint('failed to parse filter from json=$jsonString'); debugPrint('failed to parse filter from json=$jsonString');
return null; return null;
@ -45,7 +45,9 @@ abstract class CollectionFilter implements Comparable<CollectionFilter> {
const CollectionFilter(); const CollectionFilter();
Map<String, dynamic> toJson(); Map<String, dynamic> toMap();
String toJson() => jsonEncode(toMap());
bool filter(ImageEntry entry); bool filter(ImageEntry entry);

View file

@ -17,14 +17,14 @@ class LocationFilter extends CollectionFilter {
if (split.length > 1) _countryCode = split[1]; if (split.length > 1) _countryCode = split[1];
} }
LocationFilter.fromJson(Map<String, dynamic> json) LocationFilter.fromMap(Map<String, dynamic> json)
: this( : this(
LocationLevel.values.firstWhere((v) => v.toString() == json['level'], orElse: () => null), LocationLevel.values.firstWhere((v) => v.toString() == json['level'], orElse: () => null),
json['location'], json['location'],
); );
@override @override
Map<String, dynamic> toJson() => { Map<String, dynamic> toMap() => {
'type': type, 'type': type,
'level': level.toString(), 'level': level.toString(),
'location': _countryCode != null ? '$_location$locationSeparator$_countryCode' : _location, 'location': _countryCode != null ? '$_location$locationSeparator$_countryCode' : _location,

View file

@ -38,13 +38,13 @@ class MimeFilter extends CollectionFilter {
_icon ??= AIcons.vector; _icon ??= AIcons.vector;
} }
MimeFilter.fromJson(Map<String, dynamic> json) MimeFilter.fromMap(Map<String, dynamic> json)
: this( : this(
json['mime'], json['mime'],
); );
@override @override
Map<String, dynamic> toJson() => { Map<String, dynamic> toMap() => {
'type': type, 'type': type,
'mime': mime, 'mime': mime,
}; };

View file

@ -32,13 +32,13 @@ class QueryFilter extends CollectionFilter {
_filter = not ? (entry) => !entry.search(upQuery) : (entry) => entry.search(upQuery); _filter = not ? (entry) => !entry.search(upQuery) : (entry) => entry.search(upQuery);
} }
QueryFilter.fromJson(Map<String, dynamic> json) QueryFilter.fromMap(Map<String, dynamic> json)
: this( : this(
json['query'], json['query'],
); );
@override @override
Map<String, dynamic> toJson() => { Map<String, dynamic> toMap() => {
'type': type, 'type': type,
'query': query, 'query': query,
}; };

View file

@ -10,13 +10,13 @@ class TagFilter extends CollectionFilter {
const TagFilter(this.tag); const TagFilter(this.tag);
TagFilter.fromJson(Map<String, dynamic> json) TagFilter.fromMap(Map<String, dynamic> json)
: this( : this(
json['tag'], json['tag'],
); );
@override @override
Map<String, dynamic> toJson() => { Map<String, dynamic> toMap() => {
'type': type, 'type': type,
'tag': tag, 'tag': tag,
}; };

View file

@ -1,5 +1,3 @@
import 'dart:convert';
import 'package:aves/model/filters/filters.dart'; import 'package:aves/model/filters/filters.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -28,7 +26,7 @@ class AppShortcutService {
try { try {
await platform.invokeMethod('pin', <String, dynamic>{ await platform.invokeMethod('pin', <String, dynamic>{
'label': label, 'label': label,
'filters': filters.map((filter) => jsonEncode(filter.toJson())).toList(), 'filters': filters.map((filter) => filter.toJson()).toList(),
}); });
} on PlatformException catch (e) { } on PlatformException catch (e) {
debugPrint('pin failed with code=${e.code}, exception=${e.message}, details=${e.details}'); debugPrint('pin failed with code=${e.code}, exception=${e.message}, details=${e.details}');

View file

@ -1,5 +1,3 @@
import 'dart:convert';
import 'package:aves/model/filters/album.dart'; import 'package:aves/model/filters/album.dart';
import 'package:aves/model/filters/favourite.dart'; import 'package:aves/model/filters/favourite.dart';
import 'package:aves/model/filters/filters.dart'; import 'package:aves/model/filters/filters.dart';
@ -12,7 +10,7 @@ import 'package:test/test.dart';
void main() { void main() {
test('Filter serialization', () { 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'); final album = AlbumFilter('path/to/album', 'album');
expect(album, jsonRoundTrip(album)); expect(album, jsonRoundTrip(album));