Skip to content

Commit

Permalink
feat(rpc): progress bar (hope nothing breaks)
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush2622 committed Nov 3, 2024
1 parent 949ab7e commit 7ec889a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,5 @@ object Discord {
const val small_Image: String =
"mp:external/9NqpMxXs4ZNQtMG42L7hqINW92GqqDxgxS9Oh0Sp880/%3Fsize%3D48%26quality%3Dlossless%26name%3DDantotsu/https/cdn.discordapp.com/emojis/1167344924874784828.gif"
const val small_Image_AniList: String =
"mp:external/ORoccmSwMX55y1BUmiwf7OH9IlnztoO0MDpsGQnibpw/https/anilist.co/img/icons/icon.svg"
"https://anilist.co/img/icons/android-chrome-512x512.png"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ data class Activity(
@Serializable
data class Timestamps(
val start: Long? = null,
@SerialName("end")
val stop: Long? = null
)
}
121 changes: 67 additions & 54 deletions app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1114,60 +1114,6 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
)
initPlayer()
preloading = false
val context = this
val offline: Boolean = PrefManager.getVal(PrefName.OfflineMode)
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) {
"nothing" -> mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
)

"dantotsu" -> mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
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)
)
}

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)
}
DiscordServiceRunningSingleton.running = true
startService(intent)
}
}
updateProgress()
}
}
Expand Down Expand Up @@ -1358,6 +1304,72 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL

}

private fun discordRPC(){
val context = this
val ep = episode
val offline: Boolean = PrefManager.getVal(PrefName.OfflineMode)
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) {
"nothing" -> mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
)

"dantotsu" -> mutableListOf(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
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)
)
}

else -> mutableListOf()
}
val startTimestamp = Calendar.getInstance()
val durationInSeconds = if (exoPlayer.duration != C.TIME_UNSET) (exoPlayer.duration / 1000).toInt() else 1440

val endTimestamp = Calendar.getInstance().apply {
timeInMillis = startTimestamp.timeInMillis
add(Calendar.SECOND, durationInSeconds)
}
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
),
startTimestamp = startTimestamp.timeInMillis,
stopTimestamp = endTimestamp.timeInMillis,
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)
}
DiscordServiceRunningSingleton.running = true
startService(intent)
}
}
}
private fun initPlayer() {
checkNotch()

Expand Down Expand Up @@ -2042,6 +2054,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener, SessionAvailabilityL
exoPlayer.play()
if (episodeLength == 0f) {
episodeLength = exoPlayer.duration.toFloat()
discordRPC()
}
}
isBuffering = playbackState == Player.STATE_BUFFERING
Expand Down

0 comments on commit 7ec889a

Please sign in to comment.