2020-04-06 22:33:26 +02:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
2020-03-30 22:25:57 +02:00
|
|
|
import 'package:flutter_audio_query/flutter_audio_query.dart';
|
2020-03-28 14:35:41 +01:00
|
|
|
import 'package:meta/meta.dart';
|
2020-03-30 22:25:57 +02:00
|
|
|
import 'package:moor/moor.dart';
|
|
|
|
|
|
|
|
import '../../domain/entities/song.dart';
|
|
|
|
import '../datasources/moor_music_data_source.dart';
|
2020-03-28 14:35:41 +01:00
|
|
|
|
|
|
|
class SongModel extends Song {
|
2020-04-11 14:27:49 +02:00
|
|
|
const SongModel({
|
2020-03-28 14:35:41 +01:00
|
|
|
this.id,
|
|
|
|
@required String title,
|
|
|
|
@required String album,
|
|
|
|
@required String artist,
|
|
|
|
@required String path,
|
2020-04-11 14:27:49 +02:00
|
|
|
@required int duration,
|
2020-03-28 14:35:41 +01:00
|
|
|
int trackNumber,
|
|
|
|
String albumArtPath,
|
|
|
|
}) : super(
|
|
|
|
title: title,
|
|
|
|
album: album,
|
|
|
|
artist: artist,
|
|
|
|
path: path,
|
2020-04-11 14:27:49 +02:00
|
|
|
duration: duration,
|
2020-03-28 14:35:41 +01:00
|
|
|
trackNumber: trackNumber,
|
|
|
|
albumArtPath: albumArtPath,
|
|
|
|
);
|
|
|
|
|
2020-03-30 22:25:57 +02:00
|
|
|
factory SongModel.fromMoorSong(MoorSong moorSong) => SongModel(
|
|
|
|
title: moorSong.title,
|
|
|
|
artist: moorSong.artist,
|
|
|
|
album: moorSong.album,
|
|
|
|
path: moorSong.path,
|
2020-04-11 14:27:49 +02:00
|
|
|
duration: moorSong.duration,
|
2020-03-30 22:25:57 +02:00
|
|
|
albumArtPath: moorSong.albumArtPath,
|
|
|
|
trackNumber: moorSong.trackNumber,
|
|
|
|
);
|
|
|
|
|
|
|
|
factory SongModel.fromSongInfo(SongInfo songInfo) {
|
2020-04-11 14:27:49 +02:00
|
|
|
final String trackNumber = songInfo.track;
|
|
|
|
final String duration = songInfo.duration;
|
2020-03-30 22:25:57 +02:00
|
|
|
|
|
|
|
return SongModel(
|
|
|
|
title: songInfo.title,
|
|
|
|
artist: songInfo.artist,
|
|
|
|
album: songInfo.album,
|
|
|
|
path: songInfo.filePath,
|
2020-04-11 14:27:49 +02:00
|
|
|
duration: duration == null ? null : int.parse(duration),
|
|
|
|
albumArtPath: songInfo.albumArtwork,
|
|
|
|
trackNumber: trackNumber == null ? null : int.parse(trackNumber),
|
2020-03-30 22:25:57 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-09 17:50:28 +02:00
|
|
|
// TODO: test
|
|
|
|
factory SongModel.fromMediaItem(MediaItem mediaItem) {
|
2020-04-10 20:12:26 +02:00
|
|
|
if (mediaItem == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final String artUri = mediaItem.artUri?.replaceFirst('file://', '');
|
2020-04-09 17:50:28 +02:00
|
|
|
|
|
|
|
return SongModel(
|
2020-04-10 20:12:26 +02:00
|
|
|
title: mediaItem.title,
|
|
|
|
album: mediaItem.album,
|
|
|
|
artist: mediaItem.artist,
|
|
|
|
path: mediaItem.id,
|
2020-04-11 14:27:49 +02:00
|
|
|
duration: mediaItem.duration,
|
2020-04-10 20:12:26 +02:00
|
|
|
albumArtPath: artUri,
|
|
|
|
);
|
2020-04-09 17:50:28 +02:00
|
|
|
}
|
|
|
|
|
2020-03-28 14:35:41 +01:00
|
|
|
final int id;
|
2020-03-30 22:25:57 +02:00
|
|
|
|
|
|
|
SongsCompanion toSongsCompanion() => SongsCompanion(
|
|
|
|
album: Value(album),
|
|
|
|
artist: Value(artist),
|
|
|
|
title: Value(title),
|
|
|
|
path: Value(path),
|
2020-04-11 14:27:49 +02:00
|
|
|
duration: Value(duration),
|
2020-03-30 22:25:57 +02:00
|
|
|
albumArtPath: Value(albumArtPath),
|
|
|
|
trackNumber: Value(trackNumber),
|
|
|
|
);
|
2020-04-06 22:33:26 +02:00
|
|
|
|
|
|
|
MediaItem toMediaItem() => MediaItem(
|
2020-04-11 14:27:49 +02:00
|
|
|
id: path,
|
2020-04-06 22:33:26 +02:00
|
|
|
title: title,
|
|
|
|
album: album,
|
|
|
|
artist: artist,
|
2020-04-11 14:27:49 +02:00
|
|
|
duration: duration,
|
2020-04-07 22:06:43 +02:00
|
|
|
artUri: 'file://$albumArtPath',
|
2020-04-06 22:33:26 +02:00
|
|
|
);
|
2020-03-28 14:35:41 +01:00
|
|
|
}
|