Create DateFormatter extract date related logic from Task extensions

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-12-04 11:50:41 +01:00 committed by Alper Öztürk
parent 3cffeee863
commit 8d4a34c86a
3 changed files with 36 additions and 13 deletions

View file

@ -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)
}

View file

@ -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")
}

View file

@ -0,0 +1,26 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
* 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)
}
}