PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/repository/SongRepository.kt

152 lines
4.4 KiB
Kotlin

/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.repository
import android.content.Context
import code.name.monkey.retromusic.model.Song
import software.lavender.music.query.IntFilter
import software.lavender.music.query.LongFilter
import software.lavender.music.query.Query
import software.lavender.music.query.StringFilter
// TODO: sorting
data class SongQuery(
val id: LongFilter? = null,
val title: StringFilter? = null,
val year: IntFilter? = null,
val data: StringFilter? = null,
val dateModified: LongFilter? = null,
val albumId: LongFilter? = null,
val albumName: StringFilter? = null,
val artistId: LongFilter? = null,
val artistName: StringFilter? = null,
val composer: StringFilter? = null,
val albumArtist: StringFilter? = null
) : Query<Song> {
override fun apply(testee: Song): Boolean {
if (id != null && !id.matches(testee.id)) {
return false
}
if (title != null && !title.matches(testee.title)) {
return false
}
if (year != null && !year.matches(testee.year)) {
return false
}
if (data != null && !data.matches(testee.data)) {
return false
}
if (dateModified != null && !dateModified.matches(testee.dateModified)) {
return false
}
if (albumId != null && !albumId.matches(testee.albumId)) {
return false
}
return true
}
}
/**
* Created by hemanths on 10/08/17.
*/
interface SongRepository {
fun songs(): List<Song>
fun songs(query: SongQuery): List<Song>
fun songs(query: String): List<Song>
fun songsByFilePath(filePath: String): List<Song>
fun song(query: SongQuery): Song
fun song(songId: Long): Song
}
class RealSongRepository(private val context: Context) : SongRepository {
override fun songs(): List<Song> {
return listOf(
Song(
1,
"river-lake convergence",
1,
2021,
237234,
"https://versary.town/river-lake-convergence.mp3",
1639589623,
1,
"river-lake convergence",
1,
"annieversary",
null,
"annieversary",
"https://i1.sndcdn.com/artworks-T3nLbND6w681B2ey-YDI6ew-t500x500.jpg",
genres = arrayOf("Lakecore")
),
Song(
2,
"river-lake convergence 2",
1,
2021,
237234,
"https://versary.town/river-lake-convergence.mp3",
1639589623,
2,
"river-lake convergence 2",
1,
"annieversary",
null,
"annieversary",
"https://i1.sndcdn.com/artworks-T3nLbND6w681B2ey-YDI6ew-t500x500.jpg",
genres = arrayOf("Lakecore")
)
)
}
override fun songs(query: SongQuery): List<Song> {
val songs = arrayListOf<Song>()
for (song in songs()) {
if (query.apply(song)) {
songs.add(song)
}
}
return songs
}
override fun song(query: SongQuery): Song {
val songs = arrayListOf<Song>()
for (song in songs()) {
if (query.apply(song)) {
songs.add(song)
}
}
return songs.firstOrNull() ?: Song.emptySong
}
override fun songs(query: String): List<Song> {
return songs(SongQuery(title = StringFilter(like = "%$query%")))
}
override fun song(songId: Long): Song {
return song(SongQuery(id = LongFilter(eq = songId)))
}
override fun songsByFilePath(filePath: String): List<Song> {
return songs(SongQuery(data = StringFilter(eq = filePath)))
}
}