diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt index 899987333..c5b3a1c26 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/CatalogueAdapter.kt @@ -1,10 +1,14 @@ package eu.kanade.tachiyomi.ui.catalogue import android.view.ViewGroup +import android.view.ViewGroup.LayoutParams.MATCH_PARENT +import android.widget.RelativeLayout import eu.davidea.flexibleadapter.FlexibleAdapter import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.util.inflate +import kotlinx.android.synthetic.main.fragment_catalogue.* +import kotlinx.android.synthetic.main.item_catalogue_grid.view.* import java.util.* /** @@ -68,6 +72,7 @@ class CatalogueAdapter(val fragment: CatalogueFragment) : FlexibleAdapter(), FlexibleViewHold */ private var queryDebouncerSubscription: Subscription? = null + /** + * Subscription of the number of manga per row. + */ + private var numColumnsSubscription: Subscription? = null + /** * Display mode of the catalogue (list or grid mode). */ @@ -162,6 +169,12 @@ class CatalogueFragment : BaseRxFragment(), FlexibleViewHold switcher.showNext() } + numColumnsSubscription = getColumnsPreferenceForCurrentOrientation().asObservable() + .doOnNext { catalogue_grid.spanCount = it } + .skip(1) + // Set again the adapter to recalculate the covers height + .subscribe { catalogue_grid.adapter = adapter } + switcher.inAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_in) switcher.outAnimation = AnimationUtils.loadAnimation(activity, android.R.anim.fade_out) @@ -266,6 +279,7 @@ class CatalogueFragment : BaseRxFragment(), FlexibleViewHold } override fun onDestroyView() { + numColumnsSubscription?.unsubscribe() searchItem?.let { if (it.isActionViewExpanded) it.collapseActionView() } @@ -391,6 +405,18 @@ class CatalogueFragment : BaseRxFragment(), FlexibleViewHold } } + /** + * Returns a preference for the number of manga per row based on the current orientation. + * + * @return the preference. + */ + fun getColumnsPreferenceForCurrentOrientation(): Preference { + return if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) + presenter.prefs.portraitColumns() + else + presenter.prefs.landscapeColumns() + } + /** * Returns the view holder for the given manga. *