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:
parent
626c9e2797
commit
56720659ee
@ -104,11 +104,11 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
private var itemsCaching: Boolean = false
|
||||
private var updateSources: Boolean = true
|
||||
private var hiddenTags: List<String> = emptyList()
|
||||
private var apiVersionMajor: Int = 0
|
||||
|
||||
private var periodicRefresh = false
|
||||
private var refreshMinutes: Long = 360L
|
||||
private var refreshWhenChargingOnly = false
|
||||
private val dateTimeFormatter = "yyyy-MM-dd HH:mm:ss"
|
||||
|
||||
private lateinit var tabNewBadge: TextBadgeItem
|
||||
private lateinit var tabArchiveBadge: TextBadgeItem
|
||||
@ -195,6 +195,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
|
||||
handleSharedPrefs()
|
||||
|
||||
getApiMajorVersion()
|
||||
|
||||
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() {
|
||||
super.onResume()
|
||||
|
||||
@ -442,6 +466,8 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
if (refreshMinutes <= 15) {
|
||||
refreshMinutes = 15
|
||||
}
|
||||
|
||||
apiVersionMajor = sharedPref.getInt("apiVersionMajor", 0)
|
||||
}
|
||||
|
||||
private fun handleThemeBinding() {
|
||||
@ -906,7 +932,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
|
||||
thread {
|
||||
val dbItems = db.itemsDao().items().map { it.toView() }.sortedByDescending {
|
||||
SimpleDateFormat(dateTimeFormatter).parse(it.datetime)
|
||||
SimpleDateFormat(Config.dateTimeFormatter).parse(it.datetime)
|
||||
}
|
||||
runOnUiThread {
|
||||
if (dbItems.isNotEmpty()) {
|
||||
@ -1010,7 +1036,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
|
||||
thread {
|
||||
val dbItems = db.itemsDao().items().map { it.toView() }.sortedByDescending {
|
||||
SimpleDateFormat(dateTimeFormatter).parse(it.datetime)
|
||||
SimpleDateFormat(Config.dateTimeFormatter).parse(it.datetime)
|
||||
}
|
||||
runOnUiThread {
|
||||
if (dbItems.isNotEmpty()) {
|
||||
|
@ -215,6 +215,9 @@ class SelfossApi(
|
||||
fun update(): Call<String> =
|
||||
service.update(userName, password)
|
||||
|
||||
val apiVersion: Call<ApiVersion>
|
||||
get() = service.version()
|
||||
|
||||
val sources: Call<List<Source>>
|
||||
get() = service.sources(userName, password)
|
||||
|
||||
|
@ -48,6 +48,19 @@ data class Spout(
|
||||
@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(
|
||||
@SerializedName("id") val id: String,
|
||||
@SerializedName("title") val title: String,
|
||||
|
@ -103,6 +103,9 @@ internal interface SelfossService {
|
||||
@Query("password") password: String
|
||||
): Call<List<Source>>
|
||||
|
||||
@GET("api/about")
|
||||
fun version(): Call<ApiVersion>
|
||||
|
||||
@DELETE("source/{id}")
|
||||
fun deleteSource(
|
||||
@Path("id") id: String,
|
||||
|
@ -40,6 +40,8 @@ class Config(c: Context) {
|
||||
|
||||
const val newItemsChannelId = "new-items-channel-id"
|
||||
|
||||
var dateTimeFormatter = "yyyy-MM-dd HH:mm:ss"
|
||||
|
||||
fun logoutAndRedirect(
|
||||
c: Context,
|
||||
callingActivity: Activity,
|
||||
|
@ -22,7 +22,7 @@ fun String.toTextDrawableString(c: Context): String {
|
||||
fun Item.sourceAndDateText(): String {
|
||||
val formattedDate: String = try {
|
||||
" " + DateUtils.getRelativeTimeSpanString(
|
||||
SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(this.datetime).time,
|
||||
SimpleDateFormat(Config.dateTimeFormatter).parse(this.datetime).time,
|
||||
Date().time,
|
||||
DateUtils.MINUTE_IN_MILLIS,
|
||||
DateUtils.FORMAT_ABBREV_RELATIVE
|
||||
|
Loading…
Reference in New Issue
Block a user