From edd79911016953c06a2de499c699cdab78b9c2c5 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sat, 4 Jan 2020 00:49:36 +0300 Subject: [PATCH] ComposeActivity: reimplement markdown mode again after pointless java->kotlin transition --- .../components/compose/ComposeActivity.kt | 50 +++++++++++++++++++ .../components/compose/ComposeViewModel.kt | 20 ++++++-- 2 files changed, 65 insertions(+), 5 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 99d43c65..be2ede58 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 @@ -85,6 +85,7 @@ import javax.inject.Inject import kotlin.collections.ArrayList import kotlin.math.max import kotlin.math.min +import me.thanel.markdownedit.MarkdownEdit class ComposeActivity : BaseActivity(), ComposeOptionsListener, @@ -295,6 +296,7 @@ class ComposeActivity : BaseActivity(), maximumTootCharacters = instanceData.maxChars updateVisibleCharactersLeft() composeScheduleButton.visible(instanceData.supportsScheduled) + composeMarkdownButton.visible(instanceData.supportsFormatting) } viewModel.emoji.observe { emoji -> setEmojiList(emoji) } combineLiveData(viewModel.markMediaAsSensitive, viewModel.showContentWarning) { markSensitive, showContentWarning -> @@ -354,8 +356,14 @@ class ComposeActivity : BaseActivity(), composeHideMediaButton.setOnClickListener { toggleHideMedia() } composeScheduleButton.setOnClickListener { onScheduleClick() } composeScheduleView.setResetOnClickListener { resetSchedule() } + composeMarkdownButton.setOnClickListener { toggleMarkdownMode() } atButton.setOnClickListener { atButtonClicked() } hashButton.setOnClickListener { hashButtonClicked() } + codeButton.setOnClickListener { codeButtonClicked() } + linkButton.setOnClickListener { linkButtonClicked() } + strikethroughButton.setOnClickListener { strikethroughButtonClicked() } + italicButton.setOnClickListener { italicButtonClicked() } + boldButton.setOnClickListener { boldButtonClicked() } val textColor = ThemeUtils.getColor(this, android.R.attr.textColorTertiary) @@ -412,6 +420,26 @@ class ComposeActivity : BaseActivity(), // Set the cursor after the inserted text composeEditField.setSelection(start + text.length) } + + private fun toggleMarkdownMode() { + viewModel.toggleMarkdownMode() + + enableMarkdownWYSIWYGButtons(viewModel.markdownMode) + + TransitionManager.beginDelayedTransition(composeMarkdownButton.parent as ViewGroup); + + @ColorInt val color = ThemeUtils.getColor(this, if(viewModel.markdownMode) R.attr.colorPrimary else android.R.attr.textColorTertiary); + composeMarkdownButton.drawable.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN); + } + + private fun enableMarkdownWYSIWYGButtons(visible: Boolean) { + val visibility = if(visible) View.VISIBLE else View.GONE + codeButton.visibility = visibility + linkButton.visibility = visibility + strikethroughButton.visibility = visibility + italicButton.visibility = visibility + boldButton.visibility = visibility + } private fun atButtonClicked() { replaceTextAtCaret("@") @@ -420,6 +448,26 @@ class ComposeActivity : BaseActivity(), private fun hashButtonClicked() { replaceTextAtCaret("#") } + + private fun codeButtonClicked() { + MarkdownEdit.addCode(composeEditField); + } + + private fun linkButtonClicked() { + MarkdownEdit.addLink(composeEditField); + } + + private fun strikethroughButtonClicked() { + MarkdownEdit.addStrikeThrough(composeEditField); + } + + private fun italicButtonClicked() { + MarkdownEdit.addItalic(composeEditField); + } + + private fun boldButtonClicked() { + MarkdownEdit.addBold(composeEditField); + } override fun onSaveInstanceState(outState: Bundle) { if (currentInputContentInfo != null) { @@ -430,6 +478,7 @@ class ComposeActivity : BaseActivity(), currentInputContentInfo = null currentFlags = 0 outState.putParcelable("photoUploadUri", photoUploadUri) + outState.putParcelable("markdownMode", viewModel.markdownMode) super.onSaveInstanceState(outState) } @@ -485,6 +534,7 @@ class ComposeActivity : BaseActivity(), composeEmojiButton.isClickable = enable composeHideMediaButton.isClickable = enable composeScheduleButton.isClickable = enable + composeMarkdownButton.isClickable = enable composeTootButton.isEnabled = enable } 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 b4901669..22c490a7 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 @@ -61,15 +61,17 @@ class ComposeViewModel private var startingContentWarning: String? = null private var inReplyToId: String? = null private var startingVisibility: Status.Visibility = Status.Visibility.UNKNOWN - private val instance: MutableLiveData = MutableLiveData() + public val markdownMode: Boolean = false val instanceParams: LiveData = instance.map { instance -> ComposeInstanceParams( maxChars = instance?.maximumTootCharacters ?: DEFAULT_CHARACTER_LIMIT, pollMaxOptions = instance?.maxPollOptions ?: DEFAULT_MAX_OPTION_COUNT, pollMaxLength = instance?.maxPollOptionLength ?: DEFAULT_MAX_OPTION_LENGTH, - supportsScheduled = instance?.version?.let { VersionUtils(it).supportsScheduledToots() } ?: false + supportsScheduled = instance?.version?.let { VersionUtils(it).supportsScheduledToots() } ?: false, + supportsFormatting = instance?.version?.let { VersionUtils(it).isPleroma() } ?: false + hasNoAttachmentLimits = instance?.version?.let { VersionUtils(it).isPleroma() } ?: false ) } val emoji: MutableLiveData?> = MutableLiveData() @@ -84,6 +86,10 @@ class ComposeViewModel val showContentWarning = mutableLiveData(false) val poll: MutableLiveData = mutableLiveData(null) val scheduledAt: MutableLiveData = mutableLiveData(null) + + fun toggleMarkdownMode() { + this.markdownMode = !this.markdownMode!! + } val media = mutableLiveData>(listOf()) val uploadError = MutableLiveData() @@ -219,7 +225,8 @@ class ComposeViewModel replyingStatusContent, replyingStatusAuthor, statusVisibility.value!!, - poll.value + poll.value, + markdownMode ) } @@ -257,6 +264,7 @@ class ComposeViewModel poll = poll.value, replyingStatusContent = null, replyingStatusAuthorUsername = null, + markdownMode = markdownMode, savedJsonUrls = null, accountId = accountManager.activeAccount!!.id, savedTootUid = 0, @@ -449,5 +457,7 @@ data class ComposeInstanceParams( val maxChars: Int, val pollMaxOptions: Int, val pollMaxLength: Int, - val supportsScheduled: Boolean -) \ No newline at end of file + val supportsScheduled: Boolean, + val supportsFormatting: Boolean, + val hasNoAttachmentLimits: Boolean, +)