Skip to content

Commit

Permalink
fix: folder changing not being synced in playlist download page
Browse files Browse the repository at this point in the history
(top widget and the one in config dialog)
  • Loading branch information
MSOB7YY committed Dec 12, 2023
1 parent d182e77 commit d9c1a30
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
43 changes: 28 additions & 15 deletions lib/youtube/functions/video_download_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Future<void> showVideoDownloadOptionsSheet({
required String? videoUploader,
required Map<String, String?> tagMaps,
required bool supportTagging,
required void Function(String newFolderPath) onDownloadGroupNameChanged,
required void Function(String newGroupName) onDownloadGroupNameChanged,
required bool showSpecificFileOptions,
}) async {
final controllersMap = {for (final t in FFMPEGTagField.values) t: TextEditingController(text: tagMaps[t])};
Expand Down Expand Up @@ -218,8 +218,10 @@ Future<void> showVideoDownloadOptionsSheet({
}

class YTDownloadOptionFolderListTile extends StatefulWidget {
final void Function(String newFolderPath) onDownloadGroupNameChanged;
final void Function(String newGroupName) onDownloadGroupNameChanged;
final void Function(String newFolderName)? onDownloadFolderAdded;
final String initialFolder;
final String playlistName;
final String Function(String value)? subtitle;
final double trailingPadding;
final VisualDensity? visualDensity;
Expand All @@ -228,25 +230,29 @@ class YTDownloadOptionFolderListTile extends StatefulWidget {
const YTDownloadOptionFolderListTile({
super.key,
required this.onDownloadGroupNameChanged,
this.onDownloadFolderAdded,
this.initialFolder = '',
this.playlistName = '',
this.subtitle,
this.trailingPadding = 0,
this.visualDensity,
this.maxTrailingWidth,
});

@override
State<YTDownloadOptionFolderListTile> createState() => _YTDownloadOptionFolderListTileState();
State<YTDownloadOptionFolderListTile> createState() => YTDownloadOptionFolderListTileState();
}

class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderListTile> {
class YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderListTile> {
final groupName = ''.obs;
final availableDirectoriesNames = <String, int>{};

@override
void initState() {
availableDirectoriesNames[widget.initialFolder] = 0; // to put at first
availableDirectoriesNames[''] = 0; // to put at first
// -- to put at first
availableDirectoriesNames[widget.playlistName] = 0;
availableDirectoriesNames[widget.initialFolder] = 0;
availableDirectoriesNames[''] = 0;
int rootFiles = 0;
for (final d in Directory(AppDirs.YOUTUBE_DOWNLOADS).listSync()) {
if (d is Directory) {
Expand All @@ -271,7 +277,15 @@ class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderL
widget.onDownloadGroupNameChanged(val);
}

void onFolderAddTap() async {
void onFolderAdd(String name) {
onGroupNameChanged(name);
try {
availableDirectoriesNames[name] = Directory("${AppDirs.YOUTUBE_DOWNLOADS}$name").listSync().length; // prolly 0 but eghh maybe edge cases
} catch (_) {}
widget.onDownloadFolderAdded?.call(name);
}

void _onFolderAddTap() async {
final c = TextEditingController();
final fk = GlobalKey<FormState>();
await NamidaNavigator.inst.navigateDialog(
Expand All @@ -286,8 +300,7 @@ class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderL
text: lang.ADD,
onPressed: () {
if (fk.currentState?.validate() == true) {
onGroupNameChanged(c.text);
availableDirectoriesNames[c.text] = Directory("${AppDirs.YOUTUBE_DOWNLOADS}${c.text}").listSync().length; // prolly 0 but eghh maybe edge cases
onFolderAdd(c.text);
NamidaNavigator.inst.closeDialog();
}
},
Expand All @@ -310,7 +323,7 @@ class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderL
),
),
);
c.dispose();
c.disposeAfterAnimation();
}

@override
Expand All @@ -321,7 +334,7 @@ class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderL
icon: Broken.add_circle,
iconColor: context.defaultIconColor(),
horizontalPadding: 0.0,
onPressed: onFolderAddTap,
onPressed: _onFolderAddTap,
),
visualDensity: widget.visualDensity,
title: lang.FOLDER,
Expand All @@ -331,12 +344,12 @@ class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderL
NamidaPopupItem(
icon: Broken.add,
title: lang.ADD,
onTap: onFolderAddTap,
onTap: _onFolderAddTap,
),
...availableDirectoriesNames.keys.map(
(name) {
final title = name == '' ? lang.DEFAULT : name;
final icon = name == widget.initialFolder
final icon = name == widget.playlistName
? Broken.music_playlist
: name == ''
? Broken.folder_2
Expand All @@ -361,15 +374,15 @@ class _YTDownloadOptionFolderListTileState extends State<YTDownloadOptionFolderL
mainAxisSize: MainAxisSize.min,
children: [
Icon(
groupName.value == widget.initialFolder
groupName.value == widget.playlistName
? Broken.music_playlist
: groupName.value == ''
? Broken.folder_2
: Broken.folder,
size: 18.0),
const SizedBox(width: 6.0),
ConstrainedBox(
constraints: BoxConstraints(minWidth: 0, maxWidth: widget.maxTrailingWidth ?? context.width * 0.34),
constraints: BoxConstraints(minWidth: 0, maxWidth: context.width * 0.34),
child: Text(
"$title$countText",
style: context.textTheme.displayMedium,
Expand Down
31 changes: 21 additions & 10 deletions lib/youtube/pages/yt_playlist_download_subpage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
final _configMap = <String, YoutubeItemDownloadConfig>{}.obs;
final _groupName = ''.obs;

final _folderController = GlobalKey<YTDownloadOptionFolderListTileState>();

bool useCachedVersionsIfAvailable = true;
bool autoExtractTitleAndArtist = settings.ytAutoExtractVideoTagsFromInfo.value;
bool keepCachedVersionsIfDownloaded = false;
Expand Down Expand Up @@ -153,9 +155,14 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
YTDownloadOptionFolderListTile(
maxTrailingWidth: context.width * 0.2,
visualDensity: visualDensity,
playlistName: widget.playlistName,
initialFolder: _groupName.value,
onDownloadGroupNameChanged: (newFolderPath) {
_groupName.value = newFolderPath;
onDownloadGroupNameChanged: (newGroupName) {
_groupName.value = newGroupName;
_folderController.currentState?.onGroupNameChanged(newGroupName);
},
onDownloadFolderAdded: (newFolderName) {
_folderController.currentState?.onFolderAdd(newFolderName);
},
),
CustomSwitchListTile(
Expand Down Expand Up @@ -303,14 +310,18 @@ class _YTPlaylistDownloadPageState extends State<YTPlaylistDownloadPage> {
),
),
),
YTDownloadOptionFolderListTile(
visualDensity: VisualDensity.compact,
trailingPadding: 12.0,
initialFolder: _groupName.value,
subtitle: (value) => "${AppDirs.YOUTUBE_DOWNLOADS}$value",
onDownloadGroupNameChanged: (newFolderPath) {
_groupName.value = newFolderPath;
},
Obx(
() => YTDownloadOptionFolderListTile(
key: _folderController,
visualDensity: VisualDensity.compact,
trailingPadding: 12.0,
playlistName: widget.playlistName,
initialFolder: _groupName.value,
subtitle: (value) => "${AppDirs.YOUTUBE_DOWNLOADS}$value",
onDownloadGroupNameChanged: (newGroupName) {
_groupName.value = newGroupName;
},
),
),
Expanded(
child: NamidaScrollbar(
Expand Down

0 comments on commit d9c1a30

Please sign in to comment.