Date format (#340)

* Set date format according to api version.

* Check new date format in items.

* Global date formatter.

* Ensure api version has been fetched before checking.

* Move api check to MyApp.

* Store api version and select correct date formatter when offline.

* Check api in Home, allow null values.
This commit is contained in:
davidoskky 2021-03-22 19:00:42 +01:00 committed by GitHub
parent 626c9e2797
commit 56720659ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 51 additions and 4 deletions

View File

@ -104,11 +104,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
private var itemsCaching: Boolean = false private var itemsCaching: Boolean = false
private var updateSources: Boolean = true private var updateSources: Boolean = true
private var hiddenTags: List<String> = emptyList() private var hiddenTags: List<String> = emptyList()
private var apiVersionMajor: Int = 0
private var periodicRefresh = false private var periodicRefresh = false
private var refreshMinutes: Long = 360L private var refreshMinutes: Long = 360L
private var refreshWhenChargingOnly = false private var refreshWhenChargingOnly = false
private val dateTimeFormatter = "yyyy-MM-dd HH:mm:ss"
private lateinit var tabNewBadge: TextBadgeItem private lateinit var tabNewBadge: TextBadgeItem
private lateinit var tabArchiveBadge: TextBadgeItem private lateinit var tabArchiveBadge: TextBadgeItem
@ -195,6 +195,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
handleSharedPrefs() handleSharedPrefs()
getApiMajorVersion()
getElementsAccordingToTab() getElementsAccordingToTab()
} }
@ -330,6 +332,28 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
} }
} }
private fun getApiMajorVersion() {
api.apiVersion.enqueue(object : Callback<ApiVersion> {
override fun onFailure(call: Call<ApiVersion>, t: Throwable) {
if (apiVersionMajor >= 4) {
Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX"
}
}
override fun onResponse(call: Call<ApiVersion>, response: Response<ApiVersion>) {
if(response.body() != null) {
val version = response.body() as ApiVersion
apiVersionMajor = version.getApiMajorVersion()
sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).commit()
if (apiVersionMajor >= 4) {
Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX"
}
}
}
})
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
@ -442,6 +466,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
if (refreshMinutes <= 15) { if (refreshMinutes <= 15) {
refreshMinutes = 15 refreshMinutes = 15
} }
apiVersionMajor = sharedPref.getInt("apiVersionMajor", 0)
} }
private fun handleThemeBinding() { private fun handleThemeBinding() {
@ -906,7 +932,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
thread { thread {
val dbItems = db.itemsDao().items().map { it.toView() }.sortedByDescending { val dbItems = db.itemsDao().items().map { it.toView() }.sortedByDescending {
SimpleDateFormat(dateTimeFormatter).parse(it.datetime) SimpleDateFormat(Config.dateTimeFormatter).parse(it.datetime)
} }
runOnUiThread { runOnUiThread {
if (dbItems.isNotEmpty()) { if (dbItems.isNotEmpty()) {
@ -1010,7 +1036,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
thread { thread {
val dbItems = db.itemsDao().items().map { it.toView() }.sortedByDescending { val dbItems = db.itemsDao().items().map { it.toView() }.sortedByDescending {
SimpleDateFormat(dateTimeFormatter).parse(it.datetime) SimpleDateFormat(Config.dateTimeFormatter).parse(it.datetime)
} }
runOnUiThread { runOnUiThread {
if (dbItems.isNotEmpty()) { if (dbItems.isNotEmpty()) {

View File

@ -215,6 +215,9 @@ class SelfossApi(
fun update(): Call<String> = fun update(): Call<String> =
service.update(userName, password) service.update(userName, password)
val apiVersion: Call<ApiVersion>
get() = service.version()
val sources: Call<List<Source>> val sources: Call<List<Source>>
get() = service.sources(userName, password) get() = service.sources(userName, password)

View File

@ -48,6 +48,19 @@ data class Spout(
@SerializedName("description") val description: String @SerializedName("description") val description: String
) )
data class ApiVersion(
@SerializedName("version") val version: String?,
@SerializedName("apiversion") val apiversion: String?
) {
fun getApiMajorVersion() : Int {
var versionNumber = 0
if (apiversion != null) {
versionNumber = apiversion.substringBefore(".").toInt()
}
return versionNumber
}
}
data class Source( data class Source(
@SerializedName("id") val id: String, @SerializedName("id") val id: String,
@SerializedName("title") val title: String, @SerializedName("title") val title: String,

View File

@ -103,6 +103,9 @@ internal interface SelfossService {
@Query("password") password: String @Query("password") password: String
): Call<List<Source>> ): Call<List<Source>>
@GET("api/about")
fun version(): Call<ApiVersion>
@DELETE("source/{id}") @DELETE("source/{id}")
fun deleteSource( fun deleteSource(
@Path("id") id: String, @Path("id") id: String,

View File

@ -40,6 +40,8 @@ class Config(c: Context) {
const val newItemsChannelId = "new-items-channel-id" const val newItemsChannelId = "new-items-channel-id"
var dateTimeFormatter = "yyyy-MM-dd HH:mm:ss"
fun logoutAndRedirect( fun logoutAndRedirect(
c: Context, c: Context,
callingActivity: Activity, callingActivity: Activity,

View File

@ -22,7 +22,7 @@ fun String.toTextDrawableString(c: Context): String {
fun Item.sourceAndDateText(): String { fun Item.sourceAndDateText(): String {
val formattedDate: String = try { val formattedDate: String = try {
" " + DateUtils.getRelativeTimeSpanString( " " + DateUtils.getRelativeTimeSpanString(
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.datetime).time, SimpleDateFormat(Config.dateTimeFormatter).parse(this.datetime).time,
Date().time, Date().time,
DateUtils.MINUTE_IN_MILLIS, DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE DateUtils.FORMAT_ABBREV_RELATIVE