diff options
author | Allan Wang <me@allanwang.ca> | 2019-04-24 19:38:54 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-04-24 19:38:54 -0700 |
commit | 27d1b98330c25e8833279ed48a4d9bd7d7566d10 (patch) | |
tree | 8da002e91c2411cf3a25f4a8499a9ed1662839ca /app/src/main/kotlin | |
parent | 7e227078373adf0ef8c243b7e1e05804524cd3ea (diff) | |
download | frost-27d1b98330c25e8833279ed48a4d9bd7d7566d10.tar.gz frost-27d1b98330c25e8833279ed48a4d9bd7d7566d10.tar.bz2 frost-27d1b98330c25e8833279ed48a4d9bd7d7566d10.zip |
Create readable time converter
Diffstat (limited to 'app/src/main/kotlin')
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt | 48 | ||||
-rw-r--r-- | app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt | 7 |
2 files changed, 51 insertions, 4 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt index 1ca6333b..c0feab1e 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/TimeUtils.kt @@ -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) +} diff --git a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt index ae45fa7b..1897b042 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/widgets/NotificationWidget.kt @@ -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() { } -}
\ No newline at end of file +} |