Advertisement
Guest User

Untitled

a guest
Dec 8th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.58 KB | None | 0 0
  1. /*
  2.  * Copyright 2017 Andrius Baruckis
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  * http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package com.baruckis.calculator
  18.  
  19. import android.content.Context
  20. import android.os.Bundle
  21. import android.support.design.widget.BottomSheetDialogFragment
  22. import android.support.v7.widget.LinearLayoutManager
  23. import android.support.v7.widget.RecyclerView
  24. import android.view.LayoutInflater
  25. import android.view.View
  26. import android.view.ViewGroup
  27. import android.widget.ImageButton
  28. import android.widget.LinearLayout
  29. import android.widget.TextView
  30. import android.widget.Toast
  31. import java.util.*
  32.  
  33.  
  34. /**
  35.  *
  36.  * A fragment that shows a list of items as a modal bottom sheet.
  37.  *
  38.  * You activity (or fragment) needs to implement [HistoryActionListDialogFragment.Listener].
  39.  */
  40. class HistoryActionListDialogFragment : BottomSheetDialogFragment() {
  41.  
  42.     // If ? is inserted after any type name, we have explicitly instructed the compiler that the value of the type can either store an object reference or can be null.
  43.     private var mListener: Listener? = null
  44.  
  45.     private lateinit var mData: ArrayList<String>
  46.  
  47.     override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
  48.                               savedInstanceState: Bundle?): View? {
  49.         return inflater!!.inflate(R.layout.fragment_history_action_list_dialog, container, false)
  50.     }
  51.  
  52.     override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
  53.         // Initializing an ArrayList in one line when fragment is already attached to Activity and string resources are available to reach.
  54.         mData = ArrayList<String>(Arrays.asList(getString(R.string.no_history)))
  55.  
  56.         val data = arguments.getStringArrayList(ARG_HISTORY_ACTION)
  57.         if (data.isNotEmpty()) {
  58.             mData.clear()
  59.             mData.addAll(data)
  60.         }
  61.  
  62.         val recyclerView = view!!.findViewById<View>(R.id.list) as RecyclerView?
  63.         recyclerView!!.layoutManager = LinearLayoutManager(context)
  64.         recyclerView.adapter = ItemAdapter(mData)
  65.  
  66.         val buttonClearHistory: ImageButton = view.findViewById(R.id.button_clear_history)
  67.         buttonClearHistory.setOnClickListener {
  68.             data.clear()
  69.             mData.clear()
  70.             mData.add(getString(R.string.no_history))
  71.             recyclerView.adapter.notifyDataSetChanged()
  72.             Toast.makeText(activity, getString(R.string.history_cleared), Toast.LENGTH_SHORT).show()
  73.         }
  74.     }
  75.  
  76.     override fun onAttach(context: Context?) {
  77.         super.onAttach(context)
  78.         val parent = parentFragment
  79.         if (parent != null) {
  80.             mListener = parent as Listener
  81.         } else {
  82.             mListener = context as Listener?
  83.         }
  84.     }
  85.  
  86.     override fun onDetach() {
  87.         mListener = null
  88.         super.onDetach()
  89.     }
  90.  
  91.     interface Listener {
  92.         fun onHistoryItemClicked(resultText: String)
  93.     }
  94.  
  95.     private inner class ViewHolder internal constructor(inflater: LayoutInflater, parent: ViewGroup) : RecyclerView.ViewHolder(inflater.inflate(R.layout.fragment_history_action_list_dialog_item, parent, false)) {
  96.  
  97.         internal val rowLayout: LinearLayout = itemView.findViewById<View>(R.id.row) as LinearLayout
  98.         internal val actionTextView: TextView = itemView.findViewById<View>(R.id.action) as TextView
  99.         internal val resultTextView: TextView = itemView.findViewById<View>(R.id.result) as TextView
  100.  
  101.         init {
  102.             rowLayout.setOnClickListener {
  103.                 if (mListener != null) {
  104.                     // Operator with !! allows to bypass nullability checking by the Kotlin compiler.
  105.                     mListener!!.onHistoryItemClicked(resultTextView.text.toString())
  106.                     dismiss()
  107.                 }
  108.             }
  109.         }
  110.  
  111.     }
  112.  
  113.     private inner class ItemAdapter internal constructor(private val mHistoryActionList: ArrayList<String>) : RecyclerView.Adapter<ViewHolder>() {
  114.  
  115.         override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  116.             return ViewHolder(LayoutInflater.from(parent.context), parent)
  117.         }
  118.  
  119.         override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  120.             // Regular expression lookbehind with delimiter "="
  121.             val reg = Regex("(?<=[=])")
  122.             val historyActionList = mHistoryActionList.get(position).split(reg)
  123.             holder.actionTextView.text = if (historyActionList.size == 1) "" else historyActionList.first()
  124.             holder.resultTextView.text = historyActionList.last().trim()
  125.         }
  126.  
  127.         override fun getItemCount(): Int {
  128.             return mHistoryActionList.count()
  129.         }
  130.  
  131.     }
  132.  
  133.     companion object {
  134.  
  135.         private val ARG_HISTORY_ACTION = "history_action"
  136.  
  137.         fun newInstance(historyActionList: ArrayList<String>): HistoryActionListDialogFragment {
  138.             val fragment = HistoryActionListDialogFragment()
  139.             val args = Bundle()
  140.             args.putStringArrayList(ARG_HISTORY_ACTION, historyActionList)
  141.             fragment.arguments = args
  142.             return fragment
  143.         }
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement