Annual refactor

This commit is contained in:
MM20
2023-10-28 14:14:08 +02:00
parent 4f64652aae
commit 957358c79a
110 changed files with 1786 additions and 1663 deletions

View File

@@ -0,0 +1,58 @@
package de.mm20.launcher2.applications
import android.content.ComponentName
import android.content.Context
import android.os.Bundle
import android.os.Process
import android.os.UserHandle
import de.mm20.launcher2.search.AppProfile
import de.mm20.launcher2.search.Application
import de.mm20.launcher2.search.NullSerializer
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.search.SearchableSerializer
class FakeApp: Application {
override val componentName: ComponentName = ComponentName(randomString(), randomString())
override val isSystemApp: Boolean = false
override val isSuspended: Boolean = false
override val profile: AppProfile = AppProfile.Personal
override val user: UserHandle = Process.myUserHandle()
override val versionName: String = "1.0"
override val canUninstall: Boolean = false
override fun uninstall(context: Context) {
}
override fun openAppDetails(context: Context) {
}
override val canShareApk: Boolean = false
override val key: String = "fake://${randomString()}"
override val label: String = randomString()
override fun overrideLabel(label: String): SavableSearchable {
return this
}
override fun launch(context: Context, options: Bundle?): Boolean {
return false
}
override val domain: String
get() = "fake"
override fun getSerializer(): SearchableSerializer {
return NullSerializer()
}
private companion object {
private fun randomString(): String {
val charset = "abcdefghijklmnopqrstuvwxyz"
return (1..10)
.map { charset.random() }
.joinToString("")
}
}
}

View File

@@ -6,8 +6,8 @@ import android.content.Intent
import android.content.pm.LauncherApps
import android.os.Process
import androidx.core.content.getSystemService
import de.mm20.launcher2.ktx.getSerialNumber
import de.mm20.launcher2.search.data.LauncherApp
import de.mm20.launcher2.search.Application
import de.mm20.launcher2.search.SearchableRepository
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toImmutableList
@@ -16,51 +16,18 @@ import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
/**
* A fake implementation of [AppRepository] to simulate many installed apps.
* App repository that returns a fixed number of fake apps to simulate a large number of apps.
*/
class FakeAppRepository(private val context: Context, private val fakePackages: Int) : AppRepository {
class FakeAppRepository(private val context: Context, private val fakePackages: Int) : SearchableRepository<Application> {
private val fakeApp: LauncherApp
init {
val launcherApps = context.getSystemService<LauncherApps>()!!
fakeApp = LauncherApp(
context,
launcherApps.resolveActivity(
Intent().apply {
component = ComponentName(
context.packageName,
"de.mm20.launcher2.ui.launcher.LauncherActivity"
)
},
Process.myUserHandle()
),
)
}
private fun randomString(): String {
val charset = "abcdefghijklmnopqrstuvwxyz"
return (1..10)
.map { charset.random() }
.joinToString("")
}
override fun getAllInstalledApps(): Flow<List<LauncherApp>> {
return flowOf(buildList {
repeat(fakePackages) {
add(fakeApp.copy(`package` = randomString(), activity = randomString()))
}
})
}
override fun getSuspendedPackages(): Flow<List<String>> {
return flowOf(emptyList())
}
override fun search(query: String): Flow<ImmutableList<LauncherApp>> {
override fun search(query: String): Flow<ImmutableList<Application>> {
return if (query.isEmpty()) {
getAllInstalledApps().map { it.toImmutableList() }
buildList {
repeat(fakePackages) {
add(FakeApp())
}
}.toImmutableList().let { flowOf(it) }
} else {
flowOf(persistentListOf())
}