From 2f9dbe27c8d144c0a610797efccd039eb110ce38 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sat, 15 Jun 2024 02:03:27 +0930 Subject: [PATCH] Add PreviewNewConversationScreen --- .../start/NewConversationFragment.kt | 2 +- .../start/NewConversationHomeFragment.kt | 104 ++++++++++++------ 2 files changed, 73 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt index 939fd4a177..6605288345 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt @@ -37,7 +37,7 @@ class NewConversationFragment : BottomSheetDialogFragment(), NewConversationDele override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) replaceFragment( - fragment = NewConversationHomeFragment().also { it.delegate = this }, + fragment = NewConversationHomeFragment().also { it.delegate.value = this }, fragmentKey = NewConversationHomeFragment::class.java.simpleName ) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationHomeFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationHomeFragment.kt index 529affaeed..c0371e2155 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationHomeFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationHomeFragment.kt @@ -11,16 +11,23 @@ import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.material.Text import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.fragment.app.Fragment import dagger.hilt.android.AndroidEntryPoint +import kotlinx.coroutines.flow.MutableStateFlow import network.loki.messenger.R import org.session.libsession.utilities.TextSecurePreferences +import org.thoughtcrime.securesms.ui.Colors import org.thoughtcrime.securesms.ui.Divider import org.thoughtcrime.securesms.ui.ItemButton import org.thoughtcrime.securesms.ui.LocalColors import org.thoughtcrime.securesms.ui.LocalDimensions +import org.thoughtcrime.securesms.ui.PreviewTheme +import org.thoughtcrime.securesms.ui.SessionColorsParameterProvider import org.thoughtcrime.securesms.ui.components.AppBar import org.thoughtcrime.securesms.ui.components.QrImage import org.thoughtcrime.securesms.ui.contentDescription @@ -35,43 +42,76 @@ class NewConversationHomeFragment : Fragment() { @Inject lateinit var textSecurePreferences: TextSecurePreferences - lateinit var delegate: NewConversationDelegate + var delegate = MutableStateFlow(null) override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View = createThemedComposeView { NewConversationScreen() } - - @Composable - fun NewConversationScreen() { - Column(modifier = Modifier.background(LocalColors.current.backgroundSecondary)) { - AppBar(stringResource(R.string.dialog_new_conversation_title), onClose = { delegate.onDialogClosePressed() }) - ItemButton(textId = R.string.messageNew, icon = R.drawable.ic_message) { delegate.onNewMessageSelected() } - Divider(startIndent = LocalDimensions.current.dividerIndent) - ItemButton(textId = R.string.activity_create_group_title, icon = R.drawable.ic_group) { delegate.onCreateGroupSelected() } - Divider(startIndent = LocalDimensions.current.dividerIndent) - ItemButton(textId = R.string.dialog_join_community_title, icon = R.drawable.ic_globe) { delegate.onJoinCommunitySelected() } - Divider(startIndent = LocalDimensions.current.dividerIndent) - ItemButton(textId = R.string.activity_settings_invite_button_title, icon = R.drawable.ic_invite_friend, contentDescription = R.string.AccessibilityId_invite_friend_button) { delegate.onInviteFriend() } - Column( - modifier = Modifier - .padding(horizontal = LocalDimensions.current.marginMedium) - .padding(top = LocalDimensions.current.itemSpacingMedium) - ) { - Text( - text = stringResource(R.string.accountIdYours), - style = xl - ) - Spacer(modifier = Modifier.height(LocalDimensions.current.itemSpacingTiny)) - Text( - text = stringResource(R.string.qrYoursDescription), - color = LocalColors.current.textSecondary, - style = small - ) - Spacer(modifier = Modifier.height(LocalDimensions.current.itemSpacingSmall)) - QrImage(string = TextSecurePreferences.getLocalNumber(requireContext())!!, Modifier.contentDescription(R.string.AccessibilityId_qr_code)) - } + ): View = createThemedComposeView { + // Warning, brittle code, NewConversationScreen will not be visible if no delegate is set + // before onCreateView is called. + delegate.collectAsState().value?.let { + NewConversationScreen( + accountId = TextSecurePreferences.getLocalNumber(requireContext())!!, + delegate = it + ) + } + } +} + +@Preview +@Composable +fun PreviewNewConversationScreen( + @PreviewParameter(SessionColorsParameterProvider::class) colors: Colors +) { + PreviewTheme(colors) { + NewConversationScreen( + accountId = "059287129387123", + object: NewConversationDelegate { + override fun onNewMessageSelected() {} + override fun onCreateGroupSelected() {} + override fun onJoinCommunitySelected() {} + override fun onContactSelected(address: String) {} + override fun onDialogBackPressed() {} + override fun onDialogClosePressed() {} + override fun onInviteFriend() {} + } + ) + } +} + +@Composable +fun NewConversationScreen( + accountId: String, + delegate: NewConversationDelegate +) { + Column(modifier = Modifier.background(LocalColors.current.backgroundSecondary)) { + AppBar(stringResource(R.string.dialog_new_conversation_title), onClose = delegate::onDialogClosePressed) + ItemButton(textId = R.string.messageNew, icon = R.drawable.ic_message, onClick = delegate::onNewMessageSelected) + Divider(startIndent = LocalDimensions.current.dividerIndent) + ItemButton(textId = R.string.activity_create_group_title, icon = R.drawable.ic_group, onClick = delegate::onCreateGroupSelected) + Divider(startIndent = LocalDimensions.current.dividerIndent) + ItemButton(textId = R.string.dialog_join_community_title, icon = R.drawable.ic_globe, onClick = delegate::onJoinCommunitySelected) + Divider(startIndent = LocalDimensions.current.dividerIndent) + ItemButton(textId = R.string.activity_settings_invite_button_title, icon = R.drawable.ic_invite_friend, contentDescription = R.string.AccessibilityId_invite_friend_button, onClick = delegate::onInviteFriend) + Column( + modifier = Modifier + .padding(horizontal = LocalDimensions.current.marginMedium) + .padding(top = LocalDimensions.current.itemSpacingMedium) + ) { + Text( + text = stringResource(R.string.accountIdYours), + style = xl + ) + Spacer(modifier = Modifier.height(LocalDimensions.current.itemSpacingTiny)) + Text( + text = stringResource(R.string.qrYoursDescription), + color = LocalColors.current.textSecondary, + style = small + ) + Spacer(modifier = Modifier.height(LocalDimensions.current.itemSpacingSmall)) + QrImage(string = accountId, Modifier.contentDescription(R.string.AccessibilityId_qr_code)) } } }