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: Correctly handle setting / editing a status' language #780

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -413,6 +413,13 @@ class ComposeActivity :
)
binding.composeEditField.setTokenizer(ComposeTokenizer())

val mentionColour = binding.composeEditField.linkTextColors.defaultColor
highlightSpans(binding.composeEditField.text, mentionColour)
binding.composeEditField.doAfterTextChanged { editable ->
highlightSpans(editable!!, mentionColour)
viewModel.onContentChanged(editable)
}

binding.composeEditField.setText(startingText)

when (composeOptions?.initialCursorPosition ?: InitialCursorPosition.END) {
Expand All @@ -422,13 +429,6 @@ class ComposeActivity :
)
}

val mentionColour = binding.composeEditField.linkTextColors.defaultColor
highlightSpans(binding.composeEditField.text, mentionColour)
binding.composeEditField.doAfterTextChanged { editable ->
highlightSpans(editable!!, mentionColour)
viewModel.onContentChanged(editable)
}

// work around Android platform bug -> https://issuetracker.google.com/issues/67102093
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O ||
Build.VERSION.SDK_INT == Build.VERSION_CODES.O_MR1
Expand Down Expand Up @@ -594,7 +594,7 @@ class ComposeActivity :
private fun setupLanguageSpinner(initialLanguages: List<String>) {
binding.composePostLanguageButton.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
viewModel.postLanguage = (parent.adapter.getItem(position) as Locale).modernLanguageCode
viewModel.onLanguageChanged((parent.adapter.getItem(position) as Locale).modernLanguageCode)
}

override fun onNothingSelected(parent: AdapterView<*>) {
Expand Down Expand Up @@ -927,7 +927,7 @@ class ComposeActivity :

@VisibleForTesting
val selectedLanguage: String?
get() = viewModel.postLanguage
get() = viewModel.language

private fun updateVisibleCharactersLeft(textLength: Int) {
val remainingLength = maximumTootCharacters - textLength
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ class ComposeViewModel @Inject constructor(
/** The initial content warning for this status, before any edits */
private var initialContentWarning: String = ""

internal var postLanguage: String? = null
/** The initial language for this status, before any changes */
private var initialLanguage: String? = null

/** The current language for this status. */
internal var language: String? = null

/** If editing a draft then the ID of the draft, otherwise 0 */
private var draftId: Int = 0
Expand Down Expand Up @@ -279,6 +283,12 @@ class ComposeViewModel @Inject constructor(
_statusVisibility.value = newVisibility
}

/** Call this to change the status' language */
fun onLanguageChanged(newLanguage: String) {
language = newLanguage
updateCloseConfirmation()
}

@VisibleForTesting
fun updateStatusLength() {
_statusLength.value = statusLength(content, effectiveContentWarning, instanceInfo.value.charactersReservedPerUrl)
Expand Down Expand Up @@ -317,8 +327,9 @@ class ComposeViewModel @Inject constructor(
val contentWarningChanged = effectiveContentWarning != initialContentWarning
val mediaChanged = media.value.isNotEmpty()
val pollChanged = poll.value != null
val languageChanged = initialLanguage != language

return modifiedInitialState || contentChanged || contentWarningChanged || mediaChanged || pollChanged || scheduledTimeChanged
return modifiedInitialState || contentChanged || contentWarningChanged || mediaChanged || pollChanged || languageChanged || scheduledTimeChanged
}

private fun isEmpty(content: CharSequence, contentWarning: CharSequence): Boolean {
Expand Down Expand Up @@ -375,7 +386,7 @@ class ComposeViewModel @Inject constructor(
failedToSend = false,
failedToSendAlert = false,
scheduledAt = scheduledAt.value,
language = postLanguage,
language = language,
statusId = originalStatusId,
)
}
Expand Down Expand Up @@ -418,7 +429,7 @@ class ComposeViewModel @Inject constructor(
draftId = draftId,
idempotencyKey = randomAlphanumericString(16),
retries = 0,
language = postLanguage,
language = language,
statusId = originalStatusId,
)

Expand Down Expand Up @@ -538,7 +549,8 @@ class ComposeViewModel @Inject constructor(
scheduledTootId = composeOptions?.scheduledTootId
originalStatusId = composeOptions?.statusId
initialContent = composeOptions?.content ?: ""
postLanguage = composeOptions?.language
initialLanguage = composeOptions?.language
language = initialLanguage

val tootVisibility = composeOptions?.visibility ?: Status.Visibility.UNKNOWN
if (tootVisibility != Status.Visibility.UNKNOWN) {
Expand Down