Skip to content

Commit

Permalink
[fix]修复更新对话框日志显示问题
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed May 31, 2023
1 parent 0716aaf commit 6035f66
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {
applicationId "com.skyd.rays"
minSdk 24
targetSdk 33
versionCode 8
versionCode 9
versionName "1.1"
flavorDimensions "versionName"

Expand Down
144 changes: 143 additions & 1 deletion app/src/main/java/com/skyd/rays/ext/StringExt.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package com.skyd.rays.ext

import android.graphics.Typeface
import android.text.Html
import android.text.Spanned
import android.text.style.AbsoluteSizeSpan
import android.text.style.BulletSpan
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.text.style.SubscriptSpan
import android.text.style.SuperscriptSpan
import android.text.style.TypefaceSpan
import android.text.style.UnderlineSpan
import android.util.Log
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.BaselineShift
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.em

fun CharSequence.startWithBlank(): Boolean = matches("^\\s+.*".toRegex())

Expand All @@ -13,4 +36,123 @@ fun String.indexesOf(pat: String, ignoreCase: Boolean = true): List<Int> =
.map { it.range.first }
.toList()

fun String.readable(): String = Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY).toString()
fun String.readable(): CharSequence = spannableStringToAnnotatedString(
text = Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
)

private fun spannableStringToAnnotatedString(
text: CharSequence,
): CharSequence {
return if (text is Spanned) {
buildAnnotatedString {
append((text.toString()))
text.getSpans(0, text.length, Any::class.java).forEach {
val start = text.getSpanStart(it)
val end = text.getSpanEnd(it)
when (it) {
is StyleSpan -> when (it.style) {
Typeface.NORMAL -> addStyle(
SpanStyle(
fontWeight = FontWeight.Normal,
fontStyle = FontStyle.Normal
),
start,
end
)

Typeface.BOLD -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Normal
),
start,
end
)

Typeface.ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Normal,
fontStyle = FontStyle.Italic
),
start,
end
)

Typeface.BOLD_ITALIC -> addStyle(
SpanStyle(
fontWeight = FontWeight.Bold,
fontStyle = FontStyle.Italic
),
start,
end
)
}

is TypefaceSpan -> addStyle(
SpanStyle(
fontFamily = when (it.family) {
FontFamily.SansSerif.name -> FontFamily.SansSerif
FontFamily.Serif.name -> FontFamily.Serif
FontFamily.Monospace.name -> FontFamily.Monospace
FontFamily.Cursive.name -> FontFamily.Cursive
else -> FontFamily.Default
}
),
start,
end
)

is BulletSpan -> {
Log.d("StringResources", "BulletSpan not supported yet")
addStyle(SpanStyle(), start, end)
}

is AbsoluteSizeSpan -> {
Log.d("StringResources", "AbsoluteSizeSpan not supported yet")
addStyle(SpanStyle(), start, end)
}

is RelativeSizeSpan -> addStyle(
SpanStyle(fontSize = it.sizeChange.em),
start,
end
)

is StrikethroughSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.LineThrough),
start,
end
)

is UnderlineSpan -> addStyle(
SpanStyle(textDecoration = TextDecoration.Underline),
start,
end
)

is SuperscriptSpan -> addStyle(
SpanStyle(baselineShift = BaselineShift.Superscript),
start,
end
)

is SubscriptSpan -> addStyle(
SpanStyle(baselineShift = BaselineShift.Subscript),
start,
end
)

is ForegroundColorSpan -> addStyle(
SpanStyle(color = Color(it.foregroundColor)),
start,
end
)

else -> addStyle(SpanStyle(), start, end)
}
}
}
} else {
AnnotatedString(text.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fun RaysDialog(
},
title: @Composable (() -> Unit)? = null,
text: @Composable (() -> Unit)? = null,
selectable: Boolean = true,
confirmButton: @Composable () -> Unit,
dismissButton: @Composable (() -> Unit)? = null,
) {
Expand All @@ -54,7 +55,11 @@ fun RaysDialog(
icon = icon,
title = title,
text = {
SelectionContainer(modifier = Modifier.verticalScroll(rememberScrollState())) {
if (selectable) {
SelectionContainer(modifier = Modifier.verticalScroll(rememberScrollState())) {
text?.invoke()
}
} else {
text?.invoke()
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.skyd.rays.ui.screen.about.update

import android.text.Html
import android.text.method.LinkMovementMethod
import android.widget.TextView
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -29,6 +32,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.skyd.rays.R
Expand Down Expand Up @@ -115,18 +119,33 @@ private fun NewerDialog(
title = {
Text(text = stringResource(R.string.update_newer))
},
selectable = false,
text = {
Column {
SelectionContainer(modifier = Modifier.weight(weight = 1f, fill = false)) {
Text(
modifier = Modifier.verticalScroll(rememberScrollState()),
text = stringResource(
R.string.update_newer_text,
updateBean!!.name,
updateBean.publishedAt,
updateBean.assets.firstOrNull()?.downloadCount.toString(),
updateBean.body,
Column(
modifier = Modifier
.weight(weight = 1f, fill = false)
.verticalScroll(rememberScrollState())
) {
SelectionContainer {
Text(
text = stringResource(
R.string.update_newer_text,
updateBean!!.name,
updateBean.publishedAt,
updateBean.assets.firstOrNull()?.downloadCount.toString(),
)
)
}
AndroidView(
factory = { context ->
TextView(context).apply {
setTextIsSelectable(true)
movementMethod = LinkMovementMethod.getInstance()
isSingleLine = false
text = Html.fromHtml(updateBean!!.body, Html.FROM_HTML_MODE_COMPACT)
}
}
)
}
val checked = ignoreUpdateVersion == updateBean!!.tagName.toLongOrDefault(0L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class UpdateViewModel @Inject constructor(private var updateRepo: UpdateReposito
copy(
updateUiState = UpdateUiState.OpenNewerDialog(
data.copy(
body = data.body.readable(),
publishedAt = publishedAt
)
),
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<string name="update_check">检查更新</string>
<string name="update_no_update">当前已是最新版!</string>
<string name="update_newer">发现新版本</string>
<string name="update_newer_text">新版本:%s\n发布时间:%s\n下载数:%s\n日志:\n%s</string>
<string name="update_newer_text">新版本:%s\n发布时间:%s\n下载数:%s\n</string>
<string name="download_update">下载</string>
<string name="update_ignore">忽略本次更新</string>
<string name="search_result_sort_create_time">创建时间</string>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
<string name="update_check">Check updates</string>
<string name="update_no_update">This is the latest version!</string>
<string name="update_newer">Found new version</string>
<string name="update_newer_text">New version: %s\nPublished on: %s\nDownloads: %s\nLogs: \n%s</string>
<string name="update_newer_text">New version: %s\nPublished on: %s\nDownloads: %s\n</string>
<string name="download_update">Download</string>
<string name="update_ignore">Ignore this update</string>
<string name="search_result_sort_create_time">Create time</string>
Expand Down

0 comments on commit 6035f66

Please sign in to comment.