Skip to content

Commit

Permalink
Fix minor Compose performance issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre committed Jun 2, 2024
1 parent 8a81d45 commit 6079cf8
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 10 deletions.
4 changes: 4 additions & 0 deletions common/audio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id 'quran.android.library.android'
alias libs.plugins.kotlin.parcelize
alias libs.plugins.anvil
// needed for the stability of various model classes
alias libs.plugins.compose.compiler
}

anvil {
Expand All @@ -20,6 +22,8 @@ dependencies {
implementation libs.androidx.annotation
implementation libs.kotlinx.coroutines.core
implementation libs.okio
// required whenever we include the compiler plugin
implementation libs.compose.runtime

testImplementation libs.junit
testImplementation libs.truth
Expand Down
5 changes: 5 additions & 0 deletions common/data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ plugins {
id 'quran.android.library'
alias libs.plugins.anvil
alias libs.plugins.ksp
// needed for the stability of various model classes
alias libs.plugins.compose.compiler
}

anvil {
Expand All @@ -15,6 +17,9 @@ dependencies {
implementation libs.kotlinx.coroutines.core
implementation libs.kotlinx.coroutines.android

// required whenever we include the compiler plugin
implementation libs.compose.runtime

// dagger
implementation libs.dagger.runtime

Expand Down
3 changes: 3 additions & 0 deletions feature/downloadmanager/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ dependencies {
implementation(libs.compose.ui.tooling.preview)
debugImplementation(libs.compose.ui.tooling)

// immutable collections
implementation(libs.kotlinx.collections.immutable)

// coroutines
implementation(libs.kotlinx.coroutines.core)
implementation(libs.kotlinx.coroutines.android)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.quran.mobile.feature.downloadmanager.presenter.AudioManagerPresenter
import com.quran.mobile.feature.downloadmanager.ui.common.DownloadManagerToolbar
import com.quran.mobile.feature.downloadmanager.ui.LoadingIndicator
import com.quran.mobile.feature.downloadmanager.ui.ShuyookhList
import kotlinx.collections.immutable.persistentListOf
import javax.inject.Inject


Expand All @@ -38,7 +39,7 @@ class AudioManagerActivity : ComponentActivity() {
audioManagerPresenter.downloadedShuyookh { QariItem.fromQari(this, it) }

setContent {
val downloadedShuyookhState = downloadedShuyookhFlow.collectAsState(emptyList())
val downloadedShuyookhState = downloadedShuyookhFlow.collectAsState(persistentListOf())
QuranTheme {
Column(modifier = Modifier
.background(MaterialTheme.colorScheme.surface)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import com.quran.data.model.audio.Qari
import com.quran.labs.androidquran.common.audio.cache.QariDownloadInfoManager
import com.quran.labs.androidquran.common.audio.model.QariItem
import com.quran.mobile.feature.downloadmanager.model.DownloadedSheikhUiModel
import javax.inject.Inject
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

@ActivityScope
class AudioManagerPresenter @Inject constructor(
Expand All @@ -16,7 +18,7 @@ class AudioManagerPresenter @Inject constructor(

fun downloadedShuyookh(
lambda: ((Qari) -> QariItem)
): Flow<List<DownloadedSheikhUiModel>> {
): Flow<ImmutableList<DownloadedSheikhUiModel>> {
return qariDownloadInfoManager.downloadQariInfoFilteringNonDownloadedGappedQaris()
.map { qariDownloadInfoList ->
qariDownloadInfoList
Expand All @@ -28,5 +30,6 @@ class AudioManagerPresenter @Inject constructor(
}
.sortedBy { it.qariItem.name }
}
.map { it.toImmutableList() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.quran.labs.androidquran.common.audio.model.QariItem
import com.quran.mobile.feature.downloadmanager.model.DownloadedSheikhUiModel
import kotlinx.collections.immutable.ImmutableList

@Composable
fun ShuyookhList(
shuyookh: List<DownloadedSheikhUiModel>,
shuyookh: ImmutableList<DownloadedSheikhUiModel>,
onQariItemClicked: ((QariItem) -> Unit)
) {
Column(modifier = Modifier.verticalScroll(rememberScrollState())) {
Expand Down
3 changes: 3 additions & 0 deletions feature/qarilist/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ dependencies {
implementation libs.compose.ui.tooling.preview
debugImplementation libs.compose.ui.tooling

// immutable collections
implementation libs.kotlinx.collections.immutable

// coroutines
implementation libs.kotlinx.coroutines.core
implementation libs.kotlinx.coroutines.android
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.quran.labs.androidquran.common.ui.core.QuranTheme
import com.quran.mobile.feature.qarilist.di.QariListWrapperInjector
import com.quran.mobile.feature.qarilist.presenter.QariListPresenter
import com.quran.mobile.feature.qarilist.ui.QariList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -76,7 +77,7 @@ class QariListWrapper(
)
val qariListFlow =
qariListPresenter.qariList(startAyah, endAyah) { QariItem.fromQari(context, it) }
val qariListState = qariListFlow.collectAsState(emptyList())
val qariListState = qariListFlow.collectAsState(persistentListOf())
val currentQariFlow = currentQariManager.flow()
val currentQariState = currentQariFlow.collectAsState(null)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import com.quran.data.model.SuraAyah
import com.quran.data.model.audio.Qari
import com.quran.labs.androidquran.common.audio.cache.QariDownloadInfoManager
import com.quran.labs.androidquran.common.audio.extension.isRangeDownloaded
import com.quran.labs.androidquran.common.audio.model.download.QariDownloadInfo
import com.quran.labs.androidquran.common.audio.model.QariItem
import com.quran.labs.androidquran.common.audio.model.download.QariDownloadInfo
import com.quran.mobile.feature.qarilist.R
import com.quran.mobile.feature.qarilist.model.QariUiModel
import javax.inject.Inject
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

@ActivityScope
class QariListPresenter @Inject constructor(private val qariDownloadInfoManager: QariDownloadInfoManager) {

fun qariList(start: SuraAyah, end: SuraAyah, qariTranslationLambda: ((Qari) -> QariItem)): Flow<List<QariUiModel>> {
fun qariList(start: SuraAyah, end: SuraAyah, qariTranslationLambda: (
(Qari) -> QariItem)): Flow<ImmutableList<QariUiModel>> {
return qariDownloadInfoManager.downloadQariInfoFilteringNonDownloadedGappedQaris()
.map { unsortedQariList ->
val readyToPlay = unsortedQariList.filter { it.isRangeDownloaded(start, end) }
Expand All @@ -32,6 +35,7 @@ class QariListPresenter @Inject constructor(private val qariDownloadInfoManager:
gapless.map { QariUiModel(it, R.string.qarilist_gapless) } +
gapped.map { QariUiModel(it, R.string.qarilist_gapped) }
}
.map { it.toImmutableList() }
}

private fun List<QariDownloadInfo>.toSortedQariItemList(lambda: ((Qari) -> QariItem)): List<QariItem> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.quran.labs.androidquran.common.audio.model.QariItem
import com.quran.mobile.feature.qarilist.model.QariUiModel
import kotlinx.collections.immutable.ImmutableList

@Composable
fun QariList(
qaris: List<QariUiModel>,
qaris: ImmutableList<QariUiModel>,
selectedQariId: Int,
onQariSelected: ((QariItem) -> Unit),
modifier: Modifier = Modifier
Expand Down
4 changes: 3 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dnsjavaVersion = "2.1.9"
errorprone = "3.1.0"
errorproneCoreVersion = "2.28.0"
googleServices = "4.4.2"
immutableCollectionsVersion = "0.3.7"
leakcanaryAndroidVersion = "2.14"
moshiVersion = "1.15.1"
okioVersion = "3.9.0"
Expand Down Expand Up @@ -74,11 +75,11 @@ turbineVersion = "1.1.0"
[libraries]
kotlinx-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutinesVersion" }
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutinesVersion" }
kotlinx-collections-immutable = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable", version.ref = "immutableCollectionsVersion" }

# required within the Gradle convention plugins - not unused
okhttp-bom = { module = "com.squareup.okhttp3:okhttp-bom", version.ref = "okhttpBomVersion" }
compose-bom = { module = "androidx.compose:compose-bom", version.ref = "composeBomVersion" }
compose-runtime = { module = "androidx.compose.runtime:runtime" }

# androidx
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidxAppcompatVersion" }
Expand All @@ -103,6 +104,7 @@ compose-animation = { module = "androidx.compose.animation:animation" }
compose-material = { module = "androidx.compose.material:material" }
compose-material3 = { module = "androidx.compose.material3:material3", version = "1.2.1" }
compose-material-icons = { module = "androidx.compose.material:material-icons-extended" }
compose-runtime = { module = "androidx.compose.runtime:runtime" }
compose-ui = { module = "androidx.compose.ui:ui" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
Expand Down

0 comments on commit 6079cf8

Please sign in to comment.