Skip to content

Commit

Permalink
song pop up menu added 'code refactor'
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandhuA committed Nov 1, 2024
1 parent 1bcff0d commit c694d3c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 354 deletions.
5 changes: 0 additions & 5 deletions lib/core/global_variables.dart
Original file line number Diff line number Diff line change
@@ -1,5 1,3 @@
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:musiq/models/song_model/song.dart';
import 'package:musiq/services/audio_handler.dart';
Expand All @@ -25,12 23,9 @@ class AppGlobals {

// Method to update the current song index
void setCurrentSongIndex(int index) {
log("----------updated $index");
currentSongIndex = index;
}



void updateTheme(String? newTheme) {
theme = newTheme;
}
Expand Down
81 changes: 81 additions & 0 deletions lib/presentation/commanWidgets/popup_menu.dart
Original file line number Diff line number Diff line change
@@ -0,0 1,81 @@
import 'package:audio_service/audio_service.dart';
import 'package:flutter/material.dart';
import 'package:musiq/core/colors.dart';
import 'package:musiq/core/global_variables.dart';
import 'package:musiq/core/helper_funtions.dart';
import 'package:musiq/presentation/commanWidgets/snack_bar.dart';


class SongPopupMenu extends StatelessWidget {
final dynamic song; // Use your actual song type here
final BuildContext context;

const SongPopupMenu({
Key? key,
required this.song,
required this.context,
}) : super(key: key);

@override
Widget build(BuildContext context) {
final audioHandler = AppGlobals().audioHandler;

return PopupMenuButton<int>(
icon: const Icon(Icons.more_vert_sharp),
onSelected: (value) {
switch (value) {
case 0:
if (AppGlobals().lastPlayedSongNotifier.value.isNotEmpty) {
final mediaItem = MediaItem(
id: song.downloadUrl?.last.link ?? "",
album: song.album?.name ?? "No ",
title: song.label ?? "No ",
displayTitle: song.name ?? "",
artUri: Uri.parse(song.image?.last.imageUrl ?? errorImage()),
);

audioHandler.addToQueue(mediaItem: mediaItem, song: song);
customSnackbar(
context: context,
message: "${song.name} added to queue",
bgColor: AppColors.white,
textColor: AppColors.black,
duration: const Duration(seconds: 5),
);
}
break;
case 1:
// Handle "Favorite" action
break;
case 2:
// Handle "Add to Playlist" action
break;
}
},
itemBuilder: (context) => [
PopupMenuItem(
value: 0,
child: Row(
children: const [
Icon(Icons.wrap_text),
Text('Add to Queue'),
],
),
),
PopupMenuItem(
value: 1,
child: Row(
children: [
Icon(Icons.favorite), // Use your favorite icon
const Text("Favorite"),
],
),
),
PopupMenuItem(
value: 2,
child: const Text('Add to Playlist'),
),
],
);
}
}
71 changes: 2 additions & 69 deletions lib/presentation/screens/LibraryScreen/last_played_list.dart
Original file line number Diff line number Diff line change
@@ -1,14 1,12 @@
import 'package:audio_service/audio_service.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:musiq/core/colors.dart';
import 'package:musiq/core/global_variables.dart';
import 'package:musiq/core/helper_funtions.dart';
import 'package:musiq/data/hive_funtion.dart';
import 'package:musiq/models/song_model/song.dart';
import 'package:musiq/presentation/commanWidgets/empty_screen.dart';
import 'package:musiq/presentation/commanWidgets/favorite_icon.dart';
import 'package:musiq/presentation/commanWidgets/snack_bar.dart';
import 'package:musiq/presentation/commanWidgets/popup_menu.dart';
import 'package:musiq/presentation/screens/player_screen/bottomPlayer/bottom_player.dart';
import 'package:musiq/presentation/screens/player_screen/player_screen.dart';

Expand Down Expand Up @@ -88,72 86,7 @@ class _LastPlayedListState extends State<LastPlayedList> {
FavoriteIcon(
song: lastplayed[index],
),
PopupMenuButton<int>(
icon: Icon(Icons.more_vert_sharp),
onSelected: (value) {
// Handle selected menu action
final audioHandler = AppGlobals().audioHandler;

switch (value) {
case 0:
if (AppGlobals()
.lastPlayedSongNotifier
.value
.isNotEmpty) {
final mediaItem = MediaItem(
id: song.downloadUrl?.last.link ?? "",
album: song.album?.name ?? "No ",
title: song.label ?? "No ",
displayTitle: song.name ?? "",
artUri: Uri.parse(
song.image?.last.imageUrl ??
errorImage()),
);

audioHandler.addToQueue(
mediaItem: mediaItem, song: song);
customSnackbar(
context: context,
message:
"${song.name} added to queue",
bgColor: AppColors.white,
textColor: AppColors.black,
duration: Duration(seconds: 5));
}
break;
case 1:
// Handle "Add to Playlist" action
break;
case 2:
// Handle "Share" action
break;
}
},
itemBuilder: (context) => [
PopupMenuItem(
value: 0,
child: Row(
children: [
Icon(Icons.wrap_text),
Text('Add to Queue'),
],
),
),
PopupMenuItem(
value: 1,
child: Row(
children: [
FavoriteIcon(song: song),
Text("Favorite")
],
),
),
PopupMenuItem(
value: 2,
child: Text('Add to Playlist'),
),
],
),
SongPopupMenu(song: song, context: context)
],
),
leading: Container(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 11,7 @@ import 'package:musiq/models/album_model/album_model.dart';
import 'package:musiq/models/play_list_model/play_list_model.dart';
import 'package:musiq/models/song_model/song.dart';
import 'package:musiq/presentation/commanWidgets/empty_screen.dart';
import 'package:musiq/presentation/commanWidgets/favorite_icon.dart';
import 'package:musiq/presentation/commanWidgets/popup_menu.dart';
import 'package:musiq/presentation/commanWidgets/snack_bar.dart';
import 'package:musiq/presentation/screens/artist/widgets/artist_horizontal_listview.dart';
import 'package:musiq/presentation/screens/player_screen/bottomPlayer/bottom_player.dart';
Expand Down Expand Up @@ -315,74 315,7 @@ class _AlbumOrPlaylistScreenState extends State<AlbumOrPlaylistScreen> {
song.label ?? "no",
maxLines: 1,
),
trailing: PopupMenuButton<int>(
icon: Icon(Icons.more_vert_sharp),
onSelected: (value) {
// Handle selected menu action
final audioHandler =
AppGlobals().audioHandler;

switch (value) {
case 0:
if (AppGlobals()
.lastPlayedSongNotifier
.value
.isNotEmpty) {
final mediaItem = MediaItem(
id: song.downloadUrl?.last.link ?? "",
album: song.album?.name ?? "No ",
title: song.label ?? "No ",
displayTitle: song.name ?? "",
artUri: Uri.parse(
song.image?.last.imageUrl ??
errorImage()),
);

audioHandler.addToQueue(
mediaItem: mediaItem, song: song);
customSnackbar(
context: context,
message:
"${song.name} added to queue",
bgColor: AppColors.white,
textColor: AppColors.black,
duration: Duration(seconds: 5));
}

break;
case 1:
// Handle "Add to Playlist" action
break;
case 2:
// Handle "Share" action
break;
}
},
itemBuilder: (context) => [
PopupMenuItem(
value: 0,
child: Row(
children: [
Icon(Icons.wrap_text),
Text('Add to Queue'),
],
),
),
PopupMenuItem(
value: 1,
child: Row(
children: [
FavoriteIcon(song: song),
Text("Favorite")
],
),
),
PopupMenuItem(
value: 2,
child: Text('Add to Playlist'),
),
],
),
trailing: SongPopupMenu(song: song, context: context)
),
);
},
Expand Down
76 changes: 2 additions & 74 deletions lib/presentation/screens/artist/artist_screen.dart
Original file line number Diff line number Diff line change
@@ -1,5 1,4 @@
import 'dart:developer';
import 'package:audio_service/audio_service.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand All @@ -10,8 9,7 @@ import 'package:musiq/core/helper_funtions.dart';
import 'package:musiq/core/sized.dart';
import 'package:musiq/models/artist_model/artist_model.dart';
import 'package:musiq/models/song_model/song.dart';
import 'package:musiq/presentation/commanWidgets/favorite_icon.dart';
import 'package:musiq/presentation/commanWidgets/snack_bar.dart';
import 'package:musiq/presentation/commanWidgets/popup_menu.dart';
import 'package:musiq/presentation/screens/artist/widgets/artist_horizontal_listview.dart';
import 'package:musiq/presentation/screens/player_screen/bottomPlayer/bottom_player.dart';
import 'package:musiq/presentation/screens/player_screen/player_screen.dart';
Expand Down Expand Up @@ -160,77 158,7 @@ class ArtistScreen extends StatelessWidget {
),
));
},
trailing: PopupMenuButton<int>(
icon: Icon(Icons.more_vert_sharp),
onSelected: (value) {

// Handle selected menu action
final audioHandler =
AppGlobals().audioHandler;

switch (value) {
case 0:
if (AppGlobals()
.lastPlayedSongNotifier
.value
.isNotEmpty) {
final mediaItem = MediaItem(
id: song.downloadUrl?.last.link ??
"",
album: song.album?.name ?? "No ",
title: song.label ?? "No ",
displayTitle: song.name ?? "",
artUri: Uri.parse(
song.image?.last.imageUrl ??
errorImage()),
);

audioHandler.addToQueue(
mediaItem: mediaItem,
song: song);
customSnackbar(
context: context,
message:
"${song.name} added to queue",
bgColor: AppColors.white,
textColor: AppColors.black,
duration: Duration(seconds: 5));
}

break;
case 1:
// Handle "Add to Playlist" action
break;
case 2:
// Handle "Share" action
break;
}
},
itemBuilder: (context) => [
PopupMenuItem(
value: 0,
child: Row(
children: [
Icon(Icons.wrap_text),
Text('Add to Queue'),
],
),
),
PopupMenuItem(
value: 1,
child: Row(
children: [
FavoriteIcon(song: song),
Text("Favorite")
],
),
),
PopupMenuItem(
value: 2,
child: Text('Add to Playlist'),
),
],
),
trailing: SongPopupMenu(song: song, context: context)
),
);
}),
Expand Down
Loading

0 comments on commit c694d3c

Please sign in to comment.