mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 00:25:20 +00:00
REFACTOR: Break ChatViewModel into smaller specialized files
- Created ChatState.kt for centralized state management - Created DataManager.kt for persistence operations - Created MessageManager.kt for message handling and deduplication - Created ChannelManager.kt for channel operations and encryption - Created PrivateChatManager.kt for private chat functionality - Created CommandProcessor.kt for IRC-style command processing ChatViewModel.kt reduced from 1000+ lines to ~300 lines while maintaining 100% functionality. All existing functionality preserved, just better organized.
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
package com.bitchat.android.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.google.gson.Gson
|
||||
import kotlin.random.Random
|
||||
|
||||
/**
|
||||
* Handles data persistence operations for the chat system
|
||||
*/
|
||||
class DataManager(private val context: Context) {
|
||||
|
||||
private val prefs: SharedPreferences = context.getSharedPreferences("bitchat_prefs", Context.MODE_PRIVATE)
|
||||
private val gson = Gson()
|
||||
|
||||
// Channel-related maps that need to persist state
|
||||
private val _channelCreators = mutableMapOf<String, String>()
|
||||
private val _favoritePeers = mutableSetOf<String>()
|
||||
private val _blockedUsers = mutableSetOf<String>()
|
||||
private val _channelMembers = mutableMapOf<String, MutableSet<String>>()
|
||||
|
||||
val channelCreators: Map<String, String> get() = _channelCreators
|
||||
val favoritePeers: Set<String> get() = _favoritePeers
|
||||
val blockedUsers: Set<String> get() = _blockedUsers
|
||||
val channelMembers: Map<String, MutableSet<String>> get() = _channelMembers
|
||||
|
||||
// MARK: - Nickname Management
|
||||
|
||||
fun loadNickname(): String {
|
||||
val savedNickname = prefs.getString("nickname", null)
|
||||
return if (savedNickname != null) {
|
||||
savedNickname
|
||||
} else {
|
||||
val randomNickname = "anon${Random.nextInt(1000, 9999)}"
|
||||
saveNickname(randomNickname)
|
||||
randomNickname
|
||||
}
|
||||
}
|
||||
|
||||
fun saveNickname(nickname: String) {
|
||||
prefs.edit().putString("nickname", nickname).apply()
|
||||
}
|
||||
|
||||
// MARK: - Channel Data Management
|
||||
|
||||
fun loadChannelData(): Pair<Set<String>, Set<String>> {
|
||||
// Load joined channels
|
||||
val savedChannels = prefs.getStringSet("joined_channels", emptySet()) ?: emptySet()
|
||||
|
||||
// Load password protected channels
|
||||
val savedProtectedChannels = prefs.getStringSet("password_protected_channels", emptySet()) ?: emptySet()
|
||||
|
||||
// Load channel creators
|
||||
val creatorsJson = prefs.getString("channel_creators", "{}")
|
||||
try {
|
||||
val creatorsMap = gson.fromJson(creatorsJson, Map::class.java) as? Map<String, String>
|
||||
creatorsMap?.let { _channelCreators.putAll(it) }
|
||||
} catch (e: Exception) {
|
||||
// Ignore parsing errors
|
||||
}
|
||||
|
||||
// Initialize channel members for loaded channels
|
||||
savedChannels.forEach { channel ->
|
||||
if (!_channelMembers.containsKey(channel)) {
|
||||
_channelMembers[channel] = mutableSetOf()
|
||||
}
|
||||
}
|
||||
|
||||
return Pair(savedChannels, savedProtectedChannels)
|
||||
}
|
||||
|
||||
fun saveChannelData(joinedChannels: Set<String>, passwordProtectedChannels: Set<String>) {
|
||||
prefs.edit().apply {
|
||||
putStringSet("joined_channels", joinedChannels)
|
||||
putStringSet("password_protected_channels", passwordProtectedChannels)
|
||||
putString("channel_creators", gson.toJson(_channelCreators))
|
||||
apply()
|
||||
}
|
||||
}
|
||||
|
||||
fun addChannelCreator(channel: String, creatorID: String) {
|
||||
_channelCreators[channel] = creatorID
|
||||
}
|
||||
|
||||
fun removeChannelCreator(channel: String) {
|
||||
_channelCreators.remove(channel)
|
||||
}
|
||||
|
||||
fun isChannelCreator(channel: String, peerID: String): Boolean {
|
||||
return _channelCreators[channel] == peerID
|
||||
}
|
||||
|
||||
// MARK: - Channel Members Management
|
||||
|
||||
fun addChannelMember(channel: String, peerID: String) {
|
||||
if (!_channelMembers.containsKey(channel)) {
|
||||
_channelMembers[channel] = mutableSetOf()
|
||||
}
|
||||
_channelMembers[channel]?.add(peerID)
|
||||
}
|
||||
|
||||
fun removeChannelMember(channel: String, peerID: String) {
|
||||
_channelMembers[channel]?.remove(peerID)
|
||||
}
|
||||
|
||||
fun removeChannelMembers(channel: String) {
|
||||
_channelMembers.remove(channel)
|
||||
}
|
||||
|
||||
fun cleanupDisconnectedMembers(channel: String, connectedPeers: List<String>, myPeerID: String) {
|
||||
_channelMembers[channel]?.removeAll { memberID ->
|
||||
memberID != myPeerID && !connectedPeers.contains(memberID)
|
||||
}
|
||||
}
|
||||
|
||||
fun cleanupAllDisconnectedMembers(connectedPeers: List<String>, myPeerID: String) {
|
||||
_channelMembers.values.forEach { members ->
|
||||
members.removeAll { memberID ->
|
||||
memberID != myPeerID && !connectedPeers.contains(memberID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Favorites Management
|
||||
|
||||
fun loadFavorites() {
|
||||
val savedFavorites = prefs.getStringSet("favorites", emptySet()) ?: emptySet()
|
||||
_favoritePeers.addAll(savedFavorites)
|
||||
}
|
||||
|
||||
fun saveFavorites() {
|
||||
prefs.edit().putStringSet("favorites", _favoritePeers).apply()
|
||||
}
|
||||
|
||||
fun addFavorite(fingerprint: String) {
|
||||
_favoritePeers.add(fingerprint)
|
||||
saveFavorites()
|
||||
}
|
||||
|
||||
fun removeFavorite(fingerprint: String) {
|
||||
_favoritePeers.remove(fingerprint)
|
||||
saveFavorites()
|
||||
}
|
||||
|
||||
fun isFavorite(fingerprint: String): Boolean {
|
||||
return _favoritePeers.contains(fingerprint)
|
||||
}
|
||||
|
||||
// MARK: - Blocked Users Management
|
||||
|
||||
fun loadBlockedUsers() {
|
||||
val savedBlockedUsers = prefs.getStringSet("blocked_users", emptySet()) ?: emptySet()
|
||||
_blockedUsers.addAll(savedBlockedUsers)
|
||||
}
|
||||
|
||||
fun saveBlockedUsers() {
|
||||
prefs.edit().putStringSet("blocked_users", _blockedUsers).apply()
|
||||
}
|
||||
|
||||
fun addBlockedUser(fingerprint: String) {
|
||||
_blockedUsers.add(fingerprint)
|
||||
saveBlockedUsers()
|
||||
}
|
||||
|
||||
fun removeBlockedUser(fingerprint: String) {
|
||||
_blockedUsers.remove(fingerprint)
|
||||
saveBlockedUsers()
|
||||
}
|
||||
|
||||
fun isUserBlocked(fingerprint: String): Boolean {
|
||||
return _blockedUsers.contains(fingerprint)
|
||||
}
|
||||
|
||||
// MARK: - Emergency Clear
|
||||
|
||||
fun clearAllData() {
|
||||
_channelCreators.clear()
|
||||
_favoritePeers.clear()
|
||||
_blockedUsers.clear()
|
||||
_channelMembers.clear()
|
||||
prefs.edit().clear().apply()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user