Benutzte ListAdapter statt Kiel fürs Listing
parent
ae7670f816
commit
4aa9262d79
@ -0,0 +1,12 @@
|
|||||||
|
package com.cyb3rko.techniklogger.recycler
|
||||||
|
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
data class ProjectEntry(
|
||||||
|
val date: Date,
|
||||||
|
val duration: String,
|
||||||
|
val location: String,
|
||||||
|
val name: String,
|
||||||
|
val objectId: String,
|
||||||
|
val time: String
|
||||||
|
)
|
||||||
@ -1,14 +0,0 @@
|
|||||||
package com.cyb3rko.techniklogger.recycler
|
|
||||||
|
|
||||||
import android.view.View
|
|
||||||
import android.widget.TextView
|
|
||||||
|
|
||||||
import com.cyb3rko.techniklogger.R
|
|
||||||
|
|
||||||
import me.ibrahimyilmaz.kiel.core.RecyclerViewHolder
|
|
||||||
|
|
||||||
class ProjectEntryViewHolder(view: View) : RecyclerViewHolder<ProjectViewState.ProjectEntry>(view) {
|
|
||||||
val locationView: TextView = view.findViewById(R.id.item_location)
|
|
||||||
val dateView: TextView = view.findViewById(R.id.item_date)
|
|
||||||
val textView: TextView = view.findViewById(R.id.item_text)
|
|
||||||
}
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
package com.cyb3rko.techniklogger.recycler
|
|
||||||
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
sealed class ProjectViewState {
|
|
||||||
|
|
||||||
data class ProjectEntry(
|
|
||||||
val childKey: String,
|
|
||||||
val text: String,
|
|
||||||
val location: String,
|
|
||||||
val date: Date,
|
|
||||||
val time: String,
|
|
||||||
val duration: String
|
|
||||||
) : ProjectViewState()
|
|
||||||
}
|
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package com.cyb3rko.techniklogger.recycler
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.View
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import android.widget.TextView
|
||||||
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
|
import androidx.recyclerview.widget.ListAdapter
|
||||||
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.cyb3rko.techniklogger.R
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
class ProjectsAdapter(
|
||||||
|
val action: (objectId: String) -> Unit,
|
||||||
|
val actionLong: (project: ProjectEntry) -> Boolean
|
||||||
|
) : ListAdapter<ProjectEntry, ProjectsAdapter.ViewHolder>(ProjectDiffCallback) {
|
||||||
|
|
||||||
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||||
|
val view = LayoutInflater.from(parent.context)
|
||||||
|
.inflate(R.layout.item_recycler_projects, parent, false)
|
||||||
|
|
||||||
|
return ViewHolder(view)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||||
|
val entry = getItem(position)
|
||||||
|
holder.textView.text = entry.name
|
||||||
|
holder.locationView.text = entry.location
|
||||||
|
if (entry.duration != "null") {
|
||||||
|
val date = SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).format(entry.date)
|
||||||
|
@SuppressLint("SetTextI18n")
|
||||||
|
holder.dateView.text = "${date}, ${entry.duration} h"
|
||||||
|
} else {
|
||||||
|
val date = SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).format(entry.date)
|
||||||
|
holder.dateView.text = date
|
||||||
|
}
|
||||||
|
holder.itemView.setOnClickListener {
|
||||||
|
action(entry.objectId)
|
||||||
|
}
|
||||||
|
holder.itemView.setOnLongClickListener {
|
||||||
|
return@setOnLongClickListener actionLong(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||||
|
val locationView: TextView = view.findViewById(R.id.item_location)
|
||||||
|
val dateView: TextView = view.findViewById(R.id.item_date)
|
||||||
|
val textView: TextView = view.findViewById(R.id.item_text)
|
||||||
|
}
|
||||||
|
|
||||||
|
object ProjectDiffCallback : DiffUtil.ItemCallback<ProjectEntry>() {
|
||||||
|
override fun areItemsTheSame(oldItem: ProjectEntry, newItem: ProjectEntry): Boolean {
|
||||||
|
return oldItem.objectId == newItem.objectId
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: ProjectEntry, newItem: ProjectEntry): Boolean {
|
||||||
|
return oldItem.date == newItem.date &&
|
||||||
|
oldItem.duration == newItem.duration &&
|
||||||
|
oldItem.location == newItem.location &&
|
||||||
|
oldItem.name == newItem.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue