Skip to content

Commit bedb1f4

Browse files
committed
unify this to single function
fixes #143
1 parent b3abb75 commit bedb1f4

File tree

2 files changed

+44
-78
lines changed

2 files changed

+44
-78
lines changed

lib/folder_classify.dart

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,6 @@ import 'dart:io';
66
import 'package:gpth/utils.dart';
77
import '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-
399
bool 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+
6858
const _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);

test/gpth_test.dart

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -140,31 +140,19 @@ AQACEQMRAD8AIcgXf//Z""";
140140
test('test getDiskFree()', () async {
141141
expect(await getDiskFree('.'), isNotNull);
142142
});
143-
test('test isArchived() and isTrashed() on jsons', () {
144-
final normalJson = '''{ "title": "example.jpg",
145-
"photoTakenTime": {"timestamp": "1663252650"},
146-
"photoLastModifiedTime": {"timestamp": "1673339757"}
147-
}''';
148-
final archivedJson = '''{ "title": "example.jpg",
149-
"photoTakenTime": {"timestamp": "1663252650"},
150-
"archived": true,
151-
"photoLastModifiedTime": {"timestamp": "1673339757"}
152-
}''';
153-
final trashedJson = '''{ "title": "example.jpg",
154-
"photoTakenTime": {"timestamp": "1663252650"},
155-
"trashed": true,
156-
"photoLastModifiedTime": {"timestamp": "1673339757"}
157-
}''';
158-
final albumJson =
159-
'{"title": "Green", "description": "","access": "protected"}';
160-
expect(jsonIsArchived(normalJson), false);
161-
expect(jsonIsTrashed(normalJson), false);
162-
expect(jsonIsArchived(archivedJson), true);
163-
expect(jsonIsTrashed(archivedJson), false);
164-
expect(jsonIsArchived(trashedJson), false);
165-
expect(jsonIsTrashed(trashedJson), true);
166-
expect(jsonIsArchived(albumJson), null);
167-
expect(jsonIsTrashed(albumJson), null);
143+
});
144+
group('folder_classify test', () {
145+
test('is archive/trash (fast name only)', () async {
146+
expect(await isArchiveFolder(Directory('Takeout/Photos/Archive/')), true);
147+
expect(await isArchiveFolder(Directory.current), false);
148+
expect(
149+
// you must do it because Dart 🤷
150+
// https://github.com/flutter/flutter/issues/71183#issuecomment-882213036
151+
() async => await isArchiveFolder(Directory('Photos/lol-album')),
152+
throwsA(isA<FileSystemException>()),
153+
);
154+
expect(await isTrashFolder(Directory('Takeout/Photos/Trash')), true);
155+
expect(await isTrashFolder(Directory.current), false);
168156
});
169157
});
170158

0 commit comments

Comments
 (0)