@@ -6,36 +6,6 @@ import 'dart:io';
66import 'package:gpth/utils.dart' ;
77import 'package:path/path.dart' as p;
88
9- /// if json indicates that photo was put in archive folder
10- /// returns null if couldn't determine it (f.e. it's an album json)
11- bool ? jsonIsArchived (String jsonString) {
12- try {
13- final json = jsonDecode (jsonString);
14- if (json['photoTakenTime' ] != null ) {
15- return json['archived' ] == true ;
16- } else {
17- return null ;
18- }
19- } catch (e) {
20- return null ;
21- }
22- }
23-
24- /// if json indicates that photo was trashed
25- /// returns null if couldn't determine it (f.e. it's an album json)
26- bool ? jsonIsTrashed (String jsonString) {
27- try {
28- final json = jsonDecode (jsonString);
29- if (json['photoTakenTime' ] != null ) {
30- return json['trashed' ] == true ;
31- } else {
32- return null ;
33- }
34- } catch (e) {
35- return null ;
36- }
37- }
38-
399bool isYearFolder (Directory dir) =>
4010 p.basename (dir.path).startsWith ('Photos from ' );
4111
@@ -45,42 +15,50 @@ Future<bool> isAlbumFolder(Directory dir) =>
4515// Those two are so complicated because their names are 🥳localized🥳
4616// Those silly lists are an attempt to sometimes make it faster 👍
4717
48- const _archiveNames = [
49- 'Archive' , // EN
50- 'Archiwum' , // PL
51- ];
52-
53- Future <bool > isArchiveFolder (Directory dir) async {
54- if (_archiveNames.contains (p.basename (dir.path))) return true ;
18+ /// Goes through all .json files in given folder, and searches whether
19+ /// all of them have [key] key == true
20+ ///
21+ /// You can also pass [helpNames] list - if folder is straight out named
22+ /// one of them, it returns true right away
23+ ///
24+ /// This is only used to detect if folder is Archive/Trash
25+ Future <bool > _isXFolder (Directory dir, String key,
26+ [List <String >? helpNames]) async {
27+ assert (key == 'archived' || key == 'trashed' );
28+ if (helpNames? .contains (p.basename (dir.path)) ?? false ) return true ;
5529 var one = false ; // there is at least one element (every() is true with empty)
5630 final logic = await dir
5731 .list ()
5832 .whereType <File >()
5933 .where ((e) => e.path.endsWith ('.json' ))
6034 .every ((e) {
6135 one = true ;
62- final a = jsonIsArchived (e.readAsStringSync ());
63- return a != null && a;
36+ try {
37+ final json = jsonDecode (e.readAsStringSync ());
38+ if (json['photoTakenTime' ] != null ) {
39+ return json[key] == true ;
40+ } else {
41+ return false ;
42+ }
43+ } catch (_) {
44+ return false ;
45+ }
6446 });
6547 return one && logic;
6648}
6749
50+ const _archiveNames = [
51+ 'Archive' , // EN
52+ 'Archiwum' , // PL
53+ ];
54+
55+ Future <bool > isArchiveFolder (Directory dir) =>
56+ _isXFolder (dir, 'archived' , _archiveNames);
57+
6858const _trashNames = [
6959 'Trash' , // EN
7060 'Kosz' , // PL
7161];
7262
73- Future <bool > isTrashFolder (Directory dir) async {
74- if (_trashNames.contains (p.basename (dir.path))) return true ;
75- var one = false ; // there is at least one element (every() is true with empty)
76- final logic = await dir
77- .list ()
78- .whereType <File >()
79- .where ((e) => e.path.endsWith ('.json' ))
80- .every ((e) {
81- one = true ;
82- final t = jsonIsTrashed (e.readAsStringSync ());
83- return t != null && t;
84- });
85- return one && logic;
86- }
63+ Future <bool > isTrashFolder (Directory dir) =>
64+ _isXFolder (dir, 'trashed' , _trashNames);
0 commit comments