Skip to content

Instantly share code, notes, and snippets.

class AlbumDataMapper(
private val songListDataMapper: NullableInputListMapper<NetworkSong, Song>
) : Mapper<NetworkAlbum, Album> {
override fun map(input: NetworkAlbum): Album {
return Album(
input.id.orEmpty(),
input.title.orEmpty(),
songListDataMapper.map(input.songs)
)
inline fun mapAlbumDto(input: NetworkAlbum, mapSongList: (List<NetworkSong>?) -> List<Song>): Album {
return Album(
input.id.orEmpty(),
input.title.orEmpty(),
mapSongList(input.songs)
)
}
fun mapSongDto(input: NetworkSong): Song {
return Song(
class AlbumRepositoryImpl(
private val albumApiService: AlbumApiService,
private val albumDataMapper: Mapper<NetworkAlbum, Album>
) : AlbumRepository {
override fun getAlbumById(id: String): Single<SimpleResult<Album>> {
return albumApiService.getAlbumById(id).map {
it.fold(
success = { dto -> Result.Success(albumDataMapper.map(dto)) },
failure = { throwable -> Result.Failure(throwable) }
class AlbumRepositoryImpl(
private val albumApiService: AlbumApiService,
private val mapAlbumDto: (NetworkAlbum) -> Album
) : AlbumRepository {
override fun getAlbumById(id: String): Single<SimpleResult<Album>> {
return albumApiService.getAlbumById(id).map {
it.fold(
success = { dto -> Result.Success(mapAlbumDto(dto)) },
failure = { throwable -> Result.Failure(throwable) }
object OOPRepositoryFactory {
fun makeAlbumRepository(): AlbumRepository {
return AlbumRepositoryImpl(
AlbumApiServiceImpl(),
AlbumDataMapper(NullableInputListMapperImpl(SongDataMapper()))
)
}
}
object FPRepositoryFactory {
fun makeAlbumRepository(): AlbumRepository {
return AlbumRepositoryImpl(
AlbumApiServiceImpl(),
makeAlbumDataMapper()
)
}
private fun makeAlbumDataMapper(): (NetworkAlbum) -> Album = { albumDto ->
class GetTransactionsUseCaseImpl(
private val getUserUseCase: GetUserUseCase,
private val transactionRepository: TransactionRepository
) : GetTransactionsUseCase {
override fun invoke(): Single<SimpleResult<List<Transaction>>> {
return transactionRepository.getTransactions(getUserUseCase().id)
}
}
inline fun getTransactions(
getUserUseCase: GetUserUseCase,
transactionRepository: TransactionRepository
): Single<SimpleResult<List<Transaction>>> {
return transactionRepository.getTransactions(getUserUseCase().id)
}
typealias GetTransactionsUseCase = () -> Single<SimpleResult<List<Transaction>>>
interface TransactionRepository {
fun getTransactions(userId: String): Single<SimpleResult<List<Transaction>>>
}
interface UserRepository {
fun getUser(): User
class GetUserUseCaseImpl(
private val userRepository: UserRepository
) : GetUserUseCase {
override fun invoke(): User = userRepository.getUser()
}