From 8d4a34c86a3fa43c2df94d169f5e6de9d851c5a0 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 4 Dec 2024 11:50:41 +0100 Subject: [PATCH] Create DateFormatter extract date related logic from Task extensions Signed-off-by: alperozturk --- .../assistant/extensions/TaskExtensions.kt | 14 +++------- .../nextcloud/utils/date/DateFormatPattern.kt | 9 +++++-- .../com/nextcloud/utils/date/DateFormatter.kt | 26 +++++++++++++++++++ 3 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/com/nextcloud/utils/date/DateFormatter.kt diff --git a/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt b/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt index 4654dbecaa..1302b7a522 100644 --- a/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt +++ b/app/src/main/java/com/nextcloud/client/assistant/extensions/TaskExtensions.kt @@ -10,11 +10,10 @@ package com.nextcloud.client.assistant.extensions import android.content.Context -import android.icu.text.SimpleDateFormat +import com.nextcloud.utils.date.DateFormatPattern +import com.nextcloud.utils.date.DateFormatter import com.owncloud.android.R import com.owncloud.android.lib.resources.assistant.model.Task -import java.util.Date -import java.util.Locale import java.util.concurrent.TimeUnit fun Task.getInput(): String? = input?.input @@ -95,14 +94,7 @@ fun Task.getModifiedAtRepresentation(context: Context): String? { } else -> { - convertToDateFormat(modifiedAt) + DateFormatter.timestampToDateRepresentation(modifiedAt, DateFormatPattern.MonthWithDate) } } } - -@Suppress("MagicNumber") -private fun convertToDateFormat(timestamp: Long): String { - val date = Date(timestamp * 1000) - val format = SimpleDateFormat("MMM d", Locale.getDefault()) - return format.format(date) -} diff --git a/app/src/main/java/com/nextcloud/utils/date/DateFormatPattern.kt b/app/src/main/java/com/nextcloud/utils/date/DateFormatPattern.kt index 38072c3528..a3852d67af 100644 --- a/app/src/main/java/com/nextcloud/utils/date/DateFormatPattern.kt +++ b/app/src/main/java/com/nextcloud/utils/date/DateFormatPattern.kt @@ -9,7 +9,12 @@ package com.nextcloud.utils.date enum class DateFormatPattern(val pattern: String) { /** - * e.g. 10.11.2024 - 12:44 + * 10.11.2024 - 12:44 */ - FullDateWithHours("dd.MM.yyyy - HH:mm") + FullDateWithHours("dd.MM.yyyy - HH:mm"), + + /** + * Aug 3 + */ + MonthWithDate("MMM d") } diff --git a/app/src/main/java/com/nextcloud/utils/date/DateFormatter.kt b/app/src/main/java/com/nextcloud/utils/date/DateFormatter.kt new file mode 100644 index 0000000000..ea43a5a27b --- /dev/null +++ b/app/src/main/java/com/nextcloud/utils/date/DateFormatter.kt @@ -0,0 +1,26 @@ +/* + * Nextcloud - Android Client + * + * SPDX-FileCopyrightText: 2024 Alper Ozturk + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +package com.nextcloud.utils.date + +import android.icu.text.SimpleDateFormat +import java.util.Date +import java.util.Locale + +object DateFormatter { + + /** + * Converts a Unix timestamp (in milliseconds) into a formatted date string. + * For example, input 1733309160885 with "MMM d" pattern outputs "Dec 4". + */ + @Suppress("MagicNumber") + fun timestampToDateRepresentation(timestamp: Long, formatPattern: DateFormatPattern): String { + val date = Date(timestamp * 1000) + val format = SimpleDateFormat(formatPattern.pattern, Locale.getDefault()) + return format.format(date) + } +}