feat: enhance profile fetching and update mechanisms in ProtocolManager and MainScreen
This commit is contained in:
@@ -103,6 +103,10 @@ class Protocol(
|
||||
private var lastPublicKey: String? = null
|
||||
private var lastPrivateHash: String? = null
|
||||
|
||||
// Getters for ProtocolManager to fetch own profile
|
||||
fun getPublicKey(): String? = lastPublicKey
|
||||
fun getPrivateHash(): String? = lastPrivateHash
|
||||
|
||||
// Heartbeat
|
||||
private var heartbeatJob: Job? = null
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.rosetta.messenger.network
|
||||
|
||||
import android.content.Context
|
||||
import com.rosetta.messenger.data.AccountManager
|
||||
import com.rosetta.messenger.data.MessageRepository
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -21,6 +22,7 @@ object ProtocolManager {
|
||||
|
||||
private var protocol: Protocol? = null
|
||||
private var messageRepository: MessageRepository? = null
|
||||
private var appContext: Context? = null
|
||||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
// Debug logs for dev console - 🚀 ОТКЛЮЧЕНО для производительности
|
||||
@@ -59,6 +61,7 @@ object ProtocolManager {
|
||||
* Инициализация с контекстом для доступа к MessageRepository
|
||||
*/
|
||||
fun initialize(context: Context) {
|
||||
appContext = context.applicationContext
|
||||
messageRepository = MessageRepository.getInstance(context)
|
||||
setupPacketHandlers()
|
||||
setupStateMonitoring()
|
||||
@@ -156,19 +159,32 @@ object ProtocolManager {
|
||||
|
||||
// 🔥 Обработчик поиска/user info (0x03)
|
||||
// Обновляет информацию о пользователе в диалогах когда приходит ответ от сервера
|
||||
// + обновляет own profile (username/name) аналогично Desktop useUserInformation()
|
||||
waitPacket(0x03) { packet ->
|
||||
val searchPacket = packet as PacketSearch
|
||||
|
||||
// Обновляем информацию о пользователях в диалогах
|
||||
if (searchPacket.users.isNotEmpty()) {
|
||||
scope.launch(Dispatchers.IO) { // 🔥 Запускаем на IO потоке для работы с БД
|
||||
scope.launch(Dispatchers.IO) {
|
||||
val ownPublicKey = getProtocol().getPublicKey()
|
||||
searchPacket.users.forEach { user ->
|
||||
// Обновляем инфо в диалогах (для всех пользователей)
|
||||
messageRepository?.updateDialogUserInfo(
|
||||
user.publicKey,
|
||||
user.title,
|
||||
user.username,
|
||||
user.verified
|
||||
)
|
||||
|
||||
// Если это наш own profile — сохраняем username/name в AccountManager
|
||||
if (user.publicKey == ownPublicKey && appContext != null) {
|
||||
val accountManager = AccountManager(appContext!!)
|
||||
if (user.title.isNotBlank()) {
|
||||
accountManager.updateAccountName(user.publicKey, user.title)
|
||||
}
|
||||
if (user.username.isNotBlank()) {
|
||||
accountManager.updateAccountUsername(user.publicKey, user.username)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -216,13 +232,29 @@ object ProtocolManager {
|
||||
fun authenticate(publicKey: String, privateHash: String) {
|
||||
getProtocol().startHandshake(publicKey, privateHash)
|
||||
|
||||
// 🚀 Запрашиваем транспортный сервер после авторизации
|
||||
// 🚀 Запрашиваем транспортный сервер и own profile после авторизации
|
||||
scope.launch {
|
||||
delay(500) // Даём время на завершение handshake
|
||||
TransportManager.requestTransportServer()
|
||||
fetchOwnProfile()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Запрашивает собственный профиль с сервера (username, name/title).
|
||||
* Аналог Desktop: useUserInformation(ownPublicKey) → PacketSearch(0x03)
|
||||
*/
|
||||
private fun fetchOwnProfile() {
|
||||
val publicKey = getProtocol().getPublicKey() ?: return
|
||||
val privateHash = getProtocol().getPrivateHash() ?: return
|
||||
|
||||
val packet = PacketSearch().apply {
|
||||
this.privateKey = privateHash
|
||||
this.search = publicKey
|
||||
}
|
||||
send(packet)
|
||||
}
|
||||
|
||||
/**
|
||||
* Send packet (simplified)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user