Create a badge if newer notification exists on server than in db

This commit is contained in:
Matthieu 2022-08-03 22:28:20 +02:00
parent 2db94e69cd
commit ac0d0ec47d
1 changed files with 29 additions and 0 deletions

View File

@ -13,8 +13,11 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.GravityCompat
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.paging.ExperimentalPagingApi
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.adapter.FragmentStateAdapter
@ -32,6 +35,7 @@ import com.mikepenz.materialdrawer.model.interfaces.*
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader
import com.mikepenz.materialdrawer.util.DrawerImageLoader
import com.mikepenz.materialdrawer.widget.AccountHeaderView
import kotlinx.coroutines.launch
import org.ligi.tracedroid.sending.sendTraceDroidStackTracesIfExist
import org.pixeldroid.app.databinding.ActivityMainBinding
import org.pixeldroid.app.postCreation.camera.CameraFragment
@ -43,6 +47,7 @@ import org.pixeldroid.app.profile.ProfileActivity
import org.pixeldroid.app.searchDiscover.SearchDiscoverFragment
import org.pixeldroid.app.settings.SettingsActivity
import org.pixeldroid.app.utils.BaseThemedWithoutBarActivity
import org.pixeldroid.app.utils.api.objects.Notification
import org.pixeldroid.app.utils.db.addUser
import org.pixeldroid.app.utils.db.entities.HomeStatusDatabaseEntity
import org.pixeldroid.app.utils.db.entities.PublicFeedStatusDatabaseEntity
@ -240,6 +245,7 @@ class MainActivity : BaseThemedWithoutBarActivity() {
}
}
}
private fun getUpdatedAccount() {
if (hasInternet(applicationContext)) {
@ -402,6 +408,29 @@ class MainActivity : BaseThemedWithoutBarActivity() {
(page as? CachedFeedFragment<*>)?.onTabReClicked()
}
}
// Fetch one notification to show a badge if there are new notifications
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
user?.let {
val maxId = db.notificationDao().latestNotification(it.user_id, it.instance_uri)?.id
try {
val notification: List<Notification>? = apiHolder.api?.notifications(
limit = "1",
max_id = maxId
)
if(notification?.isNotEmpty() == true) binding.tabs.getOrCreateBadge(R.id.page_4)
} catch (exception: IOException) {
return@repeatOnLifecycle
} catch (exception: HttpException) {
return@repeatOnLifecycle
}
}
}
}
}
fun BottomNavigationView.uncheckAllItems() {