do not show the messages year, if it is the current one

This commit is contained in:
tibbi 2020-04-03 20:22:24 +02:00
parent d20623c44a
commit 715fa3174d
2 changed files with 52 additions and 1 deletions

View File

@ -1,18 +1,28 @@
package com.simplemobiletools.smsmessenger.adapters
import android.content.Context
import android.graphics.Typeface
import android.text.format.DateFormat
import android.text.format.DateUtils
import android.util.Log
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.baseConfig
import com.simplemobiletools.commons.extensions.formatDate
import com.simplemobiletools.commons.extensions.getTimeFormat
import com.simplemobiletools.commons.views.FastScroller
import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.smsmessenger.R
import com.simplemobiletools.smsmessenger.activities.SimpleActivity
import com.simplemobiletools.smsmessenger.extensions.formatDateOrTime
import com.simplemobiletools.smsmessenger.extensions.formatTime
import com.simplemobiletools.smsmessenger.models.Message
import kotlinx.android.synthetic.main.item_message.view.*
import java.util.*
import kotlin.collections.ArrayList
class MessagesAdapter(
activity: SimpleActivity, var messages: ArrayList<Message>,
@ -73,7 +83,7 @@ class MessagesAdapter(
message_address.text = message.address
message_body_short.text = message.body
message_date.text = message.date.formatDate(context)
message_date.text = message.date.formatDateOrTime(context)
if (message.read) {
message_address.setTypeface(null, Typeface.NORMAL)

View File

@ -0,0 +1,41 @@
package com.simplemobiletools.smsmessenger.extensions
import android.content.Context
import android.text.format.DateFormat
import android.text.format.DateUtils
import android.text.format.Time
import com.simplemobiletools.commons.extensions.getTimeFormat
import java.util.*
fun Int.formatTime(context: Context): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = this * 1000L
return DateFormat.format("${context.getTimeFormat()}", cal).toString()
}
// if the given date is today, we show only the time. Else we show only the date
fun Int.formatDateOrTime(context: Context): String {
val cal = Calendar.getInstance(Locale.ENGLISH)
cal.timeInMillis = this * 1000L
return if (DateUtils.isToday(this * 1000L)) {
DateFormat.format(context.getTimeFormat(), cal).toString()
} else {
var format = context.config.dateFormat
if (isThisYear()) {
format = format.replace("y", "").trim().trim('-').trim('.').trim('/')
}
DateFormat.format(format, cal).toString()
}
}
fun Int.isThisYear(): Boolean {
val time = Time()
time.set(this * 1000L)
val thenYear = time.year
time.set(System.currentTimeMillis())
return (thenYear == time.year)
}