Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial conversion of the top albums logic to the functional style (the biggest... #4

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ dependencies {
compile "com.android.support:cardview-v7:$supportVersion"
compile "com.android.support:palette-v7:$supportVersion"
compile "com.android.support:design:$supportVersion"
compile "de.greenrobot:eventbus:2.4.1"
compile "com.squareup.picasso:picasso:2.5.2"
compile "com.squareup.okhttp3:okhttp:$okhttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
Expand All @@ -69,7 +68,6 @@ dependencies {
exclude module: 'okhttp'
}
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
compile "com.birbit:android-priority-jobqueue:2.0.1"
compile "org.jetbrains.anko:anko-sdk15:$parent.ext.ankoVersion"
compile "org.jetbrains.anko:anko-support-v4:$parent.ext.ankoVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,38 @@ package com.antonioleiva.bandhookkotlin.data

import com.antonioleiva.bandhookkotlin.Result
import com.antonioleiva.bandhookkotlin.data.lastfm.LastFmService
import com.antonioleiva.bandhookkotlin.data.lastfm.model.LastFmResponse
import com.antonioleiva.bandhookkotlin.data.mapper.AlbumMapper
import com.antonioleiva.bandhookkotlin.domain.entity.Album
import com.antonioleiva.bandhookkotlin.domain.entity.BizException.AlbumNotFound
import com.antonioleiva.bandhookkotlin.domain.entity.BizException.TopAlbumsNotFound
import com.antonioleiva.bandhookkotlin.left
import com.antonioleiva.bandhookkotlin.repository.datasource.AlbumDataSource
import com.antonioleiva.bandhookkotlin.right

class CloudAlbumDataSource(val lastFmService: LastFmService) : AlbumDataSource {

override fun get(id: String): Result<AlbumNotFound, Album> {
return lastFmService.requestAlbum(id).asResult<AlbumNotFound, LastFmResponse, Album> {
return lastFmService.requestAlbum(id).asResult {
AlbumMapper().transform(album).fold(
{ AlbumNotFound(id).left() },
{ it.right() }
)
}
}

override fun requestTopAlbums(artistId: String?, artistName: String?): List<Album> {
val mbid = artistId ?: ""
val name = artistName ?: ""

if (!mbid.isEmpty() || !name.isEmpty()) {
return lastFmService.requestAlbums(mbid, name).unwrapCall {
AlbumMapper().transform(topAlbums.albums)
}
}

return emptyList()
override fun requestTopAlbums(artistId: String?, artistName: String?):
Result<TopAlbumsNotFound, List<Album>> {
// val mbid = artistId ?: ""
// val name = artistName ?: ""
//
// if (!mbid.isEmpty() || !name.isEmpty()) {
// return lastFmService.requestAlbums(mbid, name).unwrapCall {
// AlbumMapper().transform(topAlbums.albums)
// }
// }
//
// return emptyList()
return Result.pure(emptyList())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ interface LastFmService {

@GET("/2.0/?method=album.getInfo")
fun requestAlbum(@Query("mbid") id: String): Call<LastFmResponse>
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import org.funktionale.option.toOption
class AlbumMapper(val artistMapper: ArtistMapper = ArtistMapper(), val imageMapper: ImageMapper = ImageMapper(),
val trackMapper: TrackMapper = TrackMapper()) {

fun transform(albums: List<LastFmAlbum>): List<Album> {
return albums.filter { albumHasQualityInfo(it) }.mapNotNull { transform(it) }
fun transform(albums: List<LastFmAlbum>): Option<List<Album>> {
return albums.filter { albumHasQualityInfo(it) }.mapNotNull { transform(it) }.toOption()
}

private fun albumHasQualityInfo(album: LastFmAlbum): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import android.content.Context
import com.antonioleiva.bandhookkotlin.App
import com.antonioleiva.bandhookkotlin.di.qualifier.ApplicationQualifier
import com.antonioleiva.bandhookkotlin.di.qualifier.LanguageSelection
import com.antonioleiva.bandhookkotlin.domain.BusImpl
import com.antonioleiva.bandhookkotlin.domain.interactor.base.Bus
import com.antonioleiva.bandhookkotlin.domain.interactor.base.CustomJobManager
import com.antonioleiva.bandhookkotlin.domain.interactor.base.InteractorExecutor
import com.antonioleiva.bandhookkotlin.domain.interactor.base.InteractorExecutorImpl
import com.birbit.android.jobqueue.JobManager
import com.squareup.picasso.Picasso
import dagger.Module
import dagger.Provides
Expand All @@ -25,18 +19,10 @@ class ApplicationModule(private val app: App) {
@Provides @Singleton @ApplicationQualifier
fun provideApplicationContext(): Context = app

@Provides @Singleton
fun provideBus(): Bus = BusImpl()

@Provides @Singleton
fun providePicasso(@ApplicationQualifier context: Context): Picasso = Picasso.Builder(context).build()

@Provides @Singleton
fun provideJobManager(@ApplicationQualifier context: Context): JobManager = CustomJobManager(context)

@Provides @Singleton
fun provideInteractorExecutor(jobManager: JobManager, bus: Bus): InteractorExecutor = InteractorExecutorImpl(jobManager, bus)

@Provides @Singleton @LanguageSelection
fun provideLanguageSelection(): String = Locale.getDefault().language

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import android.support.v7.widget.LinearLayoutManager
import com.antonioleiva.bandhookkotlin.di.ActivityModule
import com.antonioleiva.bandhookkotlin.di.scope.ActivityScope
import com.antonioleiva.bandhookkotlin.domain.interactor.GetAlbumDetailInteractor
import com.antonioleiva.bandhookkotlin.domain.interactor.base.Bus
import com.antonioleiva.bandhookkotlin.ui.adapter.TracksAdapter
import com.antonioleiva.bandhookkotlin.ui.entity.mapper.AlbumDetailDataMapper
import com.antonioleiva.bandhookkotlin.ui.entity.mapper.TrackDataMapper
Expand Down Expand Up @@ -35,8 +34,7 @@ class AlbumActivityModule(activity: AlbumActivity) : ActivityModule(activity) {

@Provides @ActivityScope
fun provideAlbumPresenter(view: AlbumView,
bus: Bus,
albumInteractor: GetAlbumDetailInteractor,
albumDetailDataMapper: AlbumDetailDataMapper)
= AlbumPresenter(view, bus, albumInteractor, albumDetailDataMapper)
= AlbumPresenter(view, albumInteractor, albumDetailDataMapper)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import com.antonioleiva.bandhookkotlin.di.ActivityModule
import com.antonioleiva.bandhookkotlin.di.scope.ActivityScope
import com.antonioleiva.bandhookkotlin.domain.interactor.GetArtistDetailInteractor
import com.antonioleiva.bandhookkotlin.domain.interactor.GetTopAlbumsInteractor
import com.antonioleiva.bandhookkotlin.domain.interactor.base.Bus
import com.antonioleiva.bandhookkotlin.domain.interactor.base.InteractorExecutor
import com.antonioleiva.bandhookkotlin.ui.entity.mapper.ArtistDetailDataMapper
import com.antonioleiva.bandhookkotlin.ui.entity.mapper.ImageTitleDataMapper
import com.antonioleiva.bandhookkotlin.ui.presenter.ArtistPresenter
Expand All @@ -30,18 +28,17 @@ class ArtistActivityModule(activity: ArtistActivity) : ActivityModule(activity)

@Provides @ActivityScope
fun provideActivityPresenter(view: ArtistView,
bus: Bus,
artistDetailInteractor: GetArtistDetailInteractor,
topAlbumsInteractor: GetTopAlbumsInteractor,
interactorExecutor: InteractorExecutor,
detailDataMapper: ArtistDetailDataMapper,
imageTitleDataMapper: ImageTitleDataMapper)
= ArtistPresenter(view, bus, artistDetailInteractor, topAlbumsInteractor,
interactorExecutor, detailDataMapper, imageTitleDataMapper)
= ArtistPresenter(view, artistDetailInteractor, topAlbumsInteractor,
detailDataMapper, imageTitleDataMapper)

@Provides @ActivityScope
fun provideAlbumsFragment() = AlbumsFragment()

@Provides @ActivityScope
fun provideBiographyFragment() = BiographyFragment()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.antonioleiva.bandhookkotlin.di.subcomponent.main
import com.antonioleiva.bandhookkotlin.di.ActivityModule
import com.antonioleiva.bandhookkotlin.di.scope.ActivityScope
import com.antonioleiva.bandhookkotlin.domain.interactor.GetRecommendedArtistsInteractor
import com.antonioleiva.bandhookkotlin.domain.interactor.base.Bus
import com.antonioleiva.bandhookkotlin.domain.interactor.base.InteractorExecutor
import com.antonioleiva.bandhookkotlin.ui.adapter.ImageTitleAdapter
import com.antonioleiva.bandhookkotlin.ui.entity.mapper.ImageTitleDataMapper
import com.antonioleiva.bandhookkotlin.ui.presenter.MainPresenter
Expand All @@ -23,14 +21,12 @@ class MainActivityModule(activity: MainActivity) : ActivityModule(activity) {
fun provideImageTitleMapper() = ImageTitleDataMapper()

@Provides @ActivityScope
fun provideMainPresenter(view: MainView, bus: Bus,
fun provideMainPresenter(view: MainView,
recommendedArtistsInteractor: GetRecommendedArtistsInteractor,
interactorExecutor: InteractorExecutor,
imageMapper: ImageTitleDataMapper) = MainPresenter(view, bus, recommendedArtistsInteractor,
interactorExecutor, imageMapper)
imageMapper: ImageTitleDataMapper)
= MainPresenter(view, recommendedArtistsInteractor, imageMapper)

@Provides @ActivityScope
fun provideAdapter() = ImageTitleAdapter()


}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ sealed class BizException {
class AlbumNotFound(val id: String) : BizException()
class ArtistNotFound(val id: String) : BizException()
object RecomendationsNotFound : BizException()
}
class TopAlbumsNotFound(val artistId: String?, val artistName: String?) : BizException()
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ package com.antonioleiva.bandhookkotlin.domain.interactor

import com.antonioleiva.bandhookkotlin.Result
import com.antonioleiva.bandhookkotlin.domain.entity.Album
import com.antonioleiva.bandhookkotlin.domain.entity.BizException.*
import com.antonioleiva.bandhookkotlin.domain.entity.BizException.AlbumNotFound
import com.antonioleiva.bandhookkotlin.domain.repository.AlbumRepository

class GetAlbumDetailInteractor(val albumRepository: AlbumRepository) {

fun getAlbum(albumId: String): Result<AlbumNotFound, Album> =
albumRepository.get(albumId)
albumRepository.getAlbum(albumId)

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ import com.antonioleiva.bandhookkotlin.domain.repository.ArtistRepository

class GetArtistDetailInteractor(val artistRepository: ArtistRepository) {

fun getArtist(artistId: String) = artistRepository.get(artistId)
fun getArtist(artistId: String) = artistRepository.getArtist(artistId)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package com.antonioleiva.bandhookkotlin.domain.interactor
import com.antonioleiva.bandhookkotlin.NonEmptyList
import com.antonioleiva.bandhookkotlin.Result
import com.antonioleiva.bandhookkotlin.domain.entity.Artist
import com.antonioleiva.bandhookkotlin.domain.entity.BizException.*
import com.antonioleiva.bandhookkotlin.domain.entity.BizException.RecomendationsNotFound
import com.antonioleiva.bandhookkotlin.domain.repository.ArtistRepository

class GetRecommendedArtistsInteractor(val artistRepository: ArtistRepository) {

fun getRecommendedArtists(): Result<RecomendationsNotFound, NonEmptyList<Artist>> =
artistRepository.getRecommendedArtists()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@

package com.antonioleiva.bandhookkotlin.domain.interactor

import com.antonioleiva.bandhookkotlin.domain.interactor.base.Event
import com.antonioleiva.bandhookkotlin.domain.interactor.base.Interactor
import com.antonioleiva.bandhookkotlin.domain.interactor.event.TopAlbumsEvent
import com.antonioleiva.bandhookkotlin.domain.repository.AlbumRepository

class GetTopAlbumsInteractor(val albumRepository: AlbumRepository) : Interactor {
class GetTopAlbumsInteractor(val albumRepository: AlbumRepository) {

var artistId: String? = null
var artistName: String? = null

override fun invoke(): Event {
if (artistId == null && artistName == null) {
throw IllegalStateException("Either mbid or name should be specified")
}
val albums = albumRepository.getTopAlbums(artistId, artistName)
return TopAlbumsEvent(albums)
}
}
fun getTopAlbums(artistId: String?, artistName: String?) = albumRepository.getTopAlbums(artistId, artistName)
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading