From a2933ac7638970a9c8c86da78e0503ac677e3051 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Fri, 12 Nov 2021 21:31:17 +0100 Subject: [PATCH] Use java.date to parse dates (#377) * Use java.date to parse dates * Enable core library desugaring --- app/build.gradle | 5 +++ .../bou/readerforselfoss/HomeActivity.kt | 10 ++---- .../bou/readerforselfoss/utils/Config.kt | 2 +- .../bou/readerforselfoss/utils/DateUtils.kt | 31 +++++++++++++++++++ .../bou/readerforselfoss/utils/ItemsUtils.kt | 18 ++--------- .../bou/readerforselfoss/utils/SharedItems.kt | 2 +- 6 files changed, 43 insertions(+), 25 deletions(-) create mode 100644 app/src/main/java/apps/amine/bou/readerforselfoss/utils/DateUtils.kt diff --git a/app/build.gradle b/app/build.gradle index fb131f3..2b69397 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -32,6 +32,9 @@ apply plugin: 'kotlin-kapt' android { compileOptions { + // Flag to enable support for the new language APIs + coreLibraryDesugaringEnabled true + sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } @@ -115,6 +118,8 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:2.1.1' implementation 'org.jsoup:jsoup:1.14.3' + coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") + //multidex implementation 'androidx.multidex:multidex:2.0.1' diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index f4dd346..13f4048 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -340,20 +340,16 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { private fun getApiMajorVersion() { api.apiVersion.enqueue(object : Callback { override fun onFailure(call: Call, t: Throwable) { - if (apiVersionMajor >= 4) { - Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX" - } + Config.apiVersion = apiVersionMajor } override fun onResponse(call: Call, response: Response) { if(response.body() != null) { val version = response.body() as ApiVersion apiVersionMajor = version.getApiMajorVersion() - sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).commit() + sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).apply() - if (apiVersionMajor >= 4) { - Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX" - } + Config.apiVersion = apiVersionMajor } } }) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt index c0dca46..7c5893a 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/Config.kt @@ -40,7 +40,7 @@ class Config(c: Context) { const val newItemsChannelId = "new-items-channel-id" - var dateTimeFormatter = "yyyy-MM-dd HH:mm:ss" + var apiVersion = 0 fun logoutAndRedirect( c: Context, diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/DateUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/DateUtils.kt new file mode 100644 index 0000000..454ff5a --- /dev/null +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/DateUtils.kt @@ -0,0 +1,31 @@ +package apps.amine.bou.readerforselfoss.utils + +import android.text.format.DateUtils +import java.time.Instant +import java.time.LocalDateTime +import java.time.OffsetDateTime +import java.time.ZoneOffset +import java.time.format.DateTimeFormatter + +fun parseDate(dateString: String): Instant { + + val FORMATTERV1 = "yyyy-MM-dd HH:mm:ss" + + return if (Config.apiVersion >= 4) { + OffsetDateTime.parse(dateString).toInstant() + } else { + LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(FORMATTERV1)).toInstant(ZoneOffset.UTC) + } +} + +fun parseRelativeDate(dateString: String): String { + + val date = parseDate(dateString) + + return " " + DateUtils.getRelativeTimeSpanString( + date.toEpochMilli(), + Instant.now().toEpochMilli(), + DateUtils.MINUTE_IN_MILLIS, + DateUtils.FORMAT_ABBREV_RELATIVE + ) +} \ No newline at end of file diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt index d0a49b7..b9ba764 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/ItemsUtils.kt @@ -1,16 +1,12 @@ package apps.amine.bou.readerforselfoss.utils import android.content.Context -import android.text.format.DateUtils import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.SelfossTagType -import java.text.ParseException -import java.text.SimpleDateFormat -import java.util.* fun String.toTextDrawableString(c: Context): String { val textDrawable = StringBuilder() - for (s in this.split(" ".toRegex()).filter { !it.isEmpty() }.toTypedArray()) { + for (s in this.split(" ".toRegex()).filter { it.isNotEmpty() }.toTypedArray()) { try { textDrawable.append(s[0]) } catch (e: StringIndexOutOfBoundsException) { @@ -20,17 +16,7 @@ fun String.toTextDrawableString(c: Context): String { } fun Item.sourceAndDateText(): String { - val formattedDate: String = try { - " " + DateUtils.getRelativeTimeSpanString( - SimpleDateFormat(Config.dateTimeFormatter).parse(this.datetime).time, - Date().time, - DateUtils.MINUTE_IN_MILLIS, - DateUtils.FORMAT_ABBREV_RELATIVE - ) - } catch (e: ParseException) { - e.printStackTrace() - "" - } + val formattedDate = parseRelativeDate(this.datetime) return this.getSourceTitle() + formattedDate } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt index 42fd5d6..c5f114f 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/SharedItems.kt @@ -398,7 +398,7 @@ object SharedItems { } private fun sortItems() { - val tmpItems = ArrayList(items.sortedByDescending { SimpleDateFormat(Config.dateTimeFormatter).parse((it.datetime)) }) + val tmpItems = ArrayList(items.sortedByDescending { parseDate(it.datetime) }) items = tmpItems } } \ No newline at end of file