Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 2e9a316

Browse files
committed
Remove leading dot and warn users about it
1 parent 0f58fa2 commit 2e9a316

File tree

3 files changed

+53
-30
lines changed

3 files changed

+53
-30
lines changed

packages/file_selector/file_selector/lib/file_selector.dart

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Future<XFile> openFile({
1414
String initialDirectory,
1515
String confirmButtonText,
1616
}) {
17-
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
17+
_verifyTypeGroups(acceptedTypeGroups);
1818
return FileSelectorPlatform.instance.openFile(
1919
acceptedTypeGroups: acceptedTypeGroups,
2020
initialDirectory: initialDirectory,
@@ -27,7 +27,7 @@ Future<List<XFile>> openFiles({
2727
String initialDirectory,
2828
String confirmButtonText,
2929
}) {
30-
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
30+
_verifyTypeGroups(acceptedTypeGroups);
3131
return FileSelectorPlatform.instance.openFiles(
3232
acceptedTypeGroups: acceptedTypeGroups,
3333
initialDirectory: initialDirectory,
@@ -41,7 +41,7 @@ Future<String> getSavePath({
4141
String suggestedName,
4242
String confirmButtonText,
4343
}) async {
44-
acceptedTypeGroups = _verifyTypeGroups(acceptedTypeGroups);
44+
_verifyTypeGroups(acceptedTypeGroups);
4545
return FileSelectorPlatform.instance.getSavePath(
4646
acceptedTypeGroups: acceptedTypeGroups,
4747
initialDirectory: initialDirectory,
@@ -58,21 +58,24 @@ Future<String> getDirectoryPath({
5858
initialDirectory: initialDirectory, confirmButtonText: confirmButtonText);
5959
}
6060

61-
List<XTypeGroup> _verifyTypeGroups(List<XTypeGroup> groups) {
62-
if (groups == null) return groups;
61+
void _verifyTypeGroups(List<XTypeGroup> groups) {
62+
if (groups == null) return;
6363
for (var i = 0; i < groups.length; i++) {
64-
if (groups[i] == null || groups[i].extensions == null) continue;
65-
for (var j = 0; j < groups[i].extensions.length; j++) {
66-
if (groups[i].extensions[j] == null) continue;
67-
if (groups[i].extensions[j].startsWith('.')) {
68-
if (kDebugMode) {
69-
print('acceptedTypeGroups[${i}].extensions[${j}]'
70-
' with value "${groups[i].extensions[j]} is invalid.'
71-
' Please remove the leading dot.');
72-
}
73-
groups[i].extensions[j] = groups[i].extensions[j].substring(1);
74-
}
64+
_verifyExtensions(groups[i], i);
65+
}
66+
}
67+
68+
void _verifyExtensions(XTypeGroup group, int groupIndex) {
69+
if (group == null || group.extensions == null) return;
70+
for (var extIndex = 0; extIndex < group.extensions.length; extIndex++) {
71+
if (group.extensions[extIndex] == null) continue;
72+
if (!group.extensions[extIndex].startsWith('.')) continue;
73+
if (kDebugMode) {
74+
print('acceptedTypeGroups[${groupIndex}].extensions[${extIndex}]'
75+
' with value "${group.extensions[extIndex]} is invalid.'
76+
' We are removing the leading dot and therefore mutating acceptedTypeGroups.'
77+
' Please fix it.');
7578
}
79+
group.extensions[extIndex] = group.extensions[extIndex].substring(1);
7680
}
77-
return groups;
7881
}

packages/file_selector/file_selector/test/file_selector_test.dart

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,18 @@ void main() {
8181
});
8282

8383
test('works with an extension with leading dot', () async {
84-
final correctTypeGroups = [
85-
XTypeGroup(label: 'images', extensions: [
86-
'jpg',
87-
'png',
88-
]),
89-
];
84+
// We consider this correct because at least one extension has a leading dot.
9085
final incorrectTypeGroups = [
9186
XTypeGroup(label: 'images', extensions: [
9287
'.jpg',
9388
'.png',
9489
]),
9590
];
9691

92+
//ignore: unawaited_futures
9793
openFile(acceptedTypeGroups: incorrectTypeGroups);
9894
verify(mock.openFile(acceptedTypeGroups: incorrectTypeGroups));
95+
expect(incorrectTypeGroups[0].extensions, ['jpg', 'png']);
9996
});
10097
});
10198

@@ -149,6 +146,21 @@ void main() {
149146
final files = await openFiles(acceptedTypeGroups: acceptedTypeGroups);
150147
expect(files, expectedFiles);
151148
});
149+
150+
test('works with an extension with leading dot', () async {
151+
// We consider this correct because at least one extension has a leading dot.
152+
final incorrectTypeGroups = [
153+
XTypeGroup(label: 'images', extensions: [
154+
'.jpg',
155+
'.png',
156+
]),
157+
];
158+
159+
// ignore: unawaited_futures
160+
openFiles(acceptedTypeGroups: incorrectTypeGroups);
161+
verify(mock.openFiles(acceptedTypeGroups: incorrectTypeGroups));
162+
expect(incorrectTypeGroups[0].extensions, ['jpg', 'png']);
163+
});
152164
});
153165

154166
group('getSavePath', () {
@@ -212,6 +224,21 @@ void main() {
212224
final savePath = await getSavePath(suggestedName: suggestedName);
213225
expect(savePath, expectedSavePath);
214226
});
227+
228+
test('works with an extension with leading dot', () async {
229+
// We consider this correct because at least one extension has a leading dot.
230+
final incorrectTypeGroups = [
231+
XTypeGroup(label: 'images', extensions: [
232+
'.jpg',
233+
'.png',
234+
]),
235+
];
236+
237+
// ignore: unawaited_futures
238+
getSavePath(acceptedTypeGroups: incorrectTypeGroups);
239+
verify(mock.getSavePath(acceptedTypeGroups: incorrectTypeGroups));
240+
expect(incorrectTypeGroups[0].extensions, ['jpg', 'png']);
241+
});
215242
});
216243

217244
group('getDirectoryPath', () {

packages/file_selector/file_selector_platform_interface/lib/src/types/x_type_group/x_type_group.dart

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,4 @@ class XTypeGroup {
4141
'webWildCards': webWildCards,
4242
};
4343
}
44-
45-
bool operator ==(o) =>
46-
label == o.label &&
47-
listEquals(extensions, o.extensions) &&
48-
listEquals(mimeTypes, o.mimeTypes) &&
49-
listEquals(macUTIs, o.macUTIs) &&
50-
listEquals(webWildCards, o.webWildCards);
5144
}

0 commit comments

Comments
 (0)