1
0
mirror of https://gitlab.shinice.net/pixeldroid/PixelDroid synced 2025-02-01 08:06:43 +01:00

Show and dismiss when appropriate

This commit is contained in:
Matthieu 2022-10-23 22:58:41 +02:00
parent ac0d0ec47d
commit 03ca518415

View File

@ -13,7 +13,6 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
@ -60,6 +59,7 @@ import org.pixeldroid.app.utils.notificationsWorker.enablePullNotifications
import org.pixeldroid.app.utils.notificationsWorker.removeNotificationChannelsFromAccount import org.pixeldroid.app.utils.notificationsWorker.removeNotificationChannelsFromAccount
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException import java.io.IOException
import java.time.Instant
class MainActivity : BaseThemedWithoutBarActivity() { class MainActivity : BaseThemedWithoutBarActivity() {
@ -372,7 +372,10 @@ class MainActivity : BaseThemedWithoutBarActivity() {
0 -> R.id.page_1 0 -> R.id.page_1
1 -> R.id.page_2 1 -> R.id.page_2
2 -> R.id.page_3 2 -> R.id.page_3
3 -> R.id.page_4 3 -> {
setNotificationBadge(false)
R.id.page_4
}
4 -> R.id.page_5 4 -> R.id.page_5
else -> null else -> null
} }
@ -400,6 +403,7 @@ class MainActivity : BaseThemedWithoutBarActivity() {
true true
} ?: false } ?: false
} }
binding.tabs.setOnItemReselectedListener { item -> binding.tabs.setOnItemReselectedListener { item ->
item.itemPos()?.let { position -> item.itemPos()?.let { position ->
val page = val page =
@ -413,13 +417,18 @@ class MainActivity : BaseThemedWithoutBarActivity() {
lifecycleScope.launch { lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) { repeatOnLifecycle(Lifecycle.State.STARTED) {
user?.let { user?.let {
val maxId = db.notificationDao().latestNotification(it.user_id, it.instance_uri)?.id val lastNotification = db.notificationDao().latestNotification(it.user_id, it.instance_uri)
try { try {
val notification: List<Notification>? = apiHolder.api?.notifications( val notification: List<Notification>? = apiHolder.api?.notifications(
limit = "1", min_id = lastNotification?.id,
max_id = maxId limit = "20"
) )
if(notification?.isNotEmpty() == true) binding.tabs.getOrCreateBadge(R.id.page_4) val filtered = notification?.filter { notification ->
lastNotification == null || (notification.created_at
?: Instant.MIN) > (lastNotification.created_at ?: Instant.MIN)
}
val numberOfNewNotifications = if((filtered?.size ?: 20) >= 20) null else filtered?.size
if(filtered?.isNotEmpty() == true ) setNotificationBadge(true, numberOfNewNotifications)
} catch (exception: IOException) { } catch (exception: IOException) {
return@repeatOnLifecycle return@repeatOnLifecycle
} catch (exception: HttpException) { } catch (exception: HttpException) {
@ -433,6 +442,14 @@ class MainActivity : BaseThemedWithoutBarActivity() {
} }
} }
private fun setNotificationBadge(show: Boolean, count: Int? = null){
if(show){
val badge = binding.tabs.getOrCreateBadge(R.id.page_4)
if (count != null) badge.number = count
}
else binding.tabs.removeBadge(R.id.page_4)
}
fun BottomNavigationView.uncheckAllItems() { fun BottomNavigationView.uncheckAllItems() {
menu.setGroupCheckable(0, true, false) menu.setGroupCheckable(0, true, false)
for (i in 0 until menu.size()) { for (i in 0 until menu.size()) {