Skip to content

Commit

Permalink
add message screen
Browse files Browse the repository at this point in the history
  • Loading branch information
X1nto committed Dec 3, 2023
1 parent 9ca2b8e commit 7182850
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 23 deletions.
2 changes: 2 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ dependencies {
implementation(libs.navigationReimagined)

implementation(libs.bundles.coil)

implementation(libs.jsoup)
}
5 changes: 5 additions & 0 deletions androidApp/src/main/java/dev/xinto/argos/di/UiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import dev.xinto.argos.ui.screen.main.dialog.user.UserViewModel
import dev.xinto.argos.ui.screen.main.page.home.HomeViewModel
import dev.xinto.argos.ui.screen.main.page.messages.MessagesViewModel
import dev.xinto.argos.ui.screen.main.page.news.NewsViewModel
import dev.xinto.argos.ui.screen.message.MessageViewModel
import dev.xinto.argos.ui.screen.notifications.NotificationsViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.androidx.viewmodel.dsl.viewModelOf
import org.koin.dsl.module

Expand All @@ -18,4 +20,7 @@ val UiModule = module {
viewModelOf(::NewsViewModel)
viewModelOf(::NotificationsViewModel)
viewModelOf(::UserViewModel)
viewModel {
MessageViewModel(it.get(), get())
}
}
15 changes: 15 additions & 0 deletions androidApp/src/main/java/dev/xinto/argos/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import dev.xinto.argos.domain.user.UserRepository
import dev.xinto.argos.ui.screen.ArgosNavigation
import dev.xinto.argos.ui.screen.login.LoginScreen
import dev.xinto.argos.ui.screen.main.MainScreen
import dev.xinto.argos.ui.screen.message.MessageScreen
import dev.xinto.argos.ui.screen.notifications.NotificationsScreen
import dev.xinto.argos.ui.theme.ArgosTheme
import org.koin.android.ext.android.inject
Expand Down Expand Up @@ -57,6 +58,12 @@ class MainActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
onNotificationsClick = {
rootNavController.navigate(ArgosNavigation.Notifications)
},
onMessageClick = { messageId, semesterId ->
rootNavController.navigate(ArgosNavigation.Message(
id = messageId,
semesterId = semesterId
))
}
)
}
Expand All @@ -65,6 +72,14 @@ class MainActivity : ComponentActivity() {
onBackClick = rootNavController::pop
)
}
is ArgosNavigation.Message -> {
MessageScreen(
modifier = Modifier.fillMaxSize(),
messageId = it.id,
semesterId = it.semesterId,
onBackClick = rootNavController::pop
)
}
}
}
}
Expand Down
88 changes: 88 additions & 0 deletions androidApp/src/main/java/dev/xinto/argos/ui/component/HtmlText.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package dev.xinto.argos.ui.component

import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.UrlAnnotation
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withAnnotation
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.PreviewFontScale
import dev.xinto.argos.ui.theme.ArgosTheme
import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.nodes.TextNode
import org.jsoup.safety.Safelist

@OptIn(ExperimentalTextApi::class)
@Composable
fun HtmlText(
text: String,
modifier: Modifier = Modifier,
color: Color = LocalContentColor.current,
style: TextStyle = LocalTextStyle.current,
) {
val coloredStyle = style.merge(color = color)
val linkColor = MaterialTheme.colorScheme.primary
val uriHandler = LocalUriHandler.current
val annotatedString = remember(text, linkColor) {
val parsed = Jsoup.parse(text)
buildAnnotatedString {
parsed.body().childNodes().forEach {
when (it) {
is Element -> {
when (it.tagName()) {
"a" -> {
withAnnotation(UrlAnnotation(it.attr("href"))) {
withStyle(SpanStyle(color = linkColor)) {
append(it.text())
}
}
}
"br" -> appendLine()
"b" -> {
withStyle(SpanStyle(fontWeight = FontWeight.Bold)) {
append(it.text())
}
}
}
}
is TextNode -> {
append(Jsoup.clean(it.text(), Safelist.none()))
}
}
}
}
}
ClickableText(
modifier = modifier,
text = annotatedString,
onClick = {
uriHandler.openUri(annotatedString.getUrlAnnotations(it, it)[0].item.url)
},
style = coloredStyle
)
}

