feat: revamping text activities (#406)
* feat(storyReply): redesigned components * feat(storyReply): fixed all markdowns
This commit is contained in:
parent
5473ac8238
commit
ce7ae28e1e
19 changed files with 550 additions and 108 deletions
|
@ -2,6 +2,10 @@ package ani.dantotsu.util
|
|||
|
||||
import android.os.Bundle
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.core.widget.addTextChangedListener
|
||||
|
@ -15,6 +19,8 @@ import ani.dantotsu.openLinkInBrowser
|
|||
import ani.dantotsu.statusBarHeight
|
||||
import ani.dantotsu.themes.ThemeManager
|
||||
import ani.dantotsu.toast
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import com.google.android.material.textfield.TextInputLayout
|
||||
import io.noties.markwon.editor.MarkwonEditor
|
||||
import io.noties.markwon.editor.MarkwonEditorTextWatcher
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
|
@ -24,8 +30,30 @@ class MarkdownCreatorActivity : AppCompatActivity() {
|
|||
private lateinit var binding: ActivityMarkdownCreatorBinding
|
||||
private lateinit var type: String
|
||||
private var text: String = ""
|
||||
var ping: String? = null
|
||||
private var ping: String? = null
|
||||
private var parentId: Int = 0
|
||||
private var isPreviewMode: Boolean = false
|
||||
|
||||
enum class MarkdownFormat(
|
||||
val syntax: String,
|
||||
val selectionOffset: Int,
|
||||
val imageViewId: Int
|
||||
) {
|
||||
BOLD("****", 2, R.id.formatBold),
|
||||
ITALIC("**", 1, R.id.formatItalic),
|
||||
STRIKETHROUGH("~~~~", 2, R.id.formatStrikethrough),
|
||||
SPOILER("||", 2, R.id.formatSpoiler),
|
||||
LINK("[Placeholder](%s)", 0, R.id.formatLink),
|
||||
IMAGE("img(%s)", 0, R.id.formatImage),
|
||||
YOUTUBE("youtube(%s)", 0, R.id.formatYoutube),
|
||||
VIDEO("webm(%s)", 0, R.id.formatVideo),
|
||||
ORDERED_LIST("1. ", 3, R.id.formatListOrdered),
|
||||
UNORDERED_LIST("- ", 2, R.id.formatListUnordered),
|
||||
HEADING("# ", 2, R.id.formatTitle),
|
||||
CENTERED("~~~~~~", 3, R.id.formatCenter),
|
||||
QUOTE("> ", 2, R.id.formatQuote),
|
||||
CODE("``", 1, R.id.formatCode)
|
||||
}
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -36,11 +64,15 @@ class MarkdownCreatorActivity : AppCompatActivity() {
|
|||
binding.markdownCreatorToolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||
topMargin = statusBarHeight
|
||||
}
|
||||
binding.buttonContainer.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||
binding.markdownOptionsContainer.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||
bottomMargin += navBarHeight
|
||||
}
|
||||
setContentView(binding.root)
|
||||
|
||||
val params = binding.createButton.layoutParams as ViewGroup.MarginLayoutParams
|
||||
params.marginEnd = 16 * resources.displayMetrics.density.toInt()
|
||||
binding.createButton.layoutParams = params
|
||||
|
||||
if (intent.hasExtra("type")) {
|
||||
type = intent.getStringExtra("type")!!
|
||||
} else {
|
||||
|
@ -69,17 +101,12 @@ class MarkdownCreatorActivity : AppCompatActivity() {
|
|||
text = ping ?: ""
|
||||
binding.editText.setText(text)
|
||||
binding.editText.addTextChangedListener {
|
||||
if (!binding.markdownCreatorPreviewCheckbox.isChecked) {
|
||||
if (!isPreviewMode) {
|
||||
text = it.toString()
|
||||
}
|
||||
}
|
||||
previewMarkdown(false)
|
||||
binding.markdownCreatorPreviewCheckbox.setOnClickListener {
|
||||
previewMarkdown(binding.markdownCreatorPreviewCheckbox.isChecked)
|
||||
}
|
||||
binding.cancelButton.setOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
||||
binding.markdownCreatorBack.setOnClickListener {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
|
@ -89,10 +116,10 @@ class MarkdownCreatorActivity : AppCompatActivity() {
|
|||
toast(getString(R.string.cannot_be_empty))
|
||||
return@setOnClickListener
|
||||
}
|
||||
AlertDialogBuilder(this).apply {
|
||||
AlertDialog.Builder(this, R.style.MyPopup).apply {
|
||||
setTitle(R.string.warning)
|
||||
setMessage(R.string.post_to_anilist_warning)
|
||||
setPosButton(R.string.ok) {
|
||||
setPositiveButton(R.string.ok) { _, _ ->
|
||||
launchIO {
|
||||
val editId = intent.getIntExtra("edit", -1)
|
||||
val isEdit = editId != -1
|
||||
|
@ -119,14 +146,126 @@ class MarkdownCreatorActivity : AppCompatActivity() {
|
|||
finish()
|
||||
}
|
||||
}
|
||||
setNeutralButton(R.string.open_rules) {
|
||||
setNeutralButton(R.string.open_rules) { _, _ ->
|
||||
openLinkInBrowser("https://anilist.co/forum/thread/14")
|
||||
}
|
||||
setNegButton(R.string.cancel)
|
||||
setNegativeButton(R.string.cancel, null)
|
||||
}.show()
|
||||
}
|
||||
|
||||
binding.createButton.setOnLongClickListener {
|
||||
isPreviewMode = !isPreviewMode
|
||||
previewMarkdown(isPreviewMode)
|
||||
if (isPreviewMode) {
|
||||
toast("Preview enabled")
|
||||
} else {
|
||||
toast("Preview disabled")
|
||||
}
|
||||
true
|
||||
}
|
||||
binding.editText.requestFocus()
|
||||
setupMarkdownButtons()
|
||||
}
|
||||
|
||||
private fun setupMarkdownButtons() {
|
||||
MarkdownFormat.entries.forEach { format ->
|
||||
findViewById<ImageView>(format.imageViewId)?.setOnClickListener {
|
||||
applyMarkdownFormat(format)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun applyMarkdownFormat(format: MarkdownFormat) {
|
||||
val start = binding.editText.selectionStart
|
||||
val end = binding.editText.selectionEnd
|
||||
|
||||
if (start != end) {
|
||||
val selectedText = binding.editText.text?.substring(start, end) ?: ""
|
||||
val lines = selectedText.split("\n")
|
||||
|
||||
val newText = when (format) {
|
||||
MarkdownFormat.UNORDERED_LIST -> {
|
||||
lines.joinToString("\n") { "- $it" }
|
||||
}
|
||||
MarkdownFormat.ORDERED_LIST -> {
|
||||
lines.mapIndexed { index, line -> "${index + 1}. $line" }.joinToString("\n")
|
||||
}
|
||||
else -> {
|
||||
if (format.syntax.contains("%s")) {
|
||||
String.format(format.syntax, selectedText)
|
||||
} else {
|
||||
format.syntax.substring(0, format.selectionOffset) +
|
||||
selectedText +
|
||||
format.syntax.substring(format.selectionOffset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
binding.editText.text?.replace(start, end, newText)
|
||||
binding.editText.setSelection(start + newText.length)
|
||||
} else {
|
||||
if (format.syntax.contains("%s")) {
|
||||
showInputDialog(format, start)
|
||||
} else {
|
||||
val newText = format.syntax
|
||||
binding.editText.text?.insert(start, newText)
|
||||
binding.editText.setSelection(start + format.selectionOffset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun showInputDialog(format: MarkdownFormat, position: Int) {
|
||||
val inputLayout = TextInputLayout(this).apply {
|
||||
layoutParams = ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
boxBackgroundMode = TextInputLayout.BOX_BACKGROUND_OUTLINE
|
||||
hint = "Paste your link here"
|
||||
isHintEnabled = true
|
||||
}
|
||||
|
||||
val inputEditText = TextInputEditText(this).apply {
|
||||
layoutParams = LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
}
|
||||
|
||||
inputLayout.addView(inputEditText)
|
||||
|
||||
val container = FrameLayout(this).apply {
|
||||
addView(inputLayout)
|
||||
layoutParams = ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
setPadding(64, 64, 64, 0)
|
||||
}
|
||||
|
||||
val dialog = AlertDialog.Builder(this, R.style.MyPopup).apply {
|
||||
setView(container)
|
||||
setPositiveButton(getString(R.string.ok)) { dialog, _ ->
|
||||
val input = inputEditText.text.toString()
|
||||
val formattedText = String.format(format.syntax, input)
|
||||
binding.editText.text?.insert(position, formattedText)
|
||||
binding.editText.setSelection(position + formattedText.length)
|
||||
dialog.dismiss()
|
||||
}
|
||||
setNegativeButton(getString(R.string.cancel)) { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
}
|
||||
}.create()
|
||||
|
||||
val widthInDp = 245
|
||||
val layoutParams = ViewGroup.LayoutParams(
|
||||
(widthInDp * resources.displayMetrics.density).toInt(),
|
||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
)
|
||||
dialog.window?.setLayout(layoutParams.width, layoutParams.height)
|
||||
dialog.show()
|
||||
inputEditText.requestFocus()
|
||||
}
|
||||
|
||||
private fun previewMarkdown(preview: Boolean) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue