-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
383 additions
and
23 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
88 changes: 88 additions & 0 deletions
88
androidApp/src/main/java/dev/xinto/argos/ui/component/HtmlText.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,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>" | ||
) | ||
} | ||
} | ||
} |
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
116 changes: 116 additions & 0 deletions
116
androidApp/src/main/java/dev/xinto/argos/ui/screen/message/MessageScreen.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,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") | ||
} | ||
} | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
androidApp/src/main/java/dev/xinto/argos/ui/screen/message/MessageState.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,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 | ||
|
||
} |
Oops, something went wrong.