@Composable
@PreviewFontScale
fun HtmlText_Preview() {
ArgosTheme {
Surface {
HtmlText(
text = "test1<p>tes2</p<br><b>test3</b>"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ sealed interface ArgosNavigation : Parcelable {
@Parcelize
data object Notifications : ArgosNavigation

@Parcelize
data class Message(val id: String, val semesterId: String) : ArgosNavigation

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import org.koin.androidx.compose.getViewModel
@Composable
fun MainScreen(
onNotificationsClick: () -> Unit,
onMessageClick: (messageId: String, semesterId: String) -> Unit,
modifier: Modifier = Modifier,
) {
val viewModel: MainViewModel = getViewModel()
Expand All @@ -75,14 +76,16 @@ fun MainScreen(
modifier = modifier,
state = state,
onNotificationsClick = onNotificationsClick,
onLogoutClick = viewModel::logout
onLogoutClick = viewModel::logout,
onMessageClick = onMessageClick
)
}

@Composable
fun MainScreen(
onNotificationsClick: () -> Unit,
onLogoutClick: () -> Unit,
onMessageClick: (messageId: String, semesterId: String) -> Unit,
state: MainState,
modifier: Modifier = Modifier,
) {
Expand Down Expand Up @@ -114,7 +117,10 @@ fun MainScreen(
HomePage(modifier = Modifier.fillMaxSize())
}
is MainNavigation.Messages -> {
MessagesPage(modifier = Modifier.fillMaxSize())
MessagesPage(
modifier = Modifier.fillMaxSize(),
onMessageClick = onMessageClick
)
}
is MainNavigation.News -> {
NewsPage(modifier = Modifier.fillMaxSize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import dev.xinto.argos.R
import dev.xinto.argos.domain.messages.DomainMessage
import dev.xinto.argos.domain.messages.DomainMessagePreview
import dev.xinto.argos.domain.messages.DomainMessageReceived
import dev.xinto.argos.domain.messages.DomainMessageSent
import dev.xinto.argos.ui.component.SecondaryTabPager
import org.koin.androidx.compose.getViewModel

@Composable
fun MessagesPage(modifier: Modifier = Modifier) {
fun MessagesPage(
modifier: Modifier = Modifier,
onMessageClick: (messageId: String, semesterId: String) -> Unit
) {
val viewModel: MessagesViewModel = getViewModel()
val inbox = viewModel.inboxMessages.collectAsLazyPagingItems()
val outbox = viewModel.outboxMessages.collectAsLazyPagingItems()
Expand All @@ -37,9 +40,7 @@ fun MessagesPage(modifier: Modifier = Modifier) {
onTabChange = viewModel::switchTab,
inbox = inbox,
outbox = outbox,
onMessageClick = {

}
onMessageClick = onMessageClick
)
}

Expand All @@ -49,8 +50,8 @@ fun MessagesPage(
onTabChange: (MessagesTab) -> Unit,
inbox: LazyPagingItems<DomainMessageReceived>,
outbox: LazyPagingItems<DomainMessageSent>,
onMessageClick: (messageId: String, semesterId: String) -> Unit,
modifier: Modifier = Modifier,
onMessageClick: (String) -> Unit,
) {
SecondaryTabPager(
modifier = modifier,
Expand All @@ -77,9 +78,9 @@ fun MessagesPage(
}

@Composable
private fun <T : DomainMessage> MessagesList(
private fun <T : DomainMessagePreview> MessagesList(
messages: LazyPagingItems<T>,
onMessageClick: (String) -> Unit,
onMessageClick: (messageId: String, semesterId: String) -> Unit,
modifier: Modifier = Modifier,
) {
Box(
Expand Down Expand Up @@ -109,7 +110,7 @@ private fun <T : DomainMessage> MessagesList(
ListItem(
modifier = Modifier
.clickable {
onMessageClick(message.id)
onMessageClick(message.id, message.semId)
},
overlineContent = {
Text(message.date.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package dev.xinto.argos.ui.screen.message

import android.os.Bundle
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import dev.xinto.argos.R
import dev.xinto.argos.ui.component.HtmlText
import org.koin.androidx.compose.getStateViewModel

@Composable
fun MessageScreen(
messageId: String,
semesterId: String,
onBackClick: () -> Unit,
modifier: Modifier = Modifier,
) {
val viewModel: MessageViewModel = getStateViewModel(
state = { Bundle().apply {
putString(MessageViewModel.KEY_MESSAGE_ID, messageId)
putString(MessageViewModel.KEY_MESSAGE_SEMESTER, semesterId)
}
})
val state by viewModel.state.collectAsStateWithLifecycle()
BackHandler(onBack = onBackClick)
MessageScreen(
modifier = modifier,
state = state,
onBackClick = onBackClick
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MessageScreen(
state: MessageState,
onBackClick: () -> Unit,
modifier: Modifier = Modifier
) {
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
Scaffold(
modifier = modifier,
topBar = {
TopAppBar(
scrollBehavior = scrollBehavior,
title = { Text(stringResource(R.string.message_title)) },
navigationIcon = {
IconButton(onClick = onBackClick) {
Icon(
painter = painterResource(id = R.drawable.ic_arrow_back),
contentDescription = null
)
}
}
)
}
) { padding ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(padding)
.nestedScroll(scrollBehavior.nestedScrollConnection),
contentAlignment = Alignment.Center
) {
when (state) {
is MessageState.Loading -> {
CircularProgressIndicator()
}
is MessageState.Success -> {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(
text = state.message.subject,
style = MaterialTheme.typography.headlineMedium
)
HtmlText(text = state.message.body)
}
}
is MessageState.Error -> {
Text("Error")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dev.xinto.argos.ui.screen.message

import androidx.compose.runtime.Immutable
import dev.xinto.argos.domain.messages.DomainMessage

sealed interface MessageState {

data object Loading : MessageState

@Immutable
data class Success(val message: DomainMessage) : MessageState

data object Error : MessageState

}
Loading

0 comments on commit 7182850

Please sign in to comment.