|
@@ -24,7 +24,8 @@ import rx.android.schedulers.AndroidSchedulers
|
|
|
* Controller that shows the currently active downloads.
|
|
|
* Uses R.layout.fragment_download_queue.
|
|
|
*/
|
|
|
-class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
+class DownloadController : NucleusController<DownloadPresenter>(),
|
|
|
+ DownloadAdapter.DownloadItemListener {
|
|
|
|
|
|
/**
|
|
|
* Adapter containing the active downloads.
|
|
@@ -64,14 +65,15 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
setInformationView()
|
|
|
|
|
|
// Initialize adapter.
|
|
|
- adapter = DownloadAdapter()
|
|
|
+ adapter = DownloadAdapter(this@DownloadController)
|
|
|
recycler.adapter = adapter
|
|
|
+ adapter?.isHandleDragEnabled = true
|
|
|
|
|
|
// Set the layout manager for the recycler and fixed size.
|
|
|
recycler.layoutManager = LinearLayoutManager(view.context)
|
|
|
recycler.setHasFixedSize(true)
|
|
|
|
|
|
- // Suscribe to changes
|
|
|
+ // Subscribe to changes
|
|
|
DownloadService.runningRelay
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
.subscribeUntilDestroy { onQueueStatusChange(it) }
|
|
@@ -99,14 +101,10 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
}
|
|
|
|
|
|
override fun onPrepareOptionsMenu(menu: Menu) {
|
|
|
- // Set start button visibility.
|
|
|
menu.findItem(R.id.start_queue).isVisible = !isRunning && !presenter.downloadQueue.isEmpty()
|
|
|
-
|
|
|
- // Set pause button visibility.
|
|
|
menu.findItem(R.id.pause_queue).isVisible = isRunning
|
|
|
-
|
|
|
- // Set clear button visibility.
|
|
|
menu.findItem(R.id.clear_queue).isVisible = !presenter.downloadQueue.isEmpty()
|
|
|
+ menu.findItem(R.id.reorder).isVisible = !presenter.downloadQueue.isEmpty()
|
|
|
}
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
@@ -121,6 +119,16 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
DownloadService.stop(context)
|
|
|
presenter.clearQueue()
|
|
|
}
|
|
|
+ R.id.newest, R.id.oldest -> {
|
|
|
+ val adapter = adapter ?: return false
|
|
|
+ val items = adapter.currentItems.sortedBy { it.download.chapter.date_upload }
|
|
|
+ .toMutableList()
|
|
|
+ if (item.itemId == R.id.newest)
|
|
|
+ items.reverse()
|
|
|
+ adapter.updateDataSet(items)
|
|
|
+ val downloads = items.mapNotNull { it.download }
|
|
|
+ presenter.reorder(downloads)
|
|
|
+ }
|
|
|
}
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
}
|
|
@@ -173,7 +181,7 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
// Avoid leaking subscriptions
|
|
|
progressSubscriptions.remove(download)?.unsubscribe()
|
|
|
|
|
|
- progressSubscriptions.put(download, subscription)
|
|
|
+ progressSubscriptions[download] = subscription
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -203,10 +211,10 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
*
|
|
|
* @param downloads the downloads from the queue.
|
|
|
*/
|
|
|
- fun onNextDownloads(downloads: List<Download>) {
|
|
|
+ fun onNextDownloads(downloads: List<DownloadItem>) {
|
|
|
activity?.invalidateOptionsMenu()
|
|
|
setInformationView()
|
|
|
- adapter?.setItems(downloads)
|
|
|
+ adapter?.updateDataSet(downloads)
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -214,7 +222,7 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
*
|
|
|
* @param download the download whose progress has changed.
|
|
|
*/
|
|
|
- fun onUpdateProgress(download: Download) {
|
|
|
+ private fun onUpdateProgress(download: Download) {
|
|
|
getHolder(download)?.notifyProgress()
|
|
|
}
|
|
|
|
|
@@ -223,7 +231,7 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
*
|
|
|
* @param download the download whose page has been downloaded.
|
|
|
*/
|
|
|
- fun onUpdateDownloadedPages(download: Download) {
|
|
|
+ private fun onUpdateDownloadedPages(download: Download) {
|
|
|
getHolder(download)?.notifyDownloadedPages()
|
|
|
}
|
|
|
|
|
@@ -247,4 +255,48 @@ class DownloadController : NucleusController<DownloadPresenter>() {
|
|
|
empty_view?.hide()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Called when an item is released from a drag.
|
|
|
+ *
|
|
|
+ * @param position The position of the released item.
|
|
|
+ */
|
|
|
+ override fun onItemReleased(position: Int) {
|
|
|
+ val adapter = adapter ?: return
|
|
|
+ val downloads = (0 until adapter.itemCount).mapNotNull { adapter.getItem(it)?.download }
|
|
|
+ presenter.reorder(downloads)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Called when the menu item of a download is pressed
|
|
|
+ *
|
|
|
+ * @param position The position of the item
|
|
|
+ * @param menuItem The menu Item pressed
|
|
|
+ */
|
|
|
+ override fun onMenuItemClick(position: Int, menuItem: MenuItem) {
|
|
|
+ when (menuItem.itemId) {
|
|
|
+ R.id.move_to_top, R.id.move_to_bottom -> {
|
|
|
+ val items = adapter?.currentItems?.toMutableList() ?: return
|
|
|
+ val item = items[position]
|
|
|
+ items.remove(item)
|
|
|
+ if (menuItem.itemId == R.id.move_to_top)
|
|
|
+ items.add(0, item)
|
|
|
+ else
|
|
|
+ items.add(item)
|
|
|
+ adapter?.updateDataSet(items)
|
|
|
+ val downloads = items.mapNotNull { it.download }
|
|
|
+ presenter.reorder(downloads)
|
|
|
+ }
|
|
|
+ R.id.cancel_download -> {
|
|
|
+ val download = adapter?.getItem(position)?.download ?: return
|
|
|
+ presenter.cancelDownload(download)
|
|
|
+
|
|
|
+ adapter?.removeItem(position)
|
|
|
+ val adapter = adapter ?: return
|
|
|
+ val downloads =
|
|
|
+ (0 until adapter.itemCount).mapNotNull { adapter.getItem(it)?.download }
|
|
|
+ presenter.reorder(downloads)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|