1
0
mirror of https://github.com/AllanWang/Frost-for-Facebook.git synced 2024-11-08 20:12:39 +01:00

Create readable time converter

This commit is contained in:
Allan Wang 2019-04-24 19:38:54 -07:00
parent 7e22707837
commit 27d1b98330
No known key found for this signature in database
GPG Key ID: C93E3F9C679D7A56
3 changed files with 54 additions and 5 deletions

View File

@ -1,2 +1,50 @@
/*
* Copyright 2019 Allan Wang
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.pitchedapps.frost.utils
import android.content.Context
import ca.allanwang.kau.utils.string
import com.pitchedapps.frost.R
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
/**
* Converts time in millis to readable date,
* eg Apr 24 at 7:32 PM
*
* With regards to date modifications in calendars,
* it appears to respect calendar rules;
* see https://stackoverflow.com/a/43227817/4407321
*/
fun Long.toReadableTime(context: Context): String {
val cal = Calendar.getInstance()
cal.timeInMillis = this
val timeFormatter = SimpleDateFormat.getTimeInstance(DateFormat.SHORT)
val time = timeFormatter.format(Date(this))
val day = when {
cal >= Calendar.getInstance().apply { add(Calendar.DAY_OF_MONTH, -1) } -> context.string(R.string.today)
cal >= Calendar.getInstance().apply { add(Calendar.DAY_OF_MONTH, -2) } -> context.string(R.string.yesterday)
else -> {
val dayFormatter = SimpleDateFormat("MMM dd", Locale.getDefault())
dayFormatter.format(Date(this))
}
}
return context.getString(R.string.time_template, day, time)
}

View File

@ -35,7 +35,6 @@ import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import ca.allanwang.kau.utils.dimenPixelSize
import ca.allanwang.kau.utils.string
import ca.allanwang.kau.utils.withAlpha
import com.pitchedapps.frost.R
import com.pitchedapps.frost.activities.MainActivity
@ -47,9 +46,9 @@ import com.pitchedapps.frost.services.NotificationContent
import com.pitchedapps.frost.services.NotificationType
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Prefs
import com.pitchedapps.frost.utils.toReadableTime
import org.koin.standalone.KoinComponent
import org.koin.standalone.inject
import java.text.DateFormat
class NotificationWidget : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
@ -168,7 +167,7 @@ class NotificationWidgetDataProvider(val context: Context, val intent: Intent) :
views.setTextColor(R.id.item_content, Prefs.textColor)
views.setTextViewText(R.id.item_content, notif.text)
views.setTextColor(R.id.item_date, Prefs.textColor.withAlpha(150))
views.setTextViewText(R.id.item_date, notif.timestamp.toString()) // TODO
views.setTextViewText(R.id.item_date, notif.timestamp.toReadableTime(context))
val avatar = glide.load(notif.profileUrl).transform(FrostGlide.circleCrop).submit(avatarSize, avatarSize).get()
views.setImageViewBitmap(R.id.item_avatar, avatar)
@ -182,4 +181,4 @@ class NotificationWidgetDataProvider(val context: Context, val intent: Intent) :
override fun onDestroy() {
}
}
}

View File

@ -69,7 +69,9 @@
For instance:
Today at 1:23 PM
Mar 13 at 9:00 AM
The first element is the day, and the second element is the time
-->
<string name="time_template">%s at %s</string>
<string name="time_template">%1s at %2s</string>
</resources>