Commit aba2026e authored by Isira Seneviratne's avatar Isira Seneviratne

Add extension function to convert long values to LocalDateTime.

parent 0a59cdb2
......@@ -24,9 +24,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import edu.berkeley.boinc.databinding.EventLogClientListItemLayoutBinding
import edu.berkeley.boinc.rpc.Message
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import edu.berkeley.boinc.utils.secondsToLocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
......@@ -54,16 +52,13 @@ class ClientLogRecyclerViewAdapter(private val messages: List<Message>) :
}
}
fun getDateTimeString(position: Int): String? {
val instant = Instant.ofEpochSecond(messages[position].timestamp)
return dateTimeFormatter.format(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()))
}
fun getDateTimeString(position: Int): String = dateTimeFormatter.format(messages[position].timestamp.secondsToLocalDateTime())
fun getMessage(position: Int) = messages[position].body
fun getProject(position: Int) = messages[position].project
inner class ViewHolder(binding: EventLogClientListItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
class ViewHolder(binding: EventLogClientListItemLayoutBinding) : RecyclerView.ViewHolder(binding.root) {
val date = binding.msgsDate
val project = binding.msgsProject
val message = binding.msgsMessage
......
......@@ -32,9 +32,7 @@ import edu.berkeley.boinc.R
import edu.berkeley.boinc.databinding.NoticesLayoutListItemBinding
import edu.berkeley.boinc.rpc.Notice
import edu.berkeley.boinc.utils.Logging
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import edu.berkeley.boinc.utils.secondsToLocalDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
......@@ -79,9 +77,7 @@ class NoticesRecyclerViewAdapter(
holder.title.text = listItem.title
holder.content.text = listItem.description.parseAsHtml()
val localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(
listItem.createTime.toLong()), ZoneId.systemDefault())
holder.time.text = dateTimeFormatter.format(localDateTime)
holder.time.text = dateTimeFormatter.format(listItem.createTime.toLong().secondsToLocalDateTime())
}
private fun getIcon(position: Int): Bitmap? {
......
......@@ -35,9 +35,6 @@ import edu.berkeley.boinc.databinding.TasksLayoutListItemBinding
import edu.berkeley.boinc.rpc.RpcClient
import edu.berkeley.boinc.utils.*
import java.text.NumberFormat
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import kotlin.math.roundToInt
......@@ -46,6 +43,7 @@ class TaskRecyclerViewAdapter(
private val fragment: TasksFragment,
private val taskList: List<TasksFragment.TaskData>
) : RecyclerView.Adapter<TaskRecyclerViewAdapter.ViewHolder>() {
private val dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
private val elapsedTimeStringBuilder = StringBuilder()
private val percentNumberFormat = NumberFormat.getPercentInstance().apply { minimumFractionDigits = 3 }
......@@ -138,10 +136,7 @@ class TaskRecyclerViewAdapter(
holder.time.text = DateUtils.formatElapsedTime(elapsedTimeStringBuilder, elapsedTime)
// set deadline
val deadlineDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(
item.result.reportDeadline), ZoneId.systemDefault())
val deadline = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.format(deadlineDateTime)
val deadline = dateTimeFormatter.format(item.result.reportDeadline.secondsToLocalDateTime())
holder.deadline.text = deadline
// set application friendly name
if (result.app != null) {
......@@ -253,7 +248,7 @@ class TaskRecyclerViewAdapter(
}
}
inner class ViewHolder(binding: TasksLayoutListItemBinding) : RecyclerView.ViewHolder(binding.root) {
class ViewHolder(binding: TasksLayoutListItemBinding) : RecyclerView.ViewHolder(binding.root) {
val root = binding.root
val projectIcon = binding.projectIcon
val header = binding.taskHeader
......
......@@ -35,6 +35,9 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import java.io.IOException
import java.io.Reader
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
val ConnectivityManager.isOnline: Boolean
get() {
......@@ -105,3 +108,8 @@ fun Context.translateRPCReason(reason: Int) = when (reason) {
RPC_REASON_PROJECT_REQ -> resources.getString(R.string.rpcreason_projectreq)
else -> resources.getString(R.string.rpcreason_unknown)
}
@Suppress("NOTHING_TO_INLINE")
inline fun Long.secondsToLocalDateTime(
zoneId: ZoneId = ZoneId.systemDefault()
): LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(this), zoneId)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment