Consolidate about sheet state into a single object.

This commit is contained in:
jeffrey-signal
2026-02-19 11:24:09 -05:00
committed by Cody Henthorne
parent 28c37cb3ac
commit 9c29601b55
2 changed files with 59 additions and 65 deletions

View File

@@ -40,6 +40,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.os.bundleOf
import androidx.core.widget.TextViewCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.Navigation
import org.signal.core.ui.compose.BottomSheets
import org.signal.core.ui.compose.ComposeBottomSheetDialogFragment
@@ -96,43 +97,38 @@ class AboutSheet : ComposeBottomSheetDialogFragment() {
@Composable
override fun SheetContent() {
val recipient by viewModel.recipient
val groupsInCommonCount by viewModel.groupsInCommonCount
val verified by viewModel.verified
val memberLabel by viewModel.memberLabel
val canEditMemberLabel by viewModel.canEditMemberLabel
val state by viewModel.state.collectAsStateWithLifecycle()
val recipient = state.recipient ?: return
if (recipient.isPresent) {
Content(
model = AboutModel(
isSelf = recipient.get().isSelf,
displayName = recipient.get().getDisplayName(requireContext()),
shortName = recipient.get().getShortDisplayName(requireContext()),
profileName = recipient.get().profileName.toString(),
about = recipient.get().about,
verified = verified,
hasAvatar = recipient.get().profileAvatarFileDetails.hasFile(),
recipientForAvatar = recipient.get(),
formattedE164 = if (recipient.get().hasE164 && recipient.get().shouldShowE164) {
SignalE164Util.prettyPrint(recipient.get().requireE164())
} else {
null
},
profileSharing = recipient.get().isProfileSharing,
systemContact = recipient.get().isSystemContact,
groupsInCommon = groupsInCommonCount,
note = recipient.get().note ?: "",
memberLabel = memberLabel,
canEditMemberLabel = canEditMemberLabel
),
onClickSignalConnections = this::openSignalConnectionsSheet,
onAvatarClicked = this::openProfilePhotoViewer,
onNoteClicked = this::openNoteSheet,
onUnverifiedProfileClicked = this::openUnverifiedProfileSheet,
onGroupsInCommonClicked = this::openGroupsInCommon,
onMemberLabelClicked = this::openMemberLabelScreen
)
}
Content(
model = AboutModel(
isSelf = recipient.isSelf,
displayName = recipient.getDisplayName(requireContext()),
shortName = recipient.getShortDisplayName(requireContext()),
profileName = recipient.profileName.toString(),
about = recipient.about,
verified = state.verified,
hasAvatar = recipient.profileAvatarFileDetails.hasFile(),
recipientForAvatar = recipient,
formattedE164 = if (recipient.hasE164 && recipient.shouldShowE164) {
SignalE164Util.prettyPrint(recipient.requireE164())
} else {
null
},
profileSharing = recipient.isProfileSharing,
systemContact = recipient.isSystemContact,
groupsInCommon = state.groupsInCommonCount,
note = recipient.note ?: "",
memberLabel = state.memberLabel,
canEditMemberLabel = state.canEditMemberLabel
),
onClickSignalConnections = this::openSignalConnectionsSheet,
onAvatarClicked = this::openProfilePhotoViewer,
onNoteClicked = this::openNoteSheet,
onUnverifiedProfileClicked = this::openUnverifiedProfileSheet,
onGroupsInCommonClicked = this::openGroupsInCommon,
onMemberLabelClicked = this::openMemberLabelScreen
)
}
private fun openSignalConnectionsSheet() {

View File

@@ -5,23 +5,21 @@
package org.thoughtcrime.securesms.recipients.ui.about
import androidx.compose.runtime.IntState
import androidx.compose.runtime.MutableIntState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.kotlin.subscribeBy
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import org.signal.core.util.orNull
import org.thoughtcrime.securesms.groups.GroupId
import org.thoughtcrime.securesms.groups.memberlabel.MemberLabel
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.recipients.RecipientId
import org.thoughtcrime.securesms.util.RemoteConfig
import java.util.Optional
class AboutSheetViewModel(
recipientId: RecipientId,
@@ -29,42 +27,30 @@ class AboutSheetViewModel(
private val repository: AboutSheetRepository = AboutSheetRepository()
) : ViewModel() {
private val _recipient: MutableState<Optional<Recipient>> = mutableStateOf(Optional.empty())
val recipient: State<Optional<Recipient>> = _recipient
private val _groupsInCommonCount: MutableIntState = mutableIntStateOf(0)
val groupsInCommonCount: IntState = _groupsInCommonCount
private val _verified: MutableState<Boolean> = mutableStateOf(false)
val verified: State<Boolean> = _verified
private val _memberLabel: MutableState<MemberLabel?> = mutableStateOf(null)
val memberLabel: State<MemberLabel?> = _memberLabel
private val _canEditMemberLabel: MutableState<Boolean> = mutableStateOf(false)
val canEditMemberLabel: State<Boolean> = _canEditMemberLabel
private val internalState: MutableStateFlow<AboutSheetUiState> = MutableStateFlow(AboutSheetUiState())
val state: StateFlow<AboutSheetUiState> = internalState.asStateFlow()
private val disposables = CompositeDisposable()
private val recipientDisposable: Disposable = Recipient
.observable(recipientId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy {
_recipient.value = Optional.of(it)
.subscribeBy { recipient ->
internalState.update { it.copy(recipient = recipient) }
}
private val groupsInCommonDisposable: Disposable = repository
.getGroupsInCommonCount(recipientId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy {
_groupsInCommonCount.intValue = it
.subscribeBy { groupsInCommon ->
internalState.update { it.copy(groupsInCommonCount = groupsInCommon) }
}
private val verifiedDisposable: Disposable = repository
.getVerified(recipientId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy {
_verified.value = it
.subscribeBy { verified ->
internalState.update { it.copy(verified = verified) }
}
init {
@@ -79,13 +65,17 @@ class AboutSheetViewModel(
disposables.add(
repository.getMemberLabel(groupId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy { _memberLabel.value = it.orElse(null) }
.subscribeBy { memberLabel ->
internalState.update { it.copy(memberLabel = memberLabel.orNull()) }
}
)
disposables.add(
repository.canEditMemberLabel(groupId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy { _canEditMemberLabel.value = it }
.subscribeBy { canEditMemberLabel ->
internalState.update { it.copy(canEditMemberLabel = canEditMemberLabel) }
}
)
}
@@ -93,3 +83,11 @@ class AboutSheetViewModel(
disposables.dispose()
}
}
data class AboutSheetUiState(
val recipient: Recipient? = null,
val groupsInCommonCount: Int = 0,
val verified: Boolean = false,
val memberLabel: MemberLabel? = null,
val canEditMemberLabel: Boolean = false
)