Get badges through the repository

This commit is contained in:
davide 2022-07-24 14:14:04 +02:00
parent 77fada1b02
commit 11eac7b141
3 changed files with 30 additions and 8 deletions

View File

@ -1044,8 +1044,10 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun reloadBadges() {
if (displayUnreadCount || displayAllCount) {
CoroutineScope(Dispatchers.Main).launch {
service.reloadBadges(applicationContext.isNetworkAvailable())
reloadBadgeContent()
if (applicationContext.isNetworkAvailable()) {
repository.reloadBadges()
reloadBadgeContent()
}
}
}
}
@ -1053,15 +1055,15 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar
private fun reloadBadgeContent() {
if (displayUnreadCount) {
tabNewBadge
.setText(searchService.badgeUnread.toString())
.setText(repository.badgeUnread.toString())
.maybeShow()
}
if (displayAllCount) {
tabArchiveBadge
.setText(searchService.badgeAll.toString())
.setText(repository.badgeAll.toString())
.maybeShow()
tabStarredBadge
.setText(searchService.badgeStarred.toString())
.setText(repository.badgeStarred.toString())
.maybeShow()
}
}

View File

@ -12,10 +12,13 @@ interface Repository {
// API
var apiMajorVersion: Int
var badgeUnread: Int
var badgeAll: Int
var badgeStarred: Int
suspend fun getNewerItems(): ArrayList<SelfossModel.Item>
suspend fun getOlderItems(): ArrayList<SelfossModel.Item>
fun stats(): SelfossModel.Stats
suspend fun reloadBadges(): Boolean
fun getTags(): List<SelfossModel.Tag>
suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
suspend fun getSources(): ArrayList<SelfossModel.Source>?

View File

@ -35,11 +35,15 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
private var search: String? = null
override var apiMajorVersion = 0
override var badgeUnread = 0
override var badgeAll = 0
override var badgeStarred = 0
init {
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
CoroutineScope(Dispatchers.Main).launch {
updateApiVersion()
reloadBadges()
}
}
@ -104,8 +108,17 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return tmpItems
}
override fun stats(): SelfossModel.Stats {
TODO("Not yet implemented")
override suspend fun reloadBadges(): Boolean {
// TODO: Check connectivity, calculate from DB
var success = false
val response = api.stats()
if (response != null) {
badgeUnread = response.unread
badgeAll = response.total
badgeStarred = response.starred
success = true
}
return success
}
override fun getTags(): List<SelfossModel.Tag> {
@ -125,23 +138,27 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
override suspend fun markAsRead(id: Int): Boolean {
// TODO: Check success, store in DB
api.markAsRead(id.toString())
badgeUnread -= 1
return true
}
override suspend fun unmarkAsRead(id: Int): Boolean {
// TODO: Check success, store in DB
api.unmarkAsRead(id.toString())
badgeUnread += 1
return true }
override suspend fun starr(id: Int): Boolean {
// TODO: Check success, store in DB
api.starr(id.toString())
badgeStarred += 1
return true
}
override suspend fun unstarr(id: Int): Boolean {
// TODO: Check success, store in DB
api.unstarr(id.toString())
badgeStarred -= 1
return true
}