Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinhandoko committed Jul 6, 2024
1 parent 87772b7 commit ab3ecc2
Show file tree
Hide file tree
Showing 14 changed files with 1,399 additions and 294 deletions.
19 changes: 19 additions & 0 deletions lib/config/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:spotify_clone/screens/artist/artist_screen.dart';
import 'package:spotify_clone/screens/artist/artist_screen.dart';
import 'package:spotify_clone/screens/category_detail/category_detail.dart';
import 'package:spotify_clone/screens/common/splash_screen.dart';
import 'package:spotify_clone/screens/login/login_screen.dart';
Expand All @@ -10,6 +12,7 @@ import 'package:spotify_clone/screens/home/home_screen.dart';
import 'package:spotify_clone/screens/common/main_screen.dart';
import 'package:spotify_clone/screens/music_detail/music_detail_screen.dart';
import 'package:spotify_clone/screens/queue/queue_list_screen.dart';
import 'package:spotify_clone/screens/savedSong/saved_song_screen.dart';
import 'package:spotify_clone/screens/search_music/search_music_screens.dart';
import 'package:spotify_clone/screens/search/search_screen.dart';
import 'package:spotify_clone/screens/library/library_screen.dart';
Expand Down Expand Up @@ -51,6 +54,12 @@ class AppRouter {
);
},
),
GoRoute(
path: SavedSongScreen.routeName,
pageBuilder: (context, state) => const MaterialPage(
child: SavedSongScreen(),
),
),
GoRoute(
path: SearchMusicScreen.routeName,
pageBuilder: (context, state) {
Expand Down Expand Up @@ -86,6 +95,16 @@ class AppRouter {
),
],
),
GoRoute(
path: '${ArtistScreen.routeName}/:id',
pageBuilder: (context, state) {
final id = state.pathParameters['id']!;
return MaterialPage(
child: ArtistScreen(
artistId: id,
),
);
}),
GoRoute(
path: SplashScreen.routeName,
pageBuilder: (context, state) {
Expand Down
13 changes: 13 additions & 0 deletions lib/model/artist_detail.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:spotify/spotify.dart';

class ArtistDetail {
final Artist artist;
final List<AlbumSimple> albums;
final List<Track> topTracks;

ArtistDetail({
required this.artist,
required this.albums,
required this.topTracks,
});
}
46 changes: 0 additions & 46 deletions lib/model/custom_playlist.dart

This file was deleted.

105 changes: 84 additions & 21 deletions lib/providers/music_player_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,33 @@ class MusicPlayerProvider extends ChangeNotifier {
}

bool _isPlaying = false;

bool get isPlaying => _isPlaying;

final AudioPlayer _audioPlayer = AudioPlayer();

AudioPlayer get audioPlayer => _audioPlayer;

/// The current track that is being played
Track? _currentTrack;

Track? get currentTrack => _currentTrack;

/// The color of the current track
Color _currentTrackColor = Colors.grey;

Color get currentTrackColor => _currentTrackColor;

Duration _currentPosition = Duration.zero;

Duration get currentPosition => _currentPosition;

Duration _totalDuration = Duration.zero;

Duration get totalDuration => _totalDuration;

Artist? _currentArtist;

Artist? get currentArtist => _currentArtist;

final List<Track> _queue = [];
Expand All @@ -73,20 +80,30 @@ class MusicPlayerProvider extends ChangeNotifier {
final List<Track> _prevQueue = [];

Lyric? _lyric;

Lyric? get lyric => _lyric;

String _errorMessage = "";

String get errorMessage => _errorMessage;

bool _canPrev = false;

bool get canPrev => _canPrev;

bool _canNext = false;

bool get canNext => _canNext;

bool _isLoading = false;

bool get isLoading => _isLoading;

// TODO nanti ini dibuat dokumentasinya ya
bool _currentTrackIsLiked = false;

bool get currentTrackIsLiked => _currentTrackIsLiked;

/// resume the audio player
void resume() {
_isPlaying = true;
Expand Down Expand Up @@ -136,19 +153,30 @@ class MusicPlayerProvider extends ChangeNotifier {
/// function ini digunakan untuk menambahkan lagu dari playlist ke queue
/// semua list lagu sebelum index akan dimasukan ke prevQueue
/// semua list lagu setelah index akan dimasukan ke queue
void addFromPlaylist(List<Track> tracks, index) {
if (index == 0) {
_canPrev = false;
} else {
_canPrev = true;
_canNext = true;
}
void addFromPlaylist(List<Track> tracks, int index) {
assert(index >= 0 && index < tracks.length, "Index out of bounds");

// Determine navigation capabilities
_canPrev = index > 0;
_canNext = queue.isNotEmpty || index < tracks.length - 1;

// Stop the current playback
_audioPlayer.stop();
_prevQueue.clear();
_prevQueue.addAll(tracks.sublist(0, index));
_queue.clear();
_queue.addAll(tracks.sublist(index));
_currentTrack = _queue.first;

// Update the previous queue
_prevQueue
..clear()
..addAll(tracks.sublist(0, index));

// Update the current queue
_queue
..clear()
..addAll(tracks.sublist(index));

// Set the current track
_currentTrack = _queue.isNotEmpty ? _queue.first : null;

// Notify listeners about the changes
notifyListeners();
}

Expand All @@ -162,25 +190,46 @@ class MusicPlayerProvider extends ChangeNotifier {
/// If the queue is empty, this method does nothing.
/// If the current track is null, this method does nothing.
// TODO ini dijelasin ya diganti dengan Future.wait biar lebih cepat
Future<void> play() async {
if (_queue.isEmpty || _currentTrack == null) return;

_isLoading = true;
notifyListeners();

try {
Music music = await Youtube.getVideo(
songName: _currentTrack!.name ?? "",
artistName: _currentTrack!.artists!.first.name ?? "");
await _getCurrentArtist();
// Start fetching multiple data concurrently
final musicFuture = Youtube.getVideo(
songName: _currentTrack!.name ?? "",
artistName: _currentTrack!.artists!.first.name ?? "",
);

final artistFuture = _getCurrentArtist();

final colorFuture = ColorGenerator.getImagePalette(
NetworkImage(_currentTrack!.album!.images!.first.url ?? ""),
);

final lyricFuture = LyricService.getLyric(_currentTrack?.id ?? "");

// Wait for all the futures to complete
final results = await Future.wait([
musicFuture,
artistFuture,
colorFuture,
lyricFuture,
]);

// Extract the results
final Music music = results[0] as Music;
_totalDuration = music.duration!;
_currentTrackColor = await ColorGenerator.getImagePalette(
NetworkImage(_currentTrack!.album!.images!.first.url ?? "")) ??
Colors.grey;
_lyric = await LyricService.getLyric(_currentTrack?.id ?? "");
_currentTrackColor = results[2] as Color? ?? Colors.grey;
_lyric = results[3] as Lyric?;

_checkIfTrackIsLiked();
await _audioPlayer.play(UrlSource(music.url));
_isPlaying = true;
} catch (e) {
print(e);
_errorMessage = "This song cannot be played";
_isPlaying = false;
} finally {
Expand Down Expand Up @@ -244,6 +293,13 @@ class MusicPlayerProvider extends ChangeNotifier {
}
}

// TODO ini nanti dijelaskan ya
void _checkIfTrackIsLiked() async {
_currentTrackIsLiked =
await SpotifyService.checkTrackSaved(_currentTrack?.id ?? "");
notifyListeners();
}

void swap(int oldIndex, int newIndex) {
if (newIndex > oldIndex) {
newIndex -= 1;
Expand All @@ -260,4 +316,11 @@ class MusicPlayerProvider extends ChangeNotifier {
_currentArtist =
await SpotifyService.getArtist(_currentTrack?.artists?.first.id ?? "");
}

// TODO nanti ini dibuat dokumentasinya ya
Future<void> saveTrack(String trackId) async {
await SpotifyService.addSongToSavedTracks(trackId);
_currentTrackIsLiked = true;
notifyListeners();
}
}
Loading

0 comments on commit ab3ecc2

Please sign in to comment.