Removed hardcoded date format to use user-defined Android settings

This commit is contained in:
Sylvain Berfini 2020-06-09 14:10:06 +02:00
parent 11bf573d42
commit f00e5c4ed8
5 changed files with 43 additions and 14 deletions

View file

@ -27,8 +27,6 @@ import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import java.text.SimpleDateFormat
import java.util.*
import org.linphone.R import org.linphone.R
import org.linphone.activities.main.history.viewmodels.CallLogViewModel import org.linphone.activities.main.history.viewmodels.CallLogViewModel
import org.linphone.activities.main.viewmodels.ListTopBarViewModel import org.linphone.activities.main.viewmodels.ListTopBarViewModel
@ -122,7 +120,7 @@ class CallLogsListAdapter(val selectionViewModel: ListTopBarViewModel) : Lifecyc
} else if (TimestampUtils.isYesterday(date)) { } else if (TimestampUtils.isYesterday(date)) {
return context.getString(R.string.yesterday) return context.getString(R.string.yesterday)
} }
return SimpleDateFormat("EEE d MMM", Locale.getDefault()).format(Date(date * 1000)) return TimestampUtils.toString(date, onlyDate = true, shortDate = false)
} }
} }

View file

@ -93,8 +93,7 @@ class CallLogViewModel(val callLog: CallLog) : GenericContactViewModel(callLog.r
} }
val date: String by lazy { val date: String by lazy {
val pattern = if (TimestampUtils.isToday(callLog.startDate)) "HH:mm" else "yyyy/MM/dd - HH:mm" TimestampUtils.toString(callLog.startDate)
SimpleDateFormat(pattern, Locale.getDefault()).format(Date(callLog.startDate * 1000))
} }
val startCallEvent: MutableLiveData<Event<Address>> by lazy { val startCallEvent: MutableLiveData<Event<Address>> by lazy {

View file

@ -27,8 +27,6 @@ import android.view.ViewGroup
import androidx.databinding.DataBindingUtil import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import java.text.SimpleDateFormat
import java.util.*
import org.linphone.R import org.linphone.R
import org.linphone.activities.main.recordings.viewmodels.RecordingViewModel import org.linphone.activities.main.recordings.viewmodels.RecordingViewModel
import org.linphone.activities.main.viewmodels.ListTopBarViewModel import org.linphone.activities.main.viewmodels.ListTopBarViewModel
@ -131,7 +129,7 @@ class RecordingsListAdapter(val selectionViewModel: ListTopBarViewModel) : Lifec
} else if (TimestampUtils.isYesterday(date, false)) { } else if (TimestampUtils.isYesterday(date, false)) {
return context.getString(R.string.yesterday) return context.getString(R.string.yesterday)
} }
return SimpleDateFormat("EEE d MMM", Locale.getDefault()).format(Date(date)) return TimestampUtils.toString(date, onlyDate = true, timestampInSecs = false, shortDate = false)
} }
} }

View file

@ -21,6 +21,7 @@ package org.linphone.activities.main.recordings.viewmodels
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
@ -49,7 +50,7 @@ class RecordingViewModel(val path: String) : ViewModel(), Comparable<RecordingVi
get() = SimpleDateFormat("mm:ss", Locale.getDefault()).format(duration) // is already in milliseconds get() = SimpleDateFormat("mm:ss", Locale.getDefault()).format(duration) // is already in milliseconds
val formattedDate: String val formattedDate: String
get() = SimpleDateFormat("HH:mm", Locale.getDefault()).format(date) get() = DateFormat.getTimeInstance(DateFormat.SHORT).format(date)
val playStartedEvent = MutableLiveData<Event<Boolean>>() val playStartedEvent = MutableLiveData<Event<Boolean>>()

View file

@ -19,8 +19,10 @@
*/ */
package org.linphone.utils package org.linphone.utils
import java.text.DateFormat
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
import org.linphone.core.tools.Log
class TimestampUtils { class TimestampUtils {
companion object { companion object {
@ -53,14 +55,37 @@ class TimestampUtils {
return isSameDay(cal1.time, cal2.time) return isSameDay(cal1.time, cal2.time)
} }
fun toString(timestamp: Long, onlyDate: Boolean = false, timestampInSecs: Boolean = true): String { private fun isSameYear(timestamp: Long, timestampInSecs: Boolean = true): Boolean {
val format = if (isToday(timestamp)) { val cal = Calendar.getInstance()
"HH:mm" cal.timeInMillis = if (timestampInSecs) timestamp * 1000 else timestamp
return isSameYear(cal, Calendar.getInstance())
}
fun toString(
timestamp: Long,
onlyDate: Boolean = false,
timestampInSecs: Boolean = true,
shortDate: Boolean = true
): String {
val dateFormat = if (isToday(timestamp, timestampInSecs)) {
DateFormat.getTimeInstance(DateFormat.SHORT)
} else { } else {
if (onlyDate) "dd/MM" else "dd/MM HH:mm" if (onlyDate) {
DateFormat.getDateInstance(if (shortDate) DateFormat.SHORT else DateFormat.FULL)
} else {
DateFormat.getDateTimeInstance(if (shortDate) DateFormat.SHORT else DateFormat.MEDIUM, DateFormat.SHORT)
}
} as SimpleDateFormat
if (isSameYear(timestamp, timestampInSecs)) {
// Remove the year part of the format
dateFormat.applyPattern(
dateFormat.toPattern().replace("/?y+/?|\\s?y+\\s?".toRegex(),"")
)
} }
val millis = if (timestampInSecs) timestamp * 1000 else timestamp val millis = if (timestampInSecs) timestamp * 1000 else timestamp
return SimpleDateFormat(format, Locale.getDefault()).format(Date(millis)) return dateFormat.format(Date(millis))
} }
private fun isSameDay( private fun isSameDay(
@ -71,5 +96,13 @@ class TimestampUtils {
cal1[Calendar.YEAR] == cal2[Calendar.YEAR] && cal1[Calendar.YEAR] == cal2[Calendar.YEAR] &&
cal1[Calendar.DAY_OF_YEAR] == cal2[Calendar.DAY_OF_YEAR] cal1[Calendar.DAY_OF_YEAR] == cal2[Calendar.DAY_OF_YEAR]
} }
private fun isSameYear(
cal1: Calendar,
cal2: Calendar
): Boolean {
return cal1[Calendar.ERA] == cal2[Calendar.ERA] &&
cal1[Calendar.YEAR] == cal2[Calendar.YEAR]
}
} }
} }