2021-10-25 15:01:10 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.heckel.ntfy.list
|
|
|
|
|
|
|
|
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
|
2021-10-25 22:16:23 +02:00
|
|
|
import io.heckel.ntfy.R
|
2021-10-25 15:01:10 +02:00
|
|
|
import io.heckel.ntfy.data.Topic
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
class TopicsAdapter(private val onClick: (Topic) -> Unit) :
|
|
|
|
ListAdapter<Topic, TopicsAdapter.TopicViewHolder>(TopicDiffCallback) {
|
2021-10-25 15:01:10 +02:00
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* ViewHolder for Topic, takes in the inflated view and the onClick behavior. */
|
|
|
|
class TopicViewHolder(itemView: View, val onClick: (Topic) -> Unit) :
|
2021-10-25 15:01:10 +02:00
|
|
|
RecyclerView.ViewHolder(itemView) {
|
2021-10-25 19:45:56 +02:00
|
|
|
private val topicTextView: TextView = itemView.findViewById(R.id.topic_text)
|
|
|
|
private var currentTopic: Topic? = null
|
2021-10-25 15:01:10 +02:00
|
|
|
|
|
|
|
init {
|
|
|
|
itemView.setOnClickListener {
|
2021-10-25 19:45:56 +02:00
|
|
|
currentTopic?.let {
|
2021-10-25 15:01:10 +02:00
|
|
|
onClick(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Bind topic name and image. */
|
|
|
|
fun bind(topic: Topic) {
|
|
|
|
currentTopic = topic
|
|
|
|
topicTextView.text = topic.url
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Creates and inflates view and return TopicViewHolder. */
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopicViewHolder {
|
2021-10-25 15:01:10 +02:00
|
|
|
val view = LayoutInflater.from(parent.context)
|
2021-10-25 19:45:56 +02:00
|
|
|
.inflate(R.layout.topic_item, parent, false)
|
|
|
|
return TopicViewHolder(view, onClick)
|
2021-10-25 15:01:10 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
/* Gets current topic and uses it to bind view. */
|
|
|
|
override fun onBindViewHolder(holder: TopicViewHolder, position: Int) {
|
|
|
|
val topic = getItem(position)
|
|
|
|
holder.bind(topic)
|
2021-10-25 15:01:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-25 19:45:56 +02:00
|
|
|
object TopicDiffCallback : DiffUtil.ItemCallback<Topic>() {
|
2021-10-25 15:01:10 +02:00
|
|
|
override fun areItemsTheSame(oldItem: Topic, newItem: Topic): Boolean {
|
|
|
|
return oldItem == newItem
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun areContentsTheSame(oldItem: Topic, newItem: Topic): Boolean {
|
|
|
|
return oldItem.id == newItem.id
|
|
|
|
}
|
|
|
|
}
|