Skip to content

Commit d173db1

Browse files
committed
🚧 add songs list 80%
1 parent 96c2f50 commit d173db1

File tree

13 files changed

+465
-8
lines changed

13 files changed

+465
-8
lines changed

lib/core/constants/app_urls.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AppUrls {
2+
static const fireStorage =
3+
'https://firebasestorage.googleapis.com/v0/b/spotify-flutter-4aa82.appspot.com/o/covers%2F';
4+
static const mediaAlt = 'alt=media';
5+
}

lib/data/models/songs/songs.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:spotify_with_flutter/domain/entities/songs/songs.dart';
3+
4+
class SongModel {
5+
String? title;
6+
String? artist;
7+
num? duration;
8+
Timestamp? releaseDate;
9+
10+
SongModel.fromJson(Map<String, dynamic> data) {
11+
title = data['title'];
12+
artist = data['artist'];
13+
duration = data['duration'];
14+
releaseDate = data['releaseDate'];
15+
}
16+
}
17+
18+
extension SongModelX on SongModel {
19+
SongEntity toEntity() {
20+
return SongEntity(
21+
title: title!,
22+
artist: artist!,
23+
duration: duration!,
24+
releaseDate: releaseDate!,
25+
);
26+
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:dartz/dartz.dart';
2+
import 'package:spotify_with_flutter/data/sources/song/song_firebase_service.dart';
3+
import 'package:spotify_with_flutter/domain/repository/song/song.dart';
4+
import 'package:spotify_with_flutter/service_locator.dart';
5+
6+
class SongRepositoryImpl extends SongsRepository {
7+
@override
8+
Future<Either> getNewsSongs() async {
9+
return await sl<SongFirebaseService>().getNewsSongs();
10+
}
11+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
import 'package:dartz/dartz.dart';
3+
import 'package:spotify_with_flutter/data/models/songs/songs.dart';
4+
import 'package:spotify_with_flutter/domain/entities/songs/songs.dart';
5+
6+
abstract class SongFirebaseService {
7+
Future<Either> getNewsSongs();
8+
}
9+
10+
class SongFirebaseServiceImpl extends SongFirebaseService {
11+
@override
12+
Future<Either> getNewsSongs() async {
13+
try {
14+
List<SongEntity> songs = [];
15+
var data = await FirebaseFirestore.instance
16+
.collection('songs')
17+
.orderBy('releaseDate', descending: true)
18+
.limit(3)
19+
.get();
20+
21+
for (var element in data.docs) {
22+
var songModel = SongModel.fromJson(element.data());
23+
songs.add(songModel.toEntity());
24+
}
25+
26+
return right(songs);
27+
} on Exception catch (e) {
28+
return const Left('An error occurred, Please try again.');
29+
}
30+
}
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:cloud_firestore/cloud_firestore.dart';
2+
3+
class SongEntity {
4+
final String title;
5+
final String artist;
6+
final num duration;
7+
final Timestamp releaseDate;
8+
9+
SongEntity({
10+
required this.title,
11+
required this.artist,
12+
required this.duration,
13+
required this.releaseDate,
14+
});
15+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'package:dartz/dartz.dart';
2+
3+
abstract class SongsRepository {
4+
Future<Either> getNewsSongs();
5+
}

lib/domain/usecase/auth/signin.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import 'package:dartz/dartz.dart';
22
import 'package:spotify_with_flutter/core/usecase/usecase.dart';
3-
import 'package:spotify_with_flutter/data/models/auth/create_user_req.dart';
43
import 'package:spotify_with_flutter/data/models/auth/signin_user_req.dart';
54
import 'package:spotify_with_flutter/domain/repository/auth/auth.dart';
65
import 'package:spotify_with_flutter/service_locator.dart';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:dartz/dartz.dart';
2+
import 'package:spotify_with_flutter/core/usecase/usecase.dart';
3+
import 'package:spotify_with_flutter/domain/repository/song/song.dart';
4+
import 'package:spotify_with_flutter/service_locator.dart';
5+
6+
class GetNewsSongsUseCase implements UseCase<Either, dynamic> {
7+
@override
8+
Future<Either> call({params}) async {
9+
return sl<SongsRepository>().getNewsSongs();
10+
}
11+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:flutter_bloc/flutter_bloc.dart';
2+
import 'package:spotify_with_flutter/domain/usecase/song/get_news_songs.dart';
3+
import 'package:spotify_with_flutter/presentation/home/bloc/news_songs_state.dart';
4+
import 'package:spotify_with_flutter/service_locator.dart';
5+
6+
class NewsSongsCubit extends Cubit<NewsSongsState> {
7+
NewsSongsCubit() : super(NewsSongsLoading());
8+
9+
Future<void> getNewSongs() async {
10+
var returnedSongs = await sl<GetNewsSongsUseCase>().call();
11+
12+
returnedSongs.fold(
13+
(l) {
14+
emit(NewsSongsLoadFailure());
15+
},
16+
(data) {
17+
emit(
18+
NewsSongsLoaded(songs: data),
19+
);
20+
},
21+
);
22+
}
23+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'package:spotify_with_flutter/domain/entities/songs/songs.dart';
2+
3+
abstract class NewsSongsState {}
4+
5+
class NewsSongsLoading extends NewsSongsState {}
6+
7+
class NewsSongsLoaded extends NewsSongsState {
8+
final List<SongEntity> songs;
9+
NewsSongsLoaded({required this.songs});
10+
}
11+
12+
class NewsSongsLoadFailure extends NewsSongsState {}

0 commit comments

Comments
 (0)