Skip to content

Commit

Permalink
feat: creating activities in app
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelonion committed May 12, 2024
1 parent a0fabd3 commit fa659c7
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 6 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@
<activity
android:name=".others.imagesearch.ImageSearchActivity"
android:parentActivityName=".MainActivity" />
<activity
android:name=".util.MarkdownCreatorActivity"/>
<activity
android:name=".media.ReviewActivity"
android:parentActivityName=".media.MediaDetailsActivity" />
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/ani/dantotsu/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
}
11 changes: 11 additions & 0 deletions app/src/main/java/ani/dantotsu/media/ReviewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/ani/dantotsu/others/SpoilerPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/ani/dantotsu/profile/ProfileActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand Down
96 changes: 96 additions & 0 deletions app/src/main/java/ani/dantotsu/util/MarkdownCreatorActivity.kt
Original file line number Diff line number Diff line change
@@ -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)
)
}
}
}
104 changes: 104 additions & 0 deletions app/src/main/res/layout/activity_markdown_creator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:id="@+id/markdownCreatorToolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<ImageView
android:id="@+id/markdownCreatorBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:layout_marginStart="12dp"
android:src="@drawable/ic_round_arrow_back_ios_new_24"
app:tint="?attr/colorOnBackground"
tools:ignore="ContentDescription" />

<TextView
android:id="@+id/markdownCreatorTitle"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="44dp"
android:ellipsize="end"
android:fontFamily="@font/poppins_bold"
android:gravity="center|start"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:textColor="?attr/colorOnBackground"
android:textSize="18sp"
tools:text="@string/placeholder" />

<CheckBox
android:id="@+id/markdownCreatorPreviewCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:layout_marginEnd="8dp"
android:contentDescription="@string/preview"
android:text="@string/preview"
tools:ignore="ContentDescription" />
</FrameLayout>

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="@font/poppins"
android:inputType="textMultiLine"
android:padding="16dp"
android:textColor="?attr/colorOnBackground"
android:textIsSelectable="true"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="@+id/markdownCreatorToolbar"
tools:ignore="LabelFor" />

<LinearLayout
android:id="@+id/buttonContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="24dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_marginEnd="16dp"
android:fontFamily="@font/poppins_bold"
android:maxLines="1"
android:text="@string/cancel"
app:cornerRadius="16dp"
tools:ignore="ButtonStyle" />

<Button
android:id="@+id/createButton"
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_marginStart="16dp"
android:fontFamily="@font/poppins_bold"
android:maxLines="1"
android:text="@string/create"
app:cornerRadius="16dp"
tools:ignore="ButtonStyle" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/menu/menu_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
android:title="@string/view_on_anilist"
app:showAsAction="never" />

<item
android:id="@+id/action_create_new_activity"
android:title="@string/create_new_activity"
app:showAsAction="never" />

</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -980,4 +980,8 @@ Non quae tempore quo provident laudantium qui illo dolor vel quia dolor et exerc
<string name="no_video_selected">No video selected</string>
<string name="no_subtitles_available">No subtitles available</string>
<string name="vote_out_of_total">(%1$s out of %2$s liked this review)</string>
<string name="create_new_activity">Create New Activity</string>
<string name="create_new_review">Create New Review</string>
<string name="create">Create</string>
<string name="preview">Preview</string>
</resources>

0 comments on commit fa659c7

Please sign in to comment.