feat: notification clicking

This commit is contained in:
rebelonion 2024-03-14 14:40:48 -05:00
parent ddd59643c5
commit 4ed74b664b
3 changed files with 35 additions and 7 deletions

View file

@ -43,6 +43,8 @@ import ani.dantotsu.home.MangaFragment
import ani.dantotsu.home.NoInternet import ani.dantotsu.home.NoInternet
import ani.dantotsu.media.MediaDetailsActivity import ani.dantotsu.media.MediaDetailsActivity
import ani.dantotsu.others.CustomBottomDialog import ani.dantotsu.others.CustomBottomDialog
import ani.dantotsu.profile.activity.FeedActivity
import ani.dantotsu.profile.activity.NotificationActivity
import ani.dantotsu.settings.saving.PrefManager import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.settings.saving.PrefManager.asLiveBool import ani.dantotsu.settings.saving.PrefManager.asLiveBool
import ani.dantotsu.settings.saving.PrefName import ani.dantotsu.settings.saving.PrefName
@ -226,6 +228,7 @@ class MainActivity : AppCompatActivity() {
val fragmentToLoad = extras.getString("FRAGMENT_TO_LOAD") val fragmentToLoad = extras.getString("FRAGMENT_TO_LOAD")
val mediaId = extras.getInt("mediaId", -1) val mediaId = extras.getInt("mediaId", -1)
val commentId = extras.getInt("commentId", -1) val commentId = extras.getInt("commentId", -1)
val activityId = extras.getInt("activityId", -1)
if (fragmentToLoad != null && mediaId != -1 && commentId != -1) { if (fragmentToLoad != null && mediaId != -1 && commentId != -1) {
val detailIntent = Intent(this, MediaDetailsActivity::class.java).apply { val detailIntent = Intent(this, MediaDetailsActivity::class.java).apply {
@ -234,7 +237,19 @@ class MainActivity : AppCompatActivity() {
putExtra("commentId", commentId) putExtra("commentId", commentId)
} }
startActivity(detailIntent) startActivity(detailIntent)
return } else if (fragmentToLoad == "FEED" && activityId != -1) {
val feedIntent = Intent(this, FeedActivity::class.java).apply {
putExtra("FRAGMENT_TO_LOAD", "NOTIFICATIONS")
putExtra("activityId", activityId)
}
startActivity(feedIntent)
} else if (fragmentToLoad == "NOTIFICATIONS" && activityId != -1) {
val notificationIntent = Intent(this, NotificationActivity::class.java).apply {
putExtra("FRAGMENT_TO_LOAD", "NOTIFICATIONS")
putExtra("activityId", activityId)
}
startActivity(notificationIntent)
} }
} }
val offlineMode: Boolean = PrefManager.getVal(PrefName.OfflineMode) val offlineMode: Boolean = PrefManager.getVal(PrefName.OfflineMode)

View file

@ -10,6 +10,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.work.Worker import androidx.work.Worker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import ani.dantotsu.MainActivity
import ani.dantotsu.R import ani.dantotsu.R
import ani.dantotsu.connections.anilist.Anilist import ani.dantotsu.connections.anilist.Anilist
import ani.dantotsu.profile.activity.ActivityItemBuilder import ani.dantotsu.profile.activity.ActivityItemBuilder
@ -43,7 +44,7 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet
newNotifications?.forEach { newNotifications?.forEach {
if (!filteredTypes.contains(it.notificationType)) { if (!filteredTypes.contains(it.notificationType)) {
val content = ActivityItemBuilder.getContent(it) val content = ActivityItemBuilder.getContent(it)
val notification = createNotification(applicationContext, content) val notification = createNotification(applicationContext, content, it.id)
if (ActivityCompat.checkSelfPermission( if (ActivityCompat.checkSelfPermission(
applicationContext, applicationContext,
Manifest.permission.POST_NOTIFICATIONS Manifest.permission.POST_NOTIFICATIONS
@ -70,11 +71,17 @@ class AnilistNotificationWorker(appContext: Context, workerParams: WorkerParamet
private fun createNotification( private fun createNotification(
context: Context, context: Context,
content: String content: String,
activityId: Int? = null
): android.app.Notification { ): android.app.Notification {
val title = "New Anilist Notification" val title = "New Anilist Notification"
val intent = Intent(applicationContext, FeedActivity::class.java) val intent = Intent(applicationContext, MainActivity::class.java).apply {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
putExtra("FRAGMENT_TO_LOAD", "NOTIFICATIONS")
if (activityId != null) {
putExtra("activityId", activityId)
}
}
val pendingIntent = PendingIntent.getActivity( val pendingIntent = PendingIntent.getActivity(
applicationContext, applicationContext,
0, 0,

View file

@ -51,10 +51,16 @@ class NotificationActivity : AppCompatActivity() {
onBackPressed() onBackPressed()
} }
binding.listProgressBar.visibility = ViewGroup.VISIBLE binding.listProgressBar.visibility = ViewGroup.VISIBLE
val activityId = intent.getIntExtra("activityId", -1)
lifecycleScope.launch { lifecycleScope.launch {
val res = Anilist.query.getNotifications(Anilist.userid?:0) val resetNotification = activityId == -1
val res = Anilist.query.getNotifications(Anilist.userid?:0, resetNotification = resetNotification)
res?.data?.page?.notifications?.let { notifications -> res?.data?.page?.notifications?.let { notifications ->
notificationList = notifications notificationList = if (activityId != -1) {
notifications.filter { it.id == activityId }
} else {
notifications
}
adapter.update(notificationList.map { NotificationItem(it, ::onNotificationClick) }) adapter.update(notificationList.map { NotificationItem(it, ::onNotificationClick) })
} }
withContext(Dispatchers.Main){ withContext(Dispatchers.Main){