feat: remove other tabs when opening page from notification
This commit is contained in:
parent
5a78d68f67
commit
fb65cb601e
1 changed files with 76 additions and 95 deletions
|
@ -7,6 +7,7 @@ import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
|
import androidx.core.view.updateLayoutParams
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
|
@ -17,6 +18,7 @@ import ani.dantotsu.connections.anilist.Anilist
|
||||||
import ani.dantotsu.connections.anilist.api.Notification
|
import ani.dantotsu.connections.anilist.api.Notification
|
||||||
import ani.dantotsu.databinding.FragmentNotificationsBinding
|
import ani.dantotsu.databinding.FragmentNotificationsBinding
|
||||||
import ani.dantotsu.media.MediaDetailsActivity
|
import ani.dantotsu.media.MediaDetailsActivity
|
||||||
|
import ani.dantotsu.navBarHeight
|
||||||
import ani.dantotsu.notifications.comment.CommentStore
|
import ani.dantotsu.notifications.comment.CommentStore
|
||||||
import ani.dantotsu.notifications.subscription.SubscriptionStore
|
import ani.dantotsu.notifications.subscription.SubscriptionStore
|
||||||
import ani.dantotsu.profile.ProfileActivity
|
import ani.dantotsu.profile.ProfileActivity
|
||||||
|
@ -25,9 +27,8 @@ import ani.dantotsu.setBaseline
|
||||||
import ani.dantotsu.settings.saving.PrefManager
|
import ani.dantotsu.settings.saving.PrefManager
|
||||||
import ani.dantotsu.settings.saving.PrefName
|
import ani.dantotsu.settings.saving.PrefName
|
||||||
import com.xwray.groupie.GroupieAdapter
|
import com.xwray.groupie.GroupieAdapter
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
class NotificationFragment(
|
class NotificationFragment(
|
||||||
val type: NotificationType,
|
val type: NotificationType,
|
||||||
|
@ -49,88 +50,65 @@ class NotificationFragment(
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
val navbar = (activity as NotificationActivity).navBar
|
val navbar = (activity as NotificationActivity).navBar
|
||||||
binding.listRecyclerView.setBaseline(navbar)
|
binding.notificationRecyclerView.setBaseline(navbar)
|
||||||
binding.listRecyclerView.adapter = adapter
|
binding.notificationRecyclerView.adapter = adapter
|
||||||
binding.listRecyclerView.layoutManager = LinearLayoutManager(context)
|
binding.notificationRecyclerView.layoutManager = LinearLayoutManager(context)
|
||||||
|
binding.notificationRefresh.updateLayoutParams<ViewGroup.MarginLayoutParams> {
|
||||||
|
bottomMargin = navBarHeight
|
||||||
|
}
|
||||||
binding.emptyTextView.text = getString(R.string.no_notifications)
|
binding.emptyTextView.text = getString(R.string.no_notifications)
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
val list = when (type) {
|
getList()
|
||||||
"getOne" -> getOne(getID)
|
if (adapter.itemCount == 0) {
|
||||||
"media" -> getMediaUpdates()
|
|
||||||
"user" -> getUserUpdates()
|
|
||||||
"subscription" -> getSubscriptions()
|
|
||||||
"comment" -> getComments()
|
|
||||||
else -> listOf()
|
|
||||||
}
|
|
||||||
adapter.addAll(list.map { NotificationItem(it, ::onClick) })
|
|
||||||
if (adapter.itemCount != 0) {
|
|
||||||
binding.listProgressBar.isVisible = false
|
|
||||||
}else{
|
|
||||||
binding.listProgressBar.isVisible = false
|
|
||||||
binding.emptyTextView.isVisible = true
|
binding.emptyTextView.isVisible = true
|
||||||
}
|
}
|
||||||
|
binding.notificationProgressBar.isVisible = false
|
||||||
}
|
}
|
||||||
binding.followSwipeRefresh.setOnRefreshListener {
|
binding.notificationSwipeRefresh.setOnRefreshListener {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
adapter.clear()
|
adapter.clear()
|
||||||
currentPage = 1
|
currentPage = 1
|
||||||
val list = when (type) {
|
getList()
|
||||||
"getOne" -> getOne(getID)
|
binding.notificationSwipeRefresh.isRefreshing = false
|
||||||
"media" -> getMediaUpdates()
|
|
||||||
"user" -> getUserUpdates()
|
|
||||||
"subscription" -> getSubscriptions()
|
|
||||||
"comment" -> getComments()
|
|
||||||
else -> listOf()
|
|
||||||
}
|
|
||||||
adapter.addAll(list.map { NotificationItem(it, ::onClick) })
|
|
||||||
binding.followSwipeRefresh.isRefreshing = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
binding.listRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
binding.notificationRecyclerView.addOnScrollListener(object :
|
||||||
|
RecyclerView.OnScrollListener() {
|
||||||
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
||||||
super.onScrolled(recyclerView, dx, dy)
|
super.onScrolled(recyclerView, dx, dy)
|
||||||
if (shouldLoadMore()) {
|
if (shouldLoadMore()) {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch {
|
||||||
|
getList()
|
||||||
|
binding.notificationRefresh.isVisible = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun getList() {
|
||||||
val list = when (type) {
|
val list = when (type) {
|
||||||
"media" -> getMediaUpdates()
|
NotificationType.ONE -> getNotificationsFiltered(false) { it.id == getID }
|
||||||
"user" -> getUserUpdates()
|
NotificationType.MEDIA -> getNotificationsFiltered { it.media != null }
|
||||||
|
NotificationType.USER -> getNotificationsFiltered { it.media == null }
|
||||||
|
NotificationType.SUBSCRIPTION -> getSubscriptions()
|
||||||
|
NotificationType.COMMENT -> getComments()
|
||||||
else -> listOf()
|
else -> listOf()
|
||||||
}
|
}
|
||||||
binding.followRefresh.visibility = View.VISIBLE
|
adapter.addAll(list.map { NotificationItem(it, ::onClick) })
|
||||||
adapter.addAll(list.map {
|
|
||||||
NotificationItem(
|
|
||||||
it,
|
|
||||||
::onClick
|
|
||||||
)
|
|
||||||
})
|
|
||||||
binding.followRefresh.visibility = View.GONE
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun getNotificationsFiltered(filter: (Notification) -> Boolean): List<Notification> {
|
private suspend fun getNotificationsFiltered(
|
||||||
val userId = Anilist.userid ?: PrefManager.getVal<String>(PrefName.AnilistUserId).toIntOrNull() ?: 0
|
reset: Boolean = true,
|
||||||
val res = withContext(Dispatchers.IO) {
|
filter: (Notification) -> Boolean
|
||||||
Anilist.query.getNotifications(userId, currentPage)
|
): List<Notification> {
|
||||||
}
|
val userId =
|
||||||
currentPage = res?.data?.page?.pageInfo?.currentPage?.plus(1) ?: 1
|
Anilist.userid ?: PrefManager.getVal<String>(PrefName.AnilistUserId).toIntOrNull() ?: 0
|
||||||
hasNextPage = res?.data?.page?.pageInfo?.hasNextPage ?: false
|
val res = Anilist.query.getNotifications(userId, currentPage, reset)?.data?.page
|
||||||
return res?.data?.page?.notifications?.filter(filter) ?: listOf()
|
currentPage = res?.pageInfo?.currentPage?.plus(1) ?: 1
|
||||||
}
|
hasNextPage = res?.pageInfo?.hasNextPage ?: false
|
||||||
|
return res?.notifications?.filter(filter) ?: listOf()
|
||||||
private suspend fun getMediaUpdates(): List<Notification> {
|
|
||||||
return getNotificationsFiltered { it.media != null }
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getUserUpdates(): List<Notification> {
|
|
||||||
return getNotificationsFiltered { it.media == null }
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun getOne(id: Int): List<Notification> {
|
|
||||||
return getNotificationsFiltered { it.id == id }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSubscriptions(): List<Notification> {
|
private fun getSubscriptions(): List<Notification> {
|
||||||
|
@ -138,8 +116,8 @@ class NotificationFragment(
|
||||||
PrefName.SubscriptionNotificationStore,
|
PrefName.SubscriptionNotificationStore,
|
||||||
null
|
null
|
||||||
) ?: listOf()
|
) ?: listOf()
|
||||||
return list.sortedByDescending{ (it.time / 1000L).toInt() }
|
return list.sortedByDescending { (it.time / 1000L).toInt() }
|
||||||
.filter{ it.image != null }.map {
|
.filter { it.image != null }.map {
|
||||||
Notification(
|
Notification(
|
||||||
it.type,
|
it.type,
|
||||||
System.currentTimeMillis().toInt(),
|
System.currentTimeMillis().toInt(),
|
||||||
|
@ -160,7 +138,7 @@ class NotificationFragment(
|
||||||
null
|
null
|
||||||
) ?: listOf()
|
) ?: listOf()
|
||||||
return list
|
return list
|
||||||
.sortedByDescending {(it.time / 1000L).toInt()}
|
.sortedByDescending { (it.time / 1000L).toInt() }
|
||||||
.map {
|
.map {
|
||||||
Notification(
|
Notification(
|
||||||
it.type.toString(),
|
it.type.toString(),
|
||||||
|
@ -175,12 +153,13 @@ class NotificationFragment(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun shouldLoadMore(): Boolean {
|
private fun shouldLoadMore(): Boolean {
|
||||||
val layoutManager = binding.listRecyclerView.layoutManager as LinearLayoutManager
|
val layoutManager =
|
||||||
val adapter = binding.listRecyclerView.adapter
|
(binding.notificationRecyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
|
||||||
|
val adapter = binding.notificationRecyclerView.adapter
|
||||||
|
|
||||||
return hasNextPage && !binding.followRefresh.isVisible && adapter?.itemCount != 0 &&
|
return hasNextPage && !binding.notificationRefresh.isVisible && adapter?.itemCount != 0 &&
|
||||||
layoutManager.findLastVisibleItemPosition() == (adapter!!.itemCount - 1) &&
|
layoutManager == (adapter!!.itemCount - 1) &&
|
||||||
!binding.listRecyclerView.canScrollVertically(1)
|
!binding.notificationRecyclerView.canScrollVertically(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun onClick(
|
fun onClick(
|
||||||
|
@ -191,28 +170,28 @@ class NotificationFragment(
|
||||||
when (type) {
|
when (type) {
|
||||||
NotificationClickType.USER -> {
|
NotificationClickType.USER -> {
|
||||||
ContextCompat.startActivity(
|
ContextCompat.startActivity(
|
||||||
requireContext(), Intent( requireContext(), ProfileActivity::class.java)
|
requireContext(), Intent(requireContext(), ProfileActivity::class.java)
|
||||||
.putExtra("userId", id), null
|
.putExtra("userId", id), null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationClickType.MEDIA -> {
|
NotificationClickType.MEDIA -> {
|
||||||
ContextCompat.startActivity(
|
ContextCompat.startActivity(
|
||||||
requireContext(), Intent( requireContext(), MediaDetailsActivity::class.java)
|
requireContext(), Intent(requireContext(), MediaDetailsActivity::class.java)
|
||||||
.putExtra("mediaId", id), null
|
.putExtra("mediaId", id), null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationClickType.ACTIVITY -> {
|
NotificationClickType.ACTIVITY -> {
|
||||||
ContextCompat.startActivity(
|
ContextCompat.startActivity(
|
||||||
requireContext(), Intent( requireContext(), FeedActivity::class.java)
|
requireContext(), Intent(requireContext(), FeedActivity::class.java)
|
||||||
.putExtra("activityId", id), null
|
.putExtra("activityId", id), null
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationClickType.COMMENT -> {
|
NotificationClickType.COMMENT -> {
|
||||||
ContextCompat.startActivity(
|
ContextCompat.startActivity(
|
||||||
requireContext(), Intent( requireContext(), MediaDetailsActivity::class.java)
|
requireContext(), Intent(requireContext(), MediaDetailsActivity::class.java)
|
||||||
.putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
|
.putExtra("FRAGMENT_TO_LOAD", "COMMENTS")
|
||||||
.putExtra("mediaId", id)
|
.putExtra("mediaId", id)
|
||||||
.putExtra("commentId", optional ?: -1),
|
.putExtra("commentId", optional ?: -1),
|
||||||
|
@ -220,6 +199,7 @@ class NotificationFragment(
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NotificationClickType.UNDEFINED -> {
|
NotificationClickType.UNDEFINED -> {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
|
@ -233,6 +213,7 @@ class NotificationFragment(
|
||||||
binding.root.setBaseline((activity as NotificationActivity).navBar)
|
binding.root.setBaseline((activity as NotificationActivity).navBar)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
enum class NotificationClickType {
|
enum class NotificationClickType {
|
||||||
USER, MEDIA, ACTIVITY, COMMENT, UNDEFINED
|
USER, MEDIA, ACTIVITY, COMMENT, UNDEFINED
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue