From a73c847e3f1281205d9779b7081c6a36c05502e0 Mon Sep 17 00:00:00 2001
From: Jay <jays2kings@gmail.com>
Date: Thu, 9 Apr 2020 00:50:22 -0400
Subject: [PATCH] Added Remove Downloads to manga details

Collasaping downloads if theres no downloads when its not in view
---
 .../ui/manga/MangaDetailsController.kt        | 28 ++++++++++++++++++-
 .../ui/manga/MangaDetailsPresenter.kt         |  1 +
 .../tachiyomi/ui/recents/RecentsController.kt |  8 +++---
 app/src/main/res/menu/manga_details.xml       | 19 ++++++++++++-
 app/src/main/res/menu/manga_info.xml          | 27 ------------------
 app/src/main/res/values/strings.xml           |  7 +++++
 6 files changed, 57 insertions(+), 33 deletions(-)
 delete mode 100644 app/src/main/res/menu/manga_info.xml

diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt
index f8f39d5cc0..ed0671216d 100644
--- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt
+++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsController.kt
@@ -690,6 +690,9 @@ class MangaDetailsController : BaseController,
             presenter.getNextUnreadChapter() != null && !presenter.isLockedFromSearch
         menu.findItem(R.id.action_mark_all_as_unread).isVisible =
             !presenter.allUnread() && !presenter.isLockedFromSearch
+        menu.findItem(R.id.action_remove_downloads).isVisible = !presenter.isLockedFromSearch
+        menu.findItem(R.id.remove_non_bookmarked).isVisible =
+            presenter.hasBookmark() && !presenter.isLockedFromSearch
         menu.findItem(R.id.action_mark_all_as_unread).isVisible = presenter.isTracked()
         val iconPrimary = view?.context?.getResourceColor(android.R.attr.textColorPrimary)
             ?: Color.BLACK
@@ -748,7 +751,6 @@ class MangaDetailsController : BaseController,
                 }
             }
             R.id.action_open_in_web_view -> openInWebView()
-            R.id.action_share -> prepareToShareManga()
             R.id.action_add_to_home_screen -> addToHomeScreen()
             R.id.action_refresh_tracking -> presenter.refreshTrackers()
             R.id.action_mark_all_as_read -> {
@@ -757,6 +759,7 @@ class MangaDetailsController : BaseController,
                         markAsRead(presenter.chapters)
                     }.negativeButton(android.R.string.cancel).show()
             }
+            R.id.remove_all, R.id.remove_read, R.id.remove_non_bookmarked -> massDeleteChapters(item.itemId)
             R.id.action_mark_all_as_unread -> markAsUnread(presenter.chapters)
             R.id.download_next, R.id.download_next_5, R.id.download_custom, R.id.download_unread, R.id.download_all -> downloadChapters(
                 item.itemId
@@ -825,6 +828,29 @@ class MangaDetailsController : BaseController,
         startActivity(intent)
     }
 
+    private fun massDeleteChapters(choice: Int) {
+        val chaptersToDelete = when (choice) {
+            R.id.remove_all -> presenter.chapters
+            R.id.remove_non_bookmarked -> presenter.chapters.filter { !it.bookmark }
+            R.id.remove_read -> presenter.chapters.filter { it.read }
+            else -> emptyList()
+        }.filter { it.isDownloaded }
+        if (chaptersToDelete.isNotEmpty()) {
+            massDeleteChapters(chaptersToDelete)
+        }
+    }
+
+    private fun massDeleteChapters(chapters: List<ChapterItem>) {
+        val context = view?.context ?: return
+        MaterialDialog(context).message(
+            text = context.resources.getQuantityString(
+                R.plurals.remove_n_chapters, chapters.size, chapters.size
+            )
+        ).positiveButton(R.string.remove) {
+                presenter.deleteChapters(chapters)
+            }.negativeButton(android.R.string.cancel).show()
+    }
+
     private fun downloadChapters(choice: Int) {
         val chaptersToDownload = when (choice) {
             R.id.download_next -> presenter.getUnreadChaptersSorted().take(1)
diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt
index a9cdcbbdce..a2e43149fd 100644
--- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt
+++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaDetailsPresenter.kt
@@ -320,6 +320,7 @@ class MangaDetailsPresenter(
     }
 
     fun allUnread(): Boolean = chapters.none { it.read }
+    fun hasBookmark(): Boolean = chapters.any { it.bookmark }
 
     fun getUnreadChaptersSorted() =
         chapters.filter { !it.read && it.status == Download.NOT_DOWNLOADED }.distinctBy { it.name }
diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt
index 6e18381dce..be55d41004 100644
--- a/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt
+++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recents/RecentsController.kt
@@ -349,6 +349,7 @@ class RecentsController(bundle: Bundle? = null) : BaseController(bundle),
         if (type.isEnter) {
             view?.applyWindowInsetsForRootController(activity!!.bottom_nav)
             if (type == ControllerChangeType.POP_ENTER) presenter.onCreate()
+            dl_bottom_sheet.dismiss()
             setHasOptionsMenu(true)
         } else {
             if (type == ControllerChangeType.POP_EXIT) presenter.onDestroy()
@@ -362,10 +363,9 @@ class RecentsController(bundle: Bundle? = null) : BaseController(bundle),
     }
 
     override fun toggleSheet() {
-        if (dl_bottom_sheet.sheetBehavior?.isHideable == false) {
-            if (showingDownloads) dl_bottom_sheet.dismiss()
-            else dl_bottom_sheet.sheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
-        }
+        if (showingDownloads) dl_bottom_sheet.dismiss()
+        else if (dl_bottom_sheet.sheetBehavior?.isHideable == false) dl_bottom_sheet.sheetBehavior?.state =
+            BottomSheetBehavior.STATE_EXPANDED
     }
 
     override fun expandSearch() {
diff --git a/app/src/main/res/menu/manga_details.xml b/app/src/main/res/menu/manga_details.xml
index 82b90797d7..9c146dcd0e 100644
--- a/app/src/main/res/menu/manga_details.xml
+++ b/app/src/main/res/menu/manga_details.xml
@@ -48,8 +48,25 @@
         android:id="@+id/action_refresh_tracking"
         android:icon="@drawable/ic_refresh_white_24dp"
         android:title="@string/refresh_tracking"
-        app:showAsAction="never" />
 
+        app:showAsAction="never" />
+    <item
+        android:id="@+id/action_remove_downloads"
+        android:icon="@drawable/ic_file_download_white_24dp"
+        android:title="@string/remove_downloads"
+        app:showAsAction="ifRoom">
+        <menu>
+            <item
+                android:id="@+id/remove_all"
+                android:title="@string/all_chapters" />
+            <item
+                android:id="@+id/remove_non_bookmarked"
+                android:title="@string/all_but_bookmarked_chapters" />
+            <item
+                android:id="@+id/remove_read"
+                android:title="@string/all_read_chapters" />
+        </menu>
+    </item>
     <item
         android:id="@+id/action_edit"
         android:icon="@drawable/ic_edit_white_24dp"
diff --git a/app/src/main/res/menu/manga_info.xml b/app/src/main/res/menu/manga_info.xml
deleted file mode 100644
index 93d30b9a3e..0000000000
--- a/app/src/main/res/menu/manga_info.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<menu xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto">
-
-    <item
-        android:id="@+id/action_edit"
-        android:icon="@drawable/ic_edit_white_24dp"
-        android:title="@string/edit"
-        app:showAsAction="ifRoom" />
-
-    <item
-        android:id="@+id/action_share"
-        android:icon="@drawable/ic_share_white_24dp"
-        android:title="@string/share"
-        app:showAsAction="ifRoom" />
-
-    <item
-        android:id="@+id/action_open_in_web_view"
-        android:title="@string/open_in_web_view"
-        app:showAsAction="never" />
-
-    <item
-        android:id="@+id/action_add_to_home_screen"
-        android:title="@string/add_to_home_screen"
-        app:showAsAction="never" />
-
-</menu>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 357661ddb0..7b36ba9422 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -48,6 +48,10 @@
     <string name="marked_as_read">Marked as read</string>
     <string name="marked_as_unread">Marked as unread</string>
     <string name="removed_bookmark">Removed bookmark</string>
+    <plurals name="remove_n_chapters">
+        <item quantity="one">Remove %1$d downloaded chapter?</item>
+        <item quantity="other">Remove %1$d downloaded chapters?</item>
+    </plurals>
     <plurals name="chapters">
         <item quantity="one">%1$d chapter</item>
         <item quantity="other">%1$d chapters</item>
@@ -335,6 +339,8 @@
     <string name="custom_range">Custom range</string>
     <string name="all_chapters">All chapters</string>
     <string name="all_unread_chapters">All unread chapters</string>
+    <string name="all_read_chapters">All read chapters</string>
+    <string name="all_but_bookmarked_chapters">All but bookmarked chapters</string>
     <string name="add_x_to_library">Add %1$s to library?</string>
     <string name="show_all">Show All</string>
     <string name="show_bookmarked_chapters">Show bookmarked chapters</string>
@@ -527,6 +533,7 @@
     <string name="download">Download</string>
     <string name="start_downloading_now">Start downloading now</string>
     <string name="remove_download">Remove download</string>
+    <string name="remove_downloads">Remove downloads</string>
     <string name="downloading_progress">Downloading (%1$d/%2$d)</string>
     <string name="invalid_download_location">Invalid download location</string>
     <string name="download_paused">Download paused</string>