ntfy-android/app/src/main/java/io/heckel/ntfy/list/HeaderAdapter.kt
2021-10-25 09:01:10 -04:00

63 lines
2.2 KiB
Kotlin

/*
* 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.RecyclerView
import com.heckel.ntfy.R
/* A list always displaying one element: the number of flowers. */
class HeaderAdapter: RecyclerView.Adapter<HeaderAdapter.HeaderViewHolder>() {
private var flowerCount: Int = 0
/* ViewHolder for displaying header. */
class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view){
private val flowerNumberTextView: TextView = itemView.findViewById(R.id.flower_number_text)
fun bind(flowerCount: Int) {
flowerNumberTextView.text = flowerCount.toString()
}
}
/* Inflates view and returns HeaderViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.header_item, parent, false)
return HeaderViewHolder(view)
}
/* Binds number of flowers to the header. */
override fun onBindViewHolder(holder: HeaderViewHolder, position: Int) {
holder.bind(flowerCount)
}
/* Returns number of items, since there is only one item in the header return one */
override fun getItemCount(): Int {
return 1
}
/* Updates header to display number of flowers when a flower is added or subtracted. */
fun updateFlowerCount(updatedFlowerCount: Int) {
flowerCount = updatedFlowerCount
notifyDataSetChanged()
}
}