Skip to content

Commit

Permalink
feat/ui: Add link click functionality and optimize announcement scree…
Browse files Browse the repository at this point in the history
…n layout

- Add link click functionality to announcement screen
- Optimize announcement screen layout and add scrolling support
- Add link click handling in ChatActivity
- Update ChatScreen component to support link clicks
- Add LinkText component to display text of clickable links

feat/ui: 添加链接点击功能并优化公告屏幕布局

- 在公告屏幕中添加链接点击功能
- 优化公告屏幕布局,增加滚动支持
- 在 ChatActivity 中添加链接点击处理
- 更新 ChatScreen 组件以支持链接点击
- 新增 LinkText 组件用于显示可点击链接的文本

Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Nov 9, 2024
1 parent 3c57620 commit d26b741
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 20 deletions.
24 changes: 24 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.wearable.permission.URI_REDIRECT_TO_REMOTE" />

<uses-feature
android:name="android.hardware.type.watch"
Expand Down Expand Up @@ -128,4 +129,27 @@

</application>

<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="http" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="https" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="content" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="video/*" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="image/*" />
</intent>
</queries>

</manifest>
20 changes: 15 additions & 5 deletions app/src/main/java/com/gohj99/telewatch/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
Expand Down Expand Up @@ -243,10 +245,6 @@ class ChatActivity : ComponentActivity() {
is TdApi.MessageVoiceNote -> {
println("语音消息")
}

is TdApi.MessageAnimation -> {
println("动画消息")
}
}
},
longPress = { select, message ->
Expand Down Expand Up @@ -352,7 +350,19 @@ class ChatActivity : ComponentActivity() {
chatObject = itChatObject,
lastReadOutboxMessageId = lastReadOutboxMessageId,
lastReadInboxMessageId = lastReadInboxMessageId,
listState = listState
listState = listState,
onLinkClick = { url ->
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val packageManager: PackageManager = packageManager
val activities: List<ResolveInfo> = packageManager.queryIntentActivities(intent, 0)

if (activities.isNotEmpty()) {
startActivity(intent)
} else {
// 处理没有可用浏览器的情况
Toast.makeText(this, getString(R.string.No_app_to_handle_this_url), Toast.LENGTH_SHORT).show()
}
}
)
}
}
Expand Down
28 changes: 28 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/ui/AnnouncementScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -92,6 +94,32 @@ fun SplashAnnouncementScreen(
overflow = TextOverflow.Ellipsis // 超出部分省略
)
}

Spacer(modifier = Modifier.height(4.dp)) // 添加间距

val scrollState = rememberScrollState()

