Skip to content

Commit

Permalink
feat(ChatInfo): Add a chat information page
Browse files Browse the repository at this point in the history
Add ChatInfoActivity and ChatInfoScreen components to display chat information (not complete)

feat(ChatInfo): 添加聊天信息页面

新增 ChatInfoActivity 和 ChatInfoScreen 组件,用于显示聊天信息(未完善)

Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Nov 17, 2024
1 parent 140b081 commit e705ec6
Show file tree
Hide file tree
Showing 21 changed files with 397 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
//noinspection OldTargetApi
targetSdk = 34
versionCode = 20
versionName = "1.2.9"
versionName = "1.2.9-beta1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
2 changes: 2 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@
-dontwarn org.conscrypt.**
-dontwarn org.openjsse.**
-keepattributes Signature
-keepattributes EnclosingMethod
-keep class com.google.gson.reflect.TypeToken { *; }
-keep class com.gohj99.telewatch.Announcement { *; }
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
android:theme="@style/Theme.Telewatch"
tools:ignore="WearStandaloneAppFlag"
tools:targetApi="31">
<activity
android:name=".ChatInfoActivity"
android:exported="true"
android:label="@string/chat_info_activity_main"
android:theme="@style/Theme.Telewatch" />
<activity
android:name=".GoToAnnouncementActivity"
android:exported="true"
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/com/gohj99/telewatch/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,10 @@ class ChatActivity : ComponentActivity() {
// 返回Uri
return FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file)
}
}

private fun formatJson(jsonString: String): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val jsonElement = gson.fromJson(jsonString, Any::class.java)
return gson.toJson(jsonElement)
}
fun formatJson(jsonString: String): String {
val gson = GsonBuilder().setPrettyPrinting().create()
val jsonElement = gson.fromJson(jsonString, Any::class.java)
return gson.toJson(jsonElement)
}
60 changes: 60 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/ChatInfoActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (c) 2024 gohj99. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/

package com.gohj99.telewatch

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.gohj99.telewatch.ui.SplashChatInfoScreen
import com.gohj99.telewatch.ui.main.Chat
import com.gohj99.telewatch.ui.theme.TelewatchTheme
import com.gohj99.telewatch.utils.telegram.TgApi
import kotlinx.coroutines.runBlocking
import org.drinkless.tdlib.TdApi

class ChatInfoActivity : ComponentActivity() {
private var tgApi: TgApi? = null
private var chat: Chat? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()

tgApi = TgApiManager.tgApi

// 接收传递的 Chat 对象
chat = intent.getParcelableExtra("chat")

// 如果 chat 为 null,直接退出页面
if (chat == null) {
finish()
return
}

chat?.let { safeChat ->
var chatObject: TdApi.Chat? // 在外部声明变量

runBlocking {
chatObject = tgApi!!.getChat(safeChat.id) // 在 runBlocking 中赋值
}

//println("获取到的chatObject")
//println(chatObject)
// 这里可以使用 chatObject,因为它在 runBlocking 块外声明了
chatObject?.let { itChatObject ->
setContent {
TelewatchTheme {
SplashChatInfoScreen(itChatObject)
}
}
}
}
}
}
17 changes: 2 additions & 15 deletions app/src/main/java/com/gohj99/telewatch/ui/AnnouncementScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

package com.gohj99.telewatch.ui

import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.net.Uri
import android.widget.Toast
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand Down Expand Up @@ -44,6 +39,7 @@ import com.gohj99.telewatch.Announcement
import com.gohj99.telewatch.R
import com.gohj99.telewatch.ui.main.LinkText
import com.gohj99.telewatch.ui.main.MainCard
import com.gohj99.telewatch.utils.urlHandle
import com.google.gson.JsonObject

@Composable
Expand Down Expand Up @@ -125,16 +121,7 @@ fun SplashAnnouncementScreen(
color = Color.White,
style = MaterialTheme.typography.titleMedium,
onLinkClick = { url ->
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
val packageManager: PackageManager = context.packageManager
val activities: List<ResolveInfo> = packageManager.queryIntentActivities(intent, 0)

if (activities.isNotEmpty()) {
context.startActivity(intent)
} else {
// 处理没有可用浏览器的情况
Toast.makeText(context, context.getString(R.string.No_app_to_handle_this_url), Toast.LENGTH_SHORT).show()
}
urlHandle(url, context)
}
)
}
Expand Down
224 changes: 224 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/ui/ChatInfoScreen.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
* Copyright (c) 2024 gohj99. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/

