make the interaction between history dialog and adapter cleaner

This commit is contained in:
tibbi 2022-01-18 15:47:16 +01:00
parent 91e291e9f2
commit cf2c3e0012
2 changed files with 8 additions and 13 deletions

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.calculator.adapters package com.simplemobiletools.calculator.adapters
import android.app.Dialog
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
@ -12,10 +11,9 @@ import com.simplemobiletools.calculator.models.History
import com.simplemobiletools.commons.extensions.copyToClipboard import com.simplemobiletools.commons.extensions.copyToClipboard
import kotlinx.android.synthetic.main.history_view.view.* import kotlinx.android.synthetic.main.history_view.view.*
class HistoryAdapter(val activity: SimpleActivity, val items: List<History>, val calc: CalculatorImpl) : class HistoryAdapter(val activity: SimpleActivity, val items: List<History>, val calc: CalculatorImpl, val itemClick: () -> Unit) :
RecyclerView.Adapter<HistoryAdapter.ViewHolder>() { RecyclerView.Adapter<HistoryAdapter.ViewHolder>() {
private lateinit var dialog: Dialog
private var textColor = activity.config.textColor private var textColor = activity.config.textColor
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -30,10 +28,6 @@ class HistoryAdapter(val activity: SimpleActivity, val items: List<History>, val
override fun getItemCount() = items.size override fun getItemCount() = items.size
fun setDialog(dialog: Dialog) {
this.dialog = dialog
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(item: History): View { fun bindView(item: History): View {
itemView.apply { itemView.apply {
@ -44,7 +38,7 @@ class HistoryAdapter(val activity: SimpleActivity, val items: List<History>, val
setOnClickListener { setOnClickListener {
calc.addNumberToFormula(item.result) calc.addNumberToFormula(item.result)
dialog.dismiss() itemClick()
} }
setOnLongClickListener { setOnLongClickListener {
activity.baseContext.copyToClipboard(item.result) activity.baseContext.copyToClipboard(item.result)

View File

@ -15,19 +15,20 @@ import kotlinx.android.synthetic.main.dialog_history.view.*
class HistoryDialog() { class HistoryDialog() {
constructor(activity: SimpleActivity, items: List<History>, calculator: CalculatorImpl) : this() { constructor(activity: SimpleActivity, items: List<History>, calculator: CalculatorImpl) : this() {
val view = activity.layoutInflater.inflate(R.layout.dialog_history, null) val view = activity.layoutInflater.inflate(R.layout.dialog_history, null)
view.history_list.adapter = HistoryAdapter(activity, items, calculator)
AlertDialog.Builder(activity) val dialog = AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null) .setPositiveButton(R.string.ok, null)
.setNeutralButton(R.string.clear_history) { _, _ -> .setNeutralButton(R.string.clear_history) { _, _ ->
ensureBackgroundThread { ensureBackgroundThread {
activity.applicationContext.calculatorDB.deleteHistory() activity.applicationContext.calculatorDB.deleteHistory()
activity.toast(R.string.history_cleared) activity.toast(R.string.history_cleared)
} }
} }.create().apply {
.create().apply {
activity.setupDialogStuff(view, this, R.string.history) activity.setupDialogStuff(view, this, R.string.history)
(view.history_list.adapter as HistoryAdapter).setDialog(this) }
view.history_list.adapter = HistoryAdapter(activity, items, calculator) {
dialog.dismiss()
} }
} }
} }