@@ -71,7 +71,7 @@ data class RoomUiState(
|
||||
val isGroup: Boolean = false
|
||||
)
|
||||
|
||||
fun Room.flow(
|
||||
fun Room.uiStateFlow(
|
||||
profileCache: ProfileCache,
|
||||
currentUser: PublicKey? = null
|
||||
): Flow<RoomUiState> {
|
||||
|
||||
@@ -7,40 +7,30 @@ import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import rust.nostr.sdk.EventId
|
||||
import rust.nostr.sdk.UnsignedEvent
|
||||
import su.reya.coop.Profile
|
||||
import su.reya.coop.Room
|
||||
import su.reya.coop.repository.AccountRepository
|
||||
import su.reya.coop.repository.ChatRepository
|
||||
import su.reya.coop.roomId
|
||||
|
||||
class ChatScreenViewModel(
|
||||
initialRoom: Room,
|
||||
val id: Long,
|
||||
screening: Boolean,
|
||||
accountRepository: AccountRepository,
|
||||
private val chatRepository: ChatRepository,
|
||||
) : ViewModel(), ErrorHost by chatRepository {
|
||||
val currentUser: StateFlow<Profile?> = accountRepository.currentUserProfile
|
||||
val chatRooms: StateFlow<List<Room>> = chatRepository.chatRooms
|
||||
|
||||
var loading by mutableStateOf(true)
|
||||
var newOtherMessages by mutableIntStateOf(0)
|
||||
var requireScreening by mutableStateOf(screening)
|
||||
val messages = mutableStateListOf<UnsignedEvent>()
|
||||
|
||||
val currentUser = accountRepository.currentUserProfile
|
||||
val id = initialRoom.id
|
||||
|
||||
val room: StateFlow<Room> = chatRepository.chatRooms
|
||||
.map { rooms -> rooms.find { it.id == id } ?: initialRoom }
|
||||
.stateIn(
|
||||
scope = viewModelScope,
|
||||
started = SharingStarted.WhileSubscribed(5000),
|
||||
initialValue = initialRoom
|
||||
)
|
||||
|
||||
init {
|
||||
loadMessages()
|
||||
connect()
|
||||
|
||||
Reference in New Issue
Block a user