Skip to content

Commit 5c12d13

Browse files
authored
Merge pull request #724 from PhilKes/feat/more-date-formats
Add date format with time
2 parents 3d31aa8 + cfb36ae commit 5c12d13

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

app/src/main/java/com/philkes/notallyx/presentation/UiExtensions.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,23 @@ fun Context.displayEditLabelDialog(
595595
}
596596

597597
private fun formatTimestamp(timestamp: Long, dateFormat: DateFormat): String {
598-
val date = Date(timestamp)
598+
return Date(timestamp).format(dateFormat)
599+
}
600+
601+
fun Date.format(dateFormat: DateFormat): String {
599602
return when (dateFormat) {
600-
DateFormat.RELATIVE -> PrettyTime().format(date)
601-
else -> java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL).format(date)
603+
DateFormat.NONE -> ""
604+
DateFormat.RELATIVE -> PrettyTime().format(this)
605+
DateFormat.ABSOLUTE ->
606+
java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL).format(this)
607+
DateFormat.ABSOLUTE_SHORT ->
608+
java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT).format(this)
609+
DateFormat.TIMESTAMP_SHORT ->
610+
java.text.DateFormat.getDateTimeInstance(
611+
java.text.DateFormat.SHORT,
612+
java.text.DateFormat.SHORT,
613+
)
614+
.format(this)
602615
}
603616
}
604617

app/src/main/java/com/philkes/notallyx/presentation/viewmodel/preference/Preference.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import androidx.lifecycle.LiveData
99
import androidx.lifecycle.MediatorLiveData
1010
import androidx.lifecycle.Observer
1111
import com.philkes.notallyx.R
12+
import com.philkes.notallyx.presentation.format
1213
import com.philkes.notallyx.presentation.merge
1314
import com.philkes.notallyx.presentation.view.misc.NotNullLiveData
1415
import com.philkes.notallyx.utils.createObserverSkipFirst
@@ -19,7 +20,6 @@ import com.philkes.notallyx.utils.toPreservedString
1920
import java.security.SecureRandom
2021
import java.util.Date
2122
import javax.crypto.Cipher
22-
import org.ocpsoft.prettytime.PrettyTime
2323

2424
/**
2525
* Every Preference can be observed like a [NotNullLiveData].
@@ -302,15 +302,15 @@ enum class Theme(override val textResId: Int) : StaticTextProvider {
302302
enum class DateFormat : TextProvider {
303303
NONE,
304304
RELATIVE,
305+
ABSOLUTE_SHORT,
306+
TIMESTAMP_SHORT,
305307
ABSOLUTE;
306308

307309
override fun getText(context: Context): String {
308-
val date = Date(System.currentTimeMillis() - 86400000)
309-
return when (this) {
310-
NONE -> context.getString(R.string.none)
311-
RELATIVE -> PrettyTime().format(date)
312-
ABSOLUTE -> java.text.DateFormat.getDateInstance(java.text.DateFormat.FULL).format(date)
310+
if (this == NONE) {
311+
return context.getString(R.string.none)
313312
}
313+
return Date(System.currentTimeMillis() - 86400000).format(this)
314314
}
315315
}
316316

0 commit comments

Comments
 (0)