This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
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.
🔀 :: (#116) 추천 목록 조회 기능 구현
- Loading branch information
Showing
18 changed files
with
211 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.signal.data.api | ||
|
||
import com.signal.data.model.recommend.FetchRecommendsResponse | ||
import com.signal.domain.enums.Category | ||
import retrofit2.http.GET | ||
import retrofit2.http.Query | ||
|
||
interface RecommendApi { | ||
@GET(SignalUrl.Recommend.FetchRecommends) | ||
suspend fun fetchRecommends( | ||
@Query("category") category: Category, | ||
): FetchRecommendsResponse | ||
} |
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
8 changes: 8 additions & 0 deletions
8
data/src/main/kotlin/com/signal/data/datasource/recommend/RecommendDataSource.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,8 @@ | ||
package com.signal.data.datasource.recommend | ||
|
||
import com.signal.data.model.recommend.FetchRecommendsResponse | ||
import com.signal.domain.enums.Category | ||
|
||
interface RecommendDataSource { | ||
suspend fun fetchRecommends(category: Category): FetchRecommendsResponse | ||
} |
15 changes: 15 additions & 0 deletions
15
data/src/main/kotlin/com/signal/data/datasource/recommend/RecommendDataSourceImpl.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,15 @@ | ||
package com.signal.data.datasource.recommend | ||
|
||
import com.signal.data.api.RecommendApi | ||
import com.signal.data.model.recommend.FetchRecommendsResponse | ||
import com.signal.data.util.ExceptionHandler | ||
import com.signal.domain.enums.Category | ||
|
||
class RecommendDataSourceImpl( | ||
private val recommendApi: RecommendApi, | ||
) : RecommendDataSource { | ||
override suspend fun fetchRecommends(category: Category) = | ||
ExceptionHandler<FetchRecommendsResponse>().httpRequest { | ||
recommendApi.fetchRecommends(category = category) | ||
}.sendRequest() | ||
} |
27 changes: 27 additions & 0 deletions
27
data/src/main/kotlin/com/signal/data/model/recommend/FetchRecommendsResponse.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,27 @@ | ||
package com.signal.data.model.recommend | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import com.signal.domain.entity.RecommendsEntity | ||
import java.util.UUID | ||
|
||
data class FetchRecommendsResponse( | ||
@SerializedName("recommend_list") val recommends: List<Recommend>, | ||
) { | ||
data class Recommend( | ||
@SerializedName("id") val id: UUID, | ||
@SerializedName("title") val title: String, | ||
@SerializedName("content") val content: String, | ||
@SerializedName("image") val image: String, | ||
) | ||
} | ||
|
||
fun FetchRecommendsResponse.toEntity() = RecommendsEntity( | ||
recommends = this.recommends.map { it.toEntity() }, | ||
) | ||
|
||
fun FetchRecommendsResponse.Recommend.toEntity() = RecommendsEntity.Recommend( | ||
id = this.id, | ||
title = this.title, | ||
content = this.content, | ||
image = this.image, | ||
) |
15 changes: 15 additions & 0 deletions
15
data/src/main/kotlin/com/signal/data/repository/RecommendRepositoryImpl.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,15 @@ | ||
package com.signal.data.repository | ||
|
||
import com.signal.data.datasource.recommend.RecommendDataSource | ||
import com.signal.data.model.recommend.toEntity | ||
import com.signal.domain.enums.Category | ||
import com.signal.domain.repository.RecommendRepository | ||
|
||
class RecommendRepositoryImpl( | ||
private val recommendDataSource: RecommendDataSource, | ||
) : RecommendRepository { | ||
override suspend fun fetchRecommends(category: Category) = kotlin.runCatching { | ||
recommendDataSource.fetchRecommends(category = category).toEntity() | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
domain/src/main/kotlin/com/signal/domain/entity/RecommendsEntity.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,14 @@ | ||
package com.signal.domain.entity | ||
|
||
import java.util.UUID | ||
|
||
data class RecommendsEntity( | ||
val recommends: List<Recommend>, | ||
) { | ||
data class Recommend( | ||
val id: UUID, | ||
val title: String, | ||
val content: String, | ||
val image: String, | ||
) | ||
} |
6 changes: 3 additions & 3 deletions
6
.../com/signal/domain/enums/RecommendType.kt → ...otlin/com/signal/domain/enums/Category.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,8 +1,8 @@ | ||
package com.signal.domain.enums | ||
|
||
enum class RecommendType { | ||
enum class Category { | ||
MUSIC, | ||
EXERCISE, | ||
SPORT, | ||
VIDEO, | ||
HOSPITAL, | ||
HOBBY, | ||
} |
8 changes: 8 additions & 0 deletions
8
domain/src/main/kotlin/com/signal/domain/repository/RecommendRepository.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,8 @@ | ||
package com.signal.domain.repository | ||
|
||
import com.signal.domain.entity.RecommendsEntity | ||
import com.signal.domain.enums.Category | ||
|
||
interface RecommendRepository { | ||
suspend fun fetchRecommends(category: Category): Result<RecommendsEntity> | ||
} |
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
5 changes: 5 additions & 0 deletions
5
...ion/src/main/java/com/signal/signal_android/feature/main/recommend/RecommendSideEffect.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,5 @@ | ||
package com.signal.signal_android.feature.main.recommend | ||
|
||
interface RecommendSideEffect { | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
...entation/src/main/java/com/signal/signal_android/feature/main/recommend/RecommendState.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,18 @@ | ||
package com.signal.signal_android.feature.main.recommend | ||
|
||
import androidx.compose.runtime.mutableStateListOf | ||
import androidx.compose.runtime.snapshots.SnapshotStateList | ||
import com.signal.domain.entity.RecommendsEntity | ||
import com.signal.domain.enums.Category | ||
|
||
data class RecommendState( | ||
val recommends: SnapshotStateList<RecommendsEntity.Recommend>, | ||
val category: Category, | ||
) { | ||
companion object { | ||
fun getDefaultState() = RecommendState( | ||
recommends = mutableStateListOf(), | ||
category = Category.MUSIC, | ||
) | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...tion/src/main/java/com/signal/signal_android/feature/main/recommend/RecommendViewModel.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,27 @@ | ||
package com.signal.signal_android.feature.main.recommend | ||
|
||
import androidx.compose.runtime.toMutableStateList | ||
import androidx.lifecycle.viewModelScope | ||
import com.signal.domain.enums.Category | ||
import com.signal.domain.repository.RecommendRepository | ||
import com.signal.signal_android.BaseViewModel | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
class RecommendViewModel( | ||
private val recommendRepository: RecommendRepository, | ||
) : BaseViewModel<RecommendState, RecommendSideEffect>(RecommendState.getDefaultState()) { | ||
internal fun fetchRecommends() { | ||
with(state.value) { | ||
viewModelScope.launch(Dispatchers.IO) { | ||
recommendRepository.fetchRecommends(category = category).onSuccess { | ||
setState(copy(recommends = it.recommends.toMutableStateList())) | ||
} | ||
} | ||
} | ||
} | ||
|
||
internal fun setCategory(category: Category) { | ||
setState(state.value.copy(category = category)) | ||
} | ||
} |
Oops, something went wrong.