Skip to content

Commit

Permalink
[LH-112] Connect api to view #16
Browse files Browse the repository at this point in the history
  • Loading branch information
flash159483 committed Aug 4, 2023
1 parent 43bf86b commit aa0096f
Show file tree
Hide file tree
Showing 13 changed files with 216 additions and 0 deletions.
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 data/src/main/java/com/lighthouse/android/data/model/BoardDTO.kt
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()
)
}
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
)

}
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
)
}
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")
}
}
}
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>>
}
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)))
}
}
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>>
}
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,
)
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>,
)
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,
)
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)
}
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)
)
}
}

0 comments on commit aa0096f

Please sign in to comment.