Removed sealed class

This commit is contained in:
Hemanth S 2020-05-26 04:10:40 +05:30
parent 530b42608b
commit 7f9fa54292
21 changed files with 277 additions and 405 deletions

View file

@ -16,9 +16,6 @@ package code.name.monkey.retromusic.providers
import android.content.Context
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.Result.Error
import code.name.monkey.retromusic.Result.Success
import code.name.monkey.retromusic.adapter.HomeAdapter
import code.name.monkey.retromusic.loaders.*
import code.name.monkey.retromusic.model.*
@ -26,268 +23,114 @@ import code.name.monkey.retromusic.providers.interfaces.Repository
import code.name.monkey.retromusic.rest.LastFmClient
import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.rest.model.LastFmArtist
import java.io.IOException
import javax.inject.Inject
class RepositoryImpl @Inject constructor(private val context: Context) : Repository {
override suspend fun allAlbums(): Result<ArrayList<Album>> {
return try {
val albums = AlbumLoader.getAllAlbums(context)
if (albums.isNotEmpty()) {
Success(albums)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
override suspend fun allAlbums(): List<Album> = AlbumLoader.getAllAlbums(context)
override suspend fun allArtists(): List<Artist> = ArtistLoader.getAllArtists(context)
override suspend fun allPlaylists(): List<Playlist> = PlaylistLoader.getAllPlaylists(context)
override suspend fun allGenres(): List<Genre> = GenreLoader.getAllGenres(context)
override suspend fun allSongs(): List<Song> = SongLoader.getAllSongs(context)
override suspend fun albumById(albumId: Int): Album = AlbumLoader.getAlbum(context, albumId)
override suspend fun artistById(artistId: Int): Artist =
ArtistLoader.getArtist(context, artistId)
override suspend fun search(query: String?): MutableList<Any> =
SearchLoader.searchAll(context, query)
override suspend fun getPlaylistSongs(playlist: Playlist): ArrayList<Song> {
return if (playlist is AbsCustomPlaylist) {
playlist.getSongs(context)
} else {
PlaylistSongsLoader.getPlaylistSongList(context, playlist.id)
}
}
override suspend fun albumById(albumId: Int): Result<Album> {
return try {
val album = AlbumLoader.getAlbum(context, albumId)
if (album != null) {
Success(album)
} else {
Error(Throwable("No album"))
}
} catch (e: Exception) {
Error(e)
}
override suspend fun getGenre(genreId: Int): ArrayList<Song> =
GenreLoader.getSongs(context, genreId)
override suspend fun recentArtists(): Home? {
val artists = LastAddedSongsLoader.getLastAddedArtists(context)
return if (artists.isNotEmpty()) Home(
0,
R.string.recent_artists,
artists,
HomeAdapter.RECENT_ARTISTS,
R.drawable.ic_artist_white_24dp
) else null
}
override suspend fun allArtists(): Result<ArrayList<Artist>> {
return try {
val artists = ArtistLoader.getAllArtists(context)
if (artists.isNotEmpty()) {
Success(artists)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
override suspend fun recentAlbums(): Home? {
val albums = LastAddedSongsLoader.getLastAddedAlbums(context)
return if (albums.isNotEmpty()) {
Home(
1,
R.string.recent_albums,
albums,
HomeAdapter.RECENT_ALBUMS,
R.drawable.ic_album_white_24dp
)
} else null
}
override suspend fun allPlaylists(): Result<ArrayList<Playlist>> {
return try {
val playlists = PlaylistLoader.getAllPlaylists(context)
if (playlists.isNotEmpty()) {
Success(playlists)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
override suspend fun topAlbums(): Home? {
val albums = TopAndRecentlyPlayedTracksLoader.getTopAlbums(context)
return if (albums.isNotEmpty()) {
Home(
3,
R.string.top_albums,
albums,
HomeAdapter.TOP_ALBUMS,
R.drawable.ic_album_white_24dp
)
} else null
}
override suspend fun allGenres(): Result<ArrayList<Genre>> {
return try {
val genres = GenreLoader.getAllGenres(context)
if (genres.isNotEmpty()) {
Success(genres)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
override suspend fun topArtists(): Home? {
val artists = TopAndRecentlyPlayedTracksLoader.getTopArtists(context)
return if (artists.isNotEmpty()) {
Home(
2,
R.string.top_artists,
artists,
HomeAdapter.TOP_ARTISTS,
R.drawable.ic_artist_white_24dp
)
} else null
}
override suspend fun search(query: String?): Result<MutableList<Any>> {
return try {
val result = SearchLoader.searchAll(context, query)
if (result.isNotEmpty()) {
Success(result)
} else {
Error(Throwable("No items found"))
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun allSongs(): Result<ArrayList<Song>> {
return try {
val songs = SongLoader.getAllSongs(context)
if (songs.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(songs)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun getPlaylistSongs(playlist: Playlist): Result<ArrayList<Song>> {
return try {
val songs: ArrayList<Song> = if (playlist is AbsCustomPlaylist) {
playlist.getSongs(context)
} else {
PlaylistSongsLoader.getPlaylistSongList(context, playlist.id)
}
Success(songs)
} catch (e: Exception) {
Error(e)
}
}
override suspend fun getGenre(genreId: Int): Result<ArrayList<Song>> {
return try {
val songs = GenreLoader.getSongs(context, genreId)
if (songs.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(songs)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun recentArtists(): Result<Home> {
return try {
val artists = LastAddedSongsLoader.getLastAddedArtists(context)
if (artists.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
Home(
0,
R.string.recent_artists,
artists,
HomeAdapter.RECENT_ARTISTS,
R.drawable.ic_artist_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun recentAlbums(): Result<Home> {
return try {
val albums = LastAddedSongsLoader.getLastAddedAlbums(context)
if (albums.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
Home(
1,
R.string.recent_albums,
albums,
HomeAdapter.RECENT_ALBUMS,
R.drawable.ic_album_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun topAlbums(): Result<Home> {
return try {
val albums = TopAndRecentlyPlayedTracksLoader.getTopAlbums(context)
if (albums.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
Home(
3,
R.string.top_albums,
albums,
HomeAdapter.TOP_ALBUMS,
R.drawable.ic_album_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun topArtists(): Result<Home> {
return try {
val artists = TopAndRecentlyPlayedTracksLoader.getTopArtists(context)
if (artists.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
Home(
2,
R.string.top_artists,
artists,
HomeAdapter.TOP_ARTISTS,
R.drawable.ic_artist_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
}
override suspend fun favoritePlaylist(): Result<Home> {
return try {
val playlists = PlaylistLoader.getFavoritePlaylist(context)
if (playlists.isEmpty()) {
Error(Throwable("No items found"))
} else {
Success(
Home(
4,
R.string.favorites,
playlists,
HomeAdapter.PLAYLISTS,
R.drawable.ic_favorite_white_24dp
)
)
}
} catch (e: Exception) {
Error(e)
}
override suspend fun favoritePlaylist(): Home? {
val playlists = PlaylistLoader.getFavoritePlaylist(context)
return if (playlists.isNotEmpty()) {
Home(
4,
R.string.favorites,
playlists,
HomeAdapter.PLAYLISTS,
R.drawable.ic_favorite_white_24dp
)
} else null
}
override suspend fun artistInfo(
name: String,
lang: String?,
cache: String?
): Result<LastFmArtist> = safeApiCall(
call = {
Success(LastFmClient.getApiService().artistInfo(name, lang, cache))
},
errorMessage = "Error"
): LastFmArtist = LastFmClient.getApiService().artistInfo(name, lang, cache)
)
override suspend fun albumInfo(
artist: String,
album: String
): Result<LastFmAlbum> = safeApiCall(
call = {
Success(LastFmClient.getApiService().albumInfo(artist, album))
},
errorMessage = "Error"
)
): LastFmAlbum = LastFmClient.getApiService().albumInfo(artist, album)
override suspend fun artistById(artistId: Int): Result<Artist> {
return try {
val artist = ArtistLoader.getArtist(context, artistId)
return Success(artist)
} catch (e: Exception) {
Error(Throwable("Error loading artist"))
}
}
}
suspend fun <T : Any> safeApiCall(call: suspend () -> Result<T>, errorMessage: String): Result<T> =
try {
call.invoke()
} catch (e: Exception) {
Error(IOException(errorMessage, e))
}
}

View file

@ -14,7 +14,6 @@
package code.name.monkey.retromusic.providers.interfaces
import code.name.monkey.retromusic.Result
import code.name.monkey.retromusic.model.*
import code.name.monkey.retromusic.rest.model.LastFmAlbum
import code.name.monkey.retromusic.rest.model.LastFmArtist
@ -25,37 +24,37 @@ import code.name.monkey.retromusic.rest.model.LastFmArtist
interface Repository {
suspend fun allAlbums(): Result<ArrayList<Album>>
suspend fun allAlbums(): List<Album>
suspend fun albumById(albumId: Int): Result<Album>
suspend fun albumById(albumId: Int): Album
suspend fun allSongs(): Result<ArrayList<Song>>
suspend fun allSongs(): List<Song>
suspend fun allArtists(): Result<ArrayList<Artist>>
suspend fun allArtists(): List<Artist>
suspend fun allPlaylists(): Result<ArrayList<Playlist>>
suspend fun allPlaylists(): List<Playlist>
suspend fun allGenres(): Result<ArrayList<Genre>>
suspend fun allGenres(): List<Genre>
suspend fun search(query: String?): Result<MutableList<Any>>
suspend fun search(query: String?): MutableList<Any>
suspend fun getPlaylistSongs(playlist: Playlist): Result<ArrayList<Song>>
suspend fun getPlaylistSongs(playlist: Playlist): ArrayList<Song>
suspend fun getGenre(genreId: Int): Result<ArrayList<Song>>
suspend fun getGenre(genreId: Int): ArrayList<Song>
suspend fun recentArtists(): Result<Home>
suspend fun recentArtists(): Home?
suspend fun topArtists(): Result<Home>
suspend fun topArtists(): Home?
suspend fun topAlbums(): Result<Home>
suspend fun topAlbums(): Home?
suspend fun recentAlbums(): Result<Home>
suspend fun recentAlbums(): Home?
suspend fun favoritePlaylist(): Result<Home>
suspend fun favoritePlaylist(): Home?
suspend fun artistInfo(name: String, lang: String?, cache: String?): Result<LastFmArtist>
suspend fun artistInfo(name: String, lang: String?, cache: String?): LastFmArtist
suspend fun albumInfo(artist: String, album: String): Result<LastFmAlbum>
suspend fun albumInfo(artist: String, album: String): LastFmAlbum
suspend fun artistById(artistId: Int): Result<Artist>
suspend fun artistById(artistId: Int): Artist
}