package com.gohj99.telewatch.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import com.gohj99.telewatch.R
import com.gohj99.telewatch.TgApiManager
import com.gohj99.telewatch.formatJson
import com.gohj99.telewatch.ui.main.MainCard
import com.google.gson.Gson
import org.drinkless.tdlib.TdApi

@Composable
fun SplashChatInfoScreen(chatObject: TdApi.Chat) {
var subtitle by remember { mutableStateOf("") }
val gson = Gson()
val messageJson = formatJson(gson.toJson(chatObject))
val context = LocalContext.current

val chatType = chatObject.type
if (chatType is TdApi.ChatTypePrivate) {
TgApiManager.tgApi!!.getUser(chatType.userId) { result ->
when (result!!.status) {
is TdApi.UserStatusOnline ->
subtitle = context.getString(R.string.Online)
is TdApi.UserStatusEmpty ->
subtitle = context.getString(R.string.Unknown)
is TdApi.UserStatusRecently ->
subtitle = context.getString(R.string.Lately)
is TdApi.UserStatusLastWeek ->
subtitle = context.getString(R.string.Last_week)
is TdApi.UserStatusLastMonth ->
subtitle = context.getString(R.string.Last_month)
is TdApi.UserStatusOffline ->
subtitle = context.getString(R.string.Offline)
}
}
}

Column(
modifier = Modifier
.fillMaxSize()
) {
// 标题
Box(
modifier = Modifier
.fillMaxWidth() // 只填充宽度
.padding(top = 14.dp), // 添加顶部填充
contentAlignment = Alignment.Center
) {
Text(
text = stringResource(R.string.Chat_Info),
color = Color.White,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)
}

val listState = rememberLazyListState()
LazyColumn(
state = listState,
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
.verticalRotaryScroll(listState)
) {
item {
Spacer(modifier = Modifier.height(24.dp))
}
item {
Box(
modifier = Modifier
.fillMaxWidth() // 只填充宽度
) {
MainCard(
column = {
Row(
modifier = Modifier
.fillMaxWidth(),
verticalAlignment = Alignment.Top // 或 Alignment.Bottom,或者移除此行使用默认的 Top 对齐
) {
ThumbnailChatPhoto(chatObject.photo!!.small)
Spacer(Modifier.width(12.dp))
Column {
Spacer(Modifier.height(1.5.dp))
Text(
text = chatObject.title,
color = Color.White,
style = MaterialTheme.typography.titleMedium,
fontWeight = FontWeight.Bold
)

Spacer(Modifier.height(5.dp))

Text(
text = subtitle,
color = Color(0xFF596B7F),
style = MaterialTheme.typography.titleMedium
)
}
}
},
item = "chat",
callback = { }
)
}
}
item {
Spacer(modifier = Modifier.height(10.dp))
Box(
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.background(
Color.Black.copy(alpha = 0.8f),
shape = RoundedCornerShape(8.dp)
)
.padding(7.dp),
contentAlignment = Alignment.Center
) {
TextField(
value = messageJson,
onValueChange = { },
readOnly = true,
modifier = Modifier
.fillMaxSize()
.background(
Color(0xFF2C323A).copy(alpha = 0.3f),
shape = RoundedCornerShape(8.dp)
)
.padding(8.dp),
textStyle = TextStyle(color = Color.White, fontSize = 10.sp),
singleLine = false
)
}
}
item {
Spacer(modifier = Modifier.height(50.dp))
}
}
}
}

@Composable
fun ThumbnailChatPhoto(
thumbnail: TdApi.File
) {
val isDownloaded = remember { mutableStateOf(thumbnail.local.isDownloadingCompleted) }

if (!isDownloaded.value) {
LaunchedEffect(thumbnail) {
println("本地没图片,正在下载图片")
TgApiManager.tgApi!!.downloadPhoto(thumbnail) { success, path ->
if (success) {
isDownloaded.value = true
} else {
// 处理下载失败
}
}
}
} else {
Box(
modifier = Modifier
.size(45.dp)
.clip(CircleShape)
) {
Image(
painter = rememberAsyncImagePainter(model = thumbnail.local.path),
contentDescription = "Thumbnail",
modifier = Modifier
.clip(CircleShape)
)
}
}
}

@Preview(showBackground = true)
@Composable
fun SplashChatInfoScreenPreview() {
SplashChatInfoScreen(chatObject = TdApi.Chat())
}
Loading

0 comments on commit e705ec6

Please sign in to comment.