Created
December 4, 2018 14:19
-
-
Save iljaosintsev/5fddb8af5ce068391fa55003e470d5df to your computer and use it in GitHub Desktop.
DateTimeFormatters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.kontur.cashbox.presentation.settings.about | |
import ru.kontur.cashbox.R | |
import ru.kontur.cashbox.global.ResourceManager | |
import java.text.SimpleDateFormat | |
import java.util.* | |
class NotOccurredFormatter(private val stub: String): PossibilityFormatter() { | |
override fun should(diff: DiffTime) = diff.point.time < 1 | |
override fun format(diff: DiffTime) = stub | |
} | |
object YearFormatter : PossibilityFormatter() { | |
override fun should(diff: DiffTime) = diff.year == 1L || diff.year == -1L | |
override fun format(diff: DiffTime) = when (diff.year) { | |
1L -> "В следующем году" | |
else -> "Год назад" | |
} | |
} | |
class NewelyFormatter(private val res: ResourceManager) : PossibilityFormatter() { | |
override fun should(diff: DiffTime) = Math.abs(diff.minute) < 5 | |
override fun format(diff: DiffTime) = res.getString(R.string.newly) | |
} | |
class DayFormatter(private val res: ResourceManager, private val short: SimpleDateFormat) : PossibilityFormatter() { | |
override fun should(diff: DiffTime) = diff.day == 1L || diff.day == 0L || diff.day == -1L | |
override fun format(diff: DiffTime) = when(diff.day) { | |
1L -> this.res.getString(R.string.yesterday_at, short.format(diff)) | |
0L -> res.getString(R.string.today_at, short.format(diff)) | |
else -> res.getString(R.string.tomorrow_at, short.format(diff)) | |
} | |
} | |
class CommonFormatter(private val full: SimpleDateFormat) : PossibilityFormatter() { | |
override fun should(diff: DiffTime) = true | |
override fun format(diff: DiffTime): String = full.format(diff.point) | |
} | |
sealed class PossibilityFormatter { | |
abstract fun should(diff: DiffTime): Boolean | |
abstract fun format(diff: DiffTime): String | |
} | |
class TimeDescriptior(res: ResourceManager, short: SimpleDateFormat, full: SimpleDateFormat, stub: String) { | |
private val lst: List<PossibilityFormatter> = listOf( | |
NotOccurredFormatter(stub), | |
//YearFormatter, | |
DayFormatter(res, short), | |
CommonFormatter(full) | |
) | |
fun descript(diff: DiffTime): String { | |
for (f in lst) { | |
if (f.should(diff)) return f.format(diff) | |
} | |
return lst.last().format(diff) | |
} | |
} | |
// | |
class FactoryFormatter(val factory: DateDescriptionFactory) { | |
fun demo(diff: DiffTime): String { | |
if (diff.point.time < 1) { | |
return factory.empty | |
} | |
val isNewly = Math.abs(diff.minute) < 5 | |
val isYesterday = diff.day == -1L | |
val isToday = diff.day == 0L | |
val isTomorrow = diff.day == 1L | |
return when { | |
isNewly -> factory.moment(diff) | |
isYesterday || isToday || isTomorrow -> factory.day(diff) | |
else -> factory.full(diff) | |
} | |
} | |
} | |
class DateDescriptionFactory(private val short: SimpleDateFormat, | |
private val full: SimpleDateFormat, | |
private val res: ResourceManager) { | |
fun full(point: DiffTime) = full.format(point) ?: "" | |
fun short(point: DiffTime): String = short.format(point.point) | |
val empty = res.getString(R.string.cashbox_id) | |
fun year(diff: DiffTime): String { | |
return when(diff.year) { | |
1L -> "В следующем году" | |
-1L -> "Год назад" | |
else -> full(diff) | |
} | |
} | |
fun moment(diff: DiffTime): String { | |
if (Math.abs(diff.hour) < 5) { | |
return res.getString(R.string.newly) | |
} | |
if (Math.abs(diff.day) < 2) { | |
return "Недавно" | |
} | |
return full(diff) | |
} | |
fun day(diff: DiffTime): String { | |
return when(diff.day) { | |
1L -> res.getString(R.string.yesterday_at, short(diff)) | |
0L -> res.getString(R.string.today_at, short(diff)) | |
-1L -> res.getString(R.string.tomorrow_at, short(diff)) | |
else -> full(diff) | |
} | |
} | |
} | |
class DiffTime(origin: Date, val point: Date) { | |
val year: Long | |
val month: Long | |
val week: Long | |
val day: Long | |
val hour: Long | |
val minute: Long | |
init { | |
if (origin.time < 1) throw IllegalArgumentException("Invalid origin time") | |
val deltaSeconds = (point.time - origin.time) / 1000 | |
year = deltaSeconds / (365 * 24 * 60 * 60) | |
month = deltaSeconds / (31 * 24 * 60 * 60) | |
week = deltaSeconds / (7 * 24 * 60 * 60) | |
day = deltaSeconds / (24 * 60 * 60) | |
hour = deltaSeconds / (60 * 60) | |
minute = deltaSeconds / 60 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment