Skip to content

Commit

Permalink
fix: picking files with specific extensions using native file picker
Browse files Browse the repository at this point in the history
  • Loading branch information
MSOB7YY committed Sep 9, 2024
1 parent 1ec99ac commit 4cbc579
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/controller/backup_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class BackupController {
final sortedFiles = await _getBackupFilesSorted.thready(_backupDirectoryPath);
backupzip = sortedFiles.firstOrNull;
} else {
final filePicked = await NamidaFileBrowser.pickFile(note: lang.RESTORE_BACKUP, allowedExtensions: ['zip']);
final filePicked = await NamidaFileBrowser.pickFile(note: lang.RESTORE_BACKUP, allowedExtensions: const NamidaFileBrowserAllowedExtensions.zip());
final path = filePicked?.path;
if (path != null) {
backupzip = File(path);
Expand Down
29 changes: 21 additions & 8 deletions lib/controller/file_browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,24 @@ enum _SortType {

const _defaultMemeType = NamidaStorageFileMemeType.any;

class NamidaFileBrowserAllowedExtensions {
final List<String>? extensions;

/// initialize with custom extensions. all should start with a dot.
const NamidaFileBrowserAllowedExtensions._(this.extensions);
const NamidaFileBrowserAllowedExtensions.csv() : extensions = const ['.csv', '.CSV'];
const NamidaFileBrowserAllowedExtensions.json() : extensions = const ['.json', '.JSON'];
const NamidaFileBrowserAllowedExtensions.zip() : extensions = const ['.zip', '.ZIP', '.rar', '.RAR'];
const NamidaFileBrowserAllowedExtensions.lrcOrTxt() : extensions = const ['.lrc', '.LRC', '.txt', '.TXT'];
factory NamidaFileBrowserAllowedExtensions.m3u() => NamidaFileBrowserAllowedExtensions._(kM3UPlaylistsExtensions.toList());
}

class NamidaFileBrowser {
static Future<File?> pickFile({
String note = '',
String memeType = _defaultMemeType,
String? initialDirectory,
List<String> allowedExtensions = const [],
NamidaFileBrowserAllowedExtensions? allowedExtensions,
}) async {
return _NamidaFileBrowserBase.pickFile(
note: note,
Expand All @@ -51,7 +63,7 @@ class NamidaFileBrowser {
String note = '',
String memeType = _defaultMemeType,
String? initialDirectory,
List<String> allowedExtensions = const [],
NamidaFileBrowserAllowedExtensions? allowedExtensions,
}) async {
return _NamidaFileBrowserBase.pickFiles(
note: note,
Expand Down Expand Up @@ -114,7 +126,7 @@ class _NamidaFileBrowserBase<T extends FileSystemEntity> extends StatefulWidget
final String note;
final String? initialDirectory;
final Completer<List<T>> onSelect;
final List<String> allowedExtensions;
final NamidaFileBrowserAllowedExtensions? allowedExtensions;
final String memeType;
final bool allowMultiple;
final _NamidaFileBrowserPopCallback onPop;
Expand All @@ -124,15 +136,15 @@ class _NamidaFileBrowserBase<T extends FileSystemEntity> extends StatefulWidget
required this.note,
this.initialDirectory,
required this.onSelect,
this.allowedExtensions = const [],
this.allowedExtensions,
this.memeType = _defaultMemeType,
required this.allowMultiple,
required this.onPop,
});

static Future<File?> pickFile({
String note = '',
List<String> allowedExtensions = const [],
NamidaFileBrowserAllowedExtensions? allowedExtensions,
String memeType = _defaultMemeType,
String? initialDirectory,
required _NamidaFileBrowserNavigationCallback onNavigate,
Expand All @@ -156,7 +168,7 @@ class _NamidaFileBrowserBase<T extends FileSystemEntity> extends StatefulWidget

static Future<List<File>> pickFiles({
String note = '',
List<String> allowedExtensions = const [],
NamidaFileBrowserAllowedExtensions? allowedExtensions,
String memeType = _defaultMemeType,
String? initialDirectory,
required _NamidaFileBrowserNavigationCallback onNavigate,
Expand Down Expand Up @@ -664,7 +676,8 @@ class _NamidaFileBrowserState<T extends FileSystemEntity> extends State<_NamidaF
_fetchFiles(Directory(widget.initialDirectory ?? paths.first));
_fetchInfo(_mainStoragePaths);
});
_effectiveAllowedExtensions.addAll(widget.allowedExtensions);
final allowedExtensions = widget.allowedExtensions?.extensions;
if (allowedExtensions != null) _effectiveAllowedExtensions.addAll(allowedExtensions);
if (widget.memeType != NamidaStorageFileMemeType.any) {
switch (widget.memeType) {
case NamidaStorageFileMemeType.audio:
Expand Down Expand Up @@ -862,7 +875,7 @@ class _NamidaFileBrowserState<T extends FileSystemEntity> extends State<_NamidaF
final _iconsLookupPre = <String, int>{};
final _iconsLookup = <int, IconData>{};

Future<void> _onBackupPickerLaunch([List<String> allowedExtensions = const []]) async {
Future<void> _onBackupPickerLaunch([List<String>? allowedExtensions]) async {
final note = widget.note != '' ? widget.note : null;
if (T == File) {
final res = await NamidaStorage.inst.pickFiles(
Expand Down
12 changes: 7 additions & 5 deletions lib/controller/namida_channel_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class NamidaStorage {
Future<List<String>> pickFiles({
String? note,
bool multiple = false,
List<String> allowedExtensions = const [],
List<String>? allowedExtensions,
String? memetype = NamidaStorageFileMemeType.any,
}) async {
try {
Expand All @@ -51,10 +51,12 @@ class NamidaStorage {

final files = res?.cast<String>() ?? <String>[];

for (final f in files) {
if (!allowedExtensions.any((ext) => f.endsWith(".$ext"))) {
snackyy(title: lang.ERROR, message: "${lang.EXTENSION}: $allowedExtensions", isError: true);
return [];
if (allowedExtensions != null && allowedExtensions.isNotEmpty) {
for (final f in files) {
if (!allowedExtensions.any((ext) => f.endsWith(ext))) {
snackyy(title: lang.ERROR, message: "${lang.EXTENSION}: $allowedExtensions", isError: true);
return [];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ui/dialogs/set_lrc_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ void showLRCSetDialog(Playable item, Color colorScheme) async {
onTap: () async {
final picked = await NamidaFileBrowser.pickFile(
note: lang.ADD_LRC_FILE,
allowedExtensions: ['lrc', 'LRC', 'txt', 'TXT'],
allowedExtensions: const NamidaFileBrowserAllowedExtensions.lrcOrTxt(),
initialDirectory: lrcUtils.pickFileInitialDirectory,
);
final path = picked?.path;
Expand Down
5 changes: 3 additions & 2 deletions lib/ui/pages/playlists_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class _PlaylistsPageState extends State<PlaylistsPage> with TickerProviderStateM

Future<void> _importPlaylists({required bool keepSynced, required bool pickFolder}) async {
Set<String> playlistsFilesPath;
final m3uExtensions = kM3UPlaylistsExtensions.toList();
final m3uExtensionsWrapper = NamidaFileBrowserAllowedExtensions.m3u();
final m3uExtensions = m3uExtensionsWrapper.extensions ?? [];
if (pickFolder) {
final dirs = await NamidaFileBrowser.pickDirectories(note: "${lang.IMPORT} (${lang.FOLDERS})");
playlistsFilesPath = {};
Expand All @@ -84,7 +85,7 @@ class _PlaylistsPageState extends State<PlaylistsPage> with TickerProviderStateM
);
}
} else {
final playlistsFiles = await NamidaFileBrowser.pickFiles(note: lang.IMPORT, allowedExtensions: m3uExtensions);
final playlistsFiles = await NamidaFileBrowser.pickFiles(note: lang.IMPORT, allowedExtensions: m3uExtensionsWrapper);
playlistsFilesPath = playlistsFiles.map((f) => f.path).toSet();
}
if (playlistsFilesPath.isNotEmpty) {
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/widgets/settings/backup_restore_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ class BackupAndRestore extends SettingSubpageProvider {
child: Text("- $text", style: namida.textTheme.displayLarge),
);

final jsonfile = await NamidaFileBrowser.pickFile(note: lang.IMPORT_YOUTUBE_HISTORY, allowedExtensions: ['json', 'JSON']);
final jsonfile = await NamidaFileBrowser.pickFile(note: lang.IMPORT_YOUTUBE_HISTORY, allowedExtensions: const NamidaFileBrowserAllowedExtensions.json());
if (jsonfile != null) {
final isMatchingTypeLink = true.obs;
final isMatchingTypeTitleAndArtist = false.obs;
Expand Down Expand Up @@ -733,7 +733,7 @@ class BackupAndRestore extends SettingSubpageProvider {
onPressed: () async {
NamidaNavigator.inst.closeDialog();

final csvFiles = await NamidaFileBrowser.pickFile(note: lang.IMPORT_LAST_FM_HISTORY, allowedExtensions: ['csv']);
final csvFiles = await NamidaFileBrowser.pickFile(note: lang.IMPORT_LAST_FM_HISTORY, allowedExtensions: const NamidaFileBrowserAllowedExtensions.csv());
final csvFilePath = csvFiles?.path;
if (csvFiles != null && csvFilePath != null) {
final oldestDate = Rxn<DateTime>();
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/widgets/settings/theme_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ThemeSetting extends SettingSubpageProvider {
actions: [
NamidaButton(
onPressed: () async {
final files = await NamidaFileBrowser.pickFile(note: lang.ADD_LANGUAGE, allowedExtensions: ['json', 'JSON']);
final files = await NamidaFileBrowser.pickFile(note: lang.ADD_LANGUAGE, allowedExtensions: const NamidaFileBrowserAllowedExtensions.json());
final path = files?.path;
if (path != null) {
try {
Expand Down
2 changes: 1 addition & 1 deletion lib/youtube/pages/yt_channels_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class _YoutubeChannelsPageState extends YoutubeChannelController<YoutubeChannels
Future<void> _onSubscriptionFileImportTap() async {
final file = await NamidaFileBrowser.pickFile(
note: 'Choose a "subscriptions.csv" file from a google takeout',
allowedExtensions: ['csv', 'CSV'],
allowedExtensions: const NamidaFileBrowserAllowedExtensions.csv(),
);
final fp = file?.path;
if (fp != null) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: namida
description: A Beautiful and Feature-rich Music Player, With YouTube & Video Support Built in Flutter
publish_to: "none"
version: 4.2.5-beta+240909213
version: 4.2.6-beta+240909215

environment:
sdk: ">=3.4.0 <4.0.0"
Expand Down

0 comments on commit 4cbc579

Please sign in to comment.