ComposeActivity: reimplement markdown mode again after pointless java->kotlin transition

main
Alibek Omarov 5 years ago
parent 76a29e4308
commit edd7991101
  1. 50
      app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeActivity.kt
  2. 20
      app/src/main/java/com/keylesspalace/tusky/components/compose/ComposeViewModel.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
}

@ -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<InstanceEntity?> = MutableLiveData()
public val markdownMode: Boolean = false
val instanceParams: LiveData<ComposeInstanceParams> = 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<List<Emoji>?> = MutableLiveData()
@ -84,6 +86,10 @@ class ComposeViewModel
val showContentWarning = mutableLiveData(false)
val poll: MutableLiveData<NewPoll?> = mutableLiveData(null)
val scheduledAt: MutableLiveData<String?> = mutableLiveData(null)
fun toggleMarkdownMode() {
this.markdownMode = !this.markdownMode!!
}
val media = mutableLiveData<List<QueuedMedia>>(listOf())
val uploadError = MutableLiveData<Throwable>()
@ -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
)
val supportsScheduled: Boolean,
val supportsFormatting: Boolean,
val hasNoAttachmentLimits: Boolean,
)

Loading…
Cancel
Save