Inject repository in the Article Fragment
The Repository is now injected in the Article Fragment and the DateUtils class was modified not to rely on apiDetailsService
This commit is contained in:
@ -7,8 +7,11 @@ interface Repository {
|
||||
// TODO: remove the items variables in favor of storing everything in the database
|
||||
var items: List<SelfossModel.Item>
|
||||
var selectedItems: List<SelfossModel.Item>
|
||||
var baseUrl: String
|
||||
|
||||
// API
|
||||
var apiMajorVersion: Int
|
||||
|
||||
fun getMoreItems(): List<SelfossModel.Item>
|
||||
fun stats(): SelfossModel.Stats
|
||||
fun getTags(): List<SelfossModel.Tag>
|
||||
|
@ -2,14 +2,28 @@ package bou.amine.apps.readerforselfossv2.repository
|
||||
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossApi
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
|
||||
import com.russhwolf.settings.Settings
|
||||
import io.github.aakira.napier.Napier
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class RepositoryImpl(private val api: SelfossApi) : Repository {
|
||||
class RepositoryImpl(private val api: SelfossApi, private val apiDetails: ApiDetailsService) : Repository {
|
||||
val settings = Settings()
|
||||
|
||||
override lateinit var items: List<SelfossModel.Item>
|
||||
override lateinit var selectedItems: List<SelfossModel.Item>
|
||||
override var baseUrl = apiDetails.getBaseUrl()
|
||||
|
||||
override var apiMajorVersion = 0
|
||||
|
||||
init {
|
||||
// TODO: Dispatchers.IO not available in KMM, an alternative solution should be found
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
updateApiVersion()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getMoreItems(): List<SelfossModel.Item> {
|
||||
TODO("Not yet implemented")
|
||||
@ -92,5 +106,17 @@ class RepositoryImpl(private val api: SelfossApi) : Repository {
|
||||
|
||||
override fun refreshLoginInformation() {
|
||||
api.refreshLoginInformation()
|
||||
baseUrl = apiDetails.getBaseUrl()
|
||||
}
|
||||
|
||||
private suspend fun updateApiVersion() {
|
||||
// TODO: Handle connectivity issues
|
||||
val fetchedVersion = api.version()
|
||||
if (fetchedVersion != null) {
|
||||
apiMajorVersion = fetchedVersion.getApiMajorVersion()
|
||||
settings.putInt("apiVersionMajor", apiMajorVersion)
|
||||
} else {
|
||||
apiMajorVersion = settings.getInt("apiVersionMajor", 0)
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,6 @@ package bou.amine.apps.readerforselfossv2.utils
|
||||
|
||||
import android.text.format.DateUtils
|
||||
import bou.amine.apps.readerforselfossv2.rest.SelfossModel
|
||||
import bou.amine.apps.readerforselfossv2.service.ApiDetailsService
|
||||
import java.time.Instant
|
||||
import java.time.LocalDateTime
|
||||
import java.time.OffsetDateTime
|
||||
@ -15,12 +14,12 @@ fun SelfossModel.Item.parseDate(dateUtils: bou.amine.apps.readerforselfossv2.uti
|
||||
fun SelfossModel.Item.parseRelativeDate(dateUtils: bou.amine.apps.readerforselfossv2.utils.DateUtils): String =
|
||||
dateUtils.parseRelativeDate(this.datetime)
|
||||
|
||||
class DateUtils(private val apiDetailsService: ApiDetailsService) {
|
||||
class DateUtils(private val apiMajorVersion: Int) {
|
||||
fun parseDate(dateString: String): Instant {
|
||||
|
||||
val FORMATTERV1 = "yyyy-MM-dd HH:mm:ss"
|
||||
|
||||
return if (apiDetailsService.getApiVersion() >= 4) {
|
||||
return if (apiMajorVersion >= 4) {
|
||||
OffsetDateTime.parse(dateString).toInstant()
|
||||
} else {
|
||||
LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(FORMATTERV1)).toInstant(ZoneOffset.UTC)
|
||||
|
Reference in New Issue
Block a user