From 9aa834bf3998cae72801ee1c700ed07e5d93eea0 Mon Sep 17 00:00:00 2001 From: Paul Woitaschek Date: Thu, 17 Jan 2019 23:16:02 +0100 Subject: [PATCH] Implemented the actual sorting. --- .../bookCategory/BookCategoryController.kt | 11 +++-- .../bookCategory/BookCategoryViewModel.kt | 22 ++++++--- .../ph1b/audiobook/injection/AppComponent.kt | 3 +- .../ph1b/audiobook/injection/SortingModule.kt | 49 +++++++++++++++++++ .../de/ph1b/audiobook/misc/Observables.kt | 10 ++++ 5 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/de/ph1b/audiobook/injection/SortingModule.kt diff --git a/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryController.kt b/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryController.kt index 3c3d8ba6be..ce917d08e9 100644 --- a/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryController.kt +++ b/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryController.kt @@ -37,10 +37,6 @@ class BookCategoryController(bundle: Bundle) : BaseController(bundle), EditBookB @Inject lateinit var galleryPicker: GalleryPicker - init { - appComponent.inject(this) - } - constructor(category: BookOverviewCategory) : this(Bundle().apply { putSerializable(NI_CATEGORY, category) }) @@ -48,6 +44,11 @@ class BookCategoryController(bundle: Bundle) : BaseController(bundle), EditBookB private val category = bundle.getSerializable(NI_CATEGORY) as BookOverviewCategory private var adapter by clearAfterDestroyView() + init { + appComponent.inject(this) + viewModel.category = category + } + override val layoutRes = R.layout.book_category override fun onViewCreated() { @@ -84,7 +85,7 @@ class BookCategoryController(bundle: Bundle) : BaseController(bundle), EditBookB recyclerView.addItemDecoration(BookCategoryItemDecoration(activity, layoutManager)) (recyclerView.itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false - viewModel.get(category) + viewModel.get() .subscribe { layoutManager.spanCount = it.gridColumnCount adapter.submitList(it.models) diff --git a/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryViewModel.kt b/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryViewModel.kt index 9748fcc2a0..695b953cf6 100644 --- a/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryViewModel.kt +++ b/app/src/main/java/de/ph1b/audiobook/features/bookCategory/BookCategoryViewModel.kt @@ -21,16 +21,26 @@ class BookCategoryViewModel private val currentBookIdPref: Pref, @Named(PrefKeys.GRID_MODE) private val gridModePref: Pref, - private val gridCount: GridCount + private val gridCount: GridCount, + private val comparatorPrefForCategory: @JvmSuppressWildcards Map> ) { - fun get(category: BookOverviewCategory): Observable { - return Observables.combineLatest(gridModePref.stream, repo.booksStream()) { gridMode, books -> + lateinit var category: BookOverviewCategory + + private fun comparatorPref(): Pref = comparatorPrefForCategory[category]!! + + fun get(): Observable { + val comparatorStream = comparatorPref().stream + return Observables.combineLatest( + gridModePref.stream, + repo.booksStream(), + comparatorStream + ) { gridMode, books, comparator -> val gridColumnCount = gridCount.gridColumnCount(gridMode) val currentBookId = currentBookIdPref.value val models = books.asSequence() .filter(category.filter) - .sortedWith(BookComparator.BY_NAME) + .sortedWith(comparator) .map { book -> BookOverviewModel( book = book, @@ -44,12 +54,12 @@ class BookCategoryViewModel } fun sort(comparator: BookComparator) { + comparatorPref().value = comparator } fun bookSorting(): BookComparator { - return BookComparator.BY_NAME + return comparatorPref().value } - } data class BookCategoryState( diff --git a/app/src/main/java/de/ph1b/audiobook/injection/AppComponent.kt b/app/src/main/java/de/ph1b/audiobook/injection/AppComponent.kt index 03271de84c..e99871c35b 100644 --- a/app/src/main/java/de/ph1b/audiobook/injection/AppComponent.kt +++ b/app/src/main/java/de/ph1b/audiobook/injection/AppComponent.kt @@ -41,7 +41,8 @@ import javax.inject.Singleton AndroidModule::class, PrefsModule::class, PersistenceModule::class, - PlaybackModule::class + PlaybackModule::class, + SortingModule::class ] ) interface AppComponent : DataComponent { diff --git a/app/src/main/java/de/ph1b/audiobook/injection/SortingModule.kt b/app/src/main/java/de/ph1b/audiobook/injection/SortingModule.kt new file mode 100644 index 0000000000..9a2a221011 --- /dev/null +++ b/app/src/main/java/de/ph1b/audiobook/injection/SortingModule.kt @@ -0,0 +1,49 @@ +package de.ph1b.audiobook.injection + +import com.f2prateek.rx.preferences2.RxSharedPreferences +import dagger.MapKey +import dagger.Module +import dagger.Provides +import dagger.multibindings.IntoMap +import de.ph1b.audiobook.features.bookOverview.list.BookComparator +import de.ph1b.audiobook.features.bookOverview.list.header.BookOverviewCategory +import de.ph1b.audiobook.persistence.pref.PersistentPref +import de.ph1b.audiobook.persistence.pref.Pref +import javax.inject.Singleton + +@MapKey +annotation class BookOverviewCategoryKey(val value: BookOverviewCategory) + +@Module +object SortingModule { + + @JvmStatic + @Provides + @Singleton + @IntoMap + @BookOverviewCategoryKey(BookOverviewCategory.CURRENT) + fun currentComparatorPref(prefs: RxSharedPreferences): Pref { + val pref = prefs.getEnum(BookOverviewCategory.CURRENT.name, BookComparator.BY_NAME, BookComparator::class.java) + return PersistentPref(pref) + } + + @JvmStatic + @Provides + @Singleton + @IntoMap + @BookOverviewCategoryKey(BookOverviewCategory.NOT_STARTED) + fun notStartedComparatorPref(prefs: RxSharedPreferences): Pref { + val pref = prefs.getEnum(BookOverviewCategory.NOT_STARTED.name, BookComparator.BY_NAME, BookComparator::class.java) + return PersistentPref(pref) + } + + @JvmStatic + @Provides + @Singleton + @IntoMap + @BookOverviewCategoryKey(BookOverviewCategory.FINISHED) + fun finishedComparatorPref(prefs: RxSharedPreferences): Pref { + val pref = prefs.getEnum(BookOverviewCategory.FINISHED.name, BookComparator.BY_NAME, BookComparator::class.java) + return PersistentPref(pref) + } +} diff --git a/app/src/main/java/de/ph1b/audiobook/misc/Observables.kt b/app/src/main/java/de/ph1b/audiobook/misc/Observables.kt index 1300665cf1..ed27f1335f 100644 --- a/app/src/main/java/de/ph1b/audiobook/misc/Observables.kt +++ b/app/src/main/java/de/ph1b/audiobook/misc/Observables.kt @@ -3,6 +3,7 @@ package de.ph1b.audiobook.misc import io.reactivex.Observable import io.reactivex.ObservableSource import io.reactivex.functions.BiFunction +import io.reactivex.functions.Function3 import io.reactivex.functions.Function5 object Observables { @@ -15,6 +16,15 @@ object Observables { return Observable.combineLatest(source1, source2, BiFunction { t1, t2 -> combiner(t1, t2) }) } + inline fun combineLatest( + source1: ObservableSource, + source2: ObservableSource, + source3: ObservableSource, + crossinline combiner: (T1, T2, T3) -> R + ): Observable { + return Observable.combineLatest(source1, source2, source3, Function3 { t1, t2, t3 -> combiner(t1, t2, t3) }) + } + inline fun combineLatest( source1: ObservableSource, source2: ObservableSource,