Refactoring
This commit is contained in:
+3
-1
@@ -1,9 +1,11 @@
|
||||
package com.ericampire.android.androidstudycase.data.datasource.animator
|
||||
|
||||
import com.ericampire.android.androidstudycase.domain.entity.Animator
|
||||
import com.ericampire.android.androidstudycase.util.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
|
||||
interface AnimatorDataSource {
|
||||
suspend fun findAll(): List<Animator>
|
||||
fun findAll(): Flow<Result<List<Animator>>>
|
||||
suspend fun save(animator: Animator)
|
||||
}
|
||||
+7
-2
@@ -2,14 +2,19 @@ package com.ericampire.android.androidstudycase.data.datasource.animator
|
||||
|
||||
import com.ericampire.android.androidstudycase.data.room.AnimatorDao
|
||||
import com.ericampire.android.androidstudycase.domain.entity.Animator
|
||||
import com.ericampire.android.androidstudycase.util.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
|
||||
class LocalAnimatorDataSource @Inject constructor(
|
||||
private val animatorDao: AnimatorDao
|
||||
) : AnimatorDataSource {
|
||||
|
||||
override suspend fun findAll(): List<Animator> {
|
||||
return animatorDao.findAll()
|
||||
override fun findAll(): Flow<Result<List<Animator>>> {
|
||||
return animatorDao.findAll().map {
|
||||
Result.Success(it)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun save(animator: Animator) {
|
||||
|
||||
+10
-3
@@ -13,9 +13,16 @@ import javax.inject.Inject
|
||||
class RemoteAnimatorDataSource @Inject constructor(
|
||||
private val httpClient: HttpClient
|
||||
) : AnimatorDataSource {
|
||||
override suspend fun findAll(): List<Animator> {
|
||||
val data = httpClient.get<AnimatorApiResponse>(ApiUrl.Animator.featured)
|
||||
return data.animatorAnimatorData.featuredAnimators.results
|
||||
|
||||
override fun findAll(): Flow<Result<List<Animator>>> {
|
||||
return flow {
|
||||
try {
|
||||
val data = httpClient.get<AnimatorApiResponse>(ApiUrl.Animator.featured)
|
||||
emit(Result.Success(data.animatorAnimatorData.featuredAnimators.results))
|
||||
} catch (e: Exception) {
|
||||
emit(Result.Error(e))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun save(animator: Animator) {
|
||||
|
||||
+10
@@ -9,9 +9,12 @@ import com.ericampire.android.androidstudycase.data.datasource.blog.RemoteBlogDa
|
||||
import com.ericampire.android.androidstudycase.data.datasource.lottiefiles.LocalLottieFileDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.lottiefiles.LottieFileDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.lottiefiles.RemoteLottieFileDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.user.LocalUserDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.user.UserDataSource
|
||||
import com.ericampire.android.androidstudycase.data.room.AnimatorDao
|
||||
import com.ericampire.android.androidstudycase.data.room.BlogDao
|
||||
import com.ericampire.android.androidstudycase.data.room.LottieFilesDao
|
||||
import com.ericampire.android.androidstudycase.data.room.UserDao
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
@@ -62,6 +65,13 @@ object DataSourceModule {
|
||||
return LocalBlogDataSource(dao)
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideLocalUserDataSource(
|
||||
dao: UserDao
|
||||
): UserDataSource {
|
||||
return LocalUserDataSource(dao)
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideRemoteAnimatorDataSource(
|
||||
httpClient: HttpClient
|
||||
|
||||
+14
-2
@@ -6,12 +6,15 @@ import com.ericampire.android.androidstudycase.data.datasource.blog.LocalBlogDat
|
||||
import com.ericampire.android.androidstudycase.data.datasource.blog.RemoteBlogDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.lottiefiles.LocalLottieFileDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.lottiefiles.RemoteLottieFileDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.user.LocalUserDataSource
|
||||
import com.ericampire.android.androidstudycase.data.repository.AnimatorRepositoryImpl
|
||||
import com.ericampire.android.androidstudycase.data.repository.BlogRepositoryImpl
|
||||
import com.ericampire.android.androidstudycase.data.repository.LottieFileRepositoryImpl
|
||||
import com.ericampire.android.androidstudycase.data.repository.UserRepositoryImpl
|
||||
import com.ericampire.android.androidstudycase.domain.repository.AnimatorRepository
|
||||
import com.ericampire.android.androidstudycase.domain.repository.BlogRepository
|
||||
import com.ericampire.android.androidstudycase.domain.repository.LottieFileRepository
|
||||
import com.ericampire.android.androidstudycase.domain.repository.UserRepository
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
@@ -26,18 +29,27 @@ object RepositoryModule {
|
||||
fun provideAnimatorRepository(
|
||||
localDataSource: LocalAnimatorDataSource,
|
||||
remoteDataSource: RemoteAnimatorDataSource,
|
||||
) : AnimatorRepository {
|
||||
): AnimatorRepository {
|
||||
return AnimatorRepositoryImpl(
|
||||
localDataSource = localDataSource,
|
||||
remoteDataSource = remoteDataSource
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideUserRepository(
|
||||
localDataSource: LocalUserDataSource,
|
||||
): UserRepository {
|
||||
return UserRepositoryImpl(
|
||||
localDataSource = localDataSource,
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
fun provideLottieFileRepository(
|
||||
localDataSource: LocalLottieFileDataSource,
|
||||
remoteDataSource: RemoteLottieFileDataSource,
|
||||
) : LottieFileRepository {
|
||||
): LottieFileRepository {
|
||||
return LottieFileRepositoryImpl(
|
||||
localDataSource = localDataSource,
|
||||
remoteDataSource = remoteDataSource
|
||||
|
||||
+12
-14
@@ -6,8 +6,6 @@ import com.ericampire.android.androidstudycase.domain.repository.AnimatorReposit
|
||||
import com.ericampire.android.androidstudycase.util.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import javax.inject.Inject
|
||||
|
||||
|
||||
@@ -16,19 +14,19 @@ class AnimatorRepositoryImpl @Inject constructor(
|
||||
private val localDataSource: AnimatorDataSource,
|
||||
) : AnimatorRepository {
|
||||
override fun findAll(): Flow<Result<List<Animator>>> {
|
||||
return flow {
|
||||
try {
|
||||
refreshData()
|
||||
emit(Result.Success(localDataSource.findAll()))
|
||||
} catch (e: Exception) {
|
||||
emit(Result.Error(e))
|
||||
refreshData()
|
||||
return localDataSource.findAll()
|
||||
}
|
||||
|
||||
private fun refreshData() {
|
||||
suspend {
|
||||
remoteDataSource.findAll().collect {
|
||||
if (it is Result.Success) {
|
||||
it.data.forEach { animator ->
|
||||
localDataSource.save(animator)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun refreshData() {
|
||||
remoteDataSource.findAll().forEach {
|
||||
localDataSource.save(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-16
@@ -1,14 +1,11 @@
|
||||
package com.ericampire.android.androidstudycase.data.repository
|
||||
|
||||
import com.ericampire.android.androidstudycase.data.datasource.animator.AnimatorDataSource
|
||||
import com.ericampire.android.androidstudycase.data.datasource.blog.BlogDataSource
|
||||
import com.ericampire.android.androidstudycase.domain.entity.Animator
|
||||
import com.ericampire.android.androidstudycase.domain.entity.Blog
|
||||
import com.ericampire.android.androidstudycase.domain.repository.AnimatorRepository
|
||||
import com.ericampire.android.androidstudycase.domain.repository.BlogRepository
|
||||
import com.ericampire.android.androidstudycase.util.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import javax.inject.Inject
|
||||
|
||||
class BlogRepositoryImpl @Inject constructor(
|
||||
@@ -16,19 +13,19 @@ class BlogRepositoryImpl @Inject constructor(
|
||||
private val localDataSource: BlogDataSource,
|
||||
) : BlogRepository {
|
||||
override fun findAll(): Flow<Result<List<Blog>>> {
|
||||
return flow {
|
||||
try {
|
||||
refreshData()
|
||||
emit(Result.Success(localDataSource.findAll()))
|
||||
} catch (e: Exception) {
|
||||
emit(Result.Error(e))
|
||||
refreshData()
|
||||
return localDataSource.findAll()
|
||||
}
|
||||
|
||||
private fun refreshData() {
|
||||
suspend {
|
||||
remoteDataSource.findAll().collect {
|
||||
if (it is Result.Success) {
|
||||
it.data.forEach { blog ->
|
||||
localDataSource.save(blog)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun refreshData() {
|
||||
remoteDataSource.findAll().forEach {
|
||||
localDataSource.save(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-31
@@ -5,52 +5,39 @@ import com.ericampire.android.androidstudycase.domain.entity.Lottiefile
|
||||
import com.ericampire.android.androidstudycase.domain.repository.LottieFileRepository
|
||||
import com.ericampire.android.androidstudycase.util.Result
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import javax.inject.Inject
|
||||
|
||||
class LottieFileRepositoryImpl @Inject constructor(
|
||||
private val localDataSource: LottieFileDataSource,
|
||||
private val remoteDataSource: LottieFileDataSource,
|
||||
) : LottieFileRepository {
|
||||
|
||||
override fun findRecent(): Flow<Result<List<Lottiefile>>> {
|
||||
return flow {
|
||||
try {
|
||||
val recentFiles = remoteDataSource.findRecent()
|
||||
refreshData(recentFiles)
|
||||
emit(Result.Success(localDataSource.findRecent()))
|
||||
} catch (e: Exception) {
|
||||
emit(Result.Error(e))
|
||||
}
|
||||
}
|
||||
val recentFiles = remoteDataSource.findRecent()
|
||||
refreshData(recentFiles)
|
||||
return recentFiles
|
||||
}
|
||||
|
||||
private suspend fun refreshData(data: List<Lottiefile>) {
|
||||
data.forEach {
|
||||
localDataSource.save(it)
|
||||
private fun refreshData(data: Flow<Result<List<Lottiefile>>>) {
|
||||
suspend {
|
||||
data.collect {
|
||||
if (it is Result.Success) {
|
||||
it.data.forEach { file -> localDataSource.save(file) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun findPopular(): Flow<Result<List<Lottiefile>>> {
|
||||
return flow {
|
||||
try {
|
||||
val recentFiles = remoteDataSource.findPopular()
|
||||
refreshData(recentFiles)
|
||||
emit(Result.Success(localDataSource.findPopular()))
|
||||
} catch (e: Exception) {
|
||||
emit(Result.Error(e))
|
||||
}
|
||||
}
|
||||
val files = remoteDataSource.findPopular()
|
||||
refreshData(files)
|
||||
return files
|
||||
}
|
||||
|
||||
override fun findFeatured(): Flow<Result<List<Lottiefile>>> {
|
||||
return flow {
|
||||
try {
|
||||
val recentFiles = remoteDataSource.findFeatured()
|
||||
refreshData(recentFiles)
|
||||
emit(Result.Success(localDataSource.findFeatured()))
|
||||
} catch (e: Exception) {
|
||||
emit(Result.Error(e))
|
||||
}
|
||||
}
|
||||
val files = remoteDataSource.findFeatured()
|
||||
refreshData(files)
|
||||
return files
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.ericampire.android.androidstudycase.domain.entity.Animator
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface AnimatorDao {
|
||||
@@ -13,5 +14,5 @@ interface AnimatorDao {
|
||||
suspend fun save(animator: Animator)
|
||||
|
||||
@Query("SELECT * FROM Animator")
|
||||
suspend fun findAll(): List<Animator>
|
||||
fun findAll(): Flow<List<Animator>>
|
||||
}
|
||||
+4
-3
@@ -5,6 +5,7 @@ import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import androidx.room.Query
|
||||
import com.ericampire.android.androidstudycase.domain.entity.Lottiefile
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface LottieFilesDao {
|
||||
@@ -12,11 +13,11 @@ interface LottieFilesDao {
|
||||
suspend fun save(lottiefile: Lottiefile)
|
||||
|
||||
@Query("SELECT * FROM Lottiefile")
|
||||
suspend fun findPopular(): List<Lottiefile>
|
||||
fun findPopular(): Flow<List<Lottiefile>>
|
||||
|
||||
@Query("SELECT * FROM Lottiefile")
|
||||
suspend fun findFeatured(): List<Lottiefile>
|
||||
fun findFeatured(): Flow<List<Lottiefile>>
|
||||
|
||||
@Query("SELECT * FROM Lottiefile")
|
||||
suspend fun findRecent(): List<Lottiefile>
|
||||
fun findRecent(): Flow<List<Lottiefile>>
|
||||
}
|
||||
Reference in New Issue
Block a user