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. 18
      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.collections.ArrayList
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
import me.thanel.markdownedit.MarkdownEdit
class ComposeActivity : BaseActivity(), class ComposeActivity : BaseActivity(),
ComposeOptionsListener, ComposeOptionsListener,
@ -295,6 +296,7 @@ class ComposeActivity : BaseActivity(),
maximumTootCharacters = instanceData.maxChars maximumTootCharacters = instanceData.maxChars
updateVisibleCharactersLeft() updateVisibleCharactersLeft()
composeScheduleButton.visible(instanceData.supportsScheduled) composeScheduleButton.visible(instanceData.supportsScheduled)
composeMarkdownButton.visible(instanceData.supportsFormatting)
} }
viewModel.emoji.observe { emoji -> setEmojiList(emoji) } viewModel.emoji.observe { emoji -> setEmojiList(emoji) }
combineLiveData(viewModel.markMediaAsSensitive, viewModel.showContentWarning) { markSensitive, showContentWarning -> combineLiveData(viewModel.markMediaAsSensitive, viewModel.showContentWarning) { markSensitive, showContentWarning ->
@ -354,8 +356,14 @@ class ComposeActivity : BaseActivity(),
composeHideMediaButton.setOnClickListener { toggleHideMedia() } composeHideMediaButton.setOnClickListener { toggleHideMedia() }
composeScheduleButton.setOnClickListener { onScheduleClick() } composeScheduleButton.setOnClickListener { onScheduleClick() }
composeScheduleView.setResetOnClickListener { resetSchedule() } composeScheduleView.setResetOnClickListener { resetSchedule() }
composeMarkdownButton.setOnClickListener { toggleMarkdownMode() }
atButton.setOnClickListener { atButtonClicked() } atButton.setOnClickListener { atButtonClicked() }
hashButton.setOnClickListener { hashButtonClicked() } 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) val textColor = ThemeUtils.getColor(this, android.R.attr.textColorTertiary)
@ -413,6 +421,26 @@ class ComposeActivity : BaseActivity(),
composeEditField.setSelection(start + text.length) 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() { private fun atButtonClicked() {
replaceTextAtCaret("@") replaceTextAtCaret("@")
} }
@ -421,6 +449,26 @@ class ComposeActivity : BaseActivity(),
replaceTextAtCaret("#") 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) { override fun onSaveInstanceState(outState: Bundle) {
if (currentInputContentInfo != null) { if (currentInputContentInfo != null) {
outState.putParcelable("commitContentInputContentInfo", outState.putParcelable("commitContentInputContentInfo",
@ -430,6 +478,7 @@ class ComposeActivity : BaseActivity(),
currentInputContentInfo = null currentInputContentInfo = null
currentFlags = 0 currentFlags = 0
outState.putParcelable("photoUploadUri", photoUploadUri) outState.putParcelable("photoUploadUri", photoUploadUri)
outState.putParcelable("markdownMode", viewModel.markdownMode)
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
} }
@ -485,6 +534,7 @@ class ComposeActivity : BaseActivity(),
composeEmojiButton.isClickable = enable composeEmojiButton.isClickable = enable
composeHideMediaButton.isClickable = enable composeHideMediaButton.isClickable = enable
composeScheduleButton.isClickable = enable composeScheduleButton.isClickable = enable
composeMarkdownButton.isClickable = enable
composeTootButton.isEnabled = enable composeTootButton.isEnabled = enable
} }

@ -61,15 +61,17 @@ class ComposeViewModel
private var startingContentWarning: String? = null private var startingContentWarning: String? = null
private var inReplyToId: String? = null private var inReplyToId: String? = null
private var startingVisibility: Status.Visibility = Status.Visibility.UNKNOWN private var startingVisibility: Status.Visibility = Status.Visibility.UNKNOWN
private val instance: MutableLiveData<InstanceEntity?> = MutableLiveData() private val instance: MutableLiveData<InstanceEntity?> = MutableLiveData()
public val markdownMode: Boolean = false
val instanceParams: LiveData<ComposeInstanceParams> = instance.map { instance -> val instanceParams: LiveData<ComposeInstanceParams> = instance.map { instance ->
ComposeInstanceParams( ComposeInstanceParams(
maxChars = instance?.maximumTootCharacters ?: DEFAULT_CHARACTER_LIMIT, maxChars = instance?.maximumTootCharacters ?: DEFAULT_CHARACTER_LIMIT,
pollMaxOptions = instance?.maxPollOptions ?: DEFAULT_MAX_OPTION_COUNT, pollMaxOptions = instance?.maxPollOptions ?: DEFAULT_MAX_OPTION_COUNT,
pollMaxLength = instance?.maxPollOptionLength ?: DEFAULT_MAX_OPTION_LENGTH, 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() val emoji: MutableLiveData<List<Emoji>?> = MutableLiveData()
@ -85,6 +87,10 @@ class ComposeViewModel
val poll: MutableLiveData<NewPoll?> = mutableLiveData(null) val poll: MutableLiveData<NewPoll?> = mutableLiveData(null)
val scheduledAt: MutableLiveData<String?> = mutableLiveData(null) val scheduledAt: MutableLiveData<String?> = mutableLiveData(null)
fun toggleMarkdownMode() {
this.markdownMode = !this.markdownMode!!
}
val media = mutableLiveData<List<QueuedMedia>>(listOf()) val media = mutableLiveData<List<QueuedMedia>>(listOf())
val uploadError = MutableLiveData<Throwable>() val uploadError = MutableLiveData<Throwable>()
@ -219,7 +225,8 @@ class ComposeViewModel
replyingStatusContent, replyingStatusContent,
replyingStatusAuthor, replyingStatusAuthor,
statusVisibility.value!!, statusVisibility.value!!,
poll.value poll.value,
markdownMode
) )
} }
@ -257,6 +264,7 @@ class ComposeViewModel
poll = poll.value, poll = poll.value,
replyingStatusContent = null, replyingStatusContent = null,
replyingStatusAuthorUsername = null, replyingStatusAuthorUsername = null,
markdownMode = markdownMode,
savedJsonUrls = null, savedJsonUrls = null,
accountId = accountManager.activeAccount!!.id, accountId = accountManager.activeAccount!!.id,
savedTootUid = 0, savedTootUid = 0,
@ -449,5 +457,7 @@ data class ComposeInstanceParams(
val maxChars: Int, val maxChars: Int,
val pollMaxOptions: Int, val pollMaxOptions: Int,
val pollMaxLength: Int, val pollMaxLength: Int,
val supportsScheduled: Boolean val supportsScheduled: Boolean,
val supportsFormatting: Boolean,
val hasNoAttachmentLimits: Boolean,
) )
Loading…
Cancel
Save