-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from snuhcs-course/feat/android-reaction-API
Android - Reaction API Connection
- Loading branch information
Showing
11 changed files
with
231 additions
and
5 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
30 changes: 30 additions & 0 deletions
30
android/app/src/main/java/com/goliath/emojihub/data_sources/api/ReactionApi.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.goliath.emojihub.data_sources.api | ||
|
||
import retrofit2.Response | ||
import retrofit2.http.DELETE | ||
import retrofit2.http.GET | ||
import retrofit2.http.POST | ||
import retrofit2.http.Path | ||
import retrofit2.http.Query | ||
|
||
interface ReactionApi { | ||
@POST("reaction") | ||
suspend fun uploadReaction( | ||
@Query("postId") postId: String, | ||
@Query("emojiId") emojiId: String | ||
): Response<Unit> | ||
|
||
@GET("reactions") | ||
suspend fun fetchReactionList( | ||
): Response<Unit> | ||
|
||
@GET("reaction") | ||
suspend fun getReactionWithId( | ||
@Path("id") id: String | ||
): Response<Unit> | ||
|
||
@DELETE("reaction") | ||
suspend fun deleteReaction( | ||
@Query("reactionId") reactionId: String | ||
): Response<Unit> | ||
} |
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
37 changes: 37 additions & 0 deletions
37
android/app/src/main/java/com/goliath/emojihub/repositories/remote/ReactionRepository.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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.goliath.emojihub.repositories.remote | ||
|
||
import androidx.paging.PagingData | ||
import com.goliath.emojihub.data_sources.api.ReactionApi | ||
import com.goliath.emojihub.models.ReactionDto | ||
import kotlinx.coroutines.flow.Flow | ||
import retrofit2.Response | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
interface ReactionRepository { | ||
suspend fun fetchReactionList(): Flow<PagingData<ReactionDto>> | ||
suspend fun uploadReaction(postId: String, emojiId: String): Response<Unit> | ||
suspend fun getReactionWithId(id: String) | ||
suspend fun deleteReaction(reactionId: String) | ||
} | ||
|
||
@Singleton | ||
class ReactionRepositoryImpl @Inject constructor( | ||
private val reactionApi: ReactionApi | ||
): ReactionRepository { | ||
override suspend fun fetchReactionList(): Flow<PagingData<ReactionDto>> { | ||
TODO() | ||
} | ||
|
||
override suspend fun uploadReaction(postId: String, emojiId: String): Response<Unit> { | ||
return reactionApi.uploadReaction(postId, emojiId) | ||
} | ||
|
||
override suspend fun getReactionWithId(id: String) { | ||
TODO() | ||
} | ||
|
||
override suspend fun deleteReaction(reactionId: String) { | ||
reactionApi.deleteReaction(reactionId) | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
android/app/src/main/java/com/goliath/emojihub/usecases/ReactionUseCase.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.goliath.emojihub.usecases | ||
|
||
import androidx.paging.PagingData | ||
import androidx.paging.map | ||
import com.goliath.emojihub.data_sources.ApiErrorController | ||
import com.goliath.emojihub.models.Reaction | ||
import com.goliath.emojihub.repositories.remote.ReactionRepository | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
interface ReactionUseCase { | ||
|
||
val reactionList: StateFlow<PagingData<Reaction>> | ||
suspend fun fetchReactionList(): Flow<PagingData<Reaction>> | ||
suspend fun updateReactionList(data: PagingData<Reaction>) | ||
suspend fun uploadReaction(postId: String, emojiId: String): Boolean | ||
suspend fun getReactionWithId(id: String) | ||
suspend fun deleteReaction(reactionId: String) | ||
} | ||
|
||
@Singleton | ||
class ReactionUseCaseImpl @Inject constructor( | ||
private val repository: ReactionRepository, | ||
private val errorController: ApiErrorController | ||
): ReactionUseCase { | ||
|
||
private val _reactionList = MutableStateFlow<PagingData<Reaction>>(PagingData.empty()) | ||
override val reactionList: StateFlow<PagingData<Reaction>> | ||
get() = _reactionList | ||
|
||
override suspend fun updateReactionList(data: PagingData<Reaction>) { | ||
_reactionList.emit(data) | ||
} | ||
|
||
override suspend fun fetchReactionList(): Flow<PagingData<Reaction>> { | ||
return repository.fetchReactionList().map { it.map { dto -> Reaction(dto) } } | ||
} | ||
|
||
override suspend fun uploadReaction(postId: String, emojiId: String): Boolean { | ||
val response = repository.uploadReaction(postId, emojiId) | ||
return if (response.isSuccessful) { | ||
true | ||
} else { | ||
errorController.setErrorState(response.code()) | ||
false | ||
} | ||
} | ||
|
||
override suspend fun getReactionWithId(id: String) { | ||
repository.getReactionWithId(id) | ||
} | ||
|
||
override suspend fun deleteReaction(reactionId: String) { | ||
repository.deleteReaction(reactionId) | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
android/app/src/main/java/com/goliath/emojihub/viewmodels/ReactionViewModel.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.goliath.emojihub.viewmodels | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.paging.cachedIn | ||
import com.goliath.emojihub.usecases.ReactionUseCase | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class ReactionViewModel @Inject constructor( | ||
private val reactionUseCase: ReactionUseCase | ||
): ViewModel() { | ||
|
||
val reactionList = reactionUseCase.reactionList | ||
|
||
suspend fun fetchReactionList() { | ||
viewModelScope.launch { | ||
reactionUseCase.fetchReactionList() | ||
.cachedIn(viewModelScope) | ||
.collect { | ||
reactionUseCase.updateReactionList(it) | ||
} | ||
} | ||
} | ||
|
||
suspend fun uploadReaction(postId: String, emojiId: String): Boolean { | ||
return reactionUseCase.uploadReaction(postId, emojiId) | ||
} | ||
|
||
suspend fun getReactionWithId(id: String) { | ||
reactionUseCase.getReactionWithId(id) | ||
} | ||
|
||
suspend fun deleteReaction(reactionId: String) { | ||
reactionUseCase.deleteReaction(reactionId) | ||
} | ||
} |
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