diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt index 5320e5be98..d814bbfe1b 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryAdapter.kt @@ -85,10 +85,9 @@ class LibraryCategoryAdapter(val fragment: LibraryCategoryFragment) : */ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LibraryHolder { //depending on preferences, display a list or display a grid - if(parent.id == R.id.library_list) { + if (parent.id == R.id.library_list) { val view = parent.inflate(R.layout.item_library_list) return LibraryListHolder(view, this, fragment) - } else { val view = parent.inflate(R.layout.item_catalogue_grid).apply { card.layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, coverHeight) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt index 814dec25a3..826059805d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryCategoryFragment.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.ui.library import android.content.res.Configuration import android.os.Bundle +import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.view.LayoutInflater @@ -91,8 +92,6 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View? { return inflater.inflate(R.layout.fragment_library_category, container, false) - - } override fun onViewCreated(view: View, savedState: Bundle?) { @@ -100,14 +99,16 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli //set up grid recycler.setHasFixedSize(true) + (recycler.layoutManager as GridLayoutManager).recycleChildrenOnDetach = true + recycler.recycledViewPool = libraryFragment.pool recycler.adapter = adapter //set up list library_list.setHasFixedSize(true) - library_list.adapter = adapter library_list.layoutManager = LinearLayoutManager(activity) - - + library_list.recycledViewPool = libraryFragment.pool + (library_list.layoutManager as LinearLayoutManager).recycleChildrenOnDetach = true + library_list.adapter = adapter if (libraryFragment.actionMode != null) { setSelectionMode(FlexibleAdapter.MODE_MULTI) @@ -119,19 +120,19 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli // Set again the adapter to recalculate the covers height .subscribe { recycler.adapter = adapter } - searchSubscription = libraryPresenter.searchSubject?.subscribe { text -> + searchSubscription = libraryPresenter.searchSubject.subscribe { text -> adapter.searchText = text adapter.updateDataSet() } - toggleViewSubscription = preferences.libraryAsList().asObservable().subscribe {onViewModeChange(it)} + toggleViewSubscription = preferences.libraryAsList().asObservable() + .subscribe { onViewModeChange(it) } - if(libraryPresenter.displayAsList != displayAsList) { + if (libraryPresenter.displayAsList != displayAsList) { library_switcher.showNext() displayAsList = libraryPresenter.displayAsList } - library_switcher.inAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_in) library_switcher.outAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_out) @@ -176,11 +177,8 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli override fun onResume() { super.onResume() - - libraryMangaSubscription = libraryPresenter.libraryMangaSubject - ?.subscribe { onNextLibraryManga(it) } - + .subscribe { onNextLibraryManga(it) } } override fun onPause() { @@ -310,7 +308,7 @@ class LibraryCategoryFragment : BaseFragment(), FlexibleViewHolder.OnListItemCli fun onViewModeChange(isList: Boolean) { //do nothing if the display does not need to change - if(isList == displayAsList) return + if (isList == displayAsList) return //else change view and display mode library_switcher.showNext() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt index 0916dfd2e1..bd5784c414 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryFragment.kt @@ -6,6 +6,7 @@ import android.os.Bundle import android.support.design.widget.TabLayout import android.support.v4.view.ViewPager import android.support.v7.view.ActionMode +import android.support.v7.widget.RecyclerView import android.support.v7.widget.SearchView import android.view.* import com.afollestad.materialdialogs.MaterialDialog @@ -86,6 +87,11 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback */ var isFilterUnread = false + /** + * A pool to share view holders between all the registered categories (fragments). + */ + val pool = RecyclerView.RecycledViewPool() + companion object { /** * Key to change the cover of a manga in [onActivityResult]. @@ -138,7 +144,7 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback if (savedState != null) { activeCategory = savedState.getInt(CATEGORY_KEY) query = savedState.getString(QUERY_KEY) - presenter.searchSubject?.onNext(query) + presenter.searchSubject.onNext(query) } else { activeCategory = presenter.preferences.lastUsedCategory().getOrDefault() } @@ -311,7 +317,7 @@ class LibraryFragment : BaseRxFragment(), ActionMode.Callback view_pager.post { if (isAdded) tabs.setScrollPosition(view_pager.currentItem, 0f, true) } // Send the manga map to child fragments after the adapter is updated. - presenter.libraryMangaSubject?.onNext(LibraryMangaEvent(mangaMap)) + presenter.libraryMangaSubject.onNext(LibraryMangaEvent(mangaMap)) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt index 2e561d38cf..d0928262b7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryListHolder.kt @@ -18,8 +18,8 @@ import kotlinx.android.synthetic.main.item_library_list.view.* */ class LibraryListHolder(private val view: View, - private val adapter: LibraryCategoryAdapter, - listener: FlexibleViewHolder.OnListItemClickListener) + private val adapter: LibraryCategoryAdapter, + listener: FlexibleViewHolder.OnListItemClickListener) : LibraryHolder(view, adapter, listener) { /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt index 39efd36d64..5f44089f88 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryPresenter.kt @@ -39,12 +39,12 @@ class LibraryPresenter : BasePresenter() { /** * Search query of the library. */ - val searchSubject: BehaviorSubject? = BehaviorSubject.create() + val searchSubject: BehaviorSubject = BehaviorSubject.create() /** * Subject to notify the library's viewpager for updates. */ - val libraryMangaSubject: BehaviorSubject? = BehaviorSubject.create() + val libraryMangaSubject: BehaviorSubject = BehaviorSubject.create() /** * Database. @@ -95,9 +95,7 @@ class LibraryPresenter : BasePresenter() { start(GET_LIBRARY) } - - add(preferences.libraryAsList().asObservable().subscribe{setDisplayMode(it)}) - + add(preferences.libraryAsList().asObservable().subscribe { setDisplayMode(it) }) } /**