Skip to content

Commit

Permalink
Added Favourite Section to save songs in favourites playlist
Browse files Browse the repository at this point in the history
  • Loading branch information
sheikhhaziq committed Jan 3, 2023
1 parent 0cb825c commit f0ce8c5
Show file tree
Hide file tree
Showing 13 changed files with 326 additions and 61 deletions.
68 changes: 63 additions & 5 deletions lib/Models/Track.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';

import 'package:flutter/animation.dart';
import 'package:flutter/foundation.dart';
import 'package:palette_generator/palette_generator.dart';
// import 'package:palette_generator/palette_generator.dart';

import 'package:vibe_music/Models/Album.dart';
import 'package:vibe_music/Models/Artist.dart';
Expand All @@ -14,7 +15,7 @@ class Track {
List<Thumbnail> thumbnails;
List<Artist> artists;
Album? albums;
PaletteGenerator? colorPalette;
ColorPalette? colorPalette;
Track({
required this.title,
required this.videoId,
Expand All @@ -30,7 +31,7 @@ class Track {
List<Thumbnail>? thumbnails,
List<Artist>? artists,
Album? albums,
PaletteGenerator? colorPalette,
ColorPalette? colorPalette,
}) {
return Track(
title: title ?? this.title,
Expand All @@ -49,7 +50,7 @@ class Track {
'thumbnails': thumbnails.map((x) => x.toMap()).toList(),
'artists': artists.map((x) => x.toMap()).toList(),
'albums': albums?.toMap(),
'colorPalette': colorPalette,
'colorPalette': colorPalette?.toMap(),
};
}

Expand All @@ -70,7 +71,7 @@ class Track {
albums: map['albums'] != null
? Album.fromMap(map['albums'] as Map<String, dynamic>)
: null,
colorPalette: map['colorPalette'],
colorPalette: ColorPalette.fromMap(map['colorPalette'] ?? {}),
);
}

Expand Down Expand Up @@ -106,3 +107,60 @@ class Track {
colorPalette.hashCode;
}
}

class ColorPalette {
Color? darkMutedColor;
Color? lightMutedColor;
ColorPalette({
this.darkMutedColor,
this.lightMutedColor,
});

ColorPalette copyWith({
Color? darkMutedColor,
Color? lightMutedColor,
}) {
return ColorPalette(
darkMutedColor: darkMutedColor ?? this.darkMutedColor,
lightMutedColor: lightMutedColor ?? this.lightMutedColor,
);
}

Map<String, dynamic> toMap() {
return <String, dynamic>{
'darkMutedColor': darkMutedColor?.value,
'lightMutedColor': lightMutedColor?.value,
};
}

factory ColorPalette.fromMap(Map<String, dynamic> map) {
return ColorPalette(
darkMutedColor: map['darkMutedColor'] != null
? Color(map['darkMutedColor'] as int)
: null,
lightMutedColor: map['lightMutedColor'] != null
? Color(map['lightMutedColor'] as int)
: null,
);
}

String toJson() => json.encode(toMap());

factory ColorPalette.fromJson(String source) =>
ColorPalette.fromMap(json.decode(source) as Map<String, dynamic>);

@override
String toString() =>
'ColorPalette(darkMutedColor: $darkMutedColor, lightMutedColor: $lightMutedColor)';

@override
bool operator ==(covariant ColorPalette other) {
if (identical(this, other)) return true;

return other.darkMutedColor == darkMutedColor &&
other.lightMutedColor == lightMutedColor;
}

@override
int get hashCode => darkMutedColor.hashCode ^ lightMutedColor.hashCode;
}
14 changes: 7 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import 'package:country_picker/country_picker.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animated_theme/animated_theme_app.dart';
import 'package:flutter_animated_theme/animation_type.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:just_audio_background/just_audio_background.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand All @@ -25,7 +24,9 @@ void main() async {
androidNotificationChannelName: 'Audio playback',
androidNotificationOngoing: false,
);
HomeApi.setCountry();
await Hive.initFlutter();
Hive.openBox('myfavourites');
await HomeApi.setCountry();
runApp(MultiProvider(providers: [
ChangeNotifierProvider(create: (_) => MusicPlayer()),
ChangeNotifierProvider(create: (_) => HomeScreenProvider()),
Expand All @@ -43,8 +44,7 @@ class MyApp extends StatelessWidget {

return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) {
return AnimatedThemeApp(
animationType: AnimationType.CIRCULAR_ANIMATED_THEME,
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Vibe Music',
localizationsDelegates: const [
Expand All @@ -60,7 +60,7 @@ class MyApp extends StatelessWidget {
colorScheme: context.watch<ThemeProvider>().dynamicThemeMode
? ColorScheme.fromSwatch(
primarySwatch: createMaterialColor(
song?.colorPalette?.lightMutedColor?.color ??
song?.colorPalette?.lightMutedColor ??
const Color.fromARGB(255, 136, 240, 196)))
: lightDynamic ??
ColorScheme.fromSwatch(
Expand All @@ -72,7 +72,7 @@ class MyApp extends StatelessWidget {
colorScheme: context.watch<ThemeProvider>().dynamicThemeMode
? ColorScheme.fromSwatch(
primarySwatch: createMaterialColor(
song?.colorPalette?.darkMutedColor?.color ??
song?.colorPalette?.darkMutedColor ??
const Color.fromARGB(255, 80, 141, 115)))
: darkDynamic ??
(ColorScheme.fromSwatch(
Expand Down
8 changes: 6 additions & 2 deletions lib/providers/MusicPlayer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class MusicPlayer extends ChangeNotifier {

addToQUeue(Track newSong) async {
PaletteGenerator? color = await generateColor(newSong.thumbnails.last.url);
newSong.colorPalette = color;
newSong.colorPalette = ColorPalette(
darkMutedColor: color?.darkMutedColor?.color,
lightMutedColor: color?.lightMutedColor?.color);
_songs.add(newSong);
_initialised = true;
notifyListeners();
Expand Down Expand Up @@ -101,7 +103,9 @@ class MusicPlayer extends ChangeNotifier {
try {
PaletteGenerator? color =
await generateColor(newSong.thumbnails.last.url);
newSong.colorPalette = color;
newSong.colorPalette = ColorPalette(
darkMutedColor: color?.darkMutedColor?.color,
lightMutedColor: color?.lightMutedColor?.color);

await setPlayer();

Expand Down
3 changes: 1 addition & 2 deletions lib/providers/ThemeProvider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ class ThemeProvider extends ChangeNotifier {

setTheme(themeMode) {
_themeMode = themeMode;

notifyListeners();
if (themeMode == 'dark') {
_currentTheme = darkTheme;
} else {
_currentTheme = lightTheme;
}
_prefs.setString('themeMode', themeMode);
notifyListeners();
}
}

Expand Down
47 changes: 47 additions & 0 deletions lib/screens/FavouriteScreen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'dart:collection';
import 'dart:convert';
import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:vibe_music/Models/Track.dart';
import 'package:vibe_music/widgets/TrackTile.dart';

class FavouriteScreen extends StatelessWidget {
const FavouriteScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: const Text("My Favorites"),
centerTitle: true,
),
body: ValueListenableBuilder(
valueListenable: Hive.box('myfavourites').listenable(),
builder: (BuildContext context, Box box, child) {
List favourites = box.values.toList();
favourites.sort((a, b) => a['timeStamp'].compareTo(b['timeStamp']));
if (favourites.isEmpty) {
return Center(
child: Text(
"Nothing Here",
style: Theme.of(context).primaryTextTheme.bodyLarge,
),
);
}
return ListView(
children: favourites.map((track) {
Map<String, dynamic> newMap = jsonDecode(jsonEncode(track));
return TrackTile(track: newMap);
}).toList(),
);
},
),
);
}
}
2 changes: 1 addition & 1 deletion lib/screens/HomeScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class _HomeScreenState extends State<HomeScreen>
.sublist(0,
trending.length > 10 ? 10 : trending.length)
.map((s) {
Track song = Track.fromJson(jsonEncode(s));
Track song = Track.fromMap(s);
return Builder(
builder: (BuildContext context) {
return ClipRRect(
Expand Down
15 changes: 15 additions & 0 deletions lib/screens/MainScreen.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:miniplayer/miniplayer.dart';
import 'package:provider/provider.dart';
Expand All @@ -7,6 +8,7 @@ import 'package:vibe_music/providers/LanguageProvider.dart';
import 'package:vibe_music/providers/MusicPlayer.dart';
import 'package:vibe_music/providers/ThemeProvider.dart';
import 'package:vibe_music/screens/ArtistScreen.dart';
import 'package:vibe_music/screens/FavouriteScreen.dart';
import 'package:vibe_music/screens/HomeScreen.dart';
import 'package:vibe_music/screens/PlayListScreen.dart';
import 'package:vibe_music/screens/PlayerScreen.dart';
Expand Down Expand Up @@ -75,6 +77,11 @@ class _MainScreenState extends State<MainScreen> {
navigatorKey: _searchNavigatorKey,
),
),
Directionality(
textDirection:
context.watch<LanguageProvider>().textDirection,
child: const FavouriteScreen(),
),
Directionality(
textDirection:
context.watch<LanguageProvider>().textDirection,
Expand Down Expand Up @@ -128,6 +135,14 @@ class _MainScreenState extends State<MainScreen> {
selectedIcon: const Icon(Icons.search_rounded),
label: S.of(context).Search,
),
NavigationDestination(
icon: Icon(
CupertinoIcons.heart,
color: isDarkTheme ? Colors.white : Colors.black,
),
selectedIcon: const Icon(CupertinoIcons.heart_fill),
label: S.of(context).Settings,
),
NavigationDestination(
icon: Icon(
Icons.settings_outlined,
Expand Down
Loading

0 comments on commit f0ce8c5

Please sign in to comment.