Skip to content

Commit

Permalink
fix: Assign viewmodel to each tab in Home
Browse files Browse the repository at this point in the history
Fix No Data! because concurrency problem
  • Loading branch information
kongwoojin committed Mar 25, 2024
1 parent 8023930 commit a34e0b9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
29 changes: 15 additions & 14 deletions app/src/main/java/com/kongjak/koreatechboard/ui/main/home/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ fun HomeScreen(
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun BoardInMain(
department: Department,
homeBoardViewModel: HomeBoardViewModel = hiltViewModel(key = department.name)
department: Department
) {
val context = LocalContext.current

Expand All @@ -97,16 +96,6 @@ fun BoardInMain(
mutableStateOf(department.boards[0].board)
}

LaunchedEffect(key1 = tabIndex) {
homeBoardViewModel.getApi(department.name, key)
}

val uiState = homeBoardViewModel.uiState.collectAsState()
val isSuccess = uiState.value.isSuccess
val isLoaded = uiState.value.isLoaded
val boardList = uiState.value.boardList
val statusCode = uiState.value.statusCode

Card(
modifier = Modifier
.fillMaxWidth()
Expand Down Expand Up @@ -145,6 +134,18 @@ fun BoardInMain(
) { page ->
key = department.boards[page].board

val homeBoardViewModel: HomeBoardViewModel = hiltViewModel(key = "${department.name}:$key")

LaunchedEffect(Unit) {
homeBoardViewModel.getApi(department.name, key)
}

val uiState = homeBoardViewModel.uiState.collectAsState()
val isSuccess = uiState.value.isSuccess
val isLoaded = uiState.value.isLoaded
val boardData = uiState.value.boardData
val statusCode = uiState.value.statusCode

if (!isLoaded) {
Column(
modifier = Modifier
Expand All @@ -156,7 +157,7 @@ fun BoardInMain(
}
} else {
if (isSuccess && statusCode == 200) {
if (boardList[key] == null) {
if (boardData.isEmpty()) {
Column(
modifier = Modifier
.fillMaxWidth()
Expand All @@ -167,7 +168,7 @@ fun BoardInMain(
}
} else {
Column {
boardList[key]!!.forEach { data ->
boardData.forEach { data ->
Box(
modifier = Modifier
.clickable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.kongjak.koreatechboard.ui.base.UiState
data class HomeBoardState(
val isLoaded: Boolean = false,
val isSuccess: Boolean = false,
val boardList: Map<String, List<BoardData>?> = emptyMap(),
val boardData: List<BoardData> = emptyList(),
val statusCode: Int = 0,
val error: String = ""
) : UiState
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HomeBoardViewModel @Inject constructor(private val getBoardMinimumUseCase:
setState(
oldState.copy(
isSuccess = true,
boardList = (oldState.boardList + (event.board to (it.data.boardData))),
boardData = it.data.boardData ?: emptyList(),
statusCode = it.data.statusCode,
isLoaded = true
)
Expand Down

0 comments on commit a34e0b9

Please sign in to comment.