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

View File

@ -12,10 +12,13 @@ interface Repository {
// API // API
var apiMajorVersion: Int var apiMajorVersion: Int
var badgeUnread: Int
var badgeAll: Int
var badgeStarred: Int
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> suspend fun getNewerItems(): ArrayList<SelfossModel.Item>
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> suspend fun getOlderItems(): ArrayList<SelfossModel.Item>
fun stats(): SelfossModel.Stats suspend fun reloadBadges(): Boolean
fun getTags(): List<SelfossModel.Tag> fun getTags(): List<SelfossModel.Tag>
suspend fun getSpouts(): Map<String, SelfossModel.Spout>? suspend fun getSpouts(): Map<String, SelfossModel.Spout>?
suspend fun getSources(): ArrayList<SelfossModel.Source>? 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 private var search: String? = null
override var apiMajorVersion = 0 override var apiMajorVersion = 0
override var badgeUnread = 0
override var badgeAll = 0
override var badgeStarred = 0
init { init {
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found // TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
updateApiVersion() updateApiVersion()
reloadBadges()
} }
} }
@ -104,8 +108,17 @@ class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDet
return tmpItems return tmpItems
} }
override fun stats(): SelfossModel.Stats { override suspend fun reloadBadges(): Boolean {
TODO("Not yet implemented") // 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> { 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 { override suspend fun markAsRead(id: Int): Boolean {
// TODO: Check success, store in DB // TODO: Check success, store in DB
api.markAsRead(id.toString()) api.markAsRead(id.toString())
badgeUnread -= 1
return true return true
} }
override suspend fun unmarkAsRead(id: Int): Boolean { override suspend fun unmarkAsRead(id: Int): Boolean {
// TODO: Check success, store in DB // TODO: Check success, store in DB
api.unmarkAsRead(id.toString()) api.unmarkAsRead(id.toString())
badgeUnread += 1
return true } return true }
override suspend fun starr(id: Int): Boolean { override suspend fun starr(id: Int): Boolean {
// TODO: Check success, store in DB // TODO: Check success, store in DB
api.starr(id.toString()) api.starr(id.toString())
badgeStarred += 1
return true return true
} }
override suspend fun unstarr(id: Int): Boolean { override suspend fun unstarr(id: Int): Boolean {
// TODO: Check success, store in DB // TODO: Check success, store in DB
api.unstarr(id.toString()) api.unstarr(id.toString())
badgeStarred -= 1
return true return true
} }