feat: Implement Recent Searches functionality in SearchScreen for improved user experience
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
package com.rosetta.messenger.data
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.rosetta.messenger.network.SearchUser
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
/**
|
||||
* Менеджер для хранения недавно открытых пользователей (до 15 записей)
|
||||
*/
|
||||
object RecentSearchesManager {
|
||||
private const val PREFS_NAME = "recent_searches"
|
||||
private const val KEY_USERS = "recent_users"
|
||||
private const val MAX_USERS = 15
|
||||
|
||||
private lateinit var prefs: SharedPreferences
|
||||
private val _recentUsers = MutableStateFlow<List<SearchUser>>(emptyList())
|
||||
val recentUsers: StateFlow<List<SearchUser>> = _recentUsers.asStateFlow()
|
||||
|
||||
fun init(context: Context) {
|
||||
prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
|
||||
loadUsers()
|
||||
}
|
||||
|
||||
private fun loadUsers() {
|
||||
val usersJson = prefs.getString(KEY_USERS, "[]") ?: "[]"
|
||||
try {
|
||||
val jsonArray = JSONArray(usersJson)
|
||||
val users = mutableListOf<SearchUser>()
|
||||
for (i in 0 until jsonArray.length()) {
|
||||
val obj = jsonArray.getJSONObject(i)
|
||||
users.add(
|
||||
SearchUser(
|
||||
publicKey = obj.getString("publicKey"),
|
||||
username = obj.optString("username", ""),
|
||||
title = obj.optString("title", ""),
|
||||
verified = obj.optInt("verified", 0),
|
||||
online = obj.optInt("online", 0)
|
||||
)
|
||||
)
|
||||
}
|
||||
_recentUsers.value = users
|
||||
} catch (e: Exception) {
|
||||
_recentUsers.value = emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавить пользователя в историю
|
||||
*/
|
||||
fun addUser(user: SearchUser) {
|
||||
val currentUsers = _recentUsers.value.toMutableList()
|
||||
|
||||
// Удаляем если уже есть (чтобы переместить наверх)
|
||||
currentUsers.removeAll { it.publicKey == user.publicKey }
|
||||
|
||||
// Добавляем в начало
|
||||
currentUsers.add(0, user)
|
||||
|
||||
// Ограничиваем до MAX_USERS
|
||||
val limitedUsers = currentUsers.take(MAX_USERS)
|
||||
|
||||
// Сохраняем
|
||||
_recentUsers.value = limitedUsers
|
||||
saveUsers(limitedUsers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Удалить пользователя из истории
|
||||
*/
|
||||
fun removeUser(publicKey: String) {
|
||||
val currentUsers = _recentUsers.value.toMutableList()
|
||||
currentUsers.removeAll { it.publicKey == publicKey }
|
||||
_recentUsers.value = currentUsers
|
||||
saveUsers(currentUsers)
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистить всю историю
|
||||
*/
|
||||
fun clearAll() {
|
||||
_recentUsers.value = emptyList()
|
||||
prefs.edit().remove(KEY_USERS).apply()
|
||||
}
|
||||
|
||||
private fun saveUsers(users: List<SearchUser>) {
|
||||
val jsonArray = JSONArray()
|
||||
users.forEach { user ->
|
||||
val obj = JSONObject().apply {
|
||||
put("publicKey", user.publicKey)
|
||||
put("username", user.username)
|
||||
put("title", user.title)
|
||||
put("verified", user.verified)
|
||||
put("online", user.online)
|
||||
}
|
||||
jsonArray.put(obj)
|
||||
}
|
||||
prefs.edit().putString(KEY_USERS, jsonArray.toString()).apply()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user