Skip to content

Commit

Permalink
feat(Media): Toggleable Comments (#521)
Browse files Browse the repository at this point in the history
* Smooth theme transitions
  • Loading branch information
Sadwhy authored Nov 17, 2024
1 parent 56e5577 commit d1e2ca8
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 40 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/ani/dantotsu/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ class App : MultiDexApplication() {
LogcatLogger.install(AndroidLogcatLogger(LogPriority.VERBOSE))
}

if (PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 0) {
if (BuildConfig.FLAVOR.contains("fdroid")) {
PrefManager.setVal(PrefName.CommentsEnabled, 2)
} else {
PrefManager.setVal(PrefName.CommentsEnabled, 1)
}
}

CoroutineScope(Dispatchers.IO).launch {
animeExtensionManager = Injekt.get()
animeExtensionManager.findAvailableExtensions()
Expand All @@ -128,7 +136,9 @@ class App : MultiDexApplication() {
downloadAddonManager = Injekt.get()
torrentAddonManager.init()
downloadAddonManager.init()
CommentsAPI.fetchAuthToken(this@App)
if (PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1) {
CommentsAPI.fetchAuthToken(this@App)
}

val useAlarmManager = PrefManager.getVal<Boolean>(PrefName.UseAlarmManager)
val scheduler = TaskScheduler.create(this@App, useAlarmManager)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/ani/dantotsu/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class MainActivity : AppCompatActivity() {
.get() > 0 || preferences.mangaExtensionUpdatesCount().get() > 0
) {
snackString(R.string.extension_updates_available)
?.setDuration(Snackbar.LENGTH_LONG)
?.setDuration(Snackbar.LENGTH_SHORT)
?.setAction(R.string.review) {
startActivity(Intent(this, ExtensionsActivity::class.java))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get

object CommentsAPI {
private const val ADDRESS: String = "https://api.dantotsu.app"
private const val API_ADDRESS: String = "https://api.dantotsu.app"
private const val LOCAL_HOST: String = "https://127.0.0.1"
private var isOnline: Boolean = true
private var commentsEnabled = PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1
private val ADDRESS: String get() = if (commentsEnabled) API_ADDRESS else LOCAL_HOST
var authToken: String? = null
var userId: String? = null
var isBanned: Boolean = false
Expand Down Expand Up @@ -369,10 +372,9 @@ object CommentsAPI {
}
errorMessage("Failed to login after multiple attempts")
}

private fun errorMessage(reason: String) {
Logger.log(reason)
if (isOnline) snackString(reason)
if (commentsEnabled) Logger.log(reason)
if (isOnline && commentsEnabled) snackString(reason)
}

fun logout() {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/ani/dantotsu/media/MediaDetailsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ class MediaDetailsActivity : AppCompatActivity(), AppBarLayout.OnOffsetChangedLi
navBar.createTab(R.drawable.ic_round_comment_24, R.string.comments, R.id.comment)
navBar.addTab(infoTab)
navBar.addTab(watchTab)
navBar.addTab(commentTab)
if (PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1) {
navBar.addTab(commentTab)
}
if (model.continueMedia == null && media.cameFromContinue) {
model.continueMedia = PrefManager.getVal(PrefName.ContinueMedia)
selected = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,18 @@ class AlarmManagerScheduler(private val context: Context) : TaskScheduler {
return
}
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = when (taskType) {
TaskType.COMMENT_NOTIFICATION -> Intent(
context,
CommentNotificationReceiver::class.java
)

TaskType.ANILIST_NOTIFICATION -> Intent(
context,
AnilistNotificationReceiver::class.java
)
val intent = when {
taskType == TaskType.COMMENT_NOTIFICATION && PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1 ->
Intent(context, CommentNotificationReceiver::class.java)

TaskType.SUBSCRIPTION_NOTIFICATION -> Intent(
context,
SubscriptionNotificationReceiver::class.java
)
taskType == TaskType.ANILIST_NOTIFICATION ->
Intent(context, AnilistNotificationReceiver::class.java)

taskType == TaskType.SUBSCRIPTION_NOTIFICATION ->
Intent(context, SubscriptionNotificationReceiver::class.java)

else -> return
}

val pendingIntent = PendingIntent.getBroadcast(
Expand Down Expand Up @@ -64,21 +61,18 @@ class AlarmManagerScheduler(private val context: Context) : TaskScheduler {

override fun cancelTask(taskType: TaskType) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = when (taskType) {
TaskType.COMMENT_NOTIFICATION -> Intent(
context,
CommentNotificationReceiver::class.java
)

TaskType.ANILIST_NOTIFICATION -> Intent(
context,
AnilistNotificationReceiver::class.java
)
val intent = when {
taskType == TaskType.COMMENT_NOTIFICATION && PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1 ->
Intent(context, CommentNotificationReceiver::class.java)

taskType == TaskType.ANILIST_NOTIFICATION ->
Intent(context, AnilistNotificationReceiver::class.java)

taskType == TaskType.SUBSCRIPTION_NOTIFICATION ->
Intent(context, SubscriptionNotificationReceiver::class.java)

TaskType.SUBSCRIPTION_NOTIFICATION -> Intent(
context,
SubscriptionNotificationReceiver::class.java
)
else -> return
}

val pendingIntent = PendingIntent.getBroadcast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import ani.dantotsu.R
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.databinding.ActivityNotificationBinding
import ani.dantotsu.initActivity
import ani.dantotsu.navBarHeight
Expand All @@ -24,6 +26,8 @@ class NotificationActivity : AppCompatActivity() {
lateinit var binding: ActivityNotificationBinding
private var selected: Int = 0
lateinit var navBar: AnimatedBottomBar
private val CommentsEnabled = PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ThemeManager(this).applyTheme()
Expand All @@ -38,19 +42,23 @@ class NotificationActivity : AppCompatActivity() {
binding.root.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = navBarHeight
}
val tabs = listOf(

val tabs = mutableListOf(
Pair(R.drawable.ic_round_person_24, "User"),
Pair(R.drawable.ic_round_movie_filter_24, "Media"),
Pair(R.drawable.ic_round_notifications_active_24, "Subs"),
Pair(R.drawable.ic_round_comment_24, "Comments")
Pair(R.drawable.ic_round_notifications_active_24, "Subs")
)
if (CommentsEnabled) {
tabs.add(Pair(R.drawable.ic_round_comment_24, "Comments"))
}

tabs.forEach { (icon, title) -> navBar.addTab(navBar.createTab(icon, title)) }

binding.notificationBack.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
val getOne = intent.getIntExtra("activityId", -1)
if (getOne != -1) navBar.isVisible = false
binding.notificationViewPager.isUserInputEnabled = false
binding.notificationViewPager.adapter = ViewPagerAdapter(supportFragmentManager, lifecycle, getOne)
binding.notificationViewPager.adapter = ViewPagerAdapter(supportFragmentManager, lifecycle, getOne, CommentsEnabled)
binding.notificationViewPager.setCurrentItem(selected, false)
navBar.selectTabAt(selected)
navBar.setOnTabSelectListener(object : AnimatedBottomBar.OnTabSelectListener {
Expand All @@ -65,18 +73,21 @@ class NotificationActivity : AppCompatActivity() {
}
})
}

override fun onResume() {
super.onResume()
if (this::navBar.isInitialized) {
navBar.selectTabAt(selected)
}
}

private class ViewPagerAdapter(
fragmentManager: FragmentManager,
lifecycle: Lifecycle,
val id: Int = -1
val id: Int = -1,
val commentsEnabled: Boolean
) : FragmentStateAdapter(fragmentManager, lifecycle) {
override fun getItemCount(): Int = if (id != -1) 1 else 4
override fun getItemCount(): Int = if (id != -1) 1 else if (commentsEnabled) 4 else 3

override fun createFragment(position: Int): Fragment = when (position) {
0 -> newInstance(if (id != -1) ONE else USER, id)
Expand All @@ -86,4 +97,4 @@ class NotificationActivity : AppCompatActivity() {
else -> newInstance(MEDIA)
}
}
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/ani/dantotsu/settings/SettingsAccountActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ani.dantotsu.openLinkInBrowser
import ani.dantotsu.others.CustomBottomDialog
import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefName
import ani.dantotsu.snackString
import ani.dantotsu.startMainActivity
import ani.dantotsu.statusBarHeight
import ani.dantotsu.themes.ThemeManager
Expand Down Expand Up @@ -233,10 +234,31 @@ class SettingsAccountActivity : AppCompatActivity() {
},
isActivity = true
),
Settings(
type = 2,
name = getString(R.string.comments_button),
desc = getString(R.string.comments_button_desc),
icon = R.drawable.ic_round_comment_24,
isChecked = PrefManager.getVal<Int>(PrefName.CommentsEnabled) == 1,
switch = { isChecked, _ ->
PrefManager.setVal(PrefName.CommentsEnabled, if (isChecked) 1 else 2 )
reload()
},
isVisible = Anilist.token != null
),
)
)
binding.settingsRecyclerView.layoutManager =
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

}
fun reload() {
snackString(getString(R.string.restart_app_extra))
//snackString(R.string.restart_app_extra)
//?.setDuration(Snackbar.LENGTH_LONG)
//?.setAction(R.string.do_it) {
//startMainActivity(this@SettingsAccountActivity)
//} Disabled for now. Doesn't update the ADDRESS even after this
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enum class PrefName(val data: Pref) { //TODO: Split this into multiple files
IncludeAnimeList(Pref(Location.General, Boolean::class, true)),
IncludeMangaList(Pref(Location.General, Boolean::class, true)),
AdultOnly(Pref(Location.General, Boolean::class, false)),
CommentsEnabled(Pref(Location.General, Int::class, 0)),

//User Interface
UseOLED(Pref(Location.UI, Boolean::class, false)),
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@
<string name="fav_anime">Favourite Anime</string>
<string name="fav_manga">Favourite Manga</string>
<string name="restart_app">Restart the app?</string>
<string name="restart_app_extra">Restart the app to apply changes</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="current_page">Current Page</string>
Expand Down Expand Up @@ -746,6 +747,8 @@
<string name="restore_settings">Import Settings</string>
<string name="restore_settings_underline"><u>Restore Settings</u></string>
<string name="try_internal_cast_experimental">Try Internal Cast (Experimental)</string>
<string name="comments_button">Enable Comments</string>
<string name="comments_button_desc">Dantotsu\'s very own comments server</string>

<string name="comments">Comments</string>
<string name="newest">Newest</string>
Expand Down

0 comments on commit d1e2ca8

Please sign in to comment.