This repository has been archived by the owner on Aug 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
- Loading branch information
1 parent
5ee1075
commit 21902e8
Showing
10 changed files
with
205 additions
and
5 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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package io.aayush.relabs.ui.navigation | ||
|
||
enum class NavArg { | ||
THREAD_ID | ||
THREAD_ID, | ||
NODE_ID, | ||
NODE_TITLE | ||
} |
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
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
91 changes: 91 additions & 0 deletions
91
app/src/main/java/io/aayush/relabs/ui/screens/node/NodeScreen.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,91 @@ | ||
package io.aayush.relabs.ui.screens.node | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.ArrowBack | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
import androidx.navigation.NavHostController | ||
import io.aayush.relabs.network.data.thread.Thread | ||
import io.aayush.relabs.ui.components.ThreadPreviewItem | ||
import io.aayush.relabs.ui.navigation.Screen | ||
|
||
@Composable | ||
@OptIn(ExperimentalMaterial3Api::class) | ||
fun NodeScreen( | ||
navHostController: NavHostController, | ||
nodeID: Int, | ||
nodeTitle: String = String(), | ||
viewModel: NodeViewModel = hiltViewModel() | ||
) { | ||
val loading: Boolean by viewModel.loading.collectAsStateWithLifecycle() | ||
val threads: List<Thread>? by viewModel.threads.collectAsStateWithLifecycle() | ||
|
||
LaunchedEffect(key1 = Unit) { | ||
viewModel.getThreads(nodeID) | ||
} | ||
|
||
Scaffold( | ||
modifier = Modifier.fillMaxSize(), | ||
topBar = { | ||
TopAppBar( | ||
title = { | ||
Text( | ||
text = nodeTitle, | ||
maxLines = 1, | ||
overflow = TextOverflow.Ellipsis, | ||
style = MaterialTheme.typography.titleMedium | ||
) | ||
}, | ||
navigationIcon = { | ||
IconButton(onClick = { navHostController.navigateUp() }) { | ||
Icon( | ||
imageVector = Icons.Filled.ArrowBack, | ||
contentDescription = "" | ||
) | ||
} | ||
} | ||
) | ||
} | ||
) { | ||
Column(modifier = Modifier.padding(it)) { | ||
LazyColumn(modifier = Modifier.fillMaxHeight()) { | ||
items(items = threads ?: emptyList(), key = { t -> t.thread_id }) { thread -> | ||
ThreadPreviewItem( | ||
modifier = Modifier.padding(10.dp), | ||
avatarURL = thread.User?.avatar_urls?.values?.first() ?: "", | ||
title = thread.title, | ||
author = thread.username, | ||
totalReplies = thread.reply_count, | ||
views = thread.view_count, | ||
lastReplyDate = thread.last_post_date, | ||
lastReplyAuthor = thread.last_post_username, | ||
forum = thread.Forum.title, | ||
unread = thread.is_unread, | ||
onClicked = { | ||
navHostController.navigate(Screen.Thread.withID(thread.thread_id)) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/src/main/java/io/aayush/relabs/ui/screens/node/NodeViewModel.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,40 @@ | ||
package io.aayush.relabs.ui.screens.node | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import io.aayush.relabs.network.XenforoRepository | ||
import io.aayush.relabs.network.data.thread.Thread | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class NodeViewModel @Inject constructor( | ||
private val xenforoRepository: XenforoRepository | ||
) : ViewModel() { | ||
|
||
private val _loading = MutableStateFlow(false) | ||
val loading = _loading.asStateFlow() | ||
|
||
private val _threads = MutableStateFlow<List<Thread>?>(emptyList()) | ||
val threads = _threads.asStateFlow() | ||
|
||
fun getThreads(nodeID: Int) { | ||
if (!threads.value.isNullOrEmpty()) return | ||
viewModelScope.launch(Dispatchers.IO) { | ||
fetch { _threads.value = xenforoRepository.getThreadsByNode(nodeID)?.threads } | ||
} | ||
} | ||
|
||
private inline fun <T> fetch(block: () -> T): T? { | ||
return try { | ||
_loading.value = true | ||
block() | ||
} finally { | ||
_loading.value = false | ||
} | ||
} | ||
} |
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