Skip to content

Commit

Permalink
Implemented the actual sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulWoitaschek committed Jan 17, 2019
1 parent 8a9a385 commit 9aa834b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@ 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)
})

private val category = bundle.getSerializable(NI_CATEGORY) as BookOverviewCategory
private var adapter by clearAfterDestroyView<BookCategoryAdapter>()

init {
appComponent.inject(this)
viewModel.category = category
}

override val layoutRes = R.layout.book_category

override fun onViewCreated() {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ class BookCategoryViewModel
private val currentBookIdPref: Pref<UUID>,
@Named(PrefKeys.GRID_MODE)
private val gridModePref: Pref<GridMode>,
private val gridCount: GridCount
private val gridCount: GridCount,
private val comparatorPrefForCategory: @JvmSuppressWildcards Map<BookOverviewCategory, Pref<BookComparator>>
) {

fun get(category: BookOverviewCategory): Observable<BookCategoryState> {
return Observables.combineLatest(gridModePref.stream, repo.booksStream()) { gridMode, books ->
lateinit var category: BookOverviewCategory

private fun comparatorPref(): Pref<BookComparator> = comparatorPrefForCategory[category]!!

fun get(): Observable<BookCategoryState> {
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,
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ import javax.inject.Singleton
AndroidModule::class,
PrefsModule::class,
PersistenceModule::class,
PlaybackModule::class
PlaybackModule::class,
SortingModule::class
]
)
interface AppComponent : DataComponent {
Expand Down
49 changes: 49 additions & 0 deletions app/src/main/java/de/ph1b/audiobook/injection/SortingModule.kt
Original file line number Diff line number Diff line change
@@ -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<BookComparator> {
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<BookComparator> {
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<BookComparator> {
val pref = prefs.getEnum(BookOverviewCategory.FINISHED.name, BookComparator.BY_NAME, BookComparator::class.java)
return PersistentPref(pref)
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/de/ph1b/audiobook/misc/Observables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -15,6 +16,15 @@ object Observables {
return Observable.combineLatest(source1, source2, BiFunction { t1, t2 -> combiner(t1, t2) })
}

inline fun <T1 : Any, T2 : Any, T3 : Any, R : Any> combineLatest(
source1: ObservableSource<T1>,
source2: ObservableSource<T2>,
source3: ObservableSource<T3>,
crossinline combiner: (T1, T2, T3) -> R
): Observable<R> {
return Observable.combineLatest(source1, source2, source3, Function3 { t1, t2, t3 -> combiner(t1, t2, t3) })
}

inline fun <T1 : Any, T2 : Any, T3 : Any, T4 : Any, T5 : Any, R : Any> combineLatest(
source1: ObservableSource<T1>,
source2: ObservableSource<T2>,
Expand Down

0 comments on commit 9aa834b

Please sign in to comment.