Skip to content

Commit

Permalink
#5 - Create a ViewHolder for all the sleep data
Browse files Browse the repository at this point in the history
  • Loading branch information
jhg3410 committed Jul 5, 2022
1 parent ff7f4c0 commit 41ae621
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.android.trackmysleepquality.database.SleepNight
import java.text.SimpleDateFormat
//import java.util.concurrent.TimeUnit
//import java.util.*
import java.util.concurrent.TimeUnit
import java.util.*


//private val ONE_MINUTE_MILLIS = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES)
//private val ONE_HOUR_MILLIS = TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS)
private val ONE_MINUTE_MILLIS = TimeUnit.MILLISECONDS.convert(1, TimeUnit.MINUTES)
private val ONE_HOUR_MILLIS = TimeUnit.MILLISECONDS.convert(1, TimeUnit.HOURS)

//fun convertDurationToFormatted(startTimeMilli: Long, endTimeMilli: Long, res: Resources): String {
// val durationMilli = endTimeMilli - startTimeMilli
// val weekdayString = SimpleDateFormat("EEEE", Locale.getDefault()).format(startTimeMilli)
// return when {
// durationMilli < ONE_MINUTE_MILLIS -> {
// val seconds = TimeUnit.SECONDS.convert(durationMilli, TimeUnit.MILLISECONDS)
// res.getString(R.string.seconds_length, seconds, weekdayString)
// }
// durationMilli < ONE_HOUR_MILLIS -> {
// val minutes = TimeUnit.MINUTES.convert(durationMilli, TimeUnit.MILLISECONDS)
// res.getString(R.string.minutes_length, minutes, weekdayString)
// }
// else -> {
// val hours = TimeUnit.HOURS.convert(durationMilli, TimeUnit.MILLISECONDS)
// res.getString(R.string.hours_length, hours, weekdayString)
// }
// }
//}
fun convertDurationToFormatted(startTimeMilli: Long, endTimeMilli: Long, res: Resources): String {
val durationMilli = endTimeMilli - startTimeMilli
val weekdayString = SimpleDateFormat("EEEE", Locale.getDefault()).format(startTimeMilli)
return when {
durationMilli < ONE_MINUTE_MILLIS -> {
val seconds = TimeUnit.SECONDS.convert(durationMilli, TimeUnit.MILLISECONDS)
res.getString(R.string.seconds_length, seconds, weekdayString)
}
durationMilli < ONE_HOUR_MILLIS -> {
val minutes = TimeUnit.MINUTES.convert(durationMilli, TimeUnit.MILLISECONDS)
res.getString(R.string.minutes_length, minutes, weekdayString)
}
else -> {
val hours = TimeUnit.HOURS.convert(durationMilli, TimeUnit.MILLISECONDS)
res.getString(R.string.hours_length, hours, weekdayString)
}
}
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,52 @@ package com.example.android.trackmysleepquality.sleeptracker

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.android.trackmysleepquality.R
import com.example.android.trackmysleepquality.TextItemViewHolder
import com.example.android.trackmysleepquality.convertDurationToFormatted
import com.example.android.trackmysleepquality.convertNumericQualityToString
import com.example.android.trackmysleepquality.database.SleepNight

class SleepNightAdapter : RecyclerView.Adapter<TextItemViewHolder>(){
class SleepNightAdapter : RecyclerView.Adapter<SleepNightAdapter.ViewHolder>(){

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val sleepLength: TextView = itemView.findViewById(R.id.sleep_length)
val quality: TextView = itemView.findViewById(R.id.quality_string)
val qualityImage: ImageView = itemView.findViewById(R.id.quality_image)
}

var data = listOf<SleepNight>()
set(value) {
field = value
notifyDataSetChanged()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TextItemViewHolder {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.text_item_view,parent,false) as TextView
val view = layoutInflater.inflate(R.layout.list_item_sleep_night,parent,false)

return TextItemViewHolder(view)
return ViewHolder(view)
}

override fun onBindViewHolder(holder: TextItemViewHolder, position: Int) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = data[position]
holder.textView.text = item.sleepQuality.toString()

if (item.sleepQuality <= 1){
holder.textView.setTextColor(Color.RED)
} else{
holder.textView.setTextColor(Color.BLACK)
}
val res = holder.itemView.context.resources
holder.sleepLength.text = convertDurationToFormatted(item.startTimeMilli, item.endTimeMilli, res)
holder.quality.text = convertNumericQualityToString(item.sleepQuality, res)
holder.qualityImage.setImageResource(when (item.sleepQuality) {
0 -> R.drawable.ic_sleep_0
1 -> R.drawable.ic_sleep_1
2 -> R.drawable.ic_sleep_2
3 -> R.drawable.ic_sleep_3
4 -> R.drawable.ic_sleep_4
5 -> R.drawable.ic_sleep_5
else -> R.drawable.ic_sleep_active
})
}

override fun getItemCount() = data.size
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/quality_image"
android:layout_width="@dimen/icon_size"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@drawable/ic_sleep_5" />

<TextView
android:id="@+id/sleep_length"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/quality_image"
app:layout_constraintTop_toTopOf="@id/quality_image"
tools:text="Wednesday!!!" />

<TextView
android:id="@+id/quality_string"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="@+id/sleep_length"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/sleep_length"
app:layout_constraintTop_toBottomOf="@+id/sleep_length"
tools:text="Excellent!!!" />


</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 41ae621

Please sign in to comment.