Skip to content

Commit

Permalink
Create ChatPage
Browse files Browse the repository at this point in the history
Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Aug 9, 2024
1 parent 1317422 commit eaed7f4
Show file tree
Hide file tree
Showing 21 changed files with 785 additions and 72 deletions.
26 changes: 26 additions & 0 deletions .idea/appInsightsSettings.xml

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

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

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

15 changes: 13 additions & 2 deletions .idea/deploymentTargetSelector.xml

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

6 changes: 6 additions & 0 deletions .idea/studiobot.xml

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

67 changes: 67 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/ChatActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.lifecycleScope
import com.gohj99.telewatch.telegram.TgApi
import com.gohj99.telewatch.ui.chat.SplashChatScreen
import com.gohj99.telewatch.ui.main.Chat
import com.gohj99.telewatch.ui.theme.TelewatchTheme
import kotlinx.coroutines.launch
import org.drinkless.td.libcore.telegram.TdApi

class ChatActivity : ComponentActivity() {
private var tgApi: TgApi? = null
private var chat: Chat? = null
private var chatList = mutableStateOf(emptyList<TdApi.Message>())
private var currentUserId = mutableStateOf(-1L) // 使用 MutableState 来持有当前用户 ID

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
tgApi = TgApiManager.tgApi

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

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

enableEdgeToEdge()

// 异步获取当前用户 ID
lifecycleScope.launch {
tgApi?.let {
currentUserId.value = it.getCurrentUserId()
}
}

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

setContent {
TelewatchTheme {
SplashChatScreen(
chatTitle = chat!!.title,
chatList = chatList,
currentUserId = currentUserId.value // 传递当前用户 ID
)
}
}
}
}
53 changes: 44 additions & 9 deletions app/src/main/java/com/gohj99/telewatch/LoginActivity.kt
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.
*/

package com.gohj99.telewatch

import android.app.AlertDialog
Expand Down Expand Up @@ -29,7 +37,7 @@ import java.util.Properties

