-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8593b0d
commit 138a18a
Showing
3 changed files
with
74 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 74 additions & 20 deletions
94
core/data/src/main/java/com/kolown/data/repository/FollowerRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,84 @@ | ||
package com.kolown.data.repository | ||
|
||
import android.util.Log | ||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import com.kolown.data.datasource.fake.FollowerDataSource | ||
import com.kolown.data.datasource.paging.FollowerGalleryThumbnailPagingDataSource | ||
import com.kolown.data.datasource.remote.AuthDataSource | ||
import com.kolown.data.datasource.remote.FollowDataSource | ||
import com.kolown.model.FollowerThumbnail | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.flow | ||
import javax.inject.Inject | ||
import javax.inject.Named | ||
|
||
//interface FollowerRepository { | ||
// fun getFollowerDataSourcePagingFlow(userId: Long): Flow<PagingData<FollowerThumbnail>> | ||
//} | ||
// | ||
//class FollowerRepositoryImpl @Inject constructor( | ||
// private val dataSource: FollowerDataSource, | ||
//) : FollowerRepository { | ||
// | ||
// override fun getFollowerDataSourcePagingFlow(userId: Long): Flow<PagingData<FollowerThumbnail>> { | ||
// return Pager(config = PagingConfig( | ||
// pageSize = PAGE_SIZE, | ||
// enablePlaceholders = false, | ||
// ), pagingSourceFactory = { FollowerGalleryThumbnailPagingDataSource(dataSource) }).flow | ||
// } | ||
// | ||
// companion object { | ||
// const val PAGE_SIZE = 40 | ||
// } | ||
//} | ||
interface FollowRepository { | ||
fun getFollowerName(followerId: String): Flow<String> | ||
suspend fun unFollowUser(followerId: String): Flow<Boolean> | ||
fun followUser(followerId: String, followerName: String): Flow<Boolean> | ||
suspend fun getFollowerDataSourcePagingFlow(): Flow<PagingData<FollowerThumbnail>> | ||
} | ||
|
||
class FollowRepositoryImpl @Inject constructor( | ||
private val followDataSource: FollowDataSource, | ||
@Named("google") private val googleAuthDataSource: AuthDataSource, | ||
private val followerGalleryThumbnailPagingDataSource: FollowerGalleryThumbnailPagingDataSource, | ||
) : FollowRepository { | ||
|
||
override fun getFollowerName(followerId: String): Flow<String> = flow { | ||
val currentUserId = googleAuthDataSource.getUserId() | ||
|
||
followDataSource.getFollowerName(currentUserId, followerId) | ||
.collect { name -> | ||
emit(name) | ||
} | ||
}.catch { e -> | ||
Log.e("GetFollowerName", "repository: $e") | ||
} | ||
|
||
override fun followUser( | ||
followerId: String, | ||
followerName: String, | ||
): Flow<Boolean> = flow { | ||
val currentUserId = googleAuthDataSource.getUserId() | ||
|
||
followDataSource.uploadFollow( | ||
userId = currentUserId, | ||
followerId = followerId, | ||
followerName = followerName | ||
).collect { success -> | ||
emit(success) | ||
} | ||
} | ||
|
||
override suspend fun unFollowUser( | ||
followerId: String, | ||
): Flow<Boolean> = flow { | ||
val currentUserId = googleAuthDataSource.getUserId() | ||
|
||
followDataSource.removeFollow( | ||
userId = currentUserId, | ||
followerId = followerId | ||
).collect { success -> | ||
emit(success) | ||
} | ||
} | ||
|
||
override suspend fun getFollowerDataSourcePagingFlow(): Flow<PagingData<FollowerThumbnail>> { | ||
return Pager( | ||
config = PagingConfig( | ||
pageSize = FOLLOWER_PER_PAGE, | ||
enablePlaceholders = false, | ||
), pagingSourceFactory = { | ||
followerGalleryThumbnailPagingDataSource | ||
} | ||
).flow | ||
} | ||
|
||
companion object { | ||
const val FOLLOWER_PER_PAGE = 5 | ||
} | ||
|
||
} |