Column(
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
.verticalScroll(scrollState)
.verticalRotaryScroll(scrollState)
) {
Spacer(modifier = Modifier.height(35.dp)) // 添加一个高度为 8dp 的 Spacer
MainCard(
column = {
Text(
text = jsonObject["content"]?.asString ?: "Error",
color = Color.White,
style = MaterialTheme.typography.titleMedium
)
},
item = "content",
color = Color(0xFF2C323A)
)
Spacer(modifier = Modifier.height(50.dp))
}
}
}
}
Expand Down
36 changes: 21 additions & 15 deletions app/src/main/java/com/gohj99/telewatch/ui/chat/ChatScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import coil.compose.rememberAsyncImagePainter
import com.gohj99.telewatch.R
import com.gohj99.telewatch.TgApiManager
import com.gohj99.telewatch.ui.main.Chat
import com.gohj99.telewatch.ui.main.LinkText
import com.gohj99.telewatch.ui.main.SplashLoadingScreen
import com.gohj99.telewatch.ui.theme.TelewatchTheme
import com.gohj99.telewatch.ui.verticalRotaryScroll
Expand Down Expand Up @@ -129,7 +130,8 @@ fun SplashChatScreen(
chatObject: TdApi.Chat,
lastReadOutboxMessageId: MutableState<Long>,
lastReadInboxMessageId: MutableState<Long>,
listState: LazyListState = rememberLazyListState()
listState: LazyListState = rememberLazyListState(),
onLinkClick: (String) -> Unit
) {
var isFloatingVisible by remember { mutableStateOf(true) }
var inputText by remember { mutableStateOf(TextFieldValue("")) }
Expand Down Expand Up @@ -330,10 +332,11 @@ fun SplashChatScreen(
when (val content = message.content) {
is TdApi.MessageText -> {
SelectionContainer {
Text(
LinkText(
text = content.text.text,
color = textColor,
style = MaterialTheme.typography.bodyMedium
color = Color(0xFFFEFEFE),
style = MaterialTheme.typography.bodyMedium,
onLinkClick = onLinkClick
)
}
}
Expand All @@ -345,23 +348,24 @@ fun SplashChatScreen(
thumbnail = thumbnail.photo,
imageWidth = thumbnail.width,
imageHeight = thumbnail.height,
textColor = textColor
textColor = Color(0xFFFEFEFE)
)
} else {
// 处理没有缩略图的情况
Text(
text = stringResource(id = R.string.No_thumbnail_available),
color = textColor,
color = Color(0xFFFEFEFE),
style = MaterialTheme.typography.bodyMedium
)
}
content.caption?.text?.let {
SelectionContainer {
Text(
LinkText(
text = it,
color = textColor,
color = Color(0xFFFEFEFE),
modifier = Modifier.padding(top = 4.dp),
style = MaterialTheme.typography.bodyMedium
style = MaterialTheme.typography.bodyMedium,
onLinkClick = onLinkClick
)
}
}
Expand All @@ -377,7 +381,7 @@ fun SplashChatScreen(
thumbnail = thumbnail.file,
imageWidth = thumbnail.width,
imageHeight = thumbnail.height,
textColor = textColor
textColor = Color(0xFFFEFEFE)
)
Box(
modifier = Modifier
Expand All @@ -388,19 +392,20 @@ fun SplashChatScreen(
// 处理没有缩略图的情况
Text(
text = stringResource(id = R.string.No_thumbnail_available),
color = textColor,
color = Color(0xFFFEFEFE),
style = MaterialTheme.typography.bodyMedium
)
}

// 视频文字
content.caption?.text?.let {
SelectionContainer {
Text(
LinkText(
text = it,
color = textColor,
color = Color(0xFFFEFEFE),
modifier = Modifier.padding(top = 4.dp),
style = MaterialTheme.typography.bodyMedium
style = MaterialTheme.typography.bodyMedium,
onLinkClick = onLinkClick
)
}
}
Expand Down Expand Up @@ -822,7 +827,8 @@ fun SplashChatScreenPreview() {
},
chatObject = TdApi.Chat(),
lastReadOutboxMessageId = mutableLongStateOf(0L),
lastReadInboxMessageId = mutableLongStateOf(0L)
lastReadInboxMessageId = mutableLongStateOf(0L),
onLinkClick = {}
)
}
}
53 changes: 53 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/ui/main/MainCard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,66 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp

@Composable
fun LinkText(
text: String,
modifier: Modifier = Modifier,
color: Color = Color(0xFFFEFEFE), // 默认白色
style: androidx.compose.ui.text.TextStyle = MaterialTheme.typography.bodyLarge, // 默认使用主题的bodyLarge样式
onLinkClick: ((String) -> Unit)? = null
) {
val annotatedString = buildAnnotatedString {
val regex = Regex("(https?://[\\w-]+(\\.[\\w-]+)*(:[0-9]+)?(/[\\w-?=&%.]*)?)")
var lastIndex = 0

regex.findAll(text).forEach { result ->
val start = result.range.first
val end = result.range.last + 1

// Append text before the link
append(text.substring(lastIndex, start))

// Append the link with a different style
pushStringAnnotation(tag = "URL", annotation = result.value)
withStyle(style = SpanStyle(color = Color(0xFF66D3FE), textDecoration = TextDecoration.Underline)) {
append(result.value)
}
pop()

lastIndex = end
}

// Append remaining text
append(text.substring(lastIndex))
}

ClickableText(
text = annotatedString,
style = style.copy(color = color), // 应用传入的颜色和样式
modifier = modifier,
onClick = { offset ->
annotatedString.getStringAnnotations(tag = "URL", start = offset, end = offset)
.firstOrNull()?.let { annotation ->
// Handle click on the URL
onLinkClick?.invoke(annotation.item)
}
}
)
}

@Composable
fun <T> MainCard(
column: @Composable () -> Unit,
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">We will check for updates from the official GitHub repository.</string>
<string name="don_remind_me_again">Don\'t remind me again</string>
<string name="announcement_title">Bekanntmachung</string>
<string name="No_app_to_handle_this_url">Keine App, die diese URL verarbeiten kann</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-eo/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">Ni kontrolos ĝisdatigojn de la oficiala GitHub-deponejo.</string>
<string name="don_remind_me_again">Ne rememorigu min</string>
<string name="announcement_title">Anonco</string>
<string name="No_app_to_handle_this_url">Neniu aplikaĵo por trakti ĉi tiun URL</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">Nous vérifierons les mises à jour du référentiel officiel GitHub.</string>
<string name="don_remind_me_again">Ne me le rappelle plus</string>
<string name="announcement_title">Annonce</string>
<string name="No_app_to_handle_this_url">Aucune application pour gérer cette URL</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">公式 GitHub リポジトリからの更新を確認します。</string>
<string name="don_remind_me_again">二度と思い出さないでください</string>
<string name="announcement_title">発表</string>
<string name="No_app_to_handle_this_url">この URL を処理するアプリはありません</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">Мы проверим наличие обновлений в официальном репозитории GitHub.</string>
<string name="don_remind_me_again">Не напоминай мне больше</string>
<string name="announcement_title">Объявление</string>
<string name="No_app_to_handle_this_url">Нет приложения для обработки этого URL.</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">我们将从官方 GitHub 存储库检查更新。</string>
<string name="don_remind_me_again">不要再提醒我</string>
<string name="announcement_title">公告</string>
<string name="No_app_to_handle_this_url">没有应用程序可以处理此网址</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">我們將從官方 GitHub 儲存庫檢查更新。</string>
<string name="don_remind_me_again">不要再提醒我</string>
<string name="announcement_title">公告</string>
<string name="No_app_to_handle_this_url">沒有應用程式可以處理此網址</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,5 @@
<string name="GoToCheckUpdateActivity_description">We will check for updates from the official GitHub repository.</string>
<string name="don_remind_me_again">Don\'t remind me again</string>
<string name="announcement_title">Announcement</string>
<string name="No_app_to_handle_this_url">No app to handle this url</string>
</resources>

0 comments on commit d26b741

Please sign in to comment.