Skip to content

Commit

Permalink
Added session updates
Browse files Browse the repository at this point in the history
Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Aug 10, 2024
1 parent ee67fb4 commit 9a3c29b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 91 deletions.
4 changes: 2 additions & 2 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/*
* 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.
*/

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/java/com/gohj99/telewatch/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ class ChatActivity : ComponentActivity() {
}

// chat 不为 null 时,获取聊天消息
tgApi?.let {
chatList.value = it.getChatMessages(chat!!.id, 10)
}
tgApi!!.getChatMessages(chat!!.id, 10, chatList)

setContent {
TelewatchTheme {
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/gohj99/telewatch/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@ object TgApiManager {
}

class MainActivity : ComponentActivity() {
private var tgApi: TgApi? = null
private var isLoggedIn: Boolean = false
private var exceptionState by mutableStateOf<Exception?>(null)
private var chatsList = mutableStateOf(listOf<Chat>())

override fun onDestroy() {
super.onDestroy()
tgApi?.close()
TgApiManager.tgApi?.close()
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -74,11 +73,13 @@ class MainActivity : ComponentActivity() {
lifecycleScope.launch(Dispatchers.IO) {
try {
println("开始启动Main")
TgApiManager.tgApi = TgApi(this@MainActivity)
TgApiManager.tgApi = TgApi(
this@MainActivity,
chatsList = chatsList
)
println("实例化TgApi")
TgApiManager.tgApi?.getChats(
limit = 10,
chatsList = chatsList
limit = 10
)
println("获取消息列表")
launch(Dispatchers.Main) {
Expand Down
134 changes: 93 additions & 41 deletions app/src/main/java/com/gohj99/telewatch/telegram/TgApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.gohj99.telewatch.telegram
import android.content.Context
import android.os.Build
import androidx.compose.runtime.MutableState
import com.gohj99.telewatch.R
import com.gohj99.telewatch.ui.main.Chat
import com.gohj99.telewatch.ui.main.add
import kotlinx.coroutines.CompletableDeferred
Expand All @@ -22,7 +23,7 @@ import java.io.IOException
import java.util.Properties
import java.util.concurrent.CountDownLatch

class TgApi(private val context: Context) {
class TgApi(private val context: Context, private var chatsList: MutableState<List<Chat>>) {
private val client: Client = Client.create({ update -> handleUpdate(update) }, null, null)
private val sharedPref = context.getSharedPreferences("LoginPref", Context.MODE_PRIVATE)
@Volatile private var isAuthorized: Boolean = false
Expand Down Expand Up @@ -78,40 +79,87 @@ class TgApi(private val context: Context) {
// 处理 TDLib 更新的函数
private fun handleUpdate(update: TdApi.Object) {
when (update.constructor) {
TdApi.UpdateAuthorizationState.CONSTRUCTOR -> {
val authorizationState = (update as TdApi.UpdateAuthorizationState).authorizationState
when (authorizationState.constructor) {
TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
println("TgApi: Authorization Ready")
isAuthorized = true
authLatch.countDown()
}
TdApi.AuthorizationStateClosed.CONSTRUCTOR -> {
println("TgApi: Authorization Closed")
isAuthorized = false
authLatch.countDown()
}
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
println("TgApi: Waiting for Phone Number")
isAuthorized = false
authLatch.countDown()
}
TdApi.UpdateAuthorizationState.CONSTRUCTOR -> handleAuthorizationState(update as TdApi.UpdateAuthorizationState)
TdApi.UpdateNewMessage.CONSTRUCTOR -> handleNewMessage(update as TdApi.UpdateNewMessage)
TdApi.UpdateMessageContent.CONSTRUCTOR -> handleMessageContentUpdate(update as TdApi.UpdateMessageContent)
TdApi.UpdateMessageEdited.CONSTRUCTOR -> handleMessageEdited(update as TdApi.UpdateMessageEdited)
// 其他更新
else -> {
//println("Received update: $update")
}
}
}

TdApi.AuthorizationStateWaitTdlibParameters.CONSTRUCTOR -> {
println("TgApi: Waiting for TDLib Parameters")
}
private fun handleAuthorizationState(update: TdApi.UpdateAuthorizationState) {
val authorizationState = update.authorizationState
when (authorizationState.constructor) {
TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
println("TgApi: Authorization Ready")
isAuthorized = true
authLatch.countDown()
}

TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
println("TgApi: Waiting for Encryption Key")
}
else -> {
//println("Authorization state: $authorizationState")
}
}
TdApi.AuthorizationStateClosed.CONSTRUCTOR -> {
println("TgApi: Authorization Closed")
isAuthorized = false
authLatch.countDown()
}

TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
println("TgApi: Waiting for Phone Number")
isAuthorized = false
authLatch.countDown()
}
// 处理其他更新...

else -> {
//println("Received update: $update")
// 其他授权状态处理
}
}
}

private fun handleNewMessage(update: TdApi.UpdateNewMessage) {
val message = update.message
println("New message received in chat ID ${message.chatId}")
updateChatList(message)
}

private fun handleMessageContentUpdate(update: TdApi.UpdateMessageContent) {
val chatId = update.chatId
val messageId = update.messageId
//val newContent = update.newContent
println("Message content updated in chat ID $chatId for message ID $messageId")
// 更新消息内容逻辑
}

private fun handleMessageEdited(update: TdApi.UpdateMessageEdited) {
val chatId = update.chatId
val messageId = update.messageId
val editDate = update.editDate
println("Message edited in chat ID $chatId for message ID $messageId at $editDate")
// 更新消息编辑状态逻辑
}

private fun updateChatList(message: TdApi.Message) {
val chatId = message.chatId
val newMessageText = when (val content = message.content) {
is TdApi.MessageText -> {
val text = content.text.text
if (text.length > 20) text.take(20) + "..." else text
}

else -> context.getString(R.string.Unknown_Message)
}

chatsList.value = chatsList.value.toMutableList().apply {
// 查找现有的聊天并更新
val existingChatIndex = indexOfFirst { it.id == chatId }
if (existingChatIndex >= 0) {
val updatedChat = get(existingChatIndex).copy(message = newMessageText)
removeAt(existingChatIndex)
add(0, updatedChat)
} else {
// 新增聊天到列表顶部
add(0, Chat(id = chatId, title = "New Chat", message = newMessageText))
}
}
}
Expand Down Expand Up @@ -162,9 +210,9 @@ class TgApi(private val context: Context) {
}

// 获取聊天列表
suspend fun getChats(limit: Int = 10, chatsList: MutableState<List<Chat>>) {
suspend fun getChats(limit: Int = 10) {
val chatIds = getChatIds(limit)
fetchChatDetails(chatIds, chatsList)
fetchChatDetails(chatIds)
}

private suspend fun getChatIds(limit: Int): List<Long> = withContext(Dispatchers.IO) {
Expand All @@ -184,7 +232,7 @@ class TgApi(private val context: Context) {
return@withContext chatIds
}

private suspend fun fetchChatDetails(chatIds: List<Long>, chatsList: MutableState<List<Chat>>) =
private suspend fun fetchChatDetails(chatIds: List<Long>) =
withContext(Dispatchers.IO) {
for (chatId in chatIds) {
//println("Sending request for chat ID: $chatId")
Expand All @@ -200,14 +248,14 @@ class TgApi(private val context: Context) {
val chat = result as TdApi.Chat
//println("Chat Details for chat ID $chatId: $chat")
withContext(Dispatchers.Main) {
var message = ""
val lastMessage = chat.lastMessage
if (lastMessage != null) {
val message = if (lastMessage != null) {
val messageContent = lastMessage.content
if (messageContent is TdApi.MessageText) {
message = messageContent.text.text.toString()
}
}
val text = messageContent.text.text.toString()
if (text.length > 20) text.take(20) + "..." else text
} else context.getString(R.string.Unknown_Message)
} else context.getString(R.string.Unknown_Message)
println(chat.id)
println(chat.title)
chatsList.add(
Expand Down Expand Up @@ -246,7 +294,11 @@ class TgApi(private val context: Context) {
}

// 获取聊天记录
fun getChatMessages(chatId: Long, limit: Int = 10): List<TdApi.Message> {
fun getChatMessages(
chatId: Long,
limit: Int = 10,
chatList: MutableState<List<TdApi.Message>>
) {
val messagesList = mutableListOf<TdApi.Message>()
val getChatMessages = TdApi.GetChatHistory().apply {
this.chatId = chatId
Expand All @@ -260,10 +312,10 @@ class TgApi(private val context: Context) {
} else {
val messages = result as TdApi.Messages
messagesList.addAll(messages.messages.toList())
chatList.value = messagesList
println("Messages: ${messages.messages.joinToString(", ")}")
}
}
return messagesList
}

// 关闭连接
Expand Down
Loading

0 comments on commit 9a3c29b

Please sign in to comment.