Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinTCoughlin committed Nov 26, 2024
1 parent d0362e6 commit 40283ff
Showing 1 changed file with 9 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.kevintcoughlin.smodr.viewholders

import android.annotation.SuppressLint
import android.graphics.Color
import android.text.Html
import android.view.LayoutInflater
import android.view.ViewGroup
Expand All @@ -23,42 +22,30 @@ class EpisodeView : BinderRecyclerAdapter.Binder<Item, EpisodeViewHolder> {
formatDate(model.pubDate),
model.duration
)

val textColor = if (model.completed) COLOR_GRAY else COLOR_BLACK
mTitle.setTextColor(textColor)
mDescription.setTextColor(textColor)
mMetadata.setTextColor(textColor)
}
}

override fun createViewHolder(parent: ViewGroup): EpisodeViewHolder {
val binding = ItemListEpisodeLayoutBinding.inflate(
override fun createViewHolder(parent: ViewGroup): EpisodeViewHolder =
ItemListEpisodeLayoutBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return EpisodeViewHolder(binding)
}
).let { EpisodeViewHolder(it) }

companion object {
private val DATE_FORMAT_INPUT = SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US)
@SuppressLint("ConstantLocale")
private val DATE_FORMAT_OUTPUT = SimpleDateFormat("dd MMM", Locale.getDefault())

private const val COLOR_BLACK = Color.BLACK
private val COLOR_GRAY = Color.rgb(150, 150, 150)

/**
* Formats a date string to a localized display format.
* Returns an empty string if parsing fails.
*/
fun formatDate(dateTimeString: String?): String {
return try {
val date = dateTimeString?.let { DATE_FORMAT_INPUT.parse(it) }
date?.let { DATE_FORMAT_OUTPUT.format(it) } ?: ""
} catch (e: Exception) {
""
}
}
fun formatDate(dateTimeString: String?): String =
dateTimeString?.let {
runCatching { DATE_FORMAT_INPUT.parse(it) }
.mapCatching { DATE_FORMAT_OUTPUT.format(it!!) }
.getOrDefault("")
} ?: ""
}
}

0 comments on commit 40283ff

Please sign in to comment.