forked from upstream-mirrors/quik
add timestamps with formatting to messages
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
import android.content.Context
|
||||
import android.text.format.DateFormat
|
||||
import android.text.format.DateUtils
|
||||
import com.moez.QKSMS.R
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class DateFormatter(val context: Context) {
|
||||
|
||||
/**
|
||||
* Replace 12 hour format with 24 hour format if necessary
|
||||
*/
|
||||
private fun accountFor24HourTime(context: Context, input: SimpleDateFormat): SimpleDateFormat {
|
||||
val isUsing24HourTime = DateFormat.is24HourFormat(context)
|
||||
|
||||
return if (isUsing24HourTime) SimpleDateFormat(input.toPattern().replace('h', 'H').replace(" a".toRegex(), ""))
|
||||
else input
|
||||
}
|
||||
|
||||
fun getMessageTimestamp(date: Long): String {
|
||||
val then = Calendar.getInstance()
|
||||
then.timeInMillis = date
|
||||
|
||||
val now = Calendar.getInstance()
|
||||
|
||||
val time = ", " + accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date)
|
||||
return when {
|
||||
now.isSameDay(then) -> accountFor24HourTime(context, SimpleDateFormat("h:mm a")).format(date)
|
||||
now.isDayAfter(then) -> context.getString(R.string.date_yesterday) + time
|
||||
now.isSameWeek(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_WEEKDAY or DateUtils.FORMAT_ABBREV_WEEKDAY) + time
|
||||
now.isSameYear(then) -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_NO_YEAR or DateUtils.FORMAT_ABBREV_MONTH) + time
|
||||
else -> DateUtils.formatDateTime(context, date, DateUtils.FORMAT_SHOW_DATE or DateUtils.FORMAT_ABBREV_MONTH) + time
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,6 +2,24 @@ package com.moez.QKSMS.util
|
||||
|
||||
import android.database.Cursor
|
||||
import io.reactivex.Flowable
|
||||
import java.util.*
|
||||
|
||||
fun Calendar.isSameDay(other: Calendar): Boolean {
|
||||
return get(Calendar.YEAR) == other.get(Calendar.YEAR) && get(Calendar.DAY_OF_YEAR) == other.get(Calendar.DAY_OF_YEAR)
|
||||
}
|
||||
|
||||
fun Calendar.isSameWeek(other: Calendar): Boolean {
|
||||
return get(Calendar.YEAR) == other.get(Calendar.YEAR) && get(Calendar.WEEK_OF_YEAR) == other.get(Calendar.WEEK_OF_YEAR)
|
||||
}
|
||||
|
||||
fun Calendar.isSameYear(other: Calendar): Boolean {
|
||||
return get(Calendar.YEAR) == other.get(Calendar.YEAR)
|
||||
}
|
||||
|
||||
fun Calendar.isDayAfter(other: Calendar): Boolean {
|
||||
other.add(Calendar.DAY_OF_YEAR, 1)
|
||||
return isSameDay(other)
|
||||
}
|
||||
|
||||
fun Cursor.forEach(closeOnComplete: Boolean = true, method: (Cursor) -> Unit = {}) {
|
||||
moveToPosition(-1)
|
||||
|
||||
Reference in New Issue
Block a user