ariane/app/src/main/java/oppen/ariane/ui/modals_menus/history/HistoryDialog.kt

60 lines
2.2 KiB
Kotlin
Raw Normal View History

package oppen.ariane.ui.modals_menus.history
2020-08-20 13:52:24 +00:00
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.ariane.R
import oppen.ariane.io.gemini.RuntimeCache
import oppen.ariane.io.history.uris.HistoryInterface
2020-08-20 13:52:24 +00:00
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()
}
}