Skip to content

Commit

Permalink
Merge pull request #80 from snuhcs-course/feat/android-fetch-my-posts
Browse files Browse the repository at this point in the history
Fetch my post list at `ProfilePage`
  • Loading branch information
yangchanhk98 authored Nov 29, 2023
2 parents 7a8ed9c + 3bae6eb commit e0e2e67
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@ import com.goliath.emojihub.data_sources.api.EmojiApi
import com.goliath.emojihub.models.EmojiDto
import javax.inject.Inject

enum class EmojiFetchType {
GENERAL, MY_CREATED, MY_SAVED
}

class EmojiPagingSource @Inject constructor(
private val api: EmojiApi
private val api: EmojiApi,
private val type: EmojiFetchType
): PagingSource<Int, EmojiDto>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, EmojiDto> {
val cursor = params.key ?: 1
val count = params.loadSize
return try {
val response = api.fetchEmojiList(1, cursor, count).body()
val response: List<EmojiDto>? = when (type) {
EmojiFetchType.GENERAL -> {
api.fetchEmojiList(1, cursor, count).body()
}
EmojiFetchType.MY_CREATED -> {
api.fetchMyCreatedEmojiList(1, cursor, count).body()
}
EmojiFetchType.MY_SAVED -> {
api.fetchMySavedEmojiList(1, cursor, count).body()
}
}
val data = response ?: listOf()
LoadResult.Page(
data = data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,25 @@ import com.goliath.emojihub.data_sources.api.PostApi
import com.goliath.emojihub.models.PostDto
import javax.inject.Inject

enum class PostFetchType {
GENERAL, MY
}

class PostPagingSource @Inject constructor(
private val api: PostApi
private val api: PostApi,
private val type: PostFetchType
): PagingSource<Int, PostDto>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, PostDto> {
val cursor = params.key ?: 1
return try {
val response = api.fetchPostList(cursor).body()
val response: List<PostDto>? = when (type) {
PostFetchType.GENERAL -> {
api.fetchPostList(cursor).body()
}
PostFetchType.MY -> {
api.fetchMyPostList(cursor).body()
}
}
val data = response ?: listOf()
LoadResult.Page(
data = data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ interface EmojiApi {
@Path("id") id: String
): Response<EmojiDto>

@GET("emoji/me/created")
suspend fun fetchMyCreatedEmojiList(
@Query("sortByDate") sortByDate: Int,
@Query("index") index: Int,
@Query("count") count: Int
): Response<List<EmojiDto>>

@GET("emoji/me/saved")
suspend fun fetchMySavedEmojiList(
@Query("sortByDate") sortByDate: Int,
@Query("index") index: Int,
@Query("count") count: Int
): Response<List<EmojiDto>>

@Multipart
@POST("emoji")
suspend fun uploadEmoji(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ interface PostApi {
@Query("index") index: Int
): Response<List<PostDto>>

@GET("post/me")
suspend fun fetchMyPostList(
@Query("index") index: Int
): Response<List<PostDto>>

@GET("post")
suspend fun getPostWithId(
@Path("id") id: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.util.Log
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.goliath.emojihub.data_sources.EmojiFetchType
import com.goliath.emojihub.data_sources.EmojiPagingSource
import com.goliath.emojihub.data_sources.api.EmojiApi
import com.goliath.emojihub.models.EmojiDto
Expand All @@ -28,6 +29,8 @@ import javax.inject.Singleton

interface EmojiRepository {
suspend fun fetchEmojiList(): Flow<PagingData<EmojiDto>>
suspend fun fetchMyCreatedEmojiList(): Flow<PagingData<EmojiDto>>
suspend fun fetchMySavedEmojiList(): Flow<PagingData<EmojiDto>>
suspend fun getEmojiWithId(id: String): EmojiDto?
suspend fun uploadEmoji(videoFile: File, emojiDto: UploadEmojiDto): Boolean
suspend fun saveEmoji(id: String): Response<Unit>
Expand All @@ -43,7 +46,21 @@ class EmojiRepositoryImpl @Inject constructor(
override suspend fun fetchEmojiList(): Flow<PagingData<EmojiDto>> {
return Pager(
config = PagingConfig(pageSize = 10, initialLoadSize = 10, enablePlaceholders = false),
pagingSourceFactory = { EmojiPagingSource(emojiApi) }
pagingSourceFactory = { EmojiPagingSource(emojiApi, EmojiFetchType.GENERAL) }
).flow
}

override suspend fun fetchMyCreatedEmojiList(): Flow<PagingData<EmojiDto>> {
return Pager(
config = PagingConfig(pageSize = 10, initialLoadSize = 10, enablePlaceholders = false),
pagingSourceFactory = { EmojiPagingSource(emojiApi, EmojiFetchType.MY_CREATED) }
).flow
}

override suspend fun fetchMySavedEmojiList(): Flow<PagingData<EmojiDto>> {
return Pager(
config = PagingConfig(pageSize = 10, initialLoadSize = 10, enablePlaceholders = false),
pagingSourceFactory = { EmojiPagingSource(emojiApi, EmojiFetchType.MY_SAVED) }
).flow
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.util.Log
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.goliath.emojihub.data_sources.PostFetchType
import com.goliath.emojihub.data_sources.PostPagingSource
import com.goliath.emojihub.data_sources.api.PostApi
import com.goliath.emojihub.models.PostDto
Expand All @@ -15,6 +16,7 @@ import javax.inject.Singleton

interface PostRepository {
suspend fun fetchPostList(): Flow<PagingData<PostDto>>
suspend fun fetchMyPostList(): Flow<PagingData<PostDto>>
suspend fun uploadPost(dto: UploadPostDto): Response<Unit>
suspend fun getPostWithId(id: String): PostDto?
suspend fun editPost(id: String, content: String)
Expand All @@ -28,7 +30,14 @@ class PostRepositoryImpl @Inject constructor(
override suspend fun fetchPostList(): Flow<PagingData<PostDto>> {
return Pager(
config = PagingConfig(pageSize = 10, enablePlaceholders = false),
pagingSourceFactory = { PostPagingSource(postApi) }
pagingSourceFactory = { PostPagingSource(postApi, PostFetchType.GENERAL) }
).flow
}

override suspend fun fetchMyPostList(): Flow<PagingData<PostDto>> {
return Pager(
config = PagingConfig(pageSize = 10, enablePlaceholders = false),
pagingSourceFactory = { PostPagingSource(postApi, PostFetchType.MY) }
).flow
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ import javax.inject.Singleton

interface EmojiUseCase {
val emojiList: StateFlow<PagingData<Emoji>>
val myCreatedEmojiList: StateFlow<PagingData<Emoji>>
val mySavedEmojiList: StateFlow<PagingData<Emoji>>
suspend fun updateEmojiList(data: PagingData<Emoji>)
suspend fun updateMyCreatedEmojiList(data: PagingData<Emoji>)
suspend fun updateMySavedEmojiList(data: PagingData<Emoji>)
suspend fun fetchEmojiList(): Flow<PagingData<Emoji>>
suspend fun fetchMyCreatedEmojiList(): Flow<PagingData<Emoji>>
suspend fun fetchMySavedEmojiList(): Flow<PagingData<Emoji>>
suspend fun createEmoji(videoUri: Uri, topK: Int): List<CreatedEmoji>
suspend fun uploadEmoji(emojiUnicode: String, emojiLabel: String, videoFile: File): Boolean
suspend fun saveEmoji(id: String): Boolean
Expand All @@ -39,13 +45,38 @@ class EmojiUseCaseImpl @Inject constructor(
override val emojiList: StateFlow<PagingData<Emoji>>
get() = _emojiList

private val _myCreatedEmojiList = MutableStateFlow<PagingData<Emoji>>(PagingData.empty())
override val myCreatedEmojiList: StateFlow<PagingData<Emoji>>
get() = _myCreatedEmojiList

private val _mySavedEmojiList = MutableStateFlow<PagingData<Emoji>>(PagingData.empty())
override val mySavedEmojiList: StateFlow<PagingData<Emoji>>
get() = _mySavedEmojiList

override suspend fun updateEmojiList(data: PagingData<Emoji>) {
_emojiList.emit(data)
}

override suspend fun updateMyCreatedEmojiList(data: PagingData<Emoji>) {
_myCreatedEmojiList.emit(data)
}

override suspend fun updateMySavedEmojiList(data: PagingData<Emoji>) {
_mySavedEmojiList.emit(data)
}

override suspend fun fetchEmojiList(): Flow<PagingData<Emoji>> {
return emojiRepository.fetchEmojiList().map { it.map { dto -> Emoji(dto) } }
}

override suspend fun fetchMyCreatedEmojiList(): Flow<PagingData<Emoji>> {
return emojiRepository.fetchMyCreatedEmojiList().map { it.map { dto -> Emoji(dto) } }
}

override suspend fun fetchMySavedEmojiList(): Flow<PagingData<Emoji>> {
return emojiRepository.fetchMySavedEmojiList().map { it.map { dto -> Emoji(dto) } }
}

override suspend fun createEmoji(videoUri: Uri, topK: Int): List<CreatedEmoji> {
return x3dRepository.createEmoji(videoUri, topK)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ import javax.inject.Inject

sealed interface PostUseCase {
val postList: StateFlow<PagingData<Post>>
val myPostList: StateFlow<PagingData<Post>>
suspend fun updatePostList(data: PagingData<Post>)
suspend fun updateMyPostList(data: PagingData<Post>)
suspend fun fetchPostList(): Flow<PagingData<Post>>
suspend fun fetchMyPostList(): Flow<PagingData<Post>>
suspend fun uploadPost(content: String): Boolean
suspend fun getPostWithId(id: String): PostDto?
suspend fun editPost(id: String, content: String)
suspend fun deletePost(id: String)
}

class PostUseCaseImpl @Inject constructor(
private val repository: PostRepository,
private val errorController: ApiErrorController
Expand All @@ -31,14 +35,26 @@ class PostUseCaseImpl @Inject constructor(
override val postList: StateFlow<PagingData<Post>>
get() = _postList

private val _myPostList = MutableStateFlow<PagingData<Post>>(PagingData.empty())
override val myPostList: StateFlow<PagingData<Post>>
get() = _myPostList

override suspend fun updatePostList(data: PagingData<Post>) {
_postList.emit(data)
}

override suspend fun updateMyPostList(data: PagingData<Post>) {
_myPostList.emit(data)
}

override suspend fun fetchPostList(): Flow<PagingData<Post>> {
return repository.fetchPostList().map { it.map { dto -> Post(dto) } }
}

override suspend fun fetchMyPostList(): Flow<PagingData<Post>> {
return repository.fetchMyPostList().map { it.map { dto -> Post(dto) } }
}

override suspend fun uploadPost(content: String): Boolean {
val dto = UploadPostDto(content)
val response = repository.uploadPost(dto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class EmojiViewModel @Inject constructor(
var isBottomSheetShown by mutableStateOf(false)

val emojiList = emojiUseCase.emojiList
val myCreatedEmojiList = emojiUseCase.myCreatedEmojiList
val mySavedEmojiList = emojiUseCase.mySavedEmojiList

private val _topK = 3

fun fetchEmojiList()
{
fun fetchEmojiList() {
viewModelScope.launch {
emojiUseCase.fetchEmojiList()
.cachedIn(viewModelScope)
Expand All @@ -41,6 +42,26 @@ class EmojiViewModel @Inject constructor(
}
}

fun fetchMyCreatedEmojiList() {
viewModelScope.launch {
emojiUseCase.fetchMyCreatedEmojiList()
.cachedIn(viewModelScope)
.collect {
emojiUseCase.updateMyCreatedEmojiList(it)
}
}
}

fun fetchMySavedEmojiList() {
viewModelScope.launch {
emojiUseCase.fetchMySavedEmojiList()
.cachedIn(viewModelScope)
.collect {
emojiUseCase.updateMySavedEmojiList(it)
}
}
}

suspend fun createEmoji(videoUri: Uri): List<CreatedEmoji> {
return withContext(Dispatchers.IO) {
val createdEmojiList = emojiUseCase.createEmoji(videoUri, _topK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PostViewModel @Inject constructor(
): ViewModel() {

val postList = postUseCase.postList
val myPostList = postUseCase.myPostList

suspend fun fetchPostList() {
viewModelScope.launch {
Expand All @@ -25,6 +26,16 @@ class PostViewModel @Inject constructor(
}
}

suspend fun fetchMyPostList() {
viewModelScope.launch {
postUseCase.fetchMyPostList()
.cachedIn(viewModelScope)
.collect {
postUseCase.updateMyPostList(it)
}
}
}

suspend fun uploadPost(content: String): Boolean {
return postUseCase.uploadPost(content)
}
Expand Down
Loading

0 comments on commit e0e2e67

Please sign in to comment.