class LoginActivity : ComponentActivity() {
private lateinit var client: Client
private lateinit var LanguageCode: String
private lateinit var languageCode: String
private lateinit var appVersion: String
private var qrCodeLink by mutableStateOf<String?>(null)
private var showPasswordScreen by mutableStateOf(false)
Expand All @@ -45,14 +53,18 @@ class LoginActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
LanguageCode = this.resources.configuration.locales[0].language
languageCode = this.resources.configuration.locales[0].language
appVersion = getAppVersion(this)
setContent {
TelewatchTheme {
if (showPasswordScreen) {
SplashPasswordScreen(
onDoneClick = { password ->
client.send(TdApi.CheckAuthenticationPassword(password), { authRequestHandler(it) })
client.send(TdApi.CheckAuthenticationPassword(password)) {
authRequestHandler(
it
)
}
},
passwordHint = passwordHint,
doneStr = doneStr
Expand Down Expand Up @@ -107,17 +119,17 @@ class LoginActivity : ComponentActivity() {
useSecretChats = true
apiId = tdapiId
apiHash = tdapiHash
systemLanguageCode = LanguageCode
systemLanguageCode = languageCode
deviceModel = Build.MODEL
systemVersion = Build.VERSION.RELEASE
applicationVersion = appVersion
enableStorageOptimizer = true
}
client.send(TdApi.SetTdlibParameters(parameters), { })
client.send(TdApi.SetTdlibParameters(parameters)) { }
}
TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
// 检查本地是否有加密密钥
val sharedPref = getSharedPreferences("LoginPref", Context.MODE_PRIVATE)
val sharedPref = getSharedPreferences("LoginPref", MODE_PRIVATE)
val encryptionKeyString = sharedPref.getString("encryption_key", null)
val encryptionKey: TdApi.CheckDatabaseEncryptionKey = if (encryptionKeyString != null) {
val keyBytes = encryptionKeyString.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
Expand All @@ -133,11 +145,15 @@ class LoginActivity : ComponentActivity() {
TdApi.CheckDatabaseEncryptionKey(newKeyBytes)
}

client.send(encryptionKey, { })
client.send(encryptionKey) { }
}
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
// 请求二维码认证
client.send(TdApi.RequestQrCodeAuthentication(LongArray(0)), { authRequestHandler(it) })
client.send(TdApi.RequestQrCodeAuthentication(LongArray(0))) {
authRequestHandler(
it
)
}
}
TdApi.AuthorizationStateWaitOtherDeviceConfirmation.CONSTRUCTOR -> {
val link = (authorizationState as TdApi.AuthorizationStateWaitOtherDeviceConfirmation).link
Expand All @@ -156,7 +172,7 @@ class LoginActivity : ComponentActivity() {
Intent("ACTION_DESTROY_WELCOME_ACTIVITY")
)*/
// 存储登录成功信息
val sharedPref = getSharedPreferences("LoginPref", Context.MODE_PRIVATE)
val sharedPref = getSharedPreferences("LoginPref", MODE_PRIVATE)
with(sharedPref.edit()) {
putBoolean("isLoggedIn", true)
apply()
Expand All @@ -178,6 +194,25 @@ class LoginActivity : ComponentActivity() {
}
}
// 处理其他授权状态...
TdApi.AuthorizationStateClosed.CONSTRUCTOR -> {
TODO()
}

TdApi.AuthorizationStateClosing.CONSTRUCTOR -> {
TODO()
}

TdApi.AuthorizationStateLoggingOut.CONSTRUCTOR -> {
TODO()
}

TdApi.AuthorizationStateWaitCode.CONSTRUCTOR -> {
TODO()
}

TdApi.AuthorizationStateWaitRegistration.CONSTRUCTOR -> {
TODO()
}
}
}
// 处理其他更新...
Expand Down
35 changes: 28 additions & 7 deletions app/src/main/java/com/gohj99/telewatch/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/*
* 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.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand All @@ -18,6 +27,11 @@ import com.gohj99.telewatch.ui.theme.TelewatchTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

object TgApiManager {
@SuppressLint("StaticFieldLeak")
var tgApi: TgApi? = null
}

class MainActivity : ComponentActivity() {
private var tgApi: TgApi? = null
private var isLoggedIn: Boolean = false
Expand Down Expand Up @@ -52,15 +66,24 @@ class MainActivity : ComponentActivity() {
private fun initMain() {
lifecycleScope.launch(Dispatchers.IO) {
try {
tgApi = TgApi(this@MainActivity)
tgApi?.getChats(
TgApiManager.tgApi = TgApi(this@MainActivity)
TgApiManager.tgApi?.getChats(
limit = 10,
chatsList = chatsList
)
launch(Dispatchers.Main) {
setContent {
TelewatchTheme {
MainScreen(chatsList)
MainScreen(
chats = chatsList,
chatPage = { chat ->
startActivity(
Intent(this@MainActivity, ChatActivity::class.java).apply {
putExtra("chat", chat)
}
)
}
)
}
}
}
Expand All @@ -73,10 +96,7 @@ class MainActivity : ComponentActivity() {
onRetry = { retryInitialization() },
onSetting = {
startActivity(
Intent(
this@MainActivity,
SettingActivity::class.java
)
Intent(this@MainActivity, SettingActivity::class.java)
)
}
)
Expand All @@ -87,6 +107,7 @@ class MainActivity : ComponentActivity() {
}
}


private fun retryInitialization() {
exceptionState = null
initMain()
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/com/gohj99/telewatch/WelcomeActivity.kt
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.
*/

package com.gohj99.telewatch

import android.content.Context
Expand Down Expand Up @@ -57,7 +65,5 @@ class WelcomeActivity : ComponentActivity() {
)
}
}


}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/gohj99/telewatch/telegram/TgApi.kt
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.
*/

package com.gohj99.telewatch.telegram

import android.content.Context
Expand Down Expand Up @@ -211,6 +219,17 @@ class TgApi(private val context: Context) {
}
}

// 添加获取当前用户 ID 的方法
suspend fun getCurrentUserId(): Long {
val result = sendRequest(TdApi.GetMe())
if (result.constructor == TdApi.User.CONSTRUCTOR) {
val user = result as TdApi.User
return user.id
} else {
throw IllegalStateException("Failed to get current user ID")
}
}

private suspend fun sendRequest(request: TdApi.Function): TdApi.Object =
withContext(Dispatchers.IO) {
val result = CompletableDeferred<TdApi.Object>()
Expand Down
Loading

0 comments on commit eaed7f4

Please sign in to comment.