Skip to content

Commit

Permalink
Merge pull request #73 from rebelonion/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rebelonion authored Dec 6, 2023
2 parents 7f92ac6 + 55521ab commit 1d4257b
Show file tree
Hide file tree
Showing 33 changed files with 467 additions and 112 deletions.
44 changes: 25 additions & 19 deletions app/src/main/java/ani/dantotsu/connections/UpdateProgress.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,33 @@ import kotlinx.coroutines.launch
import kotlin.math.roundToInt

fun updateProgress(media: Media, number: String) {
if (Anilist.userid != null) {
CoroutineScope(Dispatchers.IO).launch {
val a = number.toFloatOrNull()?.roundToInt()
if ((a ?: 0) > (media.userProgress ?: 0)) {
Anilist.mutation.editList(
media.id,
a,
status = if (media.userStatus == "REPEATING") media.userStatus else "CURRENT"
)
MAL.query.editList(
media.idMAL,
media.anime != null,
a, null,
if (media.userStatus == "REPEATING") media.userStatus!! else "CURRENT"
)
toast(currContext()?.getString(R.string.setting_progress, a))
val incognito = currContext()?.getSharedPreferences("Dantotsu", 0)
?.getBoolean("incognito", false) ?: false
if (!incognito) {
if (Anilist.userid != null) {
CoroutineScope(Dispatchers.IO).launch {
val a = number.toFloatOrNull()?.toInt()
if ((a ?: 0) > (media.userProgress ?: 0)) {
Anilist.mutation.editList(
media.id,
a,
status = if (media.userStatus == "REPEATING") media.userStatus else "CURRENT"
)
MAL.query.editList(
media.idMAL,
media.anime != null,
a, null,
if (media.userStatus == "REPEATING") media.userStatus!! else "CURRENT"
)
toast(currContext()?.getString(R.string.setting_progress, a))
}
media.userProgress = a
Refresh.all()
}
media.userProgress = a
Refresh.all()
} else {
toast(currContext()?.getString(R.string.login_anilist_account))
}
} else {
toast(currContext()?.getString(R.string.login_anilist_account))
toast("Sneaky sneaky :3")
}
}
64 changes: 64 additions & 0 deletions app/src/main/java/ani/dantotsu/download/DownloadsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,75 @@ class DownloadsManager(private val context: Context) {
}
} else {
Toast.makeText(context, "Directory does not exist", Toast.LENGTH_SHORT).show()
cleanDownloads()
}
downloadsList.removeAll { it.title == title }
saveDownloads()
}

private fun cleanDownloads() {
cleanDownload(Download.Type.MANGA)
cleanDownload(Download.Type.ANIME)
cleanDownload(Download.Type.NOVEL)
}

private fun cleanDownload(type: Download.Type) {
// remove all folders that are not in the downloads list
val subDirectory = if (type == Download.Type.MANGA) {
"Manga"
} else if (type == Download.Type.ANIME) {
"Anime"
} else {
"Novel"
}
val directory = File(
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),
"Dantotsu/$subDirectory"
)
val downloadsSubList = if (type == Download.Type.MANGA) {
mangaDownloads
} else if (type == Download.Type.ANIME) {
animeDownloads
} else {
novelDownloads
}
if (directory.exists()) {
val files = directory.listFiles()
if (files != null) {
for (file in files) {
if (!downloadsSubList.any { it.title == file.name }) {
val deleted = file.deleteRecursively()
}
}
}
}
//now remove all downloads that do not have a folder
val iterator = downloadsList.iterator()
while (iterator.hasNext()) {
val download = iterator.next()
val downloadDir = File(directory, download.title)
if ((!downloadDir.exists() && download.type == type) || download.title.isBlank()) {
iterator.remove()
}
}
}

