Skip to content

Commit 4c0add4

Browse files
committed
refactor: clean up unused imports and improve playlist creation logic
1 parent f8fd6e0 commit 4c0add4

File tree

3 files changed

+105
-103
lines changed

3 files changed

+105
-103
lines changed

lib/screens/library_page.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import 'package:musify/utilities/common_variables.dart';
3535
import 'package:musify/utilities/flutter_toast.dart';
3636
import 'package:musify/utilities/offline_playlist_dialogs.dart';
3737
import 'package:musify/utilities/playlist_dialogs.dart';
38-
import 'package:musify/utilities/playlist_image_picker.dart';
3938
import 'package:musify/utilities/utils.dart';
4039
import 'package:musify/widgets/confirmation_dialog.dart';
4140
import 'package:musify/widgets/playlist_bar.dart';

lib/services/playlists_manager.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,15 @@ Future<String> addUserPlaylist(String input, BuildContext context) async {
121121
}
122122
}
123123

124-
String createCustomPlaylist(
124+
(String message, String playlistId) createCustomPlaylist(
125125
String playlistName,
126126
String? image,
127127
BuildContext context,
128128
) {
129+
final newPlaylistId = generateCustomPlaylistId();
129130
final creationTime = DateTime.now().millisecondsSinceEpoch;
130131
final customPlaylist = {
131-
'ytid': generateCustomPlaylistId(),
132+
'ytid': newPlaylistId,
132133
'title': playlistName,
133134
'source': 'user-created',
134135
if (image != null) 'image': image,
@@ -139,7 +140,7 @@ String createCustomPlaylist(
139140
unawaited(
140141
addOrUpdateData('user', 'customPlaylists', userCustomPlaylists.value),
141142
);
142-
return '${context.l10n!.addedSuccess}!';
143+
return ('${context.l10n!.addedSuccess}!', newPlaylistId);
143144
}
144145

145146
String addSongInCustomPlaylist(

lib/utilities/playlist_dialogs.dart

Lines changed: 101 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -74,111 +74,116 @@ void showCreatePlaylistDialog(BuildContext context, {dynamic songToAdd}) {
7474
child: Column(
7575
mainAxisSize: MainAxisSize.min,
7676
children: <Widget>[
77-
Container(
78-
decoration: BoxDecoration(
79-
color: colorScheme.surfaceContainerLow,
80-
borderRadius: BorderRadius.circular(16),
81-
),
82-
padding: const EdgeInsets.all(4),
83-
child: Row(
84-
children: [
85-
Expanded(
86-
child: GestureDetector(
87-
onTap: () {
88-
dialogSetState(() {
89-
isYouTubeMode = true;
90-
id = '';
91-
customPlaylistName = '';
92-
imageUrl = null;
93-
imageBase64 = null;
94-
});
95-
},
96-
child: AnimatedContainer(
97-
duration: const Duration(milliseconds: 200),
98-
padding: const EdgeInsets.symmetric(vertical: 12),
99-
decoration: BoxDecoration(
100-
color: isYouTubeMode
101-
? colorScheme.primaryContainer
102-
: Colors.transparent,
103-
borderRadius: BorderRadius.circular(12),
104-
),
105-
child: Row(
106-
mainAxisAlignment: MainAxisAlignment.center,
107-
children: [
108-
Icon(
109-
FluentIcons.globe_20_filled,
110-
size: 20,
111-
color: isYouTubeMode
112-
? colorScheme.onPrimaryContainer
113-
: colorScheme.onSurfaceVariant,
114-
),
115-
const SizedBox(width: 8),
116-
Text(
117-
'YouTube',
118-
style: TextStyle(
77+
if (songToAdd == null)
78+
Container(
79+
decoration: BoxDecoration(
80+
color: colorScheme.surfaceContainerLow,
81+
borderRadius: BorderRadius.circular(16),
82+
),
83+
padding: const EdgeInsets.all(4),
84+
child: Row(
85+
children: [
86+
Expanded(
87+
child: GestureDetector(
88+
onTap: () {
89+
dialogSetState(() {
90+
isYouTubeMode = true;
91+
id = '';
92+
customPlaylistName = '';
93+
imageUrl = null;
94+
imageBase64 = null;
95+
});
96+
},
97+
child: AnimatedContainer(
98+
duration: const Duration(milliseconds: 200),
99+
padding: const EdgeInsets.symmetric(
100+
vertical: 12,
101+
),
102+
decoration: BoxDecoration(
103+
color: isYouTubeMode
104+
? colorScheme.primaryContainer
105+
: Colors.transparent,
106+
borderRadius: BorderRadius.circular(12),
107+
),
108+
child: Row(
109+
mainAxisAlignment: MainAxisAlignment.center,
110+
children: [
111+
Icon(
112+
FluentIcons.globe_20_filled,
113+
size: 20,
119114
color: isYouTubeMode
120115
? colorScheme.onPrimaryContainer
121116
: colorScheme.onSurfaceVariant,
122-
fontWeight: isYouTubeMode
123-
? FontWeight.w600
124-
: FontWeight.w500,
125117
),
126-
),
127-
],
118+
const SizedBox(width: 8),
119+
Text(
120+
'YouTube',
121+
style: TextStyle(
122+
color: isYouTubeMode
123+
? colorScheme.onPrimaryContainer
124+
: colorScheme.onSurfaceVariant,
125+
fontWeight: isYouTubeMode
126+
? FontWeight.w600
127+
: FontWeight.w500,
128+
),
129+
),
130+
],
131+
),
128132
),
129133
),
130134
),
131-
),
132-
Expanded(
133-
child: GestureDetector(
134-
onTap: () {
135-
dialogSetState(() {
136-
isYouTubeMode = false;
137-
id = '';
138-
customPlaylistName = '';
139-
imageUrl = null;
140-
imageBase64 = null;
141-
});
142-
},
143-
child: AnimatedContainer(
144-
duration: const Duration(milliseconds: 200),
145-
padding: const EdgeInsets.symmetric(vertical: 12),
146-
decoration: BoxDecoration(
147-
color: !isYouTubeMode
148-
? colorScheme.primaryContainer
149-
: Colors.transparent,
150-
borderRadius: BorderRadius.circular(12),
151-
),
152-
child: Row(
153-
mainAxisAlignment: MainAxisAlignment.center,
154-
children: [
155-
Icon(
156-
FluentIcons.person_20_filled,
157-
size: 20,
158-
color: !isYouTubeMode
159-
? colorScheme.onPrimaryContainer
160-
: colorScheme.onSurfaceVariant,
161-
),
162-
const SizedBox(width: 8),
163-
Text(
164-
context.l10n!.custom,
165-
style: TextStyle(
135+
Expanded(
136+
child: GestureDetector(
137+
onTap: () {
138+
dialogSetState(() {
139+
isYouTubeMode = false;
140+
id = '';
141+
customPlaylistName = '';
142+
imageUrl = null;
143+
imageBase64 = null;
144+
});
145+
},
146+
child: AnimatedContainer(
147+
duration: const Duration(milliseconds: 200),
148+
padding: const EdgeInsets.symmetric(
149+
vertical: 12,
150+
),
151+
decoration: BoxDecoration(
152+
color: !isYouTubeMode
153+
? colorScheme.primaryContainer
154+
: Colors.transparent,
155+
borderRadius: BorderRadius.circular(12),
156+
),
157+
child: Row(
158+
mainAxisAlignment: MainAxisAlignment.center,
159+
children: [
160+
Icon(
161+
FluentIcons.person_20_filled,
162+
size: 20,
166163
color: !isYouTubeMode
167164
? colorScheme.onPrimaryContainer
168165
: colorScheme.onSurfaceVariant,
169-
fontWeight: !isYouTubeMode
170-
? FontWeight.w600
171-
: FontWeight.w500,
172166
),
173-
),
174-
],
167+
const SizedBox(width: 8),
168+
Text(
169+
context.l10n!.custom,
170+
style: TextStyle(
171+
color: !isYouTubeMode
172+
? colorScheme.onPrimaryContainer
173+
: colorScheme.onSurfaceVariant,
174+
fontWeight: !isYouTubeMode
175+
? FontWeight.w600
176+
: FontWeight.w500,
177+
),
178+
),
179+
],
180+
),
175181
),
176182
),
177183
),
178-
),
179-
],
184+
],
185+
),
180186
),
181-
),
182187
const SizedBox(height: 20),
183188
if (isYouTubeMode)
184189
TextField(
@@ -265,25 +270,22 @@ void showCreatePlaylistDialog(BuildContext context, {dynamic songToAdd}) {
265270
if (isYouTubeMode && id.isNotEmpty) {
266271
final result = await addUserPlaylist(id, context);
267272
if (context.mounted) showToast(context, result);
268-
273+
269274
if (!context.mounted) return;
270275
Navigator.pop(context);
271276
} else if (!isYouTubeMode && customPlaylistName.isNotEmpty) {
272-
final result = createCustomPlaylist(
277+
final (result, newPlaylistId) = createCustomPlaylist(
273278
customPlaylistName.trim(),
274279
imageBase64 ?? imageUrl,
275280
context,
276281
);
277-
if (context.mounted && songToAdd == null) {
278-
showToast(context, result);
279-
}
280-
281-
if (songToAdd != null && userCustomPlaylists.value.isNotEmpty) {
282-
final newPlaylist = userCustomPlaylists.value.last;
282+
if (context.mounted) showToast(context, result);
283+
284+
if (songToAdd != null) {
283285
if (context.mounted) {
284286
final addResult = addSongInCustomPlaylist(
285287
context,
286-
newPlaylist['ytid'],
288+
newPlaylistId,
287289
songToAdd,
288290
);
289291
showToast(context, addResult);

0 commit comments

Comments
 (0)