Allow setting a preferred date format

This commit is contained in:
Sam Lewis 2020-01-28 22:23:41 -05:00 committed by Jay
parent aea88f43d7
commit f06d61a137
8 changed files with 69 additions and 10 deletions

View File

@ -115,6 +115,8 @@ object PreferenceKeys {
const val lang = "app_language"
const val dateFormat = "app_date_format"
const val defaultCategory = "default_category"
const val skipRead = "skip_read"

View File

@ -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 androidx.preference.PreferenceManager
@ -12,12 +13,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)
@ -133,6 +152,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)

View File

@ -8,12 +8,17 @@ import eu.kanade.tachiyomi.util.system.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()

View File

@ -49,6 +49,7 @@ import eu.kanade.tachiyomi.data.database.models.MangaImpl
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
@ -113,6 +114,8 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
var fullRes:Drawable? = null
private val dateFormat: DateFormat = preferences.dateFormat().getOrDefault()
init {
setHasOptionsMenu(true)
setOptionsMenuHidden(true)
@ -368,7 +371,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)
}

View File

@ -2,6 +2,8 @@ package eu.kanade.tachiyomi.ui.recently_read
import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.davidea.flexibleadapter.items.IFlexible
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
@ -33,7 +35,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)

View File

@ -9,6 +9,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
import kotlin.math.max
@ -68,8 +69,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)

View File

@ -9,6 +9,8 @@ import androidx.preference.PreferenceScreen
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.UpdaterService
@ -19,6 +21,7 @@ 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
@ -32,6 +35,11 @@ class SettingsAboutController : SettingsController() {
*/
private val updateChecker by lazy { UpdateChecker.getUpdateChecker() }
private val userPreferences: PreferencesHelper by injectLazy()
private val dateFormat: DateFormat = userPreferences.dateFormat().getOrDefault()
/**
* The subscribtion service of the obtained release object
*/
@ -155,13 +163,11 @@ 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) ?: return BuildConfig.BUILD_TIME
val buildTime = inputDf.parse(BuildConfig.BUILD_TIME) ?: return 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
}

View File

@ -40,6 +40,7 @@ class SettingsGeneralController : SettingsController() {
true
}
}
intListPreference(activity) {
key = Keys.theme
titleRes = R.string.pref_theme
@ -54,6 +55,20 @@ class SettingsGeneralController : SettingsController() {
true
}
}
listPreference(activity) {
key= Keys.dateFormat
titleRes = R.string.pref_date_format
entryValues = listOf("", "MM/dd/yy", "dd/MM/yy", "yyyy-MM-dd")
entries = entryValues.map { value ->
if (value == "") {
context.getString(R.string.system_default)
} else {
value
}
}
defaultValue = ""
summary= "%s"
}
intListPreference(activity) {
key = Keys.startScreen
titleRes = R.string.pref_start_screen