fun saveDownloadsListToJSONFileInDownloadsFolder(downloadsList: List<Download>) //for debugging
{
val jsonString = gson.toJson(downloadsList)
val file = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"Dantotsu/downloads.json"
)
if (file.parentFile?.exists() == false) {
file.parentFile?.mkdirs()
}
if (!file.exists()) {
file.createNewFile()
}
file.writeText(jsonString)
}

fun queryDownload(download: Download): Boolean {
return downloadsList.contains(download)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class OfflineMangaFragment : Fragment(), OfflineMangaSearchListener {
builder.setNegativeButton("No") { _, _ ->
// Do nothing
}
builder.show()
val dialog = builder.show()
dialog.window?.setDimAmount(0.8f)
true
}

Expand Down Expand Up @@ -284,7 +285,7 @@ class OfflineMangaFragment : Fragment(), OfflineMangaSearchListener {
} else {
null
}
val title = mediaModel.nameMAL ?: "unknown"
val title = mediaModel.nameMAL ?: mediaModel.nameRomaji
val score = ((if (mediaModel.userScore == 0) (mediaModel.meanScore
?: 0) else mediaModel.userScore) / 10.0).toString()
val isOngoing = false
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/ani/dantotsu/media/MediaAdaptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ class MediaAdaptor(
return type
}

fun randomOptionClick() { //used for user list
val media = mediaList?.random()
if (media != null) {
mediaList?.let {
clicked(
it.indexOf(media),
null
)
}
}

}

inner class MediaViewHolder(val binding: ItemMediaCompactBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class AnimeWatchAdapter(
fragment.onIconPressed(style, reversed)
}
binding.animeScanlatorTop.visibility = View.GONE
binding.animeDownloadTop.visibility = View.GONE
//Episode Handling
handleEpisodes()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class AnimeWatchFragment : Fragment() {
if (allSettings.size > 1) {
val names = allSettings.map { it.lang }.toTypedArray()
var selectedIndex = 0
AlertDialog.Builder(requireContext())
val dialog = AlertDialog.Builder(requireContext())
.setTitle("Select a Source")
.setSingleChoiceItems(names, selectedIndex) { _, which ->
selectedIndex = which
Expand Down Expand Up @@ -347,6 +347,7 @@ class AnimeWatchFragment : Fragment() {
return@setNegativeButton
}
.show()
dialog.window?.setDimAmount(0.8f)
} else {
// If there's only one setting, proceed with the fragment transaction
requireActivity().runOnUiThread {
Expand Down
99 changes: 60 additions & 39 deletions app/src/main/java/ani/dantotsu/media/anime/ExoplayerView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.drawable.Animatable
import android.hardware.Sensor
import android.hardware.SensorManager
import android.media.AudioManager
import android.media.AudioManager.*
Expand Down Expand Up @@ -186,8 +187,6 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {

var rotation = 0

private var rpc: RPC? = null

override fun onAttachedToWindow() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val displayCutout = window.decorView.rootWindowInsets.displayCutout
Expand Down Expand Up @@ -385,14 +384,10 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
}, AUDIO_CONTENT_TYPE_MOVIE, AUDIOFOCUS_GAIN)

if (System.getInt(contentResolver, System.ACCELEROMETER_ROTATION, 0) != 1) {
requestedOrientation = rotation
exoRotate.setOnClickListener {
requestedOrientation = rotation
it.visibility = View.GONE
}
orientationListener =
object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
override fun onOrientationChanged(orientation: Int) {
println(orientation)
if (orientation in 45..135) {
if (rotation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) exoRotate.visibility =
View.VISIBLE
Expand All @@ -405,6 +400,12 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
}
}
orientationListener?.enable()

requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
exoRotate.setOnClickListener {
requestedOrientation = rotation
it.visibility = View.GONE
}
}

setupSubFormatting(playerView, settings)
Expand Down Expand Up @@ -998,36 +999,45 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
preloading = false
val context = this

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(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
RPC.Link(
"Stream on Dantotsu",
"https://github.com/rebelonion/Dantotsu/"
val incognito = baseContext.getSharedPreferences("Dantotsu", MODE_PRIVATE)
.getBoolean("incognito", false)
if (isOnline(context) && 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(
RPC.Link(getString(R.string.view_anime), media.shareLink ?: ""),
RPC.Link(
"Stream on Dantotsu",
"https://github.com/rebelonion/Dantotsu/"
)
)
)
)
)
)

val intent = Intent(context, DiscordService::class.java).apply {
putExtra("presence", presence)
val intent = Intent(context, DiscordService::class.java).apply {
putExtra("presence", presence)
}
DiscordServiceRunningSingleton.running = true
startService(intent)
}
DiscordServiceRunningSingleton.running = true
startService(intent)
}

updateProgress()
Expand Down Expand Up @@ -1101,7 +1111,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
val speedDialog =
AlertDialog.Builder(this, R.style.DialogTheme).setTitle(getString(R.string.speed))
exoSpeed.setOnClickListener {
speedDialog.setSingleChoiceItems(speedsName, curSpeed) { dialog, i ->
val dialog = speedDialog.setSingleChoiceItems(speedsName, curSpeed) { dialog, i ->
if (isInitialized) {
saveData("${media.id}_speed", i, this)
speed = speeds[i]
Expand All @@ -1112,6 +1122,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
hideSystemBars()
}
}.show()
dialog.window?.setDimAmount(0.8f)
}
speedDialog.setOnCancelListener { hideSystemBars() }

Expand Down Expand Up @@ -1151,6 +1162,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
if (showProgressDialog && Anilist.userid != null && if (media.isAdult) settings.updateForH else true)
AlertDialog.Builder(this, R.style.MyPopup)
.setTitle(getString(R.string.auto_update, media.userPreferredName))
.setMessage(getString(R.string.incognito_will_not_update))
.apply {
setOnCancelListener { hideSystemBars() }
setCancelable(false)
Expand Down Expand Up @@ -1349,7 +1361,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
)
)
)
AlertDialog.Builder(this, R.style.DialogTheme)
val dialog = AlertDialog.Builder(this, R.style.DialogTheme)
.setTitle(getString(R.string.continue_from, time)).apply {
setCancelable(false)
setPositiveButton(getString(R.string.yes)) { d, _ ->
Expand All @@ -1362,6 +1374,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
d.dismiss()
}
}.show()
dialog.window?.setDimAmount(0.8f)
} else buildExoplayer()
}

