From cec5ae1fea4a31ad08981a9999ca9a039b084b51 Mon Sep 17 00:00:00 2001 From: arran Date: Fri, 3 Nov 2017 21:51:50 +1000 Subject: [PATCH] delete torrent from long click --- .../tex/views/all/list/AllTorrentsAdapter.kt | 17 +++++++++++++++-- .../shwifty/tex/views/all/mvp/AllFragment.kt | 10 +++++++++- app/src/main/res/menu/popup_view_torrent.xml | 5 +++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 app/src/main/res/menu/popup_view_torrent.xml diff --git a/app/src/main/java/com/shwifty/tex/views/all/list/AllTorrentsAdapter.kt b/app/src/main/java/com/shwifty/tex/views/all/list/AllTorrentsAdapter.kt index f3d0e85..d2eacf2 100644 --- a/app/src/main/java/com/shwifty/tex/views/all/list/AllTorrentsAdapter.kt +++ b/app/src/main/java/com/shwifty/tex/views/all/list/AllTorrentsAdapter.kt @@ -1,9 +1,11 @@ package com.shwifty.tex.views.all.list +import android.support.v7.widget.PopupMenu import android.support.v7.widget.RecyclerView import android.view.View import android.view.ViewGroup import com.pawegio.kandroid.inflateLayout +import com.schiwfty.kotlinfilebrowser.onLongClick import com.schiwfty.torrentwrapper.models.TorrentInfo import com.shwifty.tex.R import com.shwifty.tex.utils.onClick @@ -11,13 +13,24 @@ import com.shwifty.tex.utils.onClick /** * Created by arran on 19/04/2017. */ -class AllTorrentsAdapter(val itemClickListener: (View, Int, Int) -> Unit) : RecyclerView.Adapter() { +class AllTorrentsAdapter(val itemClickListener: (View, Int, Int) -> Unit, val onDeleteClick: (View, Int, Int) -> Unit) : RecyclerView.Adapter() { var torrentFiles: List = mutableListOf() override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AllTorrentsCardHolder { val itemView = parent.context.inflateLayout(R.layout.list_item_torrent_all, parent, false) val holder = AllTorrentsCardHolder(itemView) - holder.onClick (itemClickListener) + holder.onClick(itemClickListener) + holder.onLongClick { view, position, type -> + val popup = PopupMenu(itemView.context, itemView) + popup.menuInflater.inflate(R.menu.popup_view_torrent, popup.menu) + popup.setOnMenuItemClickListener { + when (it.itemId) { + R.id.menu_item_delete -> onDeleteClick(view, position, type) + } + true + } + popup.show() + } return holder } diff --git a/app/src/main/java/com/shwifty/tex/views/all/mvp/AllFragment.kt b/app/src/main/java/com/shwifty/tex/views/all/mvp/AllFragment.kt index 5c2acce..50a4abd 100644 --- a/app/src/main/java/com/shwifty/tex/views/all/mvp/AllFragment.kt +++ b/app/src/main/java/com/shwifty/tex/views/all/mvp/AllFragment.kt @@ -29,7 +29,15 @@ class AllFragment : BaseFragment(), AllContract.View { val torrentFile = filesAdapter.torrentFiles[position] MainEventHandler.showTorrentInfo(torrentFile) } - val filesAdapter = AllTorrentsAdapter(itemOnClick) + + val onDeleteItem: (View, Int, Int) -> Unit = { _, position, _ -> + val torrentFile = filesAdapter.torrentFiles[position] + Trickl.dialogManager.showDeleteTorrentDialog(context, torrentFile, { + showError(R.string.error_deleting_torrent) + }) + } + + val filesAdapter = AllTorrentsAdapter(itemOnClick, onDeleteItem) companion object { fun newInstance(): Fragment { diff --git a/app/src/main/res/menu/popup_view_torrent.xml b/app/src/main/res/menu/popup_view_torrent.xml new file mode 100644 index 0000000..b955c3a --- /dev/null +++ b/app/src/main/res/menu/popup_view_torrent.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file