Skip to content

Commit

Permalink
feat: use default locale for dateTime format in ContributorsFragment.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
VaiTon committed Jun 19, 2022
1 parent 6233fa3 commit dbdb089
Showing 1 changed file with 23 additions and 13 deletions.
Expand Up @@ -2,6 +2,7 @@ package openfoodfacts.github.scrachx.openfood.features.product.view.contributors

import android.os.Bundle
import android.text.Spanned
import android.text.format.DateFormat
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.LayoutInflater
Expand All @@ -10,6 +11,8 @@ import android.view.ViewGroup
import androidx.core.text.buildSpannedString
import androidx.fragment.app.viewModels
import dagger.hilt.android.AndroidEntryPoint
import logcat.asLog
import logcat.logcat
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.databinding.FragmentContributorsBinding
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity.Companion.KEY_STATE
Expand All @@ -22,7 +25,7 @@ import openfoodfacts.github.scrachx.openfood.repositories.TaxonomiesRepository
import openfoodfacts.github.scrachx.openfood.utils.LocaleManager
import openfoodfacts.github.scrachx.openfood.utils.SearchType
import openfoodfacts.github.scrachx.openfood.utils.requireProductState
import java.text.SimpleDateFormat
import java.time.Instant
import java.util.*
import javax.inject.Inject

Expand All @@ -46,6 +49,14 @@ class ContributorsFragment : BaseFragment() {

private lateinit var productState: ProductState

private val dateFormat = DateFormat.getDateFormat(requireContext()).apply {
timeZone = TimeZone.getTimeZone("CET")
}
private val timeFormatter = DateFormat.getTimeFormat(requireContext()).apply {
timeZone = TimeZone.getTimeZone("CET")
}


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentContributorsBinding.inflate(inflater)
return binding.root
Expand All @@ -69,15 +80,15 @@ class ContributorsFragment : BaseFragment() {
val product = this.productState.product!!

if (!product.creator.isNullOrBlank()) {
val createdDate = getDateTime(product.createdDateTime!!)
val createdDate = formatDateTime(product.createdDateTime!!)
binding.creatorTxt.movementMethod = LinkMovementMethod.getInstance()
binding.creatorTxt.text = getString(R.string.creator_history, createdDate.first, createdDate.second, product.creator)
} else {
binding.creatorTxt.visibility = View.INVISIBLE
}

if (!product.lastModifiedBy.isNullOrBlank()) {
val lastEditDate = getDateTime(product.lastModifiedTime!!)
val lastEditDate = formatDateTime(product.lastModifiedTime!!)
binding.lastEditorTxt.movementMethod = LinkMovementMethod.getInstance()
binding.lastEditorTxt.text = getString(R.string.last_editor_history, lastEditDate.first, lastEditDate.second, product.lastModifiedBy)
} else {
Expand Down Expand Up @@ -108,18 +119,16 @@ class ContributorsFragment : BaseFragment() {
/**
* Get date and time in MMMM dd, yyyy and HH:mm:ss a format
*
* @param dateTime date and time in miliseconds
* @param epoch unix epoch in seconds
*/
private fun getDateTime(dateTime: String): Pair<String, String> {
val unixSeconds = dateTime.toLong()
val date = Date(unixSeconds * 1000L)
val sdf = SimpleDateFormat("MMMM dd, yyyy", Locale.getDefault()).apply {
timeZone = TimeZone.getTimeZone("CET")
}
val sdf2 = SimpleDateFormat("HH:mm:ss a", Locale.getDefault()).apply {
timeZone = TimeZone.getTimeZone("CET")
private fun formatDateTime(epoch: String): Pair<String, String> {
val date = try {
Instant.ofEpochSecond(epoch.toLong())
} catch (err: Exception) {
logcat { "Could not parse product date $epoch: " + err.asLog() }
return "" to ""
}
return sdf.format(date) to sdf2.format(date)
return dateFormat.format(date) to timeFormatter.format(date)
}

private fun getContributorsTag(contributor: String): CharSequence {
Expand Down Expand Up @@ -188,6 +197,7 @@ class ContributorsFragment : BaseFragment() {
}

companion object {

fun newInstance(productState: ProductState) = ContributorsFragment().apply {
arguments = Bundle().apply {
putSerializable(KEY_STATE, productState)
Expand Down

0 comments on commit dbdb089

Please sign in to comment.