Browse Source

Allow setting a preferred date format (#2175)

Sam Lewis 5 years ago
parent
commit
aeacdad484

+ 2 - 0
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"

+ 21 - 0
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 <T> Preference<T>.getOrDefault(): T = get() ?: defaultValue()!!
 
 fun Preference<Boolean>.invert(): Boolean = getOrDefault().let { set(!it); !it }
 
+private class DateFormatConverter : Preference.Adapter<DateFormat> {
+    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)

+ 6 - 1
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<ChapterItem>(null, controller, true) {
 
+    val preferences: PreferencesHelper by injectLazy()
+
     var items: List<ChapterItem> = 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<ChapterItem>?) {
         this.items = items ?: emptyList()

+ 4 - 1
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<MangaInfoPresenter>(),
      */
     private val preferences: PreferencesHelper by injectLazy()
 
+    val dateFormat: DateFormat = preferences.dateFormat().getOrDefault()
+
     init {
         setHasOptionsMenu(true)
         setOptionsMenuHidden(true)
@@ -253,7 +256,7 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
 
     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)
         }

+ 5 - 1
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)

+ 4 - 1
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)

+ 13 - 4
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
         }

+ 14 - 0
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

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -153,6 +153,7 @@
     <string name="pref_update_only_non_completed">Only update ongoing manga</string>
     <string name="pref_auto_update_manga_sync">Sync chapters after reading</string>
     <string name="pref_ask_update_manga_sync">Confirm before updating</string>
+    <string name="pref_date_format">Date format</string>
     <string name="default_category">Default category</string>
     <string name="default_category_summary">Always ask</string>