Skip to content
This repository has been archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
ForumPreviewScreen: Fetch watched forums
Browse files Browse the repository at this point in the history
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Dec 9, 2023
1 parent c1a3286 commit f574adc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,7 @@ interface XenforoInterface {

@GET("xda-devices/inventory/")
suspend fun getInventory(): Response<Devices>

@GET("nodes/audapp-watched/")
suspend fun getWatchedNodes(): Response<Nodes>
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class XenforoRepository @Inject constructor(
return safeExecute { xenforoInterface.getInventory() }?.devices?.map { it.Node }
}

suspend fun getWatchedNodes(): List<Node>? {
return safeExecute { xenforoInterface.getWatchedNodes() }?.nodes
}

private inline fun <T> safeExecute(block: () -> Response<T>): T? {
return try {
val response = block()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package io.aayush.relabs.network.data.node

data class Nodes(
val nodes: Node = Node()
val nodes: List<Node> = emptyList()
)
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ fun ForumPreviewScreen(

val loading: Boolean by viewModel.loading.collectAsStateWithLifecycle()
val inventory: List<Node>? by viewModel.inventory.collectAsStateWithLifecycle()
val watchedNodes: List<Node>? by viewModel.watchedNodes.collectAsStateWithLifecycle()

LaunchedEffect(key1 = pagerState) {
snapshotFlow { pagerState.currentPage }.collect { page ->
when (page) {
0 -> viewModel.getInventory()
1 -> viewModel.getWatchedNodes()
}
}
}
Expand Down Expand Up @@ -90,7 +92,7 @@ fun ForumPreviewScreen(
items(
items = when (it) {
0 -> inventory ?: emptyList()

1 -> watchedNodes ?: emptyList()
else -> emptyList()
},
key = { n -> n.node_id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.aayush.relabs.network.XenforoRepository
import io.aayush.relabs.network.data.node.Node
import io.aayush.relabs.network.data.thread.Thread
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -26,7 +25,7 @@ class ForumPreviewViewModel @Inject constructor(
private val _trendingNodes = MutableStateFlow<List<Node>?>(emptyList())
val trendingNodes = _trendingNodes.asStateFlow()

private val _watchedNodes = MutableStateFlow<List<Thread>?>(emptyList())
private val _watchedNodes = MutableStateFlow<List<Node>?>(emptyList())
val watchedNodes = _watchedNodes.asStateFlow()

fun getInventory() {
Expand All @@ -36,6 +35,13 @@ class ForumPreviewViewModel @Inject constructor(
}
}

fun getWatchedNodes() {
if (!watchedNodes.value.isNullOrEmpty()) return
viewModelScope.launch(Dispatchers.IO) {
fetch { _watchedNodes.value = xenforoRepository.getWatchedNodes() }
}
}

private inline fun <T> fetch(block: () -> T): T? {
return try {
_loading.value = true
Expand Down

0 comments on commit f574adc

Please sign in to comment.