package oppen.tva.ui.modals_menus.history import android.content.Context import android.view.MenuInflater import android.view.View import android.widget.Toast import androidx.appcompat.app.AppCompatDialog import androidx.appcompat.widget.PopupMenu import androidx.core.view.MenuCompat import androidx.recyclerview.widget.LinearLayoutManager import kotlinx.android.synthetic.main.dialog_about.view.close_tab_dialog import kotlinx.android.synthetic.main.dialog_history.view.* import oppen.tva.R import oppen.tva.io.gemini.RuntimeCache import oppen.tva.io.history.uris.HistoryInterface object HistoryDialog { fun show(context: Context, onHistoryItem: (address: String) -> Unit){ val historyCache = HistoryInterface.default(context) val history = historyCache.get() val dialog = AppCompatDialog(context, R.style.AppTheme) val view = View.inflate(context, R.layout.dialog_history, null) dialog.setContentView(view) view.close_tab_dialog.setOnClickListener { dialog.dismiss() } view.history_overflow.setOnClickListener { val popup = PopupMenu(view.context, view.history_overflow) val inflater: MenuInflater = popup.menuInflater inflater.inflate(R.menu.history_overflow_menu, popup.menu) popup.setOnMenuItemClickListener { menuItem -> if(menuItem.itemId == R.id.history_overflow_clear_history){ historyCache.clear() dialog.dismiss() Toast.makeText(context, "History cleared", Toast.LENGTH_SHORT).show() }else if(menuItem.itemId == R.id.history_overflow_clear_runtime_cache){ RuntimeCache.clear() dialog.dismiss() Toast.makeText(context, "Runtime cache cleared", Toast.LENGTH_SHORT).show() } true } MenuCompat.setGroupDividerEnabled(popup.menu, true) popup.show() } view.history_recycler.layoutManager = LinearLayoutManager(context) view.history_recycler.adapter = HistoryAdapter(history.asReversed()){ address -> onHistoryItem(address) dialog.dismiss() } dialog.show() } }