From ab066b031bdde91e17e524bdac3bfb4125deb161 Mon Sep 17 00:00:00 2001 From: nift4 Date: Sun, 8 Dec 2024 11:01:18 +0100 Subject: [PATCH] test --- .../gramophone/ui/adapters/BaseAdapter.kt | 48 +++++++++---------- build.gradle.kts | 2 +- libphonograph | 2 +- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/BaseAdapter.kt b/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/BaseAdapter.kt index 9d01ffccd..d7029aa3b 100644 --- a/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/BaseAdapter.kt +++ b/app/src/main/kotlin/org/akanework/gramophone/ui/adapters/BaseAdapter.kt @@ -49,8 +49,12 @@ import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.Semaphore @@ -108,7 +112,6 @@ abstract class BaseAdapter( } private var lastList: List? = null protected val list = ArrayList((flow as? SharedFlow>)?.replayCache?.lastOrNull()?.size ?: 0) - private var comparator: Sorter.HintedComparator? = null private var layoutManager: RecyclerView.LayoutManager? = null private var listLock = Semaphore(1) protected var recyclerView: MyRecyclerView? = null @@ -162,28 +165,21 @@ abstract class BaseAdapter( lockedInGridSize = false notifyDataSetChanged() // we change view type for all items } - private var reverseRaw = false - var sortType: Sorter.Type - get() = if (comparator == null && rawOrderExposed) - (if (reverseRaw) Sorter.Type.NativeOrderDescending else Sorter.Type.NativeOrder) - else comparator?.type!! - private set(value) { - reverseRaw = value == Sorter.Type.NativeOrderDescending - if (comparator?.type != value) { - comparator = sorter.getComparator(value) - } - } + val sortType: MutableStateFlow = MutableStateFlow( + if (prefSortType != Sorter.Type.None && prefSortType != initialSortType + && sortTypes.contains(prefSortType) && !isSubFragment + ) + prefSortType + else + initialSortType + ) + private val comparator: SharedFlow?> = sortType.map { + sorter.getComparator(it) + }.shareIn(CoroutineScope(Dispatchers.Default), SharingStarted.WhileSubscribed(5000)) val sortTypes: Set get() = if (canSort) sorter.getSupportedTypes() else setOf(Sorter.Type.None) init { - sortType = - if (prefSortType != Sorter.Type.None && prefSortType != initialSortType - && sortTypes.contains(prefSortType) && !isSubFragment - ) - prefSortType - else - initialSortType updateListInternal(runBlocking { flow.first() }, now = true, canDiff = false) layoutType = if (prefLayoutType != LayoutType.NONE && prefLayoutType != defaultLayoutType && !isSubFragment) @@ -261,8 +257,8 @@ abstract class BaseAdapter( ) fun sort(selector: Sorter.Type) { - sortType = selector - updateListInternal(null, now = false, canDiff = true) + sortType.value = selector + //updateListInternal(null, now = false, canDiff = true) TODO } @SuppressLint("NotifyDataSetChanged") @@ -273,13 +269,15 @@ abstract class BaseAdapter( } try { val diff = withContext(Dispatchers.Default) { - if (sortType == Sorter.Type.NativeOrderDescending) { + val st = sortType.first() + val cmp = comparator.first() + if (st == Sorter.Type.NativeOrderDescending) { newList.reverse() - } else if (sortType != Sorter.Type.NativeOrder) { + } else if (st != Sorter.Type.NativeOrder) { newList.sortWith { o1, o2 -> if (isPinned(o1) && !isPinned(o2)) -1 else if (!isPinned(o1) && isPinned(o2)) 1 - else comparator?.compare(o1, o2) ?: 0 + else cmp?.compare(o1, o2) ?: 0 } } if (((list.isNotEmpty() && newList.isNotEmpty()) || allowDiffUtils) && canDiff) @@ -477,7 +475,7 @@ abstract class BaseAdapter( // if this crashes with IndexOutOfBoundsException, list access isn't guarded enough? // lib only ever gets popup text for what RecyclerView believes to be the first view return (if (position >= 1) - sorter.getFastScrollHintFor(list[position - 1], sortType) + sorter.getFastScrollHintFor(list[position - 1], sortType.value) else null) ?: "-" } diff --git a/build.gradle.kts b/build.gradle.kts index 981c8cd20..ae81c52d4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,6 +1,6 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - val agpVersion = "8.8.0-beta01" + val agpVersion = "8.8.0-rc01" id("com.android.application") version agpVersion apply false id("com.android.library") version agpVersion apply false id("com.android.test") version agpVersion apply false diff --git a/libphonograph b/libphonograph index b767d9b4b..f7470b5db 160000 --- a/libphonograph +++ b/libphonograph @@ -1 +1 @@ -Subproject commit b767d9b4b67e16ae8d5f57152663c34a4c62e582 +Subproject commit f7470b5dbc66858dc9b502a00f382093a70cd584