feat(storyReply): fixed all markdowns

This commit is contained in:
sneazy-ibo
2024-05-29 00:10:51 +02:00
parent 1a1b4de1ef
commit 9c28cb2d35
9 changed files with 317 additions and 36 deletions

View File

@@ -2,6 +2,9 @@ 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
@@ -16,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
@@ -25,25 +30,29 @@ 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) {
BOLD("**", 2),
ITALIC("*", 1),
STRIKETHROUGH("~~", 2),
SPOILER("||", 2),
LINK("[link](url)", 6),
IMAGE("![alt text](image url)", 11),
YOUTUBE("[![YouTube](thumbnail)](video url)", 27),
VIDEO("[video](url)", 7),
ORDERED_LIST("1. ", 3),
UNORDERED_LIST("- ", 2),
HEADING("# ", 2),
CENTERED("-> <-", 3),
QUOTE("> ", 2),
CODE("`", 1);
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)
@@ -105,7 +114,7 @@ class MarkdownCreatorActivity : AppCompatActivity() {
toast(getString(R.string.cannot_be_empty))
return@setOnClickListener
}
AlertDialog.Builder(this).apply {
AlertDialog.Builder(this, R.style.MyPopup).apply {
setTitle(R.string.warning)
setMessage(R.string.post_to_anilist_warning)
setPositiveButton(R.string.ok) { _, _ ->
@@ -153,25 +162,106 @@ class MarkdownCreatorActivity : AppCompatActivity() {
}
private fun setupMarkdownButtons() {
binding.formatBold.setOnClickListener { applyMarkdownFormat(MarkdownFormat.BOLD) }
binding.formatItalic.setOnClickListener { applyMarkdownFormat(MarkdownFormat.ITALIC) }
binding.formatStrikethrough.setOnClickListener { applyMarkdownFormat(MarkdownFormat.STRIKETHROUGH) }
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) {
binding.editText.text?.insert(start, format.syntax)
binding.editText.setSelection(start + format.selectionOffset)
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 {
binding.editText.text?.insert(end, format.syntax)
binding.editText.text?.insert(start, format.syntax)
binding.editText.setSelection(end + format.syntax.length, end + format.syntax.length)
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) {
val markwon = buildMarkwon(this, false, anilist = true)
if (preview) {

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M668,692Q657,703 640.5,703Q624,703 612,691Q600,679 600,662.5Q600,646 612,634L767,479L611,323Q600,312 600.5,295.5Q601,279 612,268Q623,257 640,257Q657,257 668,268L852,452Q864,464 864,480Q864,496 852,508L668,692ZM292,692L108,508Q96,496 96,480Q96,464 108,452L292,268Q303,257 320,256.5Q337,256 349,268Q361,280 361,296.5Q361,313 349,325L193,481L349,637Q360,648 359.5,664.5Q359,681 348,692Q337,703 320,703Q303,703 292,692Z" />
</vector>

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M200,840Q167,840 143.5,816.5Q120,793 120,760L120,200Q120,167 143.5,143.5Q167,120 200,120L760,120Q793,120 816.5,143.5Q840,167 840,200L840,760Q840,793 816.5,816.5Q793,840 760,840L200,840ZM200,760L760,760Q760,760 760,760Q760,760 760,760L760,200Q760,200 760,200Q760,200 760,200L200,200Q200,200 200,200Q200,200 200,200L200,760Q200,760 200,760Q200,760 200,760ZM240,680L720,680L570,480L450,640L360,520L240,680ZM200,760Q200,760 200,760Q200,760 200,760L200,200Q200,200 200,200Q200,200 200,200L200,200Q200,200 200,200Q200,200 200,200L200,760Q200,760 200,760Q200,760 200,760L200,760Z" />
</vector>

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M440,680L280,680Q197,680 138.5,621.5Q80,563 80,480Q80,397 138.5,338.5Q197,280 280,280L440,280L440,360L280,360Q230,360 195,395Q160,430 160,480Q160,530 195,565Q230,600 280,600L440,600L440,680ZM320,520L320,440L640,440L640,520L320,520ZM520,680L520,600L680,600Q730,600 765,565Q800,530 800,480Q800,430 765,395Q730,360 680,360L520,360L520,280L680,280Q763,280 821.5,338.5Q880,397 880,480Q880,563 821.5,621.5Q763,680 680,680L520,680Z" />
</vector>

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M644,532L586,474Q595,427 559,386Q523,345 466,354L408,296Q425,288 442.5,284Q460,280 480,280Q555,280 607.5,332.5Q660,385 660,460Q660,480 656,497.5Q652,515 644,532ZM772,658L714,602Q752,573 781.5,538.5Q811,504 832,460Q782,359 688.5,299.5Q595,240 480,240Q451,240 423,244Q395,248 368,256L306,194Q347,177 390,168.5Q433,160 480,160Q631,160 749,243.5Q867,327 920,460Q897,519 859.5,569.5Q822,620 772,658ZM792,904L624,738Q589,749 553.5,754.5Q518,760 480,760Q329,760 211,676.5Q93,593 40,460Q61,407 93,361.5Q125,316 166,280L56,168L112,112L848,848L792,904ZM222,336Q193,362 169,393Q145,424 128,460Q178,561 271.5,620.5Q365,680 480,680Q500,680 519,677.5Q538,675 558,672L522,634Q511,637 501,638.5Q491,640 480,640Q405,640 352.5,587.5Q300,535 300,460Q300,449 301.5,439Q303,429 306,418L222,336ZM541,429L541,429Q541,429 541,429Q541,429 541,429Q541,429 541,429Q541,429 541,429Q541,429 541,429Q541,429 541,429ZM390,504Q390,504 390,504Q390,504 390,504L390,504Q390,504 390,504Q390,504 390,504Q390,504 390,504Q390,504 390,504Z" />
</vector>

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M420,280L260,280Q235,280 217.5,262.5Q200,245 200,220Q200,195 217.5,177.5Q235,160 260,160L700,160Q725,160 742.5,177.5Q760,195 760,220Q760,245 742.5,262.5Q725,280 700,280L540,280L540,740Q540,765 522.5,782.5Q505,800 480,800Q455,800 437.5,782.5Q420,765 420,740L420,280Z"/>
</vector>

View File

@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M160,800Q127,800 103.5,776.5Q80,753 80,720L80,240Q80,207 103.5,183.5Q127,160 160,160L640,160Q673,160 696.5,183.5Q720,207 720,240L720,420L880,260L880,700L720,540L720,720Q720,753 696.5,776.5Q673,800 640,800L160,800ZM160,720L640,720Q640,720 640,720Q640,720 640,720L640,240Q640,240 640,240Q640,240 640,240L160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720ZM160,720Q160,720 160,720Q160,720 160,720L160,240Q160,240 160,240Q160,240 160,240L160,240Q160,240 160,240Q160,240 160,240L160,720Q160,720 160,720Q160,720 160,720L160,720Z" />
</vector>

View File

@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M616,718Q589,719 564.5,719.5Q540,720 521,720Q499,720 480,720Q409,720 347,718Q294,716 242.5,712.5Q191,709 168,703Q142,696 123,677Q104,658 97,632Q91,609 87.5,576Q84,543 82,513Q80,477 80,440Q80,403 82,367Q84,337 87.5,304Q91,271 97,248Q104,222 123,203Q142,184 168,177Q191,171 242.5,167.5Q294,164 347,162Q409,160 480,160Q551,160 613,162Q666,164 717.5,167.5Q769,171 792,177Q818,184 837,203Q856,222 863,248Q869,271 872.5,304Q876,337 878,367Q880,403 880,440L880,457Q861,449 841,444.5Q821,440 800,440Q717,440 658.5,498.5Q600,557 600,640Q600,661 604,680.5Q608,700 616,718ZM400,560L608,440L400,320L400,560ZM760,760L760,680L680,680L680,600L760,600L760,520L840,520L840,600L920,600L920,680L840,680L840,760L760,760Z" />
</vector>

View File

@@ -73,7 +73,7 @@
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="590dp"
android:fontFamily="@font/poppins"
android:inputType="textMultiLine"
android:padding="16dp"
@@ -89,25 +89,26 @@
</LinearLayout>
<LinearLayout
android:id="@+id/markdownOptionsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:orientation="vertical">
android:layout_gravity="bottom|end"
android:layout_marginHorizontal="13dp"
android:orientation="vertical"
android:padding="8dp"
android:windowSoftInputMode="adjustResize">
<LinearLayout
android:id="@+id/markdownOptionsContainer"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:background="@color/nav_bg"
android:orientation="horizontal"
android:padding="8dp"
android:windowSoftInputMode="adjustResize">
android:orientation="horizontal">
<ImageView
android:id="@+id/formatBold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_bold_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
@@ -116,6 +117,8 @@
android:id="@+id/formatItalic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_italic_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
@@ -124,10 +127,127 @@
android:id="@+id/formatStrikethrough"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_strikethrough_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatSpoiler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_spoiler_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatLink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_link_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_image_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatYoutube"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_youtube_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_video_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/formatListOrdered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_list_numbered_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatListUnordered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_list_bulleted_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_title_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_align_center_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatQuote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_quote_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/formatCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="6dp"
android:layout_marginEnd="10dp"
android:src="@drawable/format_code_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />
</LinearLayout>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>