From aeacdad48473cd55b039e6db56767d1195bd383f Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Tue, 28 Jan 2020 22:23:41 -0500 Subject: [PATCH] Allow setting a preferred date format (#2175) --- .../data/preference/PreferenceKeys.kt | 2 ++ .../data/preference/PreferencesHelper.kt | 21 +++++++++++++++++++ .../ui/manga/chapter/ChaptersAdapter.kt | 7 ++++++- .../ui/manga/info/MangaInfoController.kt | 5 ++++- .../ui/recently_read/RecentlyReadAdapter.kt | 6 +++++- .../ui/recently_read/RecentlyReadHolder.kt | 5 ++++- .../ui/setting/SettingsAboutController.kt | 17 +++++++++++---- .../ui/setting/SettingsGeneralController.kt | 14 +++++++++++++ app/src/main/res/values/strings.xml | 1 + 9 files changed, 70 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt index 057def523d..070739b928 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferenceKeys.kt @@ -109,6 +109,8 @@ object PreferenceKeys { const val lang = "app_language" + const val dateFormat = "app_date_format" + const val defaultCategory = "default_category" const val skipRead = "skip_read" diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt index c988225817..4955c6ad00 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/preference/PreferencesHelper.kt @@ -1,6 +1,7 @@ package eu.kanade.tachiyomi.data.preference import android.content.Context +import android.content.SharedPreferences import android.net.Uri import android.os.Environment import android.preference.PreferenceManager @@ -11,12 +12,30 @@ import eu.kanade.tachiyomi.data.track.TrackService import eu.kanade.tachiyomi.source.Source import java.io.File import java.util.Locale +import java.text.DateFormat +import java.text.SimpleDateFormat import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys fun Preference.getOrDefault(): T = get() ?: defaultValue()!! fun Preference.invert(): Boolean = getOrDefault().let { set(!it); !it } +private class DateFormatConverter : Preference.Adapter { + override fun get(key: String, preferences: SharedPreferences): DateFormat { + var dateFormat = preferences.getString(Keys.dateFormat, "") + + if (dateFormat != "") { + return SimpleDateFormat(dateFormat) + } + + return DateFormat.getDateInstance(DateFormat.SHORT) + } + + override fun set(key: String, value: DateFormat, editor: SharedPreferences.Editor) { + TODO("not implemented") + } +} + class PreferencesHelper(val context: Context) { private val prefs = PreferenceManager.getDefaultSharedPreferences(context) @@ -126,6 +145,8 @@ class PreferencesHelper(val context: Context) { fun backupsDirectory() = rxPrefs.getString(Keys.backupDirectory, defaultBackupDir.toString()) + fun dateFormat() = rxPrefs.getObject(Keys.dateFormat, DateFormatConverter()) + fun downloadsDirectory() = rxPrefs.getString(Keys.downloadsDirectory, defaultDownloadsDir.toString()) fun downloadOnlyOverWifi() = prefs.getBoolean(Keys.downloadOnlyOverWifi, true) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt index 6bb9226d82..2485bcc111 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/chapter/ChaptersAdapter.kt @@ -8,12 +8,17 @@ import eu.kanade.tachiyomi.util.getResourceColor import java.text.DateFormat import java.text.DecimalFormat import java.text.DecimalFormatSymbols +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault +import uy.kohesive.injekt.injectLazy class ChaptersAdapter( controller: ChaptersController, context: Context ) : FlexibleAdapter(null, controller, true) { + val preferences: PreferencesHelper by injectLazy() + var items: List = emptyList() val menuItemListener: OnMenuItemClickListener = controller @@ -27,7 +32,7 @@ class ChaptersAdapter( val decimalFormat = DecimalFormat("#.###", DecimalFormatSymbols() .apply { decimalSeparator = '.' }) - val dateFormat: DateFormat = DateFormat.getDateInstance(DateFormat.SHORT) + val dateFormat: DateFormat = preferences.dateFormat().getOrDefault() override fun updateDataSet(items: List?) { this.items = items ?: emptyList() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt index d4c56142ce..0459f1ef3b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt @@ -29,6 +29,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.glide.GlideApp import eu.kanade.tachiyomi.data.notification.NotificationReceiver import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.model.SManga import eu.kanade.tachiyomi.source.online.HttpSource @@ -65,6 +66,8 @@ class MangaInfoController : NucleusController(), */ private val preferences: PreferencesHelper by injectLazy() + val dateFormat: DateFormat = preferences.dateFormat().getOrDefault() + init { setHasOptionsMenu(true) setOptionsMenuHidden(true) @@ -253,7 +256,7 @@ class MangaInfoController : NucleusController(), fun setLastUpdateDate(date: Date) { if (date.time != 0L) { - manga_last_update?.text = DateFormat.getDateInstance(DateFormat.SHORT).format(date) + manga_last_update?.text = dateFormat.format(date) } else { manga_last_update?.text = resources?.getString(R.string.unknown) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt index 766ce466db..cab68b7890 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadAdapter.kt @@ -1,6 +1,8 @@ package eu.kanade.tachiyomi.ui.recently_read import eu.davidea.flexibleadapter.FlexibleAdapter +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.source.SourceManager import uy.kohesive.injekt.injectLazy import java.text.DateFormat @@ -32,7 +34,9 @@ class RecentlyReadAdapter(controller: RecentlyReadController) val decimalFormat = DecimalFormat("#.###", DecimalFormatSymbols() .apply { decimalSeparator = '.' }) - val dateFormat: DateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT) + private val preferences: PreferencesHelper by injectLazy() + + val dateFormat: DateFormat = preferences.dateFormat().getOrDefault() interface OnResumeClickListener { fun onResumeClick(position: Int) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadHolder.kt index 64eab1a9b1..9ddda4967c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recently_read/RecentlyReadHolder.kt @@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.database.models.MangaChapterHistory import eu.kanade.tachiyomi.data.glide.GlideApp import eu.kanade.tachiyomi.ui.base.holder.BaseFlexibleViewHolder import kotlinx.android.synthetic.main.recently_read_item.* +import java.text.DateFormat import java.util.Date /** @@ -54,8 +55,10 @@ class RecentlyReadHolder( manga_source.text = itemView.context.getString(R.string.recent_manga_source) .format(adapter.sourceManager.getOrStub(manga.source).toString(), formattedNumber) + val date = adapter.dateFormat.format(Date(history.last_read)) + val time = DateFormat.getTimeInstance(DateFormat.SHORT).format(Date(history.last_read)) // Set last read timestamp title - last_read.text = adapter.dateFormat.format(Date(history.last_read)) + last_read.text = "$date $time" // Set cover GlideApp.with(itemView.context).clear(cover) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt index 8d6abf469b..455206f8ab 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsAboutController.kt @@ -9,6 +9,8 @@ import android.view.View import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.data.preference.PreferencesHelper +import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.updater.UpdateChecker import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.data.updater.UpdaterJob @@ -20,11 +22,11 @@ import rx.Subscription import rx.android.schedulers.AndroidSchedulers import rx.schedulers.Schedulers import timber.log.Timber +import uy.kohesive.injekt.injectLazy import java.text.DateFormat import java.text.ParseException import java.text.SimpleDateFormat -import java.util.Locale -import java.util.TimeZone +import java.util.* import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys class SettingsAboutController : SettingsController() { @@ -34,6 +36,11 @@ class SettingsAboutController : SettingsController() { */ private val updateChecker by lazy { UpdateChecker.getUpdateChecker() } + + private val userPreferences: PreferencesHelper by injectLazy() + + val dateFormat: DateFormat = userPreferences.dateFormat().getOrDefault() + /** * The subscribtion service of the obtained release object */ @@ -179,13 +186,15 @@ class SettingsAboutController : SettingsController() { try { val inputDf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'", Locale.US) inputDf.timeZone = TimeZone.getTimeZone("UTC") - val date = inputDf.parse(BuildConfig.BUILD_TIME) + val buildTime = inputDf.parse(BuildConfig.BUILD_TIME) val outputDf = DateFormat.getDateTimeInstance( DateFormat.MEDIUM, DateFormat.SHORT, Locale.getDefault()) outputDf.timeZone = TimeZone.getDefault() - return outputDf.format(date) + val date = dateFormat.format(buildTime) + val time = DateFormat.getTimeInstance(DateFormat.SHORT).format(buildTime) + return "$date $time" } catch (e: ParseException) { return BuildConfig.BUILD_TIME } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt index c078bdd805..096875bca3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsGeneralController.kt @@ -33,6 +33,20 @@ class SettingsGeneralController : SettingsController() { true } } + listPreference { + key= Keys.dateFormat + titleRes = R.string.pref_date_format + entryValues= arrayOf("", "MM/dd/yy", "dd/MM/yy", "yyyy-MM-dd") + entries = entryValues.map { value -> + if (value == "") { + context.getString(R.string.system_default) + } else { + value + } + }.toTypedArray() + defaultValue = "" + summary= "%s" + } intListPreference { key = Keys.theme titleRes = R.string.pref_theme diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 59423a92d3..d0182d860a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -153,6 +153,7 @@ Only update ongoing manga Sync chapters after reading Confirm before updating + Date format Default category Always ask