Created
May 22, 2017 15:14
-
-
Save Miha-x64/154efca3c280892fb00bb096f3f45383 to your computer and use it in GitHub Desktop.
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 net.aquadc.commonandroid | |
import android.content.res.Resources | |
import java.text.SimpleDateFormat | |
import java.util.* | |
/** | |
* Returns text representation of `Date` and time (in millis) | |
* after which this value should be updated; | |
* time can be -1, which means that no update will ever be needed | |
*/ | |
fun Date.relativeToNowWithTimeout(carrier: ReactiveDateCarrier) { | |
val seconds = time / 1000 | |
val secondsNow = System.currentTimeMillis() / 1000 | |
val secondsDiff = (secondsNow - seconds).toInt() | |
if (secondsDiff == 0) { | |
carrier.text = carrier.resources.getString(R.string.rightNow) | |
carrier.timeout = 1_000 | |
return | |
} | |
if (secondsDiff < 60) { | |
carrier.text = secondsDiff.toPlural(carrier.resources, R.plurals.secondsAgo) | |
carrier.timeout = 1_000 | |
return | |
} | |
val minutesDiff = secondsDiff / 60 | |
if (minutesDiff < 60) { | |
carrier.text = minutesDiff.toPlural(carrier.resources, R.plurals.minutesAgo) | |
carrier.timeout = 60_000 // minute | |
return | |
} | |
val hoursDiff = secondsDiff / 3600 | |
if (hoursDiff < 3) { | |
carrier.text = hoursDiff.toPlural(carrier.resources, R.plurals.hoursAgo) | |
carrier.timeout = 1800_000 // half an hour | |
return | |
} | |
val secondsFromMidnight = secondsFromMidnight(carrier.calendar) | |
if (secondsDiff < secondsFromMidnight) { // today | |
carrier.text = carrier.todayFormat.format(this) | |
carrier.timeout = 1000L*(86_400 - secondsFromMidnight) // next midnight | |
return | |
} | |
if (secondsDiff < secondsFromNewYear(carrier.calendar)) { // this year | |
carrier.text = carrier.thisYearFormat.format(this) | |
carrier.timeout = 1000L*secondsToNewYear(carrier.calendar) // update when year changes | |
return | |
} | |
// previous years | |
carrier.text = carrier.longTimeAgo.format(this) | |
carrier.timeout = -1 // never update at all | |
} | |
class ReactiveDateCarrier(val resources: Resources) { | |
internal val todayFormat: SimpleDateFormat | |
internal val thisYearFormat: SimpleDateFormat | |
internal val longTimeAgo: SimpleDateFormat | |
init { | |
val locale = resources.locale | |
todayFormat = SimpleDateFormat(resources.getString(R.string.atTime), locale) | |
thisYearFormat = SimpleDateFormat(resources.getString(R.string.atDate), locale) | |
longTimeAgo = SimpleDateFormat(resources.getString(R.string.atFarDate), locale) | |
} | |
internal val calendar: Calendar = Calendar.getInstance() | |
var text: String = "" | |
internal set | |
var timeout: Long = 0 | |
internal set | |
} | |
private fun secondsFromMidnight(c: Calendar): Int { | |
c.timeInMillis = System.currentTimeMillis() | |
val now = c.timeInMillis | |
c.set(Calendar.HOUR_OF_DAY, 0) | |
c.set(Calendar.MINUTE, 0) | |
c.set(Calendar.SECOND, 0) | |
c.set(Calendar.MILLISECOND, 0) | |
val passed = now - c.timeInMillis | |
return (passed / 1000).toInt() | |
} | |
private fun secondsFromNewYear(c: Calendar): Int { | |
c.timeInMillis = System.currentTimeMillis() | |
val now = c.timeInMillis | |
c.set(Calendar.DAY_OF_YEAR, 1) | |
c.set(Calendar.HOUR_OF_DAY, 0) | |
c.set(Calendar.MINUTE, 0) | |
c.set(Calendar.SECOND, 0) | |
c.set(Calendar.MILLISECOND, 0) | |
val passed = now - c.timeInMillis | |
return (passed / 1000).toInt() | |
} | |
private fun secondsToNewYear(c: Calendar): Int { | |
c.timeInMillis = System.currentTimeMillis() | |
val now = c.timeInMillis | |
c.set(Calendar.DAY_OF_YEAR, 1) | |
c.set(Calendar.HOUR_OF_DAY, 0) | |
c.set(Calendar.MINUTE, 0) | |
c.set(Calendar.SECOND, 0) | |
c.set(Calendar.MILLISECOND, 0) | |
c.add(Calendar.YEAR, +1) | |
val left = c.timeInMillis - now | |
return (left / 1000).toInt() | |
} |
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
<resources> | |
<string name="rightNow">just now</string> | |
<plurals name="secondsAgo"> | |
<item quantity="zero">%d seconds ago</item> | |
<item quantity="one">%d second ago</item> | |
<item quantity="other">%d seconds ago</item> | |
</plurals> | |
<plurals name="minutesAgo"> | |
<item quantity="zero">%d minutes ago</item> | |
<item quantity="one">%d minute ago</item> | |
<item quantity="other">%d minutes age</item> | |
</plurals> | |
<plurals name="hoursAgo"> | |
<item quantity="zero">%d hours ago</item> | |
<item quantity="one">%d hour ago</item> | |
<item quantity="other">%d hours ago</item> | |
</plurals> | |
<string name="atTime">в HH:mm</string> | |
<string name="atDate">dd MMM</string> | |
<string name="atFarDate">dd MMM yyyy г.</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment