@@ -49,8 +49,12 @@ import kotlinx.coroutines.cancel
4949import kotlinx.coroutines.flow.Flow
5050import kotlinx.coroutines.flow.MutableStateFlow
5151import kotlinx.coroutines.flow.SharedFlow
52+ import kotlinx.coroutines.flow.SharingStarted
5253import kotlinx.coroutines.flow.first
5354import kotlinx.coroutines.flow.flatMapLatest
55+ import kotlinx.coroutines.flow.map
56+ import kotlinx.coroutines.flow.onEach
57+ import kotlinx.coroutines.flow.shareIn
5458import kotlinx.coroutines.launch
5559import kotlinx.coroutines.runBlocking
5660import kotlinx.coroutines.sync.Semaphore
@@ -108,7 +112,6 @@ abstract class BaseAdapter<T>(
108112 }
109113 private var lastList: List <T >? = null
110114 protected val list = ArrayList <T >((flow as ? SharedFlow <List <T >>)?.replayCache?.lastOrNull()?.size ? : 0 )
111- private var comparator: Sorter .HintedComparator <T >? = null
112115 private var layoutManager: RecyclerView .LayoutManager ? = null
113116 private var listLock = Semaphore (1 )
114117 protected var recyclerView: MyRecyclerView ? = null
@@ -162,28 +165,21 @@ abstract class BaseAdapter<T>(
162165 lockedInGridSize = false
163166 notifyDataSetChanged() // we change view type for all items
164167 }
165- private var reverseRaw = false
166- var sortType : Sorter .Type
167- get() = if (comparator == null && rawOrderExposed)
168- ( if (reverseRaw) Sorter . Type . NativeOrderDescending else Sorter . Type . NativeOrder )
169- else comparator?.type !!
170- private set(value) {
171- reverseRaw = value == Sorter . Type . NativeOrderDescending
172- if (comparator?.type != value) {
173- comparator = sorter.getComparator(value)
174- }
175- }
168+ val sortType : MutableStateFlow < Sorter . Type > = MutableStateFlow (
169+ if (prefSortType != Sorter .Type . None && prefSortType != initialSortType
170+ && sortTypes.contains(prefSortType) && ! isSubFragment
171+ )
172+ prefSortType
173+ else
174+ initialSortType
175+ )
176+ private val comparator: SharedFlow < Sorter . HintedComparator < T > ? > = sortType.map {
177+ sorter.getComparator(it)
178+ }.shareIn( CoroutineScope ( Dispatchers . Default ), SharingStarted . WhileSubscribed ( 5000 ))
176179 val sortTypes: Set <Sorter .Type >
177180 get() = if (canSort) sorter.getSupportedTypes() else setOf (Sorter .Type .None )
178181
179182 init {
180- sortType =
181- if (prefSortType != Sorter .Type .None && prefSortType != initialSortType
182- && sortTypes.contains(prefSortType) && ! isSubFragment
183- )
184- prefSortType
185- else
186- initialSortType
187183 updateListInternal(runBlocking { flow.first() }, now = true , canDiff = false )
188184 layoutType =
189185 if (prefLayoutType != LayoutType .NONE && prefLayoutType != defaultLayoutType && ! isSubFragment)
@@ -261,8 +257,8 @@ abstract class BaseAdapter<T>(
261257 )
262258
263259 fun sort (selector : Sorter .Type ) {
264- sortType = selector
265- updateListInternal(null , now = false , canDiff = true )
260+ sortType.value = selector
261+ // updateListInternal(null, now = false, canDiff = true) TODO
266262 }
267263
268264 @SuppressLint(" NotifyDataSetChanged" )
@@ -273,13 +269,15 @@ abstract class BaseAdapter<T>(
273269 }
274270 try {
275271 val diff = withContext(Dispatchers .Default ) {
276- if (sortType == Sorter .Type .NativeOrderDescending ) {
272+ val st = sortType.first()
273+ val cmp = comparator.first()
274+ if (st == Sorter .Type .NativeOrderDescending ) {
277275 newList.reverse()
278- } else if (sortType != Sorter .Type .NativeOrder ) {
276+ } else if (st != Sorter .Type .NativeOrder ) {
279277 newList.sortWith { o1, o2 ->
280278 if (isPinned(o1) && ! isPinned(o2)) - 1
281279 else if (! isPinned(o1) && isPinned(o2)) 1
282- else comparator ?.compare(o1, o2) ? : 0
280+ else cmp ?.compare(o1, o2) ? : 0
283281 }
284282 }
285283 if (((list.isNotEmpty() && newList.isNotEmpty()) || allowDiffUtils) && canDiff)
@@ -477,7 +475,7 @@ abstract class BaseAdapter<T>(
477475 // if this crashes with IndexOutOfBoundsException, list access isn't guarded enough?
478476 // lib only ever gets popup text for what RecyclerView believes to be the first view
479477 return (if (position >= 1 )
480- sorter.getFastScrollHintFor(list[position - 1 ], sortType)
478+ sorter.getFastScrollHintFor(list[position - 1 ], sortType.value )
481479 else null ) ? : " -"
482480 }
483481
0 commit comments