Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #13331: Make toggle button update lifecycle-aware #13339

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -62,6 +62,8 @@ import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.ConversationLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -76,6 +78,7 @@ import io.reactivex.rxjava3.core.Single
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.kotlin.subscribeBy
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
Expand Down Expand Up @@ -440,6 +443,10 @@ class ConversationFragment :

private val shareDataTimestampViewModel: ShareDataTimestampViewModel by activityViewModels()

private val toggleUpdateViewModel: ToggleUpdateViewModel by viewModel {
ToggleUpdateViewModel()
}

private val inlineQueryController: InlineQueryResultsControllerV2 by lazy {
InlineQueryResultsControllerV2(
this,
Expand Down Expand Up @@ -591,6 +598,14 @@ class ConversationFragment :
inputPanel.setMediaListener(InputPanelMediaListener())

ChatColorsDrawable.attach(binding.conversationItemRecycler)

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
toggleUpdateViewModel.state.collect {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My knowledge of the Flow apis is quite limited, my understanding is that this will be subscribed between onStart / onStop and the subscription is managed automatically?

updateToggleButtonState()
}
}
}
}

override fun onViewStateRestored(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -1010,7 +1025,7 @@ class ConversationFragment :
.distinctUntilChanged { previous, next -> previous.voiceNoteDraft == next.voiceNoteDraft }
.subscribe {
inputPanel.voiceNoteDraft = it.voiceNoteDraft
updateToggleButtonState()
toggleUpdateViewModel.update()
}
)

Expand Down Expand Up @@ -1636,7 +1651,7 @@ class ConversationFragment :
inputPanel.setLinkPreview(GlideApp.with(this), state.linkPreview)
}

updateToggleButtonState()
toggleUpdateViewModel.update()
}
.addTo(disposables)
}
Expand Down Expand Up @@ -3710,7 +3725,7 @@ class ConversationFragment :
if (composeText.textTrimmed.isEmpty() || beforeLength == 0) {
composeText.postDelayed({
if (lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
updateToggleButtonState()
toggleUpdateViewModel.update()
}
}, 50)
}
Expand Down Expand Up @@ -3798,18 +3813,18 @@ class ConversationFragment :
}

override fun onRecorderLocked() {
updateToggleButtonState()
toggleUpdateViewModel.update()
voiceMessageRecordingDelegate.onRecorderLocked()
}

override fun onRecorderFinished() {
updateToggleButtonState()
toggleUpdateViewModel.update()
voiceMessageRecordingDelegate.onRecorderFinished()
}

override fun onRecorderCanceled(byUser: Boolean) {
if (lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
updateToggleButtonState()
toggleUpdateViewModel.update()
}
voiceMessageRecordingDelegate.onRecorderCanceled(byUser)
}
Expand Down Expand Up @@ -3848,7 +3863,7 @@ class ConversationFragment :
}

override fun onEnterEditMode() {
updateToggleButtonState()
toggleUpdateViewModel.update()
previousPages = keyboardPagerViewModel.pages().value
keyboardPagerViewModel.setOnlyPage(KeyboardPage.EMOJI)
onKeyboardChanged(KeyboardPage.EMOJI)
Expand All @@ -3857,7 +3872,7 @@ class ConversationFragment :
}

override fun onExitEditMode() {
updateToggleButtonState()
toggleUpdateViewModel.update()
draftViewModel.deleteMessageEditDraft()
if (previousPages != null) {
keyboardPagerViewModel.setPages(previousPages!!)
Expand Down Expand Up @@ -3915,7 +3930,7 @@ class ConversationFragment :

private inner class AttachmentManagerListener : AttachmentManager.AttachmentListener {
override fun onAttachmentChanged() {
updateToggleButtonState()
toggleUpdateViewModel.update()
updateLinkPreviewState()
}

Expand Down
@@ -0,0 +1,22 @@
/*
* Copyright 2023 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/

package org.thoughtcrime.securesms.conversation.v2

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch

class ToggleUpdateViewModel : ViewModel() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some documentation to this class describing what it's for? It's fairly unclear just looking. And I think perhaps ToggleUpdate* isn't the best name. Feels like it should have a better descriptor there (though admittedly, I realize this was probably just pulled from the original method name)


private val _state = MutableSharedFlow<Unit>(replay = 1)
val state = _state.asSharedFlow()

fun update() = viewModelScope.launch {
_state.emit(Unit)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's not really any state here... is this just a pulse? How is this different from just calling toggle update fn from the appropriate lifecycle method in the fragment? (I've yet to have coffee this morning so appologies if I missed something obvious.)

}
}