feat(discord): custom buttons

This commit is contained in:
sneazy-ibo
2024-03-27 03:49:04 +01:00
parent b5eda797b5
commit eefd1fc391
6 changed files with 376 additions and 50 deletions

View File

@@ -1083,35 +1083,45 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
val incognito: Boolean = PrefManager.getVal(PrefName.Incognito)
if ((isOnline(context) && !offline) && Discord.token != null && !incognito) {
lifecycleScope.launch {
val presence = RPC.createPresence(RPC.Companion.RPCData(
applicationId = Discord.application_Id,
type = RPC.Type.WATCHING,
activityName = media.userPreferredName,
details = ep.title?.takeIf { it.isNotEmpty() } ?: getString(
R.string.episode_num,
ep.number
),
state = "Episode : ${ep.number}/${media.anime?.totalEpisodes ?: "??"}",
largeImage = media.cover?.let {
RPC.Link(
media.userPreferredName,
it
)
},
smallImage = RPC.Link(
"Dantotsu",
Discord.small_Image
),
buttons = mutableListOf(
val discordMode = PrefManager.getCustomVal("discord_mode", "dantotsu")
val buttons = when (discordMode) {
"dantotsu" -> mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
RPC.Link(
"Stream on Dantotsu",
getString(R.string.github)
RPC.Link("Watch on Dantotsu", getString(R.string.dantotsu))
)
"anilist" -> {
val userId = PrefManager.getVal<String>(PrefName.AnilistUserId)
val anilistLink = "https://anilist.co/user/$userId/"
mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
RPC.Link("View My AniList", anilistLink)
)
}
"custom" -> {
val customButtonText = PrefManager.getCustomVal("custom_button_text", "")
val customButtonLink = PrefManager.getCustomVal("custom_button_link", "")
mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
RPC.Link(customButtonText, customButtonLink)
)
}
else -> mutableListOf()
}
val presence = RPC.createPresence(
RPC.Companion.RPCData(
applicationId = Discord.application_Id,
type = RPC.Type.WATCHING,
activityName = media.userPreferredName,
details = ep.title?.takeIf { it.isNotEmpty() } ?: getString(
R.string.episode_num,
ep.number
),
state = "Episode : ${ep.number}/${media.anime?.totalEpisodes ?: "??"}",
largeImage = media.cover?.let { RPC.Link(media.userPreferredName, it) },
smallImage = RPC.Link("Dantotsu", Discord.small_Image),
buttons = buttons
)
)
)
val intent = Intent(context, DiscordService::class.java).apply {
putExtra("presence", presence)
}
@@ -1119,7 +1129,6 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
startService(intent)
}
}
updateProgress()
}
}

View File

@@ -362,15 +362,11 @@ class MangaReaderActivity : AppCompatActivity() {
currentChapterIndex = chaptersArr.indexOf(chap.number)
binding.mangaReaderChapterSelect.setSelection(currentChapterIndex)
if (defaultSettings.direction == RIGHT_TO_LEFT || defaultSettings.direction == BOTTOM_TO_TOP) {
binding.mangaReaderNextChap.text =
chaptersTitleArr.getOrNull(currentChapterIndex - 1) ?: ""
binding.mangaReaderPrevChap.text =
chaptersTitleArr.getOrNull(currentChapterIndex + 1) ?: ""
binding.mangaReaderNextChap.text = chaptersTitleArr.getOrNull(currentChapterIndex - 1) ?: ""
binding.mangaReaderPrevChap.text = chaptersTitleArr.getOrNull(currentChapterIndex + 1) ?: ""
} else {
binding.mangaReaderNextChap.text =
chaptersTitleArr.getOrNull(currentChapterIndex + 1) ?: ""
binding.mangaReaderPrevChap.text =
chaptersTitleArr.getOrNull(currentChapterIndex - 1) ?: ""
binding.mangaReaderNextChap.text = chaptersTitleArr.getOrNull(currentChapterIndex + 1) ?: ""
binding.mangaReaderPrevChap.text = chaptersTitleArr.getOrNull(currentChapterIndex - 1) ?: ""
}
applySettings()
val context = this
@@ -378,6 +374,30 @@ class MangaReaderActivity : AppCompatActivity() {
val incognito: Boolean = PrefManager.getVal(PrefName.Incognito)
if ((isOnline(context) && !offline) && Discord.token != null && !incognito) {
lifecycleScope.launch {
val discordMode = PrefManager.getCustomVal("discord_mode", "dantotsu")
val buttons = when (discordMode) {
"dantotsu" -> mutableListOf(
RPC.Link(getString(R.string.view_manga), media.shareLink ?: ""),
RPC.Link("Read on Dantotsu", getString(R.string.dantotsu))
)
"anilist" -> {
val userId = PrefManager.getVal<String>(PrefName.AnilistUserId)
val anilistLink = "https://anilist.co/user/$userId/"
mutableListOf(
RPC.Link(getString(R.string.view_manga), media.shareLink ?: ""),
RPC.Link("View My AniList", anilistLink)
)
}
"custom" -> {
val customButtonText = PrefManager.getCustomVal("custom_button_text", "")
val customButtonLink = PrefManager.getCustomVal("custom_button_link", "")
mutableListOf(
RPC.Link(getString(R.string.view_manga), media.shareLink ?: ""),
RPC.Link(customButtonText, customButtonLink)
)
}
else -> mutableListOf()
}
val presence = RPC.createPresence(
RPC.Companion.RPCData(
applicationId = Discord.application_Id,
@@ -386,20 +406,9 @@ class MangaReaderActivity : AppCompatActivity() {
details = chap.title?.takeIf { it.isNotEmpty() }
?: getString(R.string.chapter_num, chap.number),
state = "${chap.number}/${media.manga?.totalChapters ?: "??"}",
largeImage = media.cover?.let { cover ->
RPC.Link(media.userPreferredName, cover)
},
smallImage = RPC.Link(
"Dantotsu",
Discord.small_Image
),
buttons = mutableListOf(
RPC.Link(getString(R.string.view_manga), media.shareLink ?: ""),
RPC.Link(
"Stream on Dantotsu",
getString(R.string.github)
)
)
largeImage = media.cover?.let { cover -> RPC.Link(media.userPreferredName, cover) },
smallImage = RPC.Link("Dantotsu", Discord.small_Image),
buttons = buttons
)
)
val intent = Intent(context, DiscordService::class.java).apply {

View File

@@ -13,12 +13,18 @@ import android.os.Build.VERSION.CODENAME
import android.os.Build.VERSION.RELEASE
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.activity.result.contract.ActivityResultContracts
@@ -26,6 +32,7 @@ import androidx.annotation.OptIn
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.updateLayoutParams
import androidx.core.widget.doAfterTextChanged
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.lifecycleScope
import androidx.media3.common.util.UnstableApi
@@ -79,6 +86,7 @@ import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
import ani.dantotsu.toast
import ani.dantotsu.util.Logger
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.textfield.TextInputEditText
import eltos.simpledialogfragment.SimpleDialog
import eltos.simpledialogfragment.SimpleDialog.OnDialogResultListener.BUTTON_POSITIVE
@@ -295,6 +303,74 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
imageSwitcher.setImageResource(initialStatus)
imageSwitcher.startAnimation(zoomInAnimation)
}
imageSwitcher.setOnLongClickListener {
val dialogView = LayoutInflater.from(this@SettingsActivity).inflate(R.layout.discord_rpc_layout, null)
val radioGroup = dialogView.findViewById<RadioGroup>(R.id.radio_group)
val radioDantotsu = dialogView.findViewById<RadioButton>(R.id.radio_dantotsu)
val radioAnilist = dialogView.findViewById<RadioButton>(R.id.radio_anilist)
val radioCustom = dialogView.findViewById<RadioButton>(R.id.radio_custom)
val anilistLinkPreview = dialogView.findViewById<TextView>(R.id.anilistLinkPreview)
val customButtonPreview = dialogView.findViewById<TextView>(R.id.customButtonPreview)
val customLinkPreview = dialogView.findViewById<TextView>(R.id.customLinkPreview)
val buttonEditText = dialogView.findViewById<EditText>(R.id.buttonEditText)
val linkEditText = dialogView.findViewById<EditText>(R.id.linkEditText)
buttonEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
PrefManager.setCustomVal("custom_button_text", s.toString())
customButtonPreview.text = s.toString().ifEmpty { "Sample" }
}
})
linkEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
PrefManager.setCustomVal("custom_button_link", s.toString())
customLinkPreview.text = s.toString().ifEmpty { "https://example.com/" }
}
})
val userId = PrefManager.getVal<String>(PrefName.AnilistUserName)
val currentMode = PrefManager.getCustomVal("discord_mode", "dantotsu")
val customButtonText = PrefManager.getCustomVal("custom_button_text", "")
val customButtonLink = PrefManager.getCustomVal("custom_button_link", "")
when (currentMode) {
"dantotsu" -> radioDantotsu.isChecked = true
"anilist" -> radioAnilist.isChecked = true
"custom" -> radioCustom.isChecked = true
}
anilistLinkPreview.text = getString(R.string.anilist_link, userId)
customButtonPreview.text = customButtonText.ifEmpty { "Sample" }
customLinkPreview.text = customButtonLink.ifEmpty { "https://example.com/" }
buttonEditText.setText(customButtonText)
linkEditText.setText(customButtonLink)
val bottomSheetDialog = BottomSheetDialog(this@SettingsActivity)
bottomSheetDialog.setContentView(dialogView)
radioGroup.setOnCheckedChangeListener { _, checkedId ->
when (checkedId) {
R.id.radio_dantotsu -> {
PrefManager.setCustomVal("discord_mode", "dantotsu")
}
R.id.radio_anilist -> {
PrefManager.setCustomVal("discord_mode", "anilist")
}
R.id.radio_custom -> {
PrefManager.setCustomVal("discord_mode", "custom")
PrefManager.setCustomVal("custom_button_text", buttonEditText.text.toString())
PrefManager.setCustomVal("custom_button_link", linkEditText.text.toString())
}
}
}
bottomSheetDialog.show()
true
}
} else {
imageSwitcher.visibility = View.GONE
settingsDiscordAvatar.setImageResource(R.drawable.ic_round_person_24)

View File

@@ -2,9 +2,9 @@
<androidx.core.widget.NestedScrollView 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:background="@drawable/bottom_sheet_background"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="@drawable/bottom_sheet_background">
<LinearLayout
android:layout_width="match_parent"

View File

@@ -0,0 +1,220 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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="wrap_content"
android:background="@drawable/bottom_sheet_background"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/bottomSheetCustomTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="16dp"
android:fontFamily="@font/poppins_semi_bold"
android:text="@string/discord_rich_presence"
android:textAlignment="center"
android:textSize="20sp" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio_dantotsu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/discord_dantotsu_button" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="@+id/dantotsuCard"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="start"
android:padding="8dp"
app:boxStrokeColor="@color/text_input_layout_stroke_color"
app:cardBackgroundColor="@color/nav_bg"
app:cardCornerRadius="16dp"
app:contentPadding="4dp"
app:contentPaddingLeft="8dp"
app:contentPaddingRight="8dp"
tools:ignore="ContentDescription,TextContrastCheck">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="4dp"
android:fontFamily="@font/poppins_bold"
android:text="@string/stream_on_dantotsu"
android:textColor="?attr/colorPrimary"
app:drawableTint="?attr/colorPrimary" />
</androidx.cardview.widget.CardView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:drawablePadding="4dp"
android:fontFamily="@font/poppins_bold"
android:text="@string/dantotsu"
android:textColor="?attr/colorPrimary"
app:drawableTint="?attr/colorPrimary" />
</LinearLayout>
<RadioButton
android:id="@+id/radio_anilist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/discord_anilist_button" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="@+id/anilistCard"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="start"
android:padding="8dp"
app:boxStrokeColor="@color/text_input_layout_stroke_color"
app:cardBackgroundColor="@color/nav_bg"
app:cardCornerRadius="16dp"
app:contentPadding="4dp"
app:contentPaddingLeft="8dp"
app:contentPaddingRight="8dp"
tools:ignore="ContentDescription,TextContrastCheck">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="4dp"
android:fontFamily="@font/poppins_bold"
android:text="@string/view_my_anilist"
android:textColor="?attr/colorPrimary"
app:drawableTint="?attr/colorPrimary" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/anilistLinkPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:drawablePadding="4dp"
android:fontFamily="@font/poppins_bold"
android:text="@string/anilist_link"
android:textColor="?attr/colorPrimary"
app:drawableTint="?attr/colorPrimary" />
</LinearLayout>
<RadioButton
android:id="@+id/radio_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/discord_custom_button" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/buttonTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/button_text"
app:boxCornerRadiusTopStart="16dp"
app:boxCornerRadiusTopEnd="16dp"
app:boxCornerRadiusBottomStart="16dp"
app:boxCornerRadiusBottomEnd="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/buttonEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/linkTextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/button_link"
app:boxCornerRadiusTopStart="16dp"
app:boxCornerRadiusTopEnd="16dp"
app:boxCornerRadiusBottomStart="16dp"
app:boxCornerRadiusBottomEnd="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/linkEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<androidx.cardview.widget.CardView
android:id="@+id/customCard"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_gravity="start"
android:padding="8dp"
app:boxStrokeColor="@color/text_input_layout_stroke_color"
app:cardBackgroundColor="@color/nav_bg"
app:cardCornerRadius="16dp"
app:contentPadding="4dp"
app:contentPaddingLeft="8dp"
app:contentPaddingRight="8dp"
tools:ignore="ContentDescription,TextContrastCheck">
<TextView
android:id="@+id/customButtonPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawablePadding="4dp"
android:fontFamily="@font/poppins_bold"
android:text="@string/discord_custom_sample"
android:textColor="?attr/colorPrimary"
app:drawableTint="?attr/colorPrimary" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/customLinkPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="10dp"
android:drawablePadding="4dp"
android:fontFamily="@font/poppins_bold"
android:text="https://iwanttokillmyself.com"
android:textColor="?attr/colorPrimary"
app:drawableTint="?attr/colorPrimary" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
</androidx.core.widget.NestedScrollView>

View File

@@ -11,8 +11,11 @@
<string name="discord" translatable="false">https://discord.gg/4HPZ5nAWwM</string>
<string name="github" translatable="false">https://github.com/rebelonion/Dantotsu</string>
<string name="dantotsu" translatable="false">https://dantotsu.app/</string>
<string name="telegram" tools:ignore="Typos" translatable="false">https://t.me/+gzBCQExtLQo1YTNh </string>
<string name="coffee" translatable="false">https://www.buymeacoffee.com/rebelonion</string>
<string name="anilist_link">https://anilist.co/user/%1$s/</string>
<string name="home">Home</string>
<string name="anime">Anime</string>
@@ -652,6 +655,9 @@
<string name="discord_try_again">Try logging-in again</string>
<string name="error_loading_discord_user_data">Error loading Discord User Data</string>
<string name="discord_warning"><![CDATA[By logging in, your discord will now show what you are watching & reading on Dantotsu\n\nIf you are on invisible mode, logging in will make you online, when you open Dantotsu.\n\nThis does break the Discord TOS. \nAlthough Discord has never banned anyone for using Custom Rich Presence(what Dantotsu uses), You have still been warned.\n\nDantotsu is not responsible for anything that happens to your account.]]></string>
<string name="discord_dantotsu_button">Display dantotsu in the second button</string>
<string name="discord_anilist_button">Display your AniList profile</string>
<string name="discord_custom_button">Set up your own custom button</string>
<string name="warning">Warning</string>
<string name="view_anime">View Anime</string>
<string name="view_manga">View Manga</string>
@@ -810,4 +816,10 @@ Non quae tempore quo provident laudantium qui illo dolor vel quia dolor et exerc
<string name="donate">donate :)</string>
<string name="do_it">Do it!</string>
<string name="password">Password</string>
<string name="discord_rich_presence">Discord Rich Presence</string>
<string name="stream_on_dantotsu">Stream on Dantotsu</string>
<string name="view_my_anilist">View My AniList</string>
<string name="button_text">Button Text</string>
<string name="button_link">Button Link</string>
<string name="discord_custom_sample">Sample</string>
</resources>