From 833803359ea814595033ef4987c9041aa4bfc5c8 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 31 Jan 2021 05:24:34 +0300 Subject: [PATCH] drafts: fix applying formatting syntax from drafts, fix icon in settings, small refactoring --- .../components/compose/ComposeActivity.kt | 74 ++++++++++--------- .../components/compose/ComposeViewModel.kt | 8 +- .../tusky/components/drafts/DraftsActivity.kt | 6 +- .../preference/AccountPreferencesFragment.kt | 4 +- .../com/keylesspalace/tusky/db/DraftEntity.kt | 2 +- 5 files changed, 51 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt index ea2f65d9..5539d10a 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt @@ -164,16 +164,24 @@ class ComposeActivity : BaseActivity(), composeMediaPreviewBar.adapter = mediaAdapter composeMediaPreviewBar.itemAnimator = null - subscribeToUpdates(mediaAdapter) + // set before subscribing to updates to not accidentally catch it + viewModel.formattingSyntax.value = activeAccount.defaultFormattingSyntax + + subscribeToUpdates(mediaAdapter, activeAccount) setupButtons() photoUploadUri = savedInstanceState?.getParcelable(PHOTO_UPLOAD_URI_KEY) - viewModel.formattingSyntax = activeAccount.defaultFormattingSyntax /* If the composer is started up as a reply to another post, override the "starting" state * based on what the intent from the reply request passes. */ val composeOptions = intent.getParcelableExtra(COMPOSE_OPTIONS_EXTRA) + + if (!composeOptions?.formattingSyntax.isNullOrEmpty()) { + suggestFormattingSyntax = composeOptions?.formattingSyntax!! + } else { + suggestFormattingSyntax = activeAccount.defaultFormattingSyntax + } viewModel.setup(composeOptions) setupReplyViews(composeOptions?.replyingStatusAuthor, composeOptions?.replyingStatusContent) @@ -181,13 +189,7 @@ class ComposeActivity : BaseActivity(), if (!tootText.isNullOrEmpty()) { composeEditField.setText(tootText) } - - if(viewModel.formattingSyntax.length == 0) { - suggestFormattingSyntax = "text/markdown" - } else { - suggestFormattingSyntax = viewModel.formattingSyntax - } - + if (!composeOptions?.scheduledAt.isNullOrEmpty()) { composeScheduleView.setDateTime(composeOptions?.scheduledAt) } @@ -323,7 +325,7 @@ class ComposeActivity : BaseActivity(), @VisibleForTesting var supportedFormattingSyntax = arrayListOf() - private fun subscribeToUpdates(mediaAdapter: MediaPreviewAdapter) { + private fun subscribeToUpdates(mediaAdapter: MediaPreviewAdapter, activeAccount: AccountEntity) { withLifecycleContext { viewModel.instanceParams.observe { instanceData -> maximumTootCharacters = instanceData.maxChars @@ -346,18 +348,18 @@ class ComposeActivity : BaseActivity(), if(supportedFormattingSyntax.size != 0) { composeFormattingSyntax.visible(true) - val supportsPrefferedSyntax = supportedFormattingSyntax.contains(viewModel.formattingSyntax) + val supportsPrefferedSyntax = supportedFormattingSyntax.contains(viewModel.formattingSyntax.value!!) if(!supportsPrefferedSyntax) { - viewModel.formattingSyntax = "" - - setIconForSyntax(supportedFormattingSyntax[0], false) - } else { - setIconForSyntax(viewModel.formattingSyntax, true) + suggestFormattingSyntax = if(supportedFormattingSyntax.contains(activeAccount.defaultFormattingSyntax)) + activeAccount.defaultFormattingSyntax + else supportedFormattingSyntax[0] + + viewModel.formattingSyntax.value = "" } } - if(instanceData.software.equals("pleroma")) { + if(instanceData.software == "pleroma") { composePreviewButton.visibility = View.VISIBLE reenableAttachments() } @@ -420,6 +422,17 @@ class ComposeActivity : BaseActivity(), // Focus may have changed during view model setup, ensure initial focus is on the edit field composeEditField.requestFocus() } + viewModel.formattingSyntax.observe { + if(it.isEmpty()) { + enableFormattingSyntaxButton(suggestFormattingSyntax, false) + setIconForSyntax(suggestFormattingSyntax, false) + } else { + val enable = it == suggestFormattingSyntax + + enableFormattingSyntaxButton(it, enable) + setIconForSyntax(it, enable) + } + } } } @@ -547,12 +560,10 @@ class ComposeActivity : BaseActivity(), } private fun toggleFormattingMode() { - if(viewModel.formattingSyntax.equals(suggestFormattingSyntax)) { - viewModel.formattingSyntax = "" - enableFormattingSyntaxButton(suggestFormattingSyntax, false) + if(viewModel.formattingSyntax.value!! == suggestFormattingSyntax) { + viewModel.formattingSyntax.value = "" } else { - viewModel.formattingSyntax = suggestFormattingSyntax - enableFormattingSyntaxButton(suggestFormattingSyntax, true) + viewModel.formattingSyntax.value = suggestFormattingSyntax } } @@ -579,13 +590,8 @@ class ComposeActivity : BaseActivity(), htmlId -> "text/html" else -> "" } - if(choose.length == 0) { - // leave previous - setIconForSyntax(viewModel.formattingSyntax, false) - } else { - setIconForSyntax(choose, true) - } - viewModel.formattingSyntax = choose + suggestFormattingSyntax = choose + viewModel.formattingSyntax.value = choose true } menu.show() @@ -652,7 +658,7 @@ class ComposeActivity : BaseActivity(), } private fun codeButtonClicked() { - when(viewModel.formattingSyntax) { + when(viewModel.formattingSyntax.value!!) { "text/markdown" -> MarkdownEdit.addCode(composeEditField) "text/bbcode" -> BBCodeEdit.addCode(composeEditField) "text/html" -> HTMLEdit.addCode(composeEditField) @@ -660,7 +666,7 @@ class ComposeActivity : BaseActivity(), } private fun linkButtonClicked() { - when(viewModel.formattingSyntax) { + when(viewModel.formattingSyntax.value!!) { "text/markdown" -> MarkdownEdit.addLink(composeEditField) "text/bbcode" -> BBCodeEdit.addLink(composeEditField) "text/html" -> HTMLEdit.addLink(composeEditField) @@ -668,7 +674,7 @@ class ComposeActivity : BaseActivity(), } private fun strikethroughButtonClicked() { - when(viewModel.formattingSyntax) { + when(viewModel.formattingSyntax.value!!) { "text/markdown" -> MarkdownEdit.addStrikeThrough(composeEditField) "text/bbcode" -> BBCodeEdit.addStrikeThrough(composeEditField) "text/html" -> HTMLEdit.addStrikeThrough(composeEditField) @@ -676,7 +682,7 @@ class ComposeActivity : BaseActivity(), } private fun italicButtonClicked() { - when(viewModel.formattingSyntax) { + when(viewModel.formattingSyntax.value!!) { "text/markdown" -> MarkdownEdit.addItalic(composeEditField) "text/bbcode" -> BBCodeEdit.addItalic(composeEditField) "text/html" -> HTMLEdit.addItalic(composeEditField) @@ -684,7 +690,7 @@ class ComposeActivity : BaseActivity(), } private fun boldButtonClicked() { - when(viewModel.formattingSyntax) { + when(viewModel.formattingSyntax.value!!) { "text/markdown" -> MarkdownEdit.addBold(composeEditField) "text/bbcode" -> BBCodeEdit.addBold(composeEditField) "text/html" -> HTMLEdit.addBold(composeEditField) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt index 3a2f184c..bfcfa93b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.kt @@ -60,7 +60,6 @@ class ComposeViewModel @Inject constructor( private var inReplyToId: String? = null private var startingVisibility: Status.Visibility = Status.Visibility.UNKNOWN private var contentWarningStateChanged: Boolean = false - public var formattingSyntax: String = "" private var modifiedInitialState: Boolean = false val markMediaAsSensitive = @@ -71,6 +70,7 @@ class ComposeViewModel @Inject constructor( val setupComplete = mutableLiveData(false) val poll: MutableLiveData = mutableLiveData(null) val scheduledAt: MutableLiveData = mutableLiveData(null) + val formattingSyntax: MutableLiveData = mutableLiveData("") private val isEditingScheduledToot get() = !scheduledTootId.isNullOrEmpty() @@ -126,7 +126,7 @@ class ComposeViewModel @Inject constructor( mediaUris = mediaUris, mediaDescriptions = mediaDescriptions, poll = poll.value, - formattingSyntax = formattingSyntax, + formattingSyntax = formattingSyntax.value!!, failedToSend = false ).subscribe() } @@ -173,7 +173,7 @@ class ComposeViewModel @Inject constructor( poll = poll.value, replyingStatusContent = null, replyingStatusAuthorUsername = null, - formattingSyntax = formattingSyntax, + formattingSyntax = formattingSyntax.value!!, preview = preview, accountId = accountManager.activeAccount!!.id, savedTootUid = savedTootUid, @@ -271,7 +271,7 @@ class ComposeViewModel @Inject constructor( replyingStatusContent = composeOptions?.replyingStatusContent replyingStatusAuthor = composeOptions?.replyingStatusAuthor - formattingSyntax = composeOptions?.formattingSyntax ?: accountManager.activeAccount!!.defaultFormattingSyntax + formattingSyntax.value = composeOptions?.formattingSyntax ?: accountManager.activeAccount!!.defaultFormattingSyntax } fun updatePoll(newPoll: NewPoll) { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/drafts/DraftsActivity.kt b/app/src/main/java/com/keylesspalace/tusky/components/drafts/DraftsActivity.kt index ddf8a838..b1255e89 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/drafts/DraftsActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/drafts/DraftsActivity.kt @@ -138,7 +138,8 @@ class DraftsActivity : BaseActivity(), DraftActionListener { draftAttachments = draft.attachments, poll = draft.poll, sensitive = draft.sensitive, - visibility = draft.visibility + visibility = draft.visibility, + formattingSyntax = draft.formattingSyntax ) bottomSheet.state = BottomSheetBehavior.STATE_HIDDEN @@ -174,7 +175,8 @@ class DraftsActivity : BaseActivity(), DraftActionListener { draftAttachments = draft.attachments, poll = draft.poll, sensitive = draft.sensitive, - visibility = draft.visibility + visibility = draft.visibility, + formattingSyntax = draft.formattingSyntax ) startActivity(ComposeActivity.startIntent(this, composeOptions)) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt index e7e21987..5001472e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/AccountPreferencesFragment.kt @@ -156,7 +156,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable { "text/html" -> "HTML" else -> "Plaintext" } - setIcon(getIconForSyntax(value)) + setIcon(getIconForSyntax(syntax)) setOnPreferenceChangeListener { _, newValue -> val syntax = when(newValue) { "Markdown" -> "text/markdown" @@ -372,7 +372,7 @@ class AccountPreferencesFragment : PreferenceFragmentCompat(), Injectable { "text/html" -> R.drawable.ic_html_24dp "text/bbcode" -> R.drawable.ic_bbcode_24dp "text/markdown" -> R.drawable.ic_markdown - else -> 0 + else -> android.R.color.transparent } } diff --git a/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt b/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt index b18de592..9f067408 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/DraftEntity.kt @@ -51,6 +51,6 @@ data class DraftAttachment( get() = uriString.toUri() enum class Type { - IMAGE, VIDEO, AUDIO; + IMAGE, VIDEO, AUDIO, UNKNOWN; } }