-
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
43bf86b
commit aa0096f
Showing
13 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
data/src/main/java/com/lighthouse/android/data/api/BoardApiService.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,17 @@ | ||
package com.lighthouse.android.data.api | ||
|
||
import com.lighthouse.android.data.model.BaseResponse | ||
import com.lighthouse.android.data.model.BoardDTO | ||
import retrofit2.Response | ||
import retrofit2.http.GET | ||
import retrofit2.http.Path | ||
import retrofit2.http.Query | ||
|
||
interface BoardApiService { | ||
@GET("api/v1/question/[category]") | ||
suspend fun getQuestion( | ||
@Path("category") category: Int, | ||
order: String, | ||
@Query("page") page: Int, | ||
): Response<BaseResponse<BoardDTO>> | ||
} |
13 changes: 13 additions & 0 deletions
13
data/src/main/java/com/lighthouse/android/data/model/BoardDTO.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,13 @@ | ||
package com.lighthouse.android.data.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import com.lighthouse.domain.response.vo.BoardVO | ||
|
||
data class BoardDTO( | ||
@SerializedName("questions") | ||
val questions: List<BoardQuestionDTO>?, | ||
) { | ||
fun toVO() = BoardVO( | ||
questions = questions?.map { it.toVO() } ?: listOf() | ||
) | ||
} |
26 changes: 26 additions & 0 deletions
26
data/src/main/java/com/lighthouse/android/data/model/BoardQuestionDTO.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,26 @@ | ||
package com.lighthouse.android.data.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import com.lighthouse.domain.response.vo.BoardQuestionVO | ||
|
||
data class BoardQuestionDTO( | ||
@SerializedName("questionId") | ||
val questionId: Int?, | ||
@SerializedName("userId") | ||
val userId: Int?, | ||
@SerializedName("categoryId") | ||
val categoryId: Int?, | ||
@SerializedName("contents") | ||
val contents: String?, | ||
@SerializedName("like") | ||
val like: Int?, | ||
) { | ||
fun toVO() = BoardQuestionVO( | ||
questionId = questionId ?: -1, | ||
userId = userId ?: -1, | ||
categoryId = categoryId ?: -1, | ||
contents = contents ?: " ", | ||
like = like ?: -1 | ||
) | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
data/src/main/java/com/lighthouse/android/data/model/LanguageDTO.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,16 @@ | ||
package com.lighthouse.android.data.model | ||
|
||
import com.google.gson.annotations.SerializedName | ||
import com.lighthouse.domain.response.vo.LanguageVO | ||
|
||
data class LanguageDTO( | ||
@SerializedName("code") | ||
val code: String?, | ||
@SerializedName("level") | ||
val level: Int?, | ||
) { | ||
fun toVO() = LanguageVO( | ||
code ?: "kr", | ||
level ?: -1 | ||
) | ||
} |
25 changes: 25 additions & 0 deletions
25
data/src/main/java/com/lighthouse/android/data/repository/BoardRepositoryImpl.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,25 @@ | ||
package com.lighthouse.android.data.repository | ||
|
||
import com.lighthouse.android.data.repository.datasource.BoardRemoteDataSource | ||
import com.lighthouse.domain.constriant.Resource | ||
import com.lighthouse.domain.repository.BoardRepository | ||
import com.lighthouse.domain.response.vo.BoardVO | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.map | ||
import javax.inject.Inject | ||
|
||
class BoardRepositoryImpl @Inject constructor( | ||
private val datasource: BoardRemoteDataSource, | ||
) : BoardRepository { | ||
override fun getBoardQuestions( | ||
category: Int, | ||
order: String, | ||
page: Int, | ||
): Flow<Resource<BoardVO>> = | ||
datasource.getBoardQuestions(category, order, page).map { | ||
when (it) { | ||
is Resource.Success -> Resource.Success(it.data!!.toVO()) | ||
else -> Resource.Error(it.message ?: "No message found") | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
.../src/main/java/com/lighthouse/android/data/repository/datasource/BoardRemoteDataSource.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,9 @@ | ||
package com.lighthouse.android.data.repository.datasource | ||
|
||
import com.lighthouse.android.data.model.BoardDTO | ||
import com.lighthouse.domain.constriant.Resource | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface BoardRemoteDataSource { | ||
fun getBoardQuestions(category: Int, order: String, page: Int): Flow<Resource<BoardDTO>> | ||
} |
21 changes: 21 additions & 0 deletions
21
...n/java/com/lighthouse/android/data/repository/datasourceimpl/BoardRemoteDataSourceImpl.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,21 @@ | ||
package com.lighthouse.android.data.repository.datasourceimpl | ||
|
||
import com.lighthouse.android.data.api.BoardApiService | ||
import com.lighthouse.android.data.model.BoardDTO | ||
import com.lighthouse.android.data.repository.datasource.BoardRemoteDataSource | ||
import com.lighthouse.domain.constriant.Resource | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
import javax.inject.Inject | ||
|
||
class BoardRemoteDataSourceImpl @Inject constructor( | ||
private val api: BoardApiService, | ||
) : BoardRemoteDataSource, NetworkResponse() { | ||
override fun getBoardQuestions( | ||
category: Int, | ||
order: String, | ||
page: Int, | ||
): Flow<Resource<BoardDTO>> = flow { | ||
emit(changeResult(api.getQuestion(category, order, page))) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
domain/src/main/java/com/lighthouse/domain/repository/BoardRepository.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,9 @@ | ||
package com.lighthouse.domain.repository | ||
|
||
import com.lighthouse.domain.constriant.Resource | ||
import com.lighthouse.domain.response.vo.BoardVO | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface BoardRepository { | ||
fun getBoardQuestions(category: Int, order: String, page: Int): Flow<Resource<BoardVO>> | ||
} |
9 changes: 9 additions & 0 deletions
9
domain/src/main/java/com/lighthouse/domain/response/vo/BoardQuestionVO.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,9 @@ | ||
package com.lighthouse.domain.response.vo | ||
|
||
data class BoardQuestionVO( | ||
val questionId: Int, | ||
val userId: Int, | ||
val categoryId: Int, | ||
val contents: String, | ||
val like: Int, | ||
) |
5 changes: 5 additions & 0 deletions
5
domain/src/main/java/com/lighthouse/domain/response/vo/BoardVO.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.lighthouse.domain.response.vo | ||
|
||
data class BoardVO( | ||
val questions: List<BoardQuestionVO>, | ||
) |
6 changes: 6 additions & 0 deletions
6
domain/src/main/java/com/lighthouse/domain/response/vo/LanguageVO.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,6 @@ | ||
package com.lighthouse.domain.response.vo | ||
|
||
data class LanguageVO( | ||
val code: String, | ||
val level: Int, | ||
) |
11 changes: 11 additions & 0 deletions
11
domain/src/main/java/com/lighthouse/domain/usecase/GetQuestionUseCase.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,11 @@ | ||
package com.lighthouse.domain.usecase | ||
|
||
import com.lighthouse.domain.repository.BoardRepository | ||
import javax.inject.Inject | ||
|
||
class GetQuestionUseCase @Inject constructor( | ||
private val repository: BoardRepository, | ||
) { | ||
fun invoke(category: Int, order: String, page: Int) = | ||
repository.getBoardQuestions(category, order, page) | ||
} |
49 changes: 49 additions & 0 deletions
49
feature/board/src/main/java/com/lighthouse/board/viewmodel/BoardViewModel.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,49 @@ | ||
package com.lighthouse.board.viewmodel | ||
|
||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.lighthouse.board.util.UiState | ||
import com.lighthouse.domain.constriant.Resource | ||
import com.lighthouse.domain.usecase.GetQuestionUseCase | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.stateIn | ||
import javax.inject.Inject | ||
|
||
class BoardViewModel @Inject constructor( | ||
private val getQuestionUseCase: GetQuestionUseCase, | ||
) : ViewModel() { | ||
private var cachedState: StateFlow<UiState>? = null | ||
val category = MutableLiveData<Int>() | ||
val order = MutableLiveData<String>() | ||
|
||
val state: Flow<UiState> | ||
get() { | ||
if (cachedState == null) { | ||
cachedState = fetchState() | ||
} | ||
return cachedState!! | ||
} | ||
|
||
private fun fetchState(): StateFlow<UiState> { | ||
return getQuestionUseCase.invoke(category.value!!, order.value!!, 1) | ||
.map { | ||
when (it) { | ||
is Resource.Success -> UiState.Success(it.data!!.questions) | ||
is Resource.Error -> UiState.Error(it.message ?: "Error found") | ||
} | ||
} | ||
.catch { | ||
emit(UiState.Error(it.message ?: "Error found")) | ||
} | ||
.stateIn( | ||
scope = viewModelScope, | ||
initialValue = UiState.Loading, | ||
started = SharingStarted.WhileSubscribed(5000) | ||
) | ||
} | ||
} |