mucke/lib/system/models/song_model.dart

134 lines
3.6 KiB
Dart
Raw Normal View History

2020-04-06 22:33:26 +02:00
import 'package:audio_service/audio_service.dart';
import 'package:flutter_audio_query/flutter_audio_query.dart';
2020-03-28 14:35:41 +01:00
import 'package:meta/meta.dart';
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-06-28 21:52:10 +02:00
const SongModel(
{@required String title,
@required String album,
@required this.albumId,
@required String artist,
@required String path,
@required int duration,
int trackNumber,
String albumArtPath})
: super(
2020-03-28 14:35:41 +01:00
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,
);
factory SongModel.fromMoorSong(MoorSong moorSong) => SongModel(
title: moorSong.title,
artist: moorSong.artist,
2020-06-28 10:40:53 +02:00
album: moorSong.albumTitle,
albumId: moorSong.albumId,
path: moorSong.path,
2020-04-11 14:27:49 +02:00
duration: moorSong.duration,
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;
return SongModel(
title: songInfo.title,
artist: songInfo.artist,
album: songInfo.album,
2020-06-28 10:40:53 +02:00
albumId: int.parse(songInfo.albumId),
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-04-09 17:50:28 +02:00
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-06-28 21:52:10 +02:00
final tn = mediaItem.extras['trackNumber'];
int trackNumber;
if (tn == null) {
trackNumber = null;
} else {
trackNumber = tn as int;
}
2020-04-09 17:50:28 +02:00
return SongModel(
2020-04-10 20:12:26 +02:00
title: mediaItem.title,
album: mediaItem.album,
2020-06-28 21:52:10 +02:00
albumId: mediaItem.extras['albumId'] as int,
2020-04-10 20:12:26 +02:00
artist: mediaItem.artist,
path: mediaItem.id,
2020-06-03 21:17:08 +02:00
duration: mediaItem.duration.inMilliseconds,
2020-04-10 20:12:26 +02:00
albumArtPath: artUri,
2020-06-28 21:52:10 +02:00
trackNumber: trackNumber,
2020-04-10 20:12:26 +02:00
);
2020-04-09 17:50:28 +02:00
}
2020-06-28 21:52:10 +02:00
final int albumId;
2020-07-12 18:25:34 +02:00
@override
String toString() {
return '$title';
}
2020-06-28 21:52:10 +02:00
SongModel copyWith({
String title,
String album,
String artist,
String path,
int duration,
int trackNumber,
String albumArtPath,
int albumId,
}) =>
SongModel(
album: album ?? this.album,
artist: artist ?? this.artist,
duration: duration ?? this.duration,
path: path ?? this.path,
title: title ?? this.title,
trackNumber: trackNumber ?? this.trackNumber,
albumArtPath: albumArtPath ?? this.albumArtPath,
albumId: albumId ?? this.albumId,
);
SongsCompanion toSongsCompanion() => SongsCompanion(
2020-06-28 10:40:53 +02:00
albumTitle: Value(album),
albumId: Value(albumId),
artist: Value(artist),
title: Value(title),
path: Value(path),
2020-04-11 14:27:49 +02:00
duration: Value(duration),
albumArtPath: Value(albumArtPath),
trackNumber: Value(trackNumber),
);
2020-04-06 22:33:26 +02:00
MediaItem toMediaItem() => MediaItem(
2020-07-12 18:25:34 +02:00
id: path,
title: title,
album: album,
artist: artist,
duration: Duration(milliseconds: duration),
artUri: 'file://$albumArtPath',
extras: {
'albumId': albumId,
'trackNumber': trackNumber,
});
2020-03-28 14:35:41 +01:00
}