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

fix: Design review fixes #52

Merged
merged 4 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,21 @@ class ManageContactViewModel @Inject constructor(
EDIT_CONTACT -> ManageContactTexts.EditContact()
else -> throw IllegalStateException("Unknown screen type: $screenType")
},
isActionButtonEnabled = screenType == EDIT_CONTACT,
),
)
val uiState: StateFlow<PairWithOthersUiState>
get() = _uiState

private val checkActionButtonAvailabilityFlow = MutableSharedFlow<String>()

private val _onActionCompleted = MutableSharedFlow<String>()
val onActionCompleted: SharedFlow<String>
get() = _onActionCompleted
private val _onEditContactCompleted = MutableSharedFlow<String>()
val onEditContactCompleted: SharedFlow<String>
get() = _onEditContactCompleted

private val _goBackSignal = MutableSharedFlow<Unit>()
val goBackSignal: SharedFlow<Unit>
get() = _goBackSignal

private val contacts: HashSet<Contact> = hashSetOf()

Expand Down Expand Up @@ -115,14 +120,32 @@ class ManageContactViewModel @Inject constructor(
}
}

fun onActionButtonClick() {
fun onUpdateContactButtonClick() {
when (screenType) {
NEW_CONTACT -> sendNewContactRequest()
EDIT_CONTACT -> updateContact()
NEW_CONTACT -> {
sendNewContactRequest()
viewModelScope.launch {
_uiState.update {
it.copy(
showRequestSentScreen = true,
)
}
}
}
EDIT_CONTACT -> {
updateContact()
viewModelScope.launch {
_onEditContactCompleted.emit(uiState.value.veraId)
}
}
else -> throw IllegalStateException("Unknown screen type: $screenType")
}
}

fun onGotItClick() {
contactsRepository.saveRequestWasOnceSent()
viewModelScope.launch {
_onActionCompleted.emit(uiState.value.veraId)
_goBackSignal.emit(Unit)
}
}

Expand Down Expand Up @@ -195,6 +218,7 @@ data class PairWithOthersUiState(
val isSentRequestAgainHintVisible: Boolean = false,
val isVeraIdInputEnabled: Boolean = true,
val pairingErrorCaption: PairingErrorCaption? = null,
val showRequestSentScreen: Boolean = false,
)

@Immutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ interface ContactsRepository {
fun addNewContact(contact: Contact)
fun deleteContact(contact: Contact)
fun updateContact(contact: Contact)
fun saveRequestWasOnceSent()
}

class ContactsRepositoryImpl @Inject constructor(
Expand Down Expand Up @@ -74,7 +75,6 @@ class ContactsRepositoryImpl @Inject constructor(
)

if (existingContact == null || existingContact.status <= ContactPairingStatus.REQUEST_SENT) {
preferences.putBoolean(getContactRequestHasEverBeenSentKey(contact.ownerVeraId), true)
if (existingContact == null) {
contactsDao.insert(
contact.copy(
Expand Down Expand Up @@ -111,6 +111,14 @@ class ContactsRepositoryImpl @Inject constructor(
}
}

override fun saveRequestWasOnceSent() {
val currentAccount = currentAccount ?: return
scope.launch {
preferences.putBoolean(getContactRequestHasEverBeenSentKey(currentAccount.veraId), true)
updateContactsState(currentAccount)
}
}

private fun startCollectAccountFlow() {
scope.launch {
accountRepository.currentAccount.collect {
Expand All @@ -136,14 +144,13 @@ class ContactsRepositoryImpl @Inject constructor(
ContactsState(
isPairedContactExist = isPairedContactExist,
isPairRequestWasEverSent = isPairRequestWasEverSent,
)

),
)
}

private fun getContactRequestHasEverBeenSentKey(
veraId: String
) = "${KEY_CONTACT_REQUEST_HAS_EVER_BEEN_SENT_PREFIX}${veraId}"
veraId: String,
) = "${KEY_CONTACT_REQUEST_HAS_EVER_BEEN_SENT_PREFIX}$veraId"

private companion object {
private const val TAG = "ContactsRepository"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fun ContactsScreen(
Box {
if (editBottomSheet.isShown && editBottomSheet.contact != null) {
ModalBottomSheet(
containerColor = MaterialTheme.colorScheme.surface,
onDismissRequest = { viewModel.onEditBottomSheetDismissed() },
) {
EditContactBottomSheet(
Expand Down Expand Up @@ -147,11 +148,12 @@ private fun EditContactBottomSheet(
onDeleteClick: () -> Unit,
) {
Column(
modifier = Modifier.padding(
PaddingValues(
bottom = 44.dp,
modifier = Modifier
.padding(
PaddingValues(
bottom = 44.dp,
),
),
),
) {
Text(
text = title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import tech.relaycorp.letro.R
import tech.relaycorp.letro.contacts.ManageContactViewModel
import tech.relaycorp.letro.contacts.PairWithOthersUiState
import tech.relaycorp.letro.onboarding.actionTaking.ActionTakingScreen
import tech.relaycorp.letro.onboarding.actionTaking.ActionTakingScreenUIStateModel
import tech.relaycorp.letro.ui.common.LetroButtonMaxWidthFilled
import tech.relaycorp.letro.ui.common.LetroInfoView
import tech.relaycorp.letro.ui.common.LetroOutlinedTextField
Expand All @@ -32,17 +35,47 @@ import tech.relaycorp.letro.ui.theme.LetroTheme
@Composable
fun ManageContactScreen(
onBackClick: () -> Unit,
onActionCompleted: (String) -> Unit,
onEditContactCompleted: (String) -> Unit,
viewModel: ManageContactViewModel = hiltViewModel(),
) {
val uiState by viewModel.uiState.collectAsState()

LaunchedEffect(Unit) {
viewModel.onActionCompleted.collect {
onActionCompleted(it)
viewModel.onEditContactCompleted.collect {
onEditContactCompleted(it)
}
}

LaunchedEffect(Unit) {
viewModel.goBackSignal.collect {
onBackClick()
}
}

if (!uiState.showRequestSentScreen) {
ManageContactView(
onBackClick,
uiState,
viewModel,
)
} else {
ActionTakingScreen(
actionTakingScreenUIStateModel = ActionTakingScreenUIStateModel.PairingRequestSent(
boldPartOfMessage = uiState.veraId,
onGotItClicked = {
viewModel.onGotItClick()
},
),
)
}
}

@Composable
private fun ManageContactView(
onBackClick: () -> Unit,
uiState: PairWithOthersUiState,
viewModel: ManageContactViewModel,
) {
val errorCaption = uiState.pairingErrorCaption

Column(
Expand Down Expand Up @@ -119,7 +152,7 @@ fun ManageContactScreen(
)
LetroButtonMaxWidthFilled(
text = stringResource(id = uiState.manageContactTexts.button),
onClick = { viewModel.onActionButtonClick() },
onClick = { viewModel.onUpdateContactButtonClick() },
isEnabled = uiState.isActionButtonEnabled,
)
}
Expand All @@ -131,7 +164,7 @@ private fun UseExistingAccountPreview() {
LetroTheme {
ManageContactScreen(
onBackClick = {},
onActionCompleted = {},
onEditContactCompleted = {},
viewModel = hiltViewModel(),
)
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/tech/relaycorp/letro/main/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class MainViewModel @Inject constructor(
combine(
accountRepository.currentAccount,
contactsRepository.contactsState,
) { currentAccount, isPairedContactExist ->
Pair(currentAccount, isPairedContactExist)
) { currentAccount, contactsState ->
Pair(currentAccount, contactsState)
}
.distinctUntilChanged()
.onStart { Log.d(TAG, "Start collecting the combined Flow") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import tech.relaycorp.letro.ui.theme.LargeProminent
import tech.relaycorp.letro.ui.theme.LetroColor

@Composable
Expand Down Expand Up @@ -68,7 +69,7 @@ fun LetroButton(
}
Text(
text = text,
style = MaterialTheme.typography.labelLarge,
style = MaterialTheme.typography.LargeProminent,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.text.ParagraphStyle
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.sp

@Composable
fun HyperlinkText(
Expand All @@ -24,34 +28,42 @@ fun HyperlinkText(
fontSize: TextUnit = TextUnit.Unspecified,
) {
val annotatedString = buildAnnotatedString {
append(fullText)
addStyle(
style = SpanStyle(
fontSize = fontSize,
color = textColor,
withStyle(
style = ParagraphStyle(
lineHeight = 18.sp,
),
start = 0,
end = fullText.length,
)
for ((key, value) in hyperLinks) {
val startIndex = fullText.indexOf(key)
val endIndex = startIndex + key.length
) {
append(fullText)
addStyle(
style = SpanStyle(
color = linkTextColor,
fontSize = fontSize,
fontWeight = linkTextFontWeight,
textDecoration = linkTextDecoration,
color = textColor,
letterSpacing = TextUnit(0.2f, TextUnitType.Sp),
),
start = startIndex,
end = endIndex,
)
addStringAnnotation(
tag = "URL",
annotation = value,
start = startIndex,
end = endIndex,
start = 0,
end = fullText.length,
)
for ((key, value) in hyperLinks) {
val startIndex = fullText.indexOf(key)
val endIndex = startIndex + key.length
addStyle(
style = SpanStyle(
color = linkTextColor,
fontSize = fontSize,
fontWeight = linkTextFontWeight,
textDecoration = linkTextDecoration,
letterSpacing = TextUnit(0.2f, TextUnitType.Sp),
),
start = startIndex,
end = endIndex,
)
addStringAnnotation(
tag = "URL",
annotation = value,
start = startIndex,
end = endIndex,
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import tech.relaycorp.letro.ui.theme.LetroColor
import tech.relaycorp.letro.ui.utils.SnackbarStringsProvider
import tech.relaycorp.letro.utils.compose.rememberLifecycleEvent
import tech.relaycorp.letro.utils.ext.encodeToUTF
import tech.relaycorp.letro.utils.navigation.navigateWithDropCurrentScreen
import tech.relaycorp.letro.utils.navigation.navigateWithPoppingAllBackStack

@Composable
Expand Down Expand Up @@ -182,24 +181,6 @@ fun LetroNavHost(
actionTakingScreenUIStateModel = ActionTakingScreenUIStateModel.RegistrationWaiting,
)
}
composable(
route = "${Route.PairingRequestSent.name}/{${Route.PairingRequestSent.RECEIVER_ARGUMENT_VERA_ID}}",
arguments = listOf(
navArgument(Route.PairingRequestSent.RECEIVER_ARGUMENT_VERA_ID) {
type = NavType.StringType
nullable = false
},
),
) {
ActionTakingScreen(
actionTakingScreenUIStateModel = ActionTakingScreenUIStateModel.PairingRequestSent(
boldPartOfMessage = it.arguments?.getString(Route.PairingRequestSent.RECEIVER_ARGUMENT_VERA_ID)!!,
onGotItClicked = {
navController.popBackStack()
},
),
)
}
composable(
route = "${Route.ManageContact.name}/{${Route.ManageContact.KEY_CURRENT_ACCOUNT_ID_ENCODED}}&{${Route.ManageContact.KEY_SCREEN_TYPE}}&{${Route.ManageContact.KEY_CONTACT_ID_TO_EDIT}}",
arguments = listOf(
Expand All @@ -222,15 +203,8 @@ fun LetroNavHost(
onBackClick = {
navController.popBackStack()
},
onActionCompleted = {
onEditContactCompleted = {
when (val type = entry.arguments?.getInt(Route.ManageContact.KEY_SCREEN_TYPE)) {
ManageContactViewModel.Type.NEW_CONTACT -> {
navController.navigateWithDropCurrentScreen(
Route.PairingRequestSent.getRouteName(
receiverVeraId = it,
),
)
}
ManageContactViewModel.Type.EDIT_CONTACT -> {
navController.popBackStack()
scope.launch {
Expand Down
Loading