Expand Down Expand Up @@ -1394,7 +1407,7 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
exoPlayer.addAnalyticsListener(EventLogger())
isInitialized = true
}
/*private fun selectSubtitleTrack() {
/*private fun selectSubtitleTrack() { saving this for later
// Get the current track groups
val trackGroups = exoPlayer.currentTrackGroups
Expand Down Expand Up @@ -1426,9 +1439,11 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
exoPlayer.release()
VideoCache.release()
mediaSession?.release()
val stopIntent = Intent(this, DiscordService::class.java)
DiscordServiceRunningSingleton.running = false
stopService(stopIntent)
if(DiscordServiceRunningSingleton.running) {
val stopIntent = Intent(this, DiscordService::class.java)
DiscordServiceRunningSingleton.running = false
stopService(stopIntent)
}

}

Expand Down Expand Up @@ -1532,6 +1547,12 @@ class ExoplayerView : AppCompatActivity(), Player.Listener {
if (exoPlayer.duration < playbackPosition)
exoPlayer.seekTo(0)

//if playbackPosition is within 92% of the episode length, reset it to 0
if (playbackPosition > exoPlayer.duration.toFloat() * 0.92) {
playbackPosition = 0
exoPlayer.seekTo(0)
}

if (!isTimeStampsLoaded && settings.timeStampsEnabled) {
val dur = exoPlayer.duration
lifecycleScope.launch(Dispatchers.IO) {
Expand Down
Loading

0 comments on commit 1d4257b

Please sign in to comment.