Skip to content

Commit

Permalink
Merge pull request #787 from Corvus400/feature/update_session_detail
Browse files Browse the repository at this point in the history
♻️ Update Handle multiple URL in session detail
  • Loading branch information
takahirom authored Sep 28, 2022
2 parents 1ed28be + 063759f commit 1bb125e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fun KaigiApp(
val showNavigationIcon = !usePersistentNavigationDrawer
sessionsNavGraph(
showNavigationIcon = showNavigationIcon,
onLinkClick = kaigiExternalNavigationController::navigate,
onNavigationIconClick = kaigiAppScaffoldState::onNavigationClick,
onBackIconClick = kaigiAppScaffoldState::onBackIconClick,
onSearchIconClick = kaigiAppScaffoldState::onSearchClick,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.CircularProgressIndicator
Expand Down Expand Up @@ -47,6 +48,9 @@ import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -85,6 +89,7 @@ import java.util.Locale
@Composable
fun SessionDetailScreenRoot(
timetableItemId: TimetableItemId,
onLinkClick: (url: String) -> Unit,
onShareClick: (TimetableItem) -> Unit,
onRegisterCalendarClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
Expand All @@ -99,6 +104,7 @@ fun SessionDetailScreenRoot(
uiModel = uiModel,
onRetryButtonClick = { viewModel.onRetryButtonClick() },
onAppErrorNotified = { viewModel.onAppErrorNotified() },
onLinkClick = onLinkClick,
onBackIconClick = onBackIconClick,
onFavoriteClick = { currentFavorite ->
viewModel.onFavoriteToggle(timetableItemId, currentFavorite)
Expand Down Expand Up @@ -141,6 +147,7 @@ fun SessionDetailScreen(
onRetryButtonClick: () -> Unit,
onAppErrorNotified: () -> Unit,
modifier: Modifier = Modifier,
onLinkClick: (url: String) -> Unit = { _ -> },
onBackIconClick: () -> Unit = {},
onFavoriteClick: (Boolean) -> Unit = {},
onShareClick: (TimetableItem) -> Unit = {},
Expand Down Expand Up @@ -199,7 +206,8 @@ fun SessionDetailScreen(

if (item is Session)
SessionDetailDescription(
description = item.description
description = item.description,
onLinkClick = onLinkClick,
)

SessionDetailTargetAudience(
Expand Down Expand Up @@ -441,24 +449,76 @@ fun SessionDetailSessionInfo(
@Composable
fun SessionDetailDescription(
description: String,
onLinkClick: (url: String) -> Unit,
modifier: Modifier = Modifier,
) {
var isReadMore by remember { mutableStateOf(false) }
var isOverFlow by remember { mutableStateOf(false) }

val urlRegex = "(https)(://[\\w/:%#$&?()~.=+\\-]+)".toRegex()
val findUrlResults = remember(description) {
urlRegex.findAll(description)
}
val annotatedString = buildAnnotatedString {
pushStyle(
style = SpanStyle(
color = Color(0xFFE2E3DE)
)
)
append(description)
pop()

var lastIndex = 0
findUrlResults.forEach { matchResult ->
val startIndex = description.indexOf(
string = matchResult.value,
startIndex = lastIndex,
)
val endIndex = startIndex + matchResult.value.length
addStyle(
style = SpanStyle(
color = Color.Cyan,
textDecoration = TextDecoration.Underline
),
start = startIndex,
end = endIndex,
)
addStringAnnotation(
tag = matchResult.value,
annotation = matchResult.value,
start = startIndex,
end = endIndex,
)

lastIndex = endIndex
}
}

Column(modifier = modifier) {
Spacer(modifier = Modifier.padding(16.dp))
Text(
ClickableText(
modifier = modifier
.animateContentSize()
.clickable {
isReadMore = true
},
text = description,
text = annotatedString,
style = MaterialTheme.typography.bodyMedium,
maxLines = if (isReadMore) Int.MAX_VALUE else 5,
overflow = if (isReadMore) TextOverflow.Visible else TextOverflow.Ellipsis,
onTextLayout = { result ->
isOverFlow = result.isLineEllipsized(result.lineCount - 1)
},
onClick = { offset ->
findUrlResults.forEach { matchResult ->
annotatedString.getStringAnnotations(
tag = matchResult.value,
start = offset,
end = offset
).firstOrNull()?.let {
onLinkClick(matchResult.value)
}
}
}
)
if (isOverFlow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import io.github.droidkaigi.confsched2022.model.TimetableItemId
fun NavGraphBuilder.sessionsNavGraph(
showNavigationIcon: Boolean,
onNavigationIconClick: () -> Unit,
onLinkClick: (url: String) -> Unit,
onBackIconClick: () -> Unit,
onSearchIconClick: () -> Unit,
onTimetableClick: (TimetableItemId) -> Unit,
Expand Down Expand Up @@ -38,6 +39,7 @@ fun NavGraphBuilder.sessionsNavGraph(
val id = it.arguments?.getString("id") ?: ""
SessionDetailScreenRoot(
timetableItemId = TimetableItemId(id),
onLinkClick = onLinkClick,
onBackIconClick = onBackIconClick,
onNavigateFloorMapClick = onNavigateFloorMapClick,
onShareClick = onShareClick,
Expand Down

0 comments on commit 1bb125e

Please sign in to comment.