Skip to content

Commit

Permalink
Version updated to 1.1.6, fixes and optimizations for multiple functions
Browse files Browse the repository at this point in the history
- Updated version number to 1.1.6
- Fixed the method for getting the current user ID, now returns the user ID and user name
- Optimized the user data loading logic in MainActivity and improved the initialization process
- Fixed the asynchronous operation of getting the current user ID in ChatActivity
- Optimized the setting logic of the user data storage path in the TgApi class

版本更新至1.1.6,修复和优化多项功能

- 更新版本号至1.1.6
- 修复获取当前用户ID的方法,现在返回用户ID和用户名
- 优化MainActivity中的用户数据加载逻辑,改进了初始化流程
- 在ChatActivity中修正了获取当前用户ID的异步操作
- 优化了TgApi类中用户数据存储路径的设置逻辑

Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Oct 5, 2024
1 parent 03029b2 commit 1236ae2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 92 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ android {
minSdk = 26
//noinspection OldTargetApi
targetSdk = 34
versionCode = 10
versionName = "1.1.5"
versionCode = 11
versionName = "1.1.6"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/gohj99/telewatch/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ChatActivity : ComponentActivity() {

// 异步获取当前用户 ID 和聊天记录
lifecycleScope.launch {
currentUserId.value = tgApi!!.getCurrentUserId()
currentUserId.value = tgApi!!.getCurrentUserId()[0].toLong()
tgApi!!.getChatMessages(chat!!.id, chatList) // 异步加载全部聊天消息
}

Expand Down
180 changes: 96 additions & 84 deletions app/src/main/java/com/gohj99/telewatch/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,88 +99,6 @@ class MainActivity : ComponentActivity() {
if (!isLoggedIn) {
startWelcomeActivity()
} else {
val gson = Gson()
val sharedPref = getSharedPreferences("LoginPref", MODE_PRIVATE)
val userList = sharedPref.getString("userList", "")
if (userList == "") throw Exception("No user data found")
val jsonObject: JsonObject = gson.fromJson(userList, JsonObject::class.java)
val accounts = mutableListOf<SettingItem>()
var a = 0
for (account in jsonObject.keySet()) {
if (a == 0) {
accounts.add(
SettingItem.Click(
itemName = jsonObject.get(account.toString()).asString,
onClick = {},
color = Color(0xFF2C323A)
)
)
} else {
accounts.add(
SettingItem.Click(
itemName = jsonObject.get(account.toString()).asString,
onClick = {
startActivity(
Intent(
this@MainActivity,
SwitchAccountActivity::class.java
).putExtra("account", account)
)
}
)
)
}
a += 1
}
accounts.addAll(listOf<SettingItem>(
SettingItem.Click(
itemName = getString(R.string.Add_Account),
onClick = {
startActivity(
Intent(
this@MainActivity,
LoginActivity::class.java
)
)
}
),
SettingItem.Click(
itemName = getString(R.string.Check_Update),
onClick = {
startActivity(
Intent(
this@MainActivity,
CheckUpdateActivity::class.java
)
)
}
),
SettingItem.Click(
itemName = getString(R.string.About),
onClick = {
startActivity(
Intent(
this@MainActivity,
AboutActivity::class.java
)
)
}
),
SettingItem.Click(
itemName = getString(R.string.setting_all),
onClick = {
startActivity(
Intent(
this@MainActivity,
SettingActivity::class.java
)
)
}
)
)
)
settingList.value = accounts

// 显示加载页面
setContent {
TelewatchTheme {
Expand All @@ -201,9 +119,103 @@ class MainActivity : ComponentActivity() {
try {
val gson = Gson()
val sharedPref = getSharedPreferences("LoginPref", MODE_PRIVATE)
val userList = sharedPref.getString("userList", "")
if (userList == "") throw Exception("No user data found")
var userList = sharedPref.getString("userList", "")
if (userList == "") {
val tempTgApi = TgApi(
this@MainActivity,
chatsList = chatsList
)
val currentUserId = tempTgApi.getCurrentUserId()
val jsonObject = JsonObject()
jsonObject.addProperty(
currentUserId[0],
currentUserId[1]
)
userList = jsonObject.toString()
with(sharedPref.edit()) {
putString("userList", userList)
apply()
}
tempTgApi.close()
}
val jsonObject: JsonObject = gson.fromJson(userList, JsonObject::class.java)
val accounts = mutableListOf<SettingItem>()
var a = 0
for (account in jsonObject.keySet()) {
if (a == 0) {
accounts.add(
SettingItem.Click(
itemName = jsonObject.get(account.toString()).asString,
onClick = {},
color = Color(0xFF2C323A)
)
)
} else {
accounts.add(
SettingItem.Click(
itemName = jsonObject.get(account.toString()).asString,
onClick = {
startActivity(
Intent(
this@MainActivity,
SwitchAccountActivity::class.java
).putExtra("account", account)
)
}
)
)
}
a += 1
}
accounts.addAll(listOf<SettingItem>(
SettingItem.Click(
itemName = getString(R.string.Add_Account),
onClick = {
startActivity(
Intent(
this@MainActivity,
LoginActivity::class.java
)
)
}
),
SettingItem.Click(
itemName = getString(R.string.Check_Update),
onClick = {
startActivity(
Intent(
this@MainActivity,
CheckUpdateActivity::class.java
)
)
}
),
SettingItem.Click(
itemName = getString(R.string.About),
onClick = {
startActivity(
Intent(
this@MainActivity,
AboutActivity::class.java
)
)
}
),
SettingItem.Click(
itemName = getString(R.string.setting_all),
onClick = {
startActivity(
Intent(
this@MainActivity,
SettingActivity::class.java
)
)
}
)
)
)
settingList.value = accounts

TgApiManager.tgApi = TgApi(
this@MainActivity,
chatsList = chatsList,
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/gohj99/telewatch/telegram/TgApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import java.util.concurrent.CountDownLatch
class TgApi(
private val context: Context,
private var chatsList: MutableState<List<Chat>>,
private val UserId: String
private val UserId: String = ""
) {
private var saveChatId = 1L
private var saveChatList = mutableStateOf(emptyList<TdApi.Message>())
Expand All @@ -49,7 +49,9 @@ class TgApi(
val tdapiId = config.getProperty("api_id").toInt()
val tdapiHash = config.getProperty("api_hash")
val parameters = TdApi.TdlibParameters().apply {
databaseDirectory = externalDir.absolutePath + "/$UserId/tdlib"
databaseDirectory = externalDir.absolutePath + (if (UserId == "") "/tdlib" else {
"/$UserId/tdlib"
})
useMessageDatabase = true
useSecretChats = true
apiId = tdapiId
Expand Down Expand Up @@ -628,12 +630,12 @@ class TgApi(
}
}

// 添加获取当前用户 ID 的方法
suspend fun getCurrentUserId(): Long {
// 获取当前用户 ID 的方法
suspend fun getCurrentUserId(): List<String> {
val result = sendRequest(TdApi.GetMe())
if (result.constructor == TdApi.User.CONSTRUCTOR) {
val user = result as TdApi.User
return user.id
return listOf(user.id.toString(), "${user.firstName} ${user.lastName}")
} else {
throw IllegalStateException("Failed to get current user ID")
}
Expand Down

0 comments on commit 1236ae2

Please sign in to comment.