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
import android.app.Dialog
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
@ -12,10 +11,9 @@ import com.simplemobiletools.calculator.models.History
import com.simplemobiletools.commons.extensions.copyToClipboard
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>() {
private lateinit var dialog: Dialog
private var textColor = activity.config.textColor
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
fun setDialog(dialog: Dialog) {
this.dialog = dialog
}
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindView(item: History): View {
itemView.apply {
@ -44,7 +38,7 @@ class HistoryAdapter(val activity: SimpleActivity, val items: List<History>, val
setOnClickListener {
calc.addNumberToFormula(item.result)
dialog.dismiss()
itemClick()
}
setOnLongClickListener {
activity.baseContext.copyToClipboard(item.result)

View File

@ -15,19 +15,20 @@ import kotlinx.android.synthetic.main.dialog_history.view.*
class HistoryDialog() {
constructor(activity: SimpleActivity, items: List<History>, calculator: CalculatorImpl) : this() {
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)
.setNeutralButton(R.string.clear_history) { _, _ ->
ensureBackgroundThread {
activity.applicationContext.calculatorDB.deleteHistory()
activity.toast(R.string.history_cleared)
}
}
.create().apply {
}.create().apply {
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()
}
}
}