taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

TosAdapter.kt (3466B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.wallet.withdraw
     18 
     19 import android.transition.ChangeBounds
     20 import android.transition.TransitionManager.beginDelayedTransition
     21 import android.view.LayoutInflater
     22 import android.view.View
     23 import android.view.View.GONE
     24 import android.view.View.VISIBLE
     25 import android.view.ViewGroup
     26 import android.widget.ImageView
     27 import android.widget.TextView
     28 import androidx.recyclerview.widget.RecyclerView
     29 import io.noties.markwon.Markwon
     30 import net.taler.wallet.R
     31 
     32 class TosAdapter(
     33     private val markwon: Markwon
     34 ) : RecyclerView.Adapter<TosAdapter.TosSectionViewHolder>() {
     35 
     36     private val items = ArrayList<TosSection>()
     37 
     38     init {
     39         setHasStableIds(true)
     40     }
     41 
     42     override fun getItemCount() = items.size
     43 
     44     override fun getItemId(position: Int): Long {
     45         return items[position].node.hashCode().toLong()
     46     }
     47 
     48     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TosSectionViewHolder {
     49         val v = LayoutInflater.from(parent.context).inflate(R.layout.list_item_tos, parent, false)
     50         return TosSectionViewHolder(v)
     51     }
     52 
     53     override fun onBindViewHolder(holder: TosSectionViewHolder, position: Int) {
     54         holder.bind(items[position])
     55     }
     56 
     57     fun setSections(sections: List<TosSection>) {
     58         items.clear()
     59         items.addAll(sections)
     60         notifyDataSetChanged()
     61     }
     62 
     63     inner class TosSectionViewHolder(private val v: View) : RecyclerView.ViewHolder(v) {
     64         private val sectionTitle: TextView = v.findViewById(R.id.sectionTitle)
     65         private val expandButton: ImageView = v.findViewById(R.id.expandButton)
     66         private val sectionText: TextView = v.findViewById(R.id.sectionText)
     67 
     68         fun bind(item: TosSection) {
     69             sectionTitle.text = item.title
     70                 ?: v.context.getString(R.string.exchange_tos)
     71             showSection(item, item.expanded)
     72             val onClickListener = View.OnClickListener {
     73                 val transition = ChangeBounds()
     74                 transition.duration = 200L
     75                 if (!item.expanded) beginDelayedTransition(v as ViewGroup, transition)
     76                 item.expanded = !item.expanded
     77                 showSection(item, item.expanded)
     78             }
     79             sectionTitle.setOnClickListener(onClickListener)
     80         }
     81 
     82         private fun showSection(item: TosSection, show: Boolean) {
     83             if (show) {
     84                 expandButton.setImageResource(R.drawable.ic_keyboard_arrow_up)
     85                 markwon.setParsedMarkdown(sectionText, markwon.render(item.node))
     86                 sectionText.visibility = VISIBLE
     87             } else {
     88                 expandButton.setImageResource(R.drawable.ic_keyboard_arrow_down)
     89                 sectionText.visibility = GONE
     90             }
     91         }
     92     }
     93 
     94 }