Compare commits
2 Commits
f7a29d66ca
...
b7b4ff1485
Author | SHA1 | Date | |
---|---|---|---|
b7b4ff1485 | |||
|
728eb747c3 |
@ -13,6 +13,7 @@ import bou.amine.apps.readerforselfossv2.DI.networkModule
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.Config
|
||||
import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth
|
||||
import bou.amine.apps.readerforselfossv2.repository.Repository
|
||||
import bou.amine.apps.readerforselfossv2.utils.NetworkStatus
|
||||
import com.bumptech.glide.Glide
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import com.ftinc.scoop.Scoop
|
||||
@ -25,7 +26,7 @@ class MyApp : MultiDexApplication(), DIAware {
|
||||
|
||||
override val di by DI.lazy {
|
||||
import(networkModule)
|
||||
bind<Repository>() with singleton { Repository(instance(), instance()) }
|
||||
bind<Repository>() with singleton { Repository(instance(), instance(), NetworkStatus(applicationContext)) }
|
||||
}
|
||||
|
||||
private lateinit var config: Config
|
||||
|
@ -36,6 +36,9 @@ kotlin {
|
||||
|
||||
//Logging
|
||||
implementation("io.github.aakira:napier:2.6.1")
|
||||
|
||||
// Network information
|
||||
implementation("com.github.ln-12:multiplatform-connectivity-status:1.1.0")
|
||||
}
|
||||
}
|
||||
val commonTest by getting {
|
||||
|
@ -0,0 +1,17 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import android.content.Context
|
||||
import com.github.`ln-12`.library.ConnectivityStatus
|
||||
|
||||
actual class NetworkStatus(context: Context) {
|
||||
private val connectivityStatus = ConnectivityStatus(context)
|
||||
actual val current = connectivityStatus.isNetworkConnected
|
||||
|
||||
actual fun start() {
|
||||
connectivityStatus.start()
|
||||
}
|
||||
|
||||
actual fun stop() {
|
||||
connectivityStatus.stop()
|
||||
}
|
||||
}
|
@ -5,16 +5,18 @@ import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
|
||||
import bou.amine.apps.readerforselfossv2.utils.DateUtils
|
||||
import bou.amine.apps.readerforselfossv2.utils.ItemType
|
||||
import bou.amine.apps.readerforselfossv2.utils.NetworkStatus
|
||||
import com.russhwolf.settings.Settings
|
||||
import io.github.aakira.napier.Napier
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService) {
|
||||
class Repository(private val api: SelfossApi, private val apiDetails: ApiDetailsService, networkStatus: NetworkStatus) {
|
||||
val settings = Settings()
|
||||
|
||||
var items = ArrayList<SelfossModel.Item>()
|
||||
private val isConnectionAvailable = networkStatus.current
|
||||
|
||||
var baseUrl = apiDetails.getBaseUrl()
|
||||
lateinit var dateUtils: DateUtils
|
||||
@ -36,6 +38,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
set(value) {field = if (value < 0) { 0 } else { value } }
|
||||
|
||||
init {
|
||||
networkStatus.start()
|
||||
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
updateApiVersion()
|
||||
@ -45,40 +48,65 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
}
|
||||
|
||||
suspend fun getNewerItems(): ArrayList<SelfossModel.Item> {
|
||||
// TODO: Check connectivity, use the updatedSince parameter
|
||||
val fetchedItems = api.getItems(displayedItems.type,
|
||||
// TODO: Use the updatedSince parameter
|
||||
if (isConnectionAvailable.value) {
|
||||
val fetchedItems = api.getItems(
|
||||
displayedItems.type,
|
||||
settings.getString("prefer_api_items_number", "200").toInt(),
|
||||
offset = 0,
|
||||
tagFilter?.tag,
|
||||
sourceFilter?.id?.toLong(),
|
||||
searchFilter,
|
||||
null)
|
||||
null
|
||||
)
|
||||
|
||||
if (fetchedItems != null) {
|
||||
items = ArrayList(fetchedItems)
|
||||
}
|
||||
} else {
|
||||
// TODO: Provide an error message if the connection is not available.
|
||||
// TODO: Get items from the database
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
suspend fun getOlderItems(): ArrayList<SelfossModel.Item> {
|
||||
// TODO: Check connectivity
|
||||
if (isConnectionAvailable.value) {
|
||||
val offset = items.size
|
||||
val fetchedItems = api.getItems(displayedItems.type,
|
||||
val fetchedItems = api.getItems(
|
||||
displayedItems.type,
|
||||
settings.getString("prefer_api_items_number", "200").toInt(),
|
||||
offset,
|
||||
tagFilter?.tag,
|
||||
sourceFilter?.id?.toLong(),
|
||||
searchFilter,
|
||||
null)
|
||||
null
|
||||
)
|
||||
|
||||
if (fetchedItems != null) {
|
||||
appendItems(fetchedItems)
|
||||
}
|
||||
} else {
|
||||
// TODO: Provide an error message
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? =
|
||||
api.getItems(itemType.type, 200, 0, tagFilter?.tag, sourceFilter?.id?.toLong(), searchFilter, null)
|
||||
suspend fun allItems(itemType: ItemType): List<SelfossModel.Item>? {
|
||||
return if (isConnectionAvailable.value) {
|
||||
api.getItems(
|
||||
itemType.type,
|
||||
200,
|
||||
0,
|
||||
tagFilter?.tag,
|
||||
sourceFilter?.id?.toLong(),
|
||||
searchFilter,
|
||||
null
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
private fun appendItems(fetchedItems: List<SelfossModel.Item>) {
|
||||
// TODO: Store in DB if enabled by user
|
||||
@ -94,8 +122,8 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
}
|
||||
|
||||
suspend fun reloadBadges(): Boolean {
|
||||
// TODO: Check connectivity, calculate from DB
|
||||
var success = false
|
||||
if (isConnectionAvailable.value) {
|
||||
val response = api.stats()
|
||||
if (response != null) {
|
||||
badgeUnread = response.unread
|
||||
@ -103,68 +131,99 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
badgeStarred = response.starred
|
||||
success = true
|
||||
}
|
||||
} else {
|
||||
// TODO: Compute badges from database
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
suspend fun getTags(): List<SelfossModel.Tag>? {
|
||||
// TODO: Check success, store in DB
|
||||
return api.tags()
|
||||
// TODO: Store in DB
|
||||
return if (isConnectionAvailable.value) {
|
||||
api.tags()
|
||||
} else {
|
||||
// TODO: Compute from database
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getSpouts(): Map<String, SelfossModel.Spout>? {
|
||||
// TODO: Check success, store in DB
|
||||
return api.spouts()
|
||||
// TODO: Store in DB
|
||||
return if (isConnectionAvailable.value) {
|
||||
api.spouts()
|
||||
} else {
|
||||
// TODO: Compute from database
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getSources(): ArrayList<SelfossModel.Source>? {
|
||||
// TODO: Check success
|
||||
return api.sources()
|
||||
// TODO: Store in DB
|
||||
return if (isConnectionAvailable.value) {
|
||||
api.sources()
|
||||
} else {
|
||||
// TODO: Compute from database
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun markAsRead(id: Int): Boolean {
|
||||
// TODO: Check internet connection
|
||||
val success = api.markAsRead(id.toString())?.isSuccess == true
|
||||
var success = false
|
||||
|
||||
if (isConnectionAvailable.value) {
|
||||
success = api.markAsRead(id.toString())?.isSuccess == true
|
||||
|
||||
if (success) {
|
||||
markAsReadLocally(items.first { it.id == id })
|
||||
}
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
suspend fun unmarkAsRead(id: Int): Boolean {
|
||||
// TODO: Check internet connection
|
||||
val success = api.unmarkAsRead(id.toString())?.isSuccess == true
|
||||
var success = false
|
||||
|
||||
if (isConnectionAvailable.value) {
|
||||
success = api.unmarkAsRead(id.toString())?.isSuccess == true
|
||||
|
||||
if (success) {
|
||||
unmarkAsReadLocally(items.first { it.id == id })
|
||||
}
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
suspend fun starr(id: Int): Boolean {
|
||||
// TODO: Check success, store in DB
|
||||
val success = api.starr(id.toString())?.isSuccess == true
|
||||
var success = false
|
||||
|
||||
if (isConnectionAvailable.value) {
|
||||
success = api.starr(id.toString())?.isSuccess == true
|
||||
|
||||
if (success) {
|
||||
starrLocally(items.first { it.id == id })
|
||||
}
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
suspend fun unstarr(id: Int): Boolean {
|
||||
// TODO: Check internet connection
|
||||
val success = api.unstarr(id.toString())?.isSuccess == true
|
||||
var success = false
|
||||
|
||||
if (isConnectionAvailable.value) {
|
||||
success = api.unstarr(id.toString())?.isSuccess == true
|
||||
|
||||
if (success) {
|
||||
unstarrLocally(items.first { it.id == id })
|
||||
}
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
suspend fun markAllAsRead(ids: List<Int>): Boolean {
|
||||
// TODO: Check Internet connectivity, store in DB
|
||||
var success = false
|
||||
|
||||
val success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true
|
||||
if (isConnectionAvailable.value) {
|
||||
success = api.markAllAsRead(ids.map { it.toString() })?.isSuccess == true
|
||||
|
||||
if (success) {
|
||||
val itemsToMark = items.filter { it.id in ids }
|
||||
@ -172,6 +231,7 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
markAsReadLocally(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
return success
|
||||
}
|
||||
|
||||
@ -214,46 +274,52 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
tags: String,
|
||||
filter: String
|
||||
): Boolean {
|
||||
// TODO: Check connectivity
|
||||
val response = api.createSourceForVersion(
|
||||
var response = false
|
||||
if (isConnectionAvailable.value) {
|
||||
response = api.createSourceForVersion(
|
||||
title,
|
||||
url,
|
||||
spout,
|
||||
tags,
|
||||
filter,
|
||||
apiMajorVersion
|
||||
)
|
||||
)?.isSuccess == true
|
||||
}
|
||||
|
||||
return response != null
|
||||
return response
|
||||
}
|
||||
|
||||
suspend fun deleteSource(id: Int): Boolean {
|
||||
// TODO: Check connectivity, store in DB
|
||||
// TODO: Store in DB
|
||||
var success = false
|
||||
if (isConnectionAvailable.value) {
|
||||
val response = api.deleteSource(id)
|
||||
if (response != null) {
|
||||
success = response.isSuccess
|
||||
}
|
||||
}
|
||||
|
||||
return success
|
||||
}
|
||||
|
||||
suspend fun updateRemote(): Boolean {
|
||||
// TODO: Handle connectivity issues
|
||||
val response = api.update()
|
||||
return response?.isSuccess ?: false
|
||||
var response = false
|
||||
if (isConnectionAvailable.value) {
|
||||
response = api.update()?.isSuccess == true
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
suspend fun login(): Boolean {
|
||||
var result = false
|
||||
if (isConnectionAvailable.value) {
|
||||
try {
|
||||
val response = api.login()
|
||||
if (response != null && response.isSuccess) {
|
||||
result = true
|
||||
}
|
||||
result = response?.isSuccess == true
|
||||
} catch (cause: Throwable) {
|
||||
Napier.e(cause.stackTraceToString(), tag = "RepositoryImpl.updateRemote")
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@ -271,13 +337,14 @@ class Repository(private val api: SelfossApi, private val apiDetails: ApiDetails
|
||||
}
|
||||
|
||||
private suspend fun updateApiVersion() {
|
||||
// TODO: Handle connectivity issues
|
||||
apiMajorVersion = settings.getInt("apiVersionMajor", 0)
|
||||
|
||||
if (isConnectionAvailable.value) {
|
||||
val fetchedVersion = api.version()
|
||||
if (fetchedVersion != null) {
|
||||
apiMajorVersion = fetchedVersion.getApiMajorVersion()
|
||||
settings.putInt("apiVersionMajor", apiMajorVersion)
|
||||
} else {
|
||||
apiMajorVersion = settings.getInt("apiVersionMajor", 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,9 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
|
||||
expect class NetworkStatus {
|
||||
val current: MutableStateFlow<Boolean>
|
||||
fun start()
|
||||
fun stop()
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import com.github.`ln-12`.library.ConnectivityStatus
|
||||
import kotlinx.coroutines.MainScope
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
actual class NetworkStatus {
|
||||
private val connectivityStatus: ConnectivityStatus = ConnectivityStatus()
|
||||
actual val current: MutableStateFlow<Boolean> = connectivityStatus.isNetworkConnected
|
||||
|
||||
actual fun start() {
|
||||
connectivityStatus.start()
|
||||
}
|
||||
|
||||
actual fun stop() {
|
||||
connectivityStatus.stop()
|
||||
}
|
||||
|
||||
fun getStatus(success: (Boolean) -> Unit) {
|
||||
MainScope().launch {
|
||||
connectivityStatus.isNetworkConnected.collect { status ->
|
||||
success(status)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user