feat: creating activities in app

This commit is contained in:
rebelonion 2024-05-12 06:01:51 -05:00
parent a0fabd3ca6
commit fa659c7da0
10 changed files with 266 additions and 6 deletions

View file

@ -1399,7 +1399,8 @@ fun Context.getThemeColor(@AttrRes attribute: Int): Int {
fun buildMarkwon(
activity: Context,
userInputContent: Boolean = true,
fragment: Fragment? = null
fragment: Fragment? = null,
anilist: Boolean = false
): Markwon {
val glideContext = fragment?.let { Glide.with(it) } ?: Glide.with(activity)
val markwon = Markwon.builder(activity)
@ -1415,7 +1416,7 @@ fun buildMarkwon(
.usePlugin(StrikethroughPlugin.create())
.usePlugin(TablePlugin.create(activity))
.usePlugin(TaskListPlugin.create(activity))
.usePlugin(SpoilerPlugin())
.usePlugin(SpoilerPlugin(anilist))
.usePlugin(HtmlPlugin.create { plugin ->
if (userInputContent) {
plugin.addHandler(

View file

@ -3,6 +3,9 @@ package ani.dantotsu.connections.anilist
import ani.dantotsu.connections.anilist.Anilist.executeQuery
import ani.dantotsu.connections.anilist.api.FuzzyDate
import ani.dantotsu.connections.anilist.api.Query
import ani.dantotsu.currContext
import com.google.gson.Gson
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
class AnilistMutations {
@ -76,4 +79,23 @@ class AnilistMutations {
val query = "mutation{RateReview(reviewId:$reviewId,rating:$rating){id mediaId mediaType summary body(asHtml:true)rating ratingAmount userRating score private siteUrl createdAt updatedAt user{id name bannerImage avatar{medium large}}}}"
return executeQuery<Query.RateReviewResponse>(query)
}
suspend fun postActivity(text:String): String {
val encodedText = Gson().toJson(text)
val query = "mutation{SaveTextActivity(text:$encodedText){siteUrl}}"
val result = executeQuery<JsonObject>(query)
val errors = result?.get("errors")
return errors?.toString()
?: (currContext()?.getString(ani.dantotsu.R.string.success) ?: "Success")
}
suspend fun postReview(summary: String, body: String, mediaId: Int, score: Int): String {
val encodedSummary = Gson().toJson(summary)
val encodedBody = Gson().toJson(body)
val query = "mutation{SaveReview(mediaId:$mediaId,summary:$encodedSummary,body:$encodedBody,score:$score){siteUrl}}"
val result = executeQuery<JsonObject>(query)
val errors = result?.get("errors")
return errors?.toString()
?: (currContext()?.getString(ani.dantotsu.R.string.success) ?: "Success")
}
}

View file

@ -8,6 +8,7 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
@ -21,6 +22,7 @@ import ani.dantotsu.navBarHeight
import ani.dantotsu.profile.FollowerItem
import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
import ani.dantotsu.util.MarkdownCreatorActivity
import com.xwray.groupie.GroupieAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -54,6 +56,15 @@ class ReviewActivity : AppCompatActivity() {
}
binding.followerGrid.visibility = View.GONE
binding.followerList.visibility = View.GONE
binding.followFilterButton.setImageResource(R.drawable.ic_add)
binding.followFilterButton.setOnClickListener {
ContextCompat.startActivity(
this,
Intent(this, MarkdownCreatorActivity::class.java)
.putExtra("type", "review"),
null
)
}
binding.followFilterButton.visibility = View.GONE
binding.listTitle.text = getString(R.string.reviews)
binding.listRecyclerView.adapter = adapter

View file

@ -12,9 +12,13 @@ import io.noties.markwon.AbstractMarkwonPlugin
import io.noties.markwon.utils.ColorUtils
import java.util.regex.Pattern
class SpoilerPlugin : AbstractMarkwonPlugin() {
class SpoilerPlugin(private val anilist: Boolean = false) : AbstractMarkwonPlugin() {
override fun beforeSetText(textView: TextView, markdown: Spanned) {
applySpoilerSpans(markdown as Spannable)
if (anilist) {
applySpoilerSpans(markdown as Spannable, ARE)
} else {
applySpoilerSpans(markdown as Spannable)
}
}
private class RedditSpoilerSpan : CharacterStyle() {
@ -45,9 +49,10 @@ class SpoilerPlugin : AbstractMarkwonPlugin() {
companion object {
private val RE = Pattern.compile("\\|\\|.+?\\|\\|")
private fun applySpoilerSpans(spannable: Spannable) {
private val ARE = Pattern.compile("~!.+?!~")
private fun applySpoilerSpans(spannable: Spannable, regex: Pattern = RE) {
val text = spannable.toString()
val matcher = RE.matcher(text)
val matcher = regex.matcher(text)
while (matcher.find()) {
val spoilerSpan = RedditSpoilerSpan()
val clickableSpan: ClickableSpan = object : ClickableSpan() {

View file

@ -37,6 +37,7 @@ import ani.dantotsu.snackString
import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
import ani.dantotsu.toast
import ani.dantotsu.util.MarkdownCreatorActivity
import com.google.android.material.appbar.AppBarLayout
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -154,6 +155,15 @@ class ProfileActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedListene
openLinkInBrowser("https://anilist.co/user/${user.name}")
true
}
R.id.action_create_new_activity -> {
ContextCompat.startActivity(
context,
Intent(context, MarkdownCreatorActivity::class.java)
.putExtra("type", "activity"),
null
)
true
}
else -> false
}
}

View file

@ -0,0 +1,96 @@
package ani.dantotsu.util
import android.os.Bundle
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.updateLayoutParams
import androidx.core.widget.addTextChangedListener
import ani.dantotsu.R
import ani.dantotsu.buildMarkwon
import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.databinding.ActivityMarkdownCreatorBinding
import ani.dantotsu.initActivity
import ani.dantotsu.navBarHeight
import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
import ani.dantotsu.toast
import io.noties.markwon.editor.MarkwonEditor
import io.noties.markwon.editor.MarkwonEditorTextWatcher
import kotlinx.coroutines.DelicateCoroutinesApi
import tachiyomi.core.util.lang.launchIO
import java.util.Locale
class MarkdownCreatorActivity : AppCompatActivity() {
private lateinit var binding: ActivityMarkdownCreatorBinding
private lateinit var type: String
private var text: String = ""
@OptIn(DelicateCoroutinesApi::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ThemeManager(this).applyTheme()
initActivity(this)
binding = ActivityMarkdownCreatorBinding.inflate(layoutInflater)
binding.markdownCreatorToolbar.updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = statusBarHeight
}
binding.buttonContainer.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin += navBarHeight
}
setContentView(binding.root)
if (intent.hasExtra("type")) {
type = intent.getStringExtra("type")!!
} else {
finish()
}
binding.markdownCreatorTitle.text = when (type) {
"activity" -> getString(R.string.create_new_activity)
"review" -> getString(R.string.create_new_review)
else -> ""
}
binding.editText.setText(text)
binding.editText.addTextChangedListener {
if (!binding.markdownCreatorPreviewCheckbox.isChecked) {
text = it.toString()
}
}
previewMarkdown(false)
binding.markdownCreatorPreviewCheckbox.setOnClickListener {
previewMarkdown(binding.markdownCreatorPreviewCheckbox.isChecked)
}
binding.cancelButton.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
binding.markdownCreatorBack.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
binding.createButton.setOnClickListener {
launchIO {
val success = when (type) {
"activity" -> Anilist.mutation.postActivity(text)
//"review" -> Anilist.mutation.postReview(text)
else -> "Error: Unknown type"
}
toast(success)
}
onBackPressedDispatcher.onBackPressed()
}
binding.editText.requestFocus()
}
private fun previewMarkdown(preview: Boolean) {
val markwon = buildMarkwon(this, false, anilist = true)
if (preview) {
binding.editText.isEnabled = false
markwon.setMarkdown(binding.editText, text)
} else {
binding.editText.setText(text)
binding.editText.isEnabled = true
val markwonEditor = MarkwonEditor.create(markwon)
binding.editText.addTextChangedListener(
MarkwonEditorTextWatcher.withProcess(markwonEditor)
)
}
}
}