Refresh chat room info when state is going to Created, prevents chatroom-xxxx display

This commit is contained in:
Sylvain Berfini 2023-11-27 12:03:55 +01:00
parent 0bbab221a2
commit 39a6254ee1
6 changed files with 65 additions and 47 deletions

View file

@ -489,7 +489,7 @@ class ConversationFragment : GenericFragment() {
}
}
sharedViewModel.forceRefreshConversationEvent.observe(viewLifecycleOwner) {
sharedViewModel.forceRefreshConversationEvents.observe(viewLifecycleOwner) {
it.consume {
Log.i("$TAG Force refreshing chat messages list")
viewModel.applyFilter("")

View file

@ -144,7 +144,7 @@ class ConversationInfoFragment : GenericFragment() {
viewModel.historyDeletedEvent.observe(viewLifecycleOwner) {
it.consume {
Log.i("$TAG History has been deleted, leaving conversation info...")
sharedViewModel.forceRefreshConversationEvent.value = Event(true)
sharedViewModel.forceRefreshConversationEvents.value = Event(true)
goBack()
val message = getString(R.string.toast_conversation_history_deleted)
(requireActivity() as MainActivity).showGreenToast(

View file

@ -83,6 +83,15 @@ class ConversationModel @WorkerThread constructor(val chatRoom: ChatRoom) {
private var lastMessage: ChatMessage? = null
private val chatRoomListener = object : ChatRoomListenerStub() {
@WorkerThread
override fun onStateChanged(chatRoom: ChatRoom, newState: ChatRoom.State?) {
Log.i("$TAG Chat room state changed [${chatRoom.state}]")
if (chatRoom.state == ChatRoom.State.Created) {
subject.postValue(chatRoom.subject)
computeParticipants()
}
}
@WorkerThread
override fun onIsComposingReceived(
chatRoom: ChatRoom,
@ -112,6 +121,7 @@ class ConversationModel @WorkerThread constructor(val chatRoom: ChatRoom) {
@WorkerThread
override fun onSubjectChanged(chatRoom: ChatRoom, eventLog: EventLog) {
Log.i("$TAG Chat room subject changed [${chatRoom.subject}]")
subject.postValue(chatRoom.subject)
updateLastUpdatedTime()
}
@ -139,45 +149,9 @@ class ConversationModel @WorkerThread constructor(val chatRoom: ChatRoom) {
init {
chatRoom.addListener(chatRoomListener)
subject.postValue(chatRoom.subject)
lastUpdateTime.postValue(chatRoom.lastUpdateTime)
computeComposingLabel()
val friends = arrayListOf<Friend>()
val address = if (chatRoom.hasCapability(Capabilities.Basic.toInt())) {
Log.i("$TAG Chat room [$id] is 'Basic'")
chatRoom.peerAddress
} else {
val firstParticipant = chatRoom.participants.firstOrNull()
if (isGroup) {
Log.i("$TAG Group chat room [$id] has [${chatRoom.nbParticipants}] participant(s)")
for (participant in chatRoom.participants) {
val friend = coreContext.contactsManager.findContactByAddress(
participant.address
)
if (friend != null && !friends.contains(friend)) {
friends.add(friend)
}
}
} else {
Log.i(
"$TAG Chat room [$id] is with participant [${firstParticipant?.address?.asStringUriOnly()}]"
)
}
firstParticipant?.address ?: chatRoom.peerAddress
}
if (isGroup) {
val fakeFriend = coreContext.core.createFriend()
val model = ContactAvatarModel(fakeFriend)
model.defaultToConversationIcon.postValue(true)
model.setPicturesFromFriends(friends)
avatarModel.postValue(model)
} else {
avatarModel.postValue(
coreContext.contactsManager.getContactAvatarModelForAddress(address)
)
}
subject.postValue(chatRoom.subject)
computeParticipants()
isMuted.postValue(chatRoom.muted)
isEphemeral.postValue(chatRoom.isEphemeralEnabled)
@ -308,6 +282,46 @@ class ConversationModel @WorkerThread constructor(val chatRoom: ChatRoom) {
}
}
dateTime.postValue(humanReadableTimestamp)
lastUpdateTime.postValue(timestamp)
}
@WorkerThread
private fun computeParticipants() {
val friends = arrayListOf<Friend>()
val address = if (chatRoom.hasCapability(Capabilities.Basic.toInt())) {
Log.i("$TAG Chat room [$id] is 'Basic'")
chatRoom.peerAddress
} else {
val firstParticipant = chatRoom.participants.firstOrNull()
if (isGroup) {
Log.i("$TAG Group chat room [$id] has [${chatRoom.nbParticipants}] participant(s)")
for (participant in chatRoom.participants) {
val friend = coreContext.contactsManager.findContactByAddress(
participant.address
)
if (friend != null && !friends.contains(friend)) {
friends.add(friend)
}
}
} else {
Log.i(
"$TAG Chat room [$id] is with participant [${firstParticipant?.address?.asStringUriOnly()}]"
)
}
firstParticipant?.address ?: chatRoom.peerAddress
}
if (isGroup) {
val fakeFriend = coreContext.core.createFriend()
val model = ContactAvatarModel(fakeFriend)
model.defaultToConversationIcon.postValue(true)
model.setPicturesFromFriends(friends)
avatarModel.postValue(model)
} else {
avatarModel.postValue(
coreContext.contactsManager.getContactAvatarModelForAddress(address)
)
}
}
@WorkerThread

View file

@ -67,10 +67,6 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
var scrollingPosition: Int = SCROLLING_POSITION_NOT_SET
val requestKeyboardHidingEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}
val focusSearchBarEvent: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}
@ -92,6 +88,14 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
lateinit var chatRoom: ChatRoom
private val chatRoomListener = object : ChatRoomListenerStub() {
@WorkerThread
override fun onStateChanged(chatRoom: ChatRoom, newState: ChatRoom.State?) {
Log.i("$TAG Chat room state changed [${chatRoom.state}]")
if (chatRoom.state == ChatRoom.State.Created) {
computeConversationInfo()
}
}
@WorkerThread
override fun onChatMessageSending(chatRoom: ChatRoom, eventLog: EventLog) {
val message = eventLog.chatMessage
@ -144,7 +148,6 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
@WorkerThread
override fun onChatMessagesReceived(chatRoom: ChatRoom, eventLogs: Array<EventLog>) {
Log.i("$TAG Received [${eventLogs.size}] new message(s)")
chatRoom.markAsRead()
computeComposingLabel()
val list = arrayListOf<EventLogModel>()
@ -176,6 +179,7 @@ class ConversationViewModel @UiThread constructor() : ViewModel() {
}
events.postValue(list)
chatRoom.markAsRead()
}
@WorkerThread

View file

@ -63,7 +63,7 @@ class ConversationsListViewModel @UiThread constructor() : AbstractTopBarViewMod
)
when (state) {
ChatRoom.State.Created, ChatRoom.State.Instantiated -> {
ChatRoom.State.Created -> {
computeChatRoomsList(currentFilter)
}
ChatRoom.State.Deleted -> {

View file

@ -114,7 +114,7 @@ class SharedMainViewModel @UiThread constructor() : ViewModel() {
MutableLiveData<Event<Boolean>>()
}
val forceRefreshConversationEvent: MutableLiveData<Event<Boolean>> by lazy {
val forceRefreshConversationEvents: MutableLiveData<Event<Boolean>> by lazy {
MutableLiveData<Event<Boolean>>()
}