Compare commits

...
2 Commits
Author SHA1 Message Date
Amirmahdi dd5cbaa3bc feat: clean text 2026-05-27 20:42:39 +03:30
Amirmahdi e65c699bcb feat: clean text 2026-05-27 20:42:16 +03:30
2 changed files with 218 additions and 134 deletions
@@ -535,23 +535,94 @@ class DrugDetailParser {
* Converts block elements to newlines for better readability.
*/
private fun cleanText(element: Element): String {
// Clone to avoid affecting the original
val clone = element.clone()
val output = StringBuilder()
// Replace block-level elements with newlines for structure
clone.select("p, div, h2, h3, h4, li, br").before("\n")
// Process all nodes in order (both elements and text nodes)
processNode(clone, output)
// Get the text and clean it up
var text = clone.text()
.replace(Regex("\\n\\s*\\n+"), "\n\n") // Collapse multiple newlines
.replace(Regex("[ \\t]+"), " ") // Collapse spaces/tabs
return output.toString()
.replace(Regex("\\n{3,}"), "\n\n") // Collapse 3+ newlines to 2
.trim()
}
// Ensure lists look decent
if (element.tagName() == "li") {
text = "$text"
/**
* Recursively process nodes to build formatted text
*/
private fun processNode(node: org.jsoup.nodes.Node, output: StringBuilder) {
when (node) {
is Element -> {
when (node.tagName()) {
"h2" -> {
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append("\n\n📌 $text\n\n")
output.append("".repeat(minOf(30, text.length))).append("\n\n")
}
}
"h3" -> {
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append("\n\n🔹 $text\n\n")
}
}
"h4" -> {
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append("\n$text\n")
}
}
"p" -> {
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append(text).append("\n\n")
}
}
"ul", "ol" -> {
for (li in node.select("li")) {
val liText = li.text().trim()
if (liText.isNotEmpty()) {
output.append("$liText\n")
}
}
output.append("\n")
}
"li" -> {
// Handled by ul/ol processing, but if standalone:
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append("$text\n")
}
}
"br" -> {
output.append("\n")
}
"strong", "b" -> {
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append("**$text**")
}
}
"div", "section", "article" -> {
// Process children of container elements
for (child in node.childNodes()) {
processNode(child, output)
}
}
else -> {
// For other elements, just process their children
for (child in node.childNodes()) {
processNode(child, output)
}
}
}
}
is org.jsoup.nodes.TextNode -> {
val text = node.text().trim()
if (text.isNotEmpty()) {
output.append(text)
}
}
}
return text
}
}
@@ -25,12 +25,14 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SecondaryTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
@@ -39,8 +41,10 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
@@ -70,145 +74,154 @@ fun DrugDetailScreen(
viewModel.loadDrugDetail(detailUrl)
}
Scaffold(
topBar = {
TopAppBar(
title = {
when (val state = detailState) {
is DrugDetailYabState.Success -> {
Column {
Text(
text = state.drugDetail.persianName.take(25),
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
maxLines = 1
)
if (state.drugDetail.englishName.isNotEmpty()) {
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
Scaffold(
topBar = {
TopAppBar(
title = {
when (val state = detailState) {
is DrugDetailYabState.Success -> {
Column {
Text(
text = state.drugDetail.englishName.take(30),
fontSize = 11.sp,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
text = state.drugDetail.persianName.take(25),
fontSize = 16.sp,
fontWeight = FontWeight.Medium,
maxLines = 1
)
}
}
}
else -> {
Text("جزئیات دارو", fontSize = 16.sp)
}
}
},
navigationIcon = {
IconButton(onClick = { navController.popBackStack() }) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "بازگشت"
)
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surface
)
)
}
) { paddingValues ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
when (val state = detailState) {
DrugDetailYabState.Idle -> {}
DrugDetailYabState.Loading -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
CircularProgressIndicator(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(48.dp)
)
Spacer(modifier = Modifier.height(dime.md))
Text(
text = "در حال دریافت اطلاعات دارو...",
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
)
}
}
}
is DrugDetailYabState.Success -> {
val drugDetail = state.drugDetail
Column(modifier = Modifier.fillMaxSize()) {
// Manufacturer Info Card (برای صفحات برند)
if (!drugDetail.isGeneric && drugDetail.manufacturer != null) {
ManufacturerInfoCard(
manufacturer = drugDetail.manufacturer!!,
genericInfo = drugDetail.genericInfo,
modifier = Modifier.padding(horizontal = dime.md, vertical = dime.sm)
)
}
// Tab Row
TabRow(
selectedTabIndex = selectedTab,
containerColor = MaterialTheme.colorScheme.surface,
contentColor = MaterialTheme.colorScheme.primary
) {
tabs.forEachIndexed { index, title ->
Tab(
selected = selectedTab == index,
onClick = { selectedTab = index },
text = {
if (state.drugDetail.englishName.isNotEmpty()) {
Text(
text = title,
fontSize = 13.sp,
fontWeight = if (selectedTab == index) FontWeight.Bold else FontWeight.Normal,
text = state.drugDetail.englishName.take(30),
fontSize = 11.sp,
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
maxLines = 1
)
}
}
}
else -> {
Text("جزئیات دارو", fontSize = 16.sp)
}
}
},
navigationIcon = {
IconButton(onClick = { navController.popBackStack() }) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = "بازگشت"
)
}
},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surface
)
)
}
) { paddingValues ->
Box(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues)
) {
when (val state = detailState) {
DrugDetailYabState.Idle -> {}
DrugDetailYabState.Loading -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
CircularProgressIndicator(
color = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(48.dp)
)
Spacer(modifier = Modifier.height(dime.md))
Text(
text = "در حال دریافت اطلاعات دارو...",
color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.7f)
)
}
}
}
// Tab Content
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = dime.md)
.padding(top = dime.md),
verticalArrangement = Arrangement.spacedBy(dime.md)
) {
when (selectedTab) {
0 -> {
item { GeneralInfoTabContent(drugDetail) }
is DrugDetailYabState.Success -> {
val drugDetail = state.drugDetail
Column(modifier = Modifier.fillMaxSize()) {
// Manufacturer Info Card (برای صفحات برند)
if (!drugDetail.isGeneric && drugDetail.manufacturer != null) {
ManufacturerInfoCard(
manufacturer = drugDetail.manufacturer!!,
genericInfo = drugDetail.genericInfo,
modifier = Modifier.padding(
horizontal = dime.md,
vertical = dime.sm
)
)
}
// Tab Row
TabRow(
selectedTabIndex = selectedTab,
containerColor = MaterialTheme.colorScheme.surface,
contentColor = MaterialTheme.colorScheme.primary
) {
tabs.forEachIndexed { index, title ->
Tab(
selected = selectedTab == index,
onClick = { selectedTab = index },
text = {
Text(
text = title,
fontSize = 13.sp,
fontWeight = if (selectedTab == index) FontWeight.Bold else FontWeight.Normal,
maxLines = 1
)
}
)
}
1 -> {
item { SpecializedInfoTabContent(drugDetail) }
}
2 -> {
item { DosageFormsTabContent(drugDetail) }
}
3 -> {
item { BrandNamesTabContent(drugDetail) }
}
// Tab Content
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = dime.md)
.padding(top = dime.md),
verticalArrangement = Arrangement.spacedBy(dime.md)
) {
when (selectedTab) {
0 -> {
item { GeneralInfoTabContent(drugDetail) }
}
1 -> {
item { SpecializedInfoTabContent(drugDetail) }
}
2 -> {
item { DosageFormsTabContent(drugDetail) }
}
3 -> {
item { BrandNamesTabContent(drugDetail) }
}
}
}
}
}
}
is DrugDetailYabState.Error -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
ErrorState(
message = state.message,
onRetry = { viewModel.loadDrugDetail(detailUrl) }
)
is DrugDetailYabState.Error -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
ErrorState(
message = state.message,
onRetry = { viewModel.loadDrugDetail(detailUrl) }
)
}
}
}
}