diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c42fee0d98..1104ba865f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -201,7 +201,8 @@ android:name=".others.imagesearch.ImageSearchActivity" android:parentActivityName=".MainActivity" /> + android:name=".util.MarkdownCreatorActivity" + android:windowSoftInputMode="adjustResize" /> ", 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 { topMargin = statusBarHeight } - binding.buttonContainer.updateLayoutParams { + binding.markdownOptionsContainer.updateLayoutParams { 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(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) { diff --git a/app/src/main/res/drawable/format_align_center_24.xml b/app/src/main/res/drawable/format_align_center_24.xml new file mode 100644 index 0000000000..c174a0ea79 --- /dev/null +++ b/app/src/main/res/drawable/format_align_center_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_bold_24.xml b/app/src/main/res/drawable/format_bold_24.xml new file mode 100644 index 0000000000..c640aaaa40 --- /dev/null +++ b/app/src/main/res/drawable/format_bold_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_code_24.xml b/app/src/main/res/drawable/format_code_24.xml new file mode 100644 index 0000000000..61eaae2dc4 --- /dev/null +++ b/app/src/main/res/drawable/format_code_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_image_24.xml b/app/src/main/res/drawable/format_image_24.xml new file mode 100644 index 0000000000..f6cc30a4eb --- /dev/null +++ b/app/src/main/res/drawable/format_image_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_italic_24.xml b/app/src/main/res/drawable/format_italic_24.xml new file mode 100644 index 0000000000..7f9ea8f94b --- /dev/null +++ b/app/src/main/res/drawable/format_italic_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_link_24.xml b/app/src/main/res/drawable/format_link_24.xml new file mode 100644 index 0000000000..c8c0c1b61e --- /dev/null +++ b/app/src/main/res/drawable/format_link_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_list_bulleted_24.xml b/app/src/main/res/drawable/format_list_bulleted_24.xml new file mode 100644 index 0000000000..d2558d8514 --- /dev/null +++ b/app/src/main/res/drawable/format_list_bulleted_24.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/drawable/format_list_numbered_24.xml b/app/src/main/res/drawable/format_list_numbered_24.xml new file mode 100644 index 0000000000..bc344d3224 --- /dev/null +++ b/app/src/main/res/drawable/format_list_numbered_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_quote_24.xml b/app/src/main/res/drawable/format_quote_24.xml new file mode 100644 index 0000000000..74ecc15081 --- /dev/null +++ b/app/src/main/res/drawable/format_quote_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_spoiler_24.xml b/app/src/main/res/drawable/format_spoiler_24.xml new file mode 100644 index 0000000000..647711b4d3 --- /dev/null +++ b/app/src/main/res/drawable/format_spoiler_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_strikethrough_24.xml b/app/src/main/res/drawable/format_strikethrough_24.xml new file mode 100644 index 0000000000..a7556219ac --- /dev/null +++ b/app/src/main/res/drawable/format_strikethrough_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_title_24.xml b/app/src/main/res/drawable/format_title_24.xml new file mode 100644 index 0000000000..4b15683f54 --- /dev/null +++ b/app/src/main/res/drawable/format_title_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/drawable/format_video_24.xml b/app/src/main/res/drawable/format_video_24.xml new file mode 100644 index 0000000000..9813485dae --- /dev/null +++ b/app/src/main/res/drawable/format_video_24.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/main/res/drawable/format_youtube_24.xml b/app/src/main/res/drawable/format_youtube_24.xml new file mode 100644 index 0000000000..909e5a21b2 --- /dev/null +++ b/app/src/main/res/drawable/format_youtube_24.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/activity_markdown_creator.xml b/app/src/main/res/layout/activity_markdown_creator.xml index 87af585d2d..504557a0db 100644 --- a/app/src/main/res/layout/activity_markdown_creator.xml +++ b/app/src/main/res/layout/activity_markdown_creator.xml @@ -1,107 +1,253 @@ - + android:fitsSystemWindows="false"> - - - - - + + - - - - - + android:orientation="horizontal" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + + + + +