Fix back button in library search
This commit is contained in:
NerdNumber9
2017-11-29 21:22:48 -05:00
committed by NerdNumber9
32 changed files with 395 additions and 211 deletions

View File

@@ -79,7 +79,7 @@ class LibraryAdapter(private val controller: LibraryController) : RecyclerViewPa
/**
* Returns the position of the view.
*/
override fun getItemPosition(obj: Any?): Int {
override fun getItemPosition(obj: Any): Int {
val view = obj as? LibraryCategoryView ?: return POSITION_NONE
val index = categories.indexOfFirst { it.id == view.category.id }
return if (index == -1) POSITION_NONE else index

View File

@@ -192,14 +192,10 @@ class LibraryController(
super.onChangeStarted(handler, type)
if (type.isEnter) {
activity?.tabs?.setupWithViewPager(view?.view_pager)
presenter.subscribeLibrary()
}
}
override fun onAttach(view: View) {
super.onAttach(view)
presenter.subscribeLibrary()
}
override fun onDestroyView(view: View) {
super.onDestroyView(view)
adapter = null
@@ -464,7 +460,7 @@ class LibraryController(
presenter.onOpenManga()
router.pushController(RouterTransaction.with(MangaController(manga))
.pushChangeHandler(FadeChangeHandler())
.pushChangeHandler(FadeChangeHandler(false))
.popChangeHandler(FadeChangeHandler()))
}

View File

@@ -1,7 +1,6 @@
package eu.kanade.tachiyomi.ui.library
import android.os.Bundle
import com.hippo.unifile.UniFile
import com.jakewharton.rxrelay.BehaviorRelay
import eu.kanade.tachiyomi.data.cache.CoverCache
import eu.kanade.tachiyomi.data.database.DatabaseHelper
@@ -107,12 +106,6 @@ class LibraryPresenter(
* @param map the map to filter.
*/
private fun applyFilters(map: LibraryMap): LibraryMap {
// Cached list of downloaded manga directories given a source id.
val mangaDirsForSource = mutableMapOf<Long, Map<String?, UniFile>>()
// Cached list of downloaded chapter directories for a manga.
val chapterDirectories = mutableMapOf<Long, Boolean>()
val filterDownloaded = preferences.filterDownloaded().getOrDefault()
val filterUnread = preferences.filterUnread().getOrDefault()
@@ -121,7 +114,7 @@ class LibraryPresenter(
val filterFn: (LibraryItem) -> Boolean = f@ { item ->
// Filter out manga without source.
val source = sourceManager.get(item.manga.source) ?: return@f false
sourceManager.get(item.manga.source) ?: return@f false
// Filter when there isn't unread chapters.
if (filterUnread && item.manga.unread == 0) {
@@ -132,28 +125,14 @@ class LibraryPresenter(
return@f false
}
// Filter when the download directory doesn't exist or is null.
// Filter when there are no downloads.
if (filterDownloaded) {
// Don't bother with directory checking if download count has been set.
if (item.downloadCount != -1) {
return@f item.downloadCount > 0
}
// Get the directories for the source of the manga.
val dirsForSource = mangaDirsForSource.getOrPut(source.id) {
val sourceDir = downloadManager.findSourceDir(source)
sourceDir?.listFiles()?.associateBy { it.name }.orEmpty()
}
val mangaDirName = downloadManager.getMangaDirName(item.manga)
val mangaDir = dirsForSource[mangaDirName] ?: return@f false
val hasDirs = chapterDirectories.getOrPut(item.manga.id!!) {
mangaDir.listFiles()?.isNotEmpty() ?: false
}
if (!hasDirs) {
return@f false
}
return@f downloadManager.getDownloadCount(item.manga) > 0
}
true
}
@@ -177,31 +156,9 @@ class LibraryPresenter(
return
}
// Cached list of downloaded manga directories given a source id.
val mangaDirsForSource = mutableMapOf<Long, Map<String?, UniFile>>()
// Cached list of downloaded chapter directories for a manga.
val chapterDirectories = mutableMapOf<Long, Int>()
val downloadCountFn: (LibraryItem) -> Int = f@ { item ->
val source = sourceManager.get(item.manga.source) ?: return@f 0
// Get the directories for the source of the manga.
val dirsForSource = mangaDirsForSource.getOrPut(source.id) {
val sourceDir = downloadManager.findSourceDir(source)
sourceDir?.listFiles()?.associateBy { it.name }.orEmpty()
}
val mangaDirName = downloadManager.getMangaDirName(item.manga)
val mangaDir = dirsForSource[mangaDirName] ?: return@f 0
chapterDirectories.getOrPut(item.manga.id!!) {
mangaDir.listFiles()?.size ?: 0
}
}
for ((_, itemList) in map) {
for (item in itemList) {
item.downloadCount = downloadCountFn(item)
item.downloadCount = downloadManager.getDownloadCount(item.manga)
}
}
}
@@ -360,7 +317,7 @@ class LibraryPresenter(
if (deleteChapters) {
val source = sourceManager.get(manga.source) as? HttpSource
if (source != null) {
downloadManager.findMangaDir(source, manga)?.delete()
downloadManager.deleteManga(manga, source)
}
}
}

View File

@@ -128,13 +128,11 @@ class ChaptersPresenter(
* @param chapters the list of chapter from the database.
*/
private fun setDownloadedChapters(chapters: List<ChapterItem>) {
val files = downloadManager.findMangaDir(source, manga)?.listFiles() ?: return
val cached = mutableMapOf<Chapter, String>()
files.mapNotNull { it.name }
.mapNotNull { name -> chapters.find {
name == cached.getOrPut(it) { downloadManager.getChapterDirName(it) }
} }
.forEach { it.status = Download.DOWNLOADED }
for (chapter in chapters) {
if (downloadManager.isChapterDownloaded(chapter, manga)) {
chapter.status = Download.DOWNLOADED
}
}
}
/**
@@ -283,7 +281,7 @@ class ChaptersPresenter(
*/
private fun deleteChapter(chapter: ChapterItem) {
downloadManager.queue.remove(chapter)
downloadManager.deleteChapter(source, manga, chapter)
downloadManager.deleteChapter(chapter, manga, source)
chapter.status = Download.NOT_DOWNLOADED
chapter.download = null
}

View File

@@ -115,14 +115,14 @@ class MangaInfoPresenter(
* Returns true if the manga has any downloads.
*/
fun hasDownloads(): Boolean {
return downloadManager.findMangaDir(source, manga) != null
return downloadManager.getDownloadCount(manga) > 0
}
/**
* Deletes all the downloads for the manga.
*/
fun deleteDownloads() {
downloadManager.findMangaDir(source, manga)?.delete()
downloadManager.deleteManga(manga, source)
}
/**

View File

@@ -79,7 +79,7 @@ class ChapterLoader(
private fun retrievePageList(chapter: ReaderChapter) = Observable.just(chapter)
.flatMap {
// Check if the chapter is downloaded.
chapter.isDownloaded = downloadManager.findChapterDir(source, manga, chapter) != null
chapter.isDownloaded = downloadManager.isChapterDownloaded(chapter, manga, true)
if (chapter.isDownloaded) {
// Fetch the page list from disk.

View File

@@ -64,7 +64,7 @@ class ReaderCustomFilterDialog : DialogFragment() {
* @param savedState The last saved instance state of the Fragment.
*/
override fun onCreateDialog(savedState: Bundle?): Dialog {
val dialog = MaterialDialog.Builder(activity)
val dialog = MaterialDialog.Builder(activity!!)
.customView(R.layout.reader_custom_filter_dialog, false)
.positiveText(android.R.string.ok)
.build()

View File

@@ -417,7 +417,7 @@ class ReaderPresenter(
fun deleteChapter(chapter: ReaderChapter, manga: Manga) {
chapter.isDownloaded = false
chapter.pages?.forEach { it.status == Page.QUEUE }
downloadManager.deleteChapter(source, manga, chapter)
downloadManager.deleteChapter(chapter, manga, source)
}
/**

View File

@@ -24,7 +24,7 @@ class ReaderSettingsDialog : DialogFragment() {
private lateinit var subscriptions: CompositeSubscription
override fun onCreateDialog(savedState: Bundle?): Dialog {
val dialog = MaterialDialog.Builder(activity)
val dialog = MaterialDialog.Builder(activity!!)
.title(R.string.label_settings)
.customView(R.layout.reader_settings_dialog, true)
.positiveText(android.R.string.ok)
@@ -40,8 +40,11 @@ class ReaderSettingsDialog : DialogFragment() {
viewer.onItemSelectedListener = IgnoreFirstSpinnerListener { position ->
subscriptions += Observable.timer(250, MILLISECONDS, AndroidSchedulers.mainThread())
.subscribe {
(activity as ReaderActivity).presenter.updateMangaViewer(position)
activity.recreate()
val readerActivity = activity as? ReaderActivity
if (readerActivity != null) {
readerActivity.presenter.updateMangaViewer(position)
readerActivity.recreate()
}
}
}
viewer.setSelection((activity as ReaderActivity).presenter.manga.viewer, false)

View File

@@ -100,12 +100,12 @@ abstract class PagerReader : BaseReader() {
/**
* Text color for black theme.
*/
val whiteColor by lazy { ContextCompat.getColor(context, R.color.textColorSecondaryDark) }
val whiteColor by lazy { ContextCompat.getColor(context!!, R.color.textColorSecondaryDark) }
/**
* Text color for white theme.
*/
val blackColor by lazy { ContextCompat.getColor(context, R.color.textColorSecondaryLight) }
val blackColor by lazy { ContextCompat.getColor(context!!, R.color.textColorSecondaryLight) }
/**
* Initializes the pager.

View File

@@ -24,7 +24,7 @@ class HorizontalPager(context: Context) : ViewPager(context), Pager {
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
try {
if (ev.action and MotionEvent.ACTION_MASK == MotionEvent.ACTION_DOWN) {
if (currentItem == 0 || currentItem == adapter.count - 1) {
if (currentItem == 0 || currentItem == adapter!!.count - 1) {
startDragX = ev.x
}
}
@@ -50,7 +50,7 @@ class HorizontalPager(context: Context) : ViewPager(context), Pager {
startDragX = 0f
}
} else if (currentItem == adapter.count - 1) {
} else if (currentItem == adapter!!.count - 1) {
if (ev.action and MotionEvent.ACTION_MASK == MotionEvent.ACTION_UP) {
val displacement = startDragX - ev.x

View File

@@ -13,7 +13,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerReader
class LeftToRightReader : PagerReader() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? {
return HorizontalPager(activity).apply { initializePager(this) }
return HorizontalPager(activity!!).apply { initializePager(this) }
}
}

View File

@@ -13,7 +13,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerReader
class RightToLeftReader : PagerReader() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? {
return HorizontalPager(activity).apply {
return HorizontalPager(activity!!).apply {
rotation = 180f
initializePager(this)
}

View File

@@ -13,7 +13,7 @@ import eu.kanade.tachiyomi.ui.reader.viewer.pager.PagerReader
class VerticalReader : PagerReader() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? {
return VerticalPager(activity).apply { initializePager(this) }
return VerticalPager(activity!!).apply { initializePager(this) }
}
}

View File

@@ -33,7 +33,6 @@ import android.support.annotation.DrawableRes;
import android.support.v4.os.ParcelableCompat;
import android.support.v4.os.ParcelableCompatCreatorCallbacks;
import android.support.v4.view.AccessibilityDelegateCompat;
import android.support.v4.view.KeyEventCompat;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.VelocityTrackerCompat;
@@ -2598,14 +2597,10 @@ public class VerticalViewPagerImpl extends ViewGroup {
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
if (event.hasNoModifiers()) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
break;
default:

View File

@@ -73,7 +73,7 @@ class WebtoonReader : BaseReader() {
private var scrollDistance: Int = 0
val screenHeight by lazy {
val display = activity.windowManager.defaultDisplay
val display = activity!!.windowManager.defaultDisplay
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
val metrics = DisplayMetrics()
@@ -91,7 +91,7 @@ class WebtoonReader : BaseReader() {
val screenHeight = resources.displayMetrics.heightPixels
scrollDistance = screenHeight * 3 / 4
layoutManager = PreCachingLayoutManager(activity)
layoutManager = PreCachingLayoutManager(activity!!)
layoutManager.extraLayoutSpace = screenHeight / 2
recycler = RecyclerView(activity).apply {

View File

@@ -1,7 +1,6 @@
package eu.kanade.tachiyomi.ui.recent_updates
import android.os.Bundle
import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.MangaChapter
import eu.kanade.tachiyomi.data.download.DownloadManager
@@ -114,36 +113,11 @@ class RecentChaptersPresenter(
* @param items the list of chapter from the database.
*/
private fun setDownloadedChapters(items: List<RecentChapterItem>) {
// Cached list of downloaded manga directories. Directory name is also cached because
// it's slow when using SAF.
val mangaDirsForSource = mutableMapOf<Long, Map<String?, UniFile>>()
// Cached list of downloaded chapter directories for a manga.
val chapterDirsForManga = mutableMapOf<Long, Map<String?, UniFile>>()
for (item in items) {
val manga = item.manga
val chapter = item.chapter
val source = sourceManager.get(manga.source) ?: continue
// Get the directories for the source of the manga.
val dirsForSource = mangaDirsForSource.getOrPut(source.id) {
val sourceDir = downloadManager.findSourceDir(source)
sourceDir?.listFiles()?.associateBy { it.name }.orEmpty()
}
// Get the manga directory in the source or continue.
val mangaDirName = downloadManager.getMangaDirName(manga)
val mangaDir = dirsForSource[mangaDirName] ?: continue
// Get the directories for the manga.
val chapterDirs = chapterDirsForManga.getOrPut(manga.id!!) {
mangaDir.listFiles()?.associateBy { it.name }.orEmpty()
}
// Assign the download if the directory exists.
val chapterDirName = downloadManager.getChapterDirName(chapter)
if (chapterDirName in chapterDirs) {
if (downloadManager.isChapterDownloaded(chapter, manga)) {
item.status = Download.DOWNLOADED
}
}
@@ -216,7 +190,7 @@ class RecentChaptersPresenter(
private fun deleteChapter(item: RecentChapterItem) {
val source = sourceManager.get(item.manga.source) ?: return
downloadManager.queue.remove(item.chapter)
downloadManager.deleteChapter(source, item.manga, item.chapter)
downloadManager.deleteChapter(item.chapter, item.manga, source)
item.status = Download.NOT_DOWNLOADED
item.download = null
}

View File

@@ -1,6 +1,5 @@
package eu.kanade.tachiyomi.ui.setting
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.app.Activity
import android.app.Dialog
@@ -135,7 +134,7 @@ class SettingsBackupController : SettingsController() {
preferences.backupsDirectory().asObservable()
.subscribeUntilDestroy { path ->
val dir = UniFile.fromUri(context, Uri.parse(path))
summary = dir.filePath ?: path
summary = dir.filePath + "/automatic"
}
}
val backupNumber = intListPreference {
@@ -160,19 +159,19 @@ class SettingsBackupController : SettingsController() {
when (requestCode) {
CODE_BACKUP_DIR -> if (data != null && resultCode == Activity.RESULT_OK) {
val activity = activity ?: return
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
val uri = Uri.fromFile(File(data.data.path))
preferences.backupsDirectory().set(uri.toString())
} else {
val uri = data.data
// Get uri of backup folder.
val uri = data.data
// Get UriPermission so it's possible to write files post kitkat.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
activity.contentResolver.takePersistableUriPermission(uri, flags)
val file = UniFile.fromUri(activity, uri)
preferences.backupsDirectory().set(file.uri.toString())
}
// Set backup Uri.
preferences.backupsDirectory().set(uri.toString())
}
CODE_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) {
val activity = activity ?: return
@@ -240,7 +239,7 @@ class SettingsBackupController : SettingsController() {
.itemsDisabledIndices(0)
.itemsCallbackMultiChoice(arrayOf(0, 1, 2, 3, 4), { _, positions, _ ->
var flags = 0
for (i in 1..positions.size - 1) {
for (i in 1 until positions.size) {
when (positions[i]) {
1 -> flags = flags or BackupCreateService.BACKUP_CATEGORY
2 -> flags = flags or BackupCreateService.BACKUP_CHAPTER
@@ -281,7 +280,7 @@ class SettingsBackupController : SettingsController() {
override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val activity = activity!!
val unifile = UniFile.fromUri(activity, args.getParcelable<Uri>(KEY_URI))
val unifile = UniFile.fromUri(activity, args.getParcelable(KEY_URI))
return MaterialDialog.Builder(activity)
.title(R.string.backup_created)
.content(activity.getString(R.string.file_saved, unifile.filePath))
@@ -315,7 +314,7 @@ class SettingsBackupController : SettingsController() {
val context = applicationContext
if (context != null) {
RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG)
BackupRestoreService.start(context, args.getParcelable<Uri>(KEY_URI))
BackupRestoreService.start(context, args.getParcelable(KEY_URI))
}
}
.build()

View File

@@ -194,7 +194,7 @@ class SettingsDownloadController : SettingsController() {
File.separator + "downloads"
return mutableListOf(File(defaultDir)) +
ContextCompat.getExternalFilesDirs(activity, "").filterNotNull()
ContextCompat.getExternalFilesDirs(activity!!, "").filterNotNull()
}
}