Skip to content

Commit

Permalink
chore: update UnlauncherDataSource to directly inject repos (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkuester authored Jan 25, 2025
1 parent b361b02 commit 592dc54
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 173 deletions.
50 changes: 49 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id("com.google.devtools.ksp")
id("com.google.protobuf")
id("org.jlleitschuh.gradle.ktlint")
id("jacoco")
kotlin("android")
}

Expand Down Expand Up @@ -45,6 +46,7 @@ android {
}
named("debug").configure {
isMinifyEnabled = false
enableUnitTestCoverage = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
Expand All @@ -59,7 +61,9 @@ android {
jvmTarget = JavaVersion.VERSION_17.toString()
}
testOptions {
unitTests.isIncludeAndroidResources = true
unitTests.all {
it.useJUnitPlatform()
}
}
lint {
warningsAsErrors = true
Expand Down Expand Up @@ -106,6 +110,12 @@ dependencies {
implementation("com.google.dagger:hilt-android:2.54")
ksp("androidx.hilt:hilt-compiler:1.2.0")
ksp("com.google.dagger:hilt-android-compiler:2.54")

// Test libs
testImplementation(kotlin("test"))
testImplementation("org.junit.jupiter:junit-jupiter:5.11.4")
testImplementation("io.mockk:mockk-android:1.13.14")
testImplementation("io.mockk:mockk-agent:1.13.14")
}
protobuf {
protoc {
Expand All @@ -125,3 +135,41 @@ ktlint {
android = true
ignoreFailures = false
}
tasks.withType(Test::class) {
configure<JacocoTaskExtension> {
isIncludeNoLocationClasses = true
excludes = listOf("jdk.internal.*")
}
}

val jacocoExclusions = listOf("**/sduduzog/*")
val jacocoSourceDirs = layout.projectDirectory.dir("src/main")
val jacocoClassDirs = files(
fileTree(layout.buildDirectory.dir("tmp/kotlin-classes/debug")) {
exclude(jacocoExclusions)
}
)
val jacocoExecutionData =
files(fileTree(layout.buildDirectory) { include(listOf("**/*.exec", "**/*.ec")) })
tasks.register<JacocoCoverageVerification>("jacocoCoverageVerification") {
dependsOn(listOf("testDebugUnitTest", "createDebugUnitTestCoverageReport"))
group = "Verification"
violationRules {
rule {
limit {
minimum = "1".toBigDecimal()
}
}
}
sourceDirectories.setFrom(jacocoSourceDirs)
classDirectories.setFrom(jacocoClassDirs)
executionData.setFrom(jacocoExecutionData)
}
tasks.build { finalizedBy("jacocoCoverageVerification") }
tasks.register<JacocoReport>("jacocoCoverageReport") {
dependsOn(listOf("testDebugUnitTest", "createDebugUnitTestCoverageReport"))
group = "Reporting"
sourceDirectories.setFrom(jacocoSourceDirs)
classDirectories.setFrom(jacocoClassDirs)
executionData.setFrom(jacocoExecutionData)
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.sduduzog.slimlauncher.datasource
package com.jkuester.unlauncher.datasource

import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.dataStore
import androidx.lifecycle.LifecycleCoroutineScope
import com.jkuester.unlauncher.datastore.CorePreferences
import com.jkuester.unlauncher.datastore.QuickButtonPreferences
import com.jkuester.unlauncher.datastore.UnlauncherApps
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsMigrations
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsRepository
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsSerializer
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesMigrations
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesRepository
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesSerializer
import com.sduduzog.slimlauncher.datasource.quickbuttonprefs.QuickButtonPreferencesMigrations
import com.sduduzog.slimlauncher.datasource.quickbuttonprefs.QuickButtonPreferencesRepository
import com.sduduzog.slimlauncher.datasource.quickbuttonprefs.QuickButtonPreferencesSerializer
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

private val Context.quickButtonPreferencesStore: DataStore<QuickButtonPreferences> by dataStore(
fileName = "quick_button_preferences.proto",
Expand All @@ -35,10 +37,24 @@ private val Context.corePreferencesStore: DataStore<CorePreferences> by dataStor
produceMigrations = { _ -> CorePreferencesMigrations().get() }
)

class UnlauncherDataSource(context: Context, lifecycleScope: LifecycleCoroutineScope) {
val quickButtonPreferencesRepo =
QuickButtonPreferencesRepository(context.quickButtonPreferencesStore, lifecycleScope)
val unlauncherAppsRepo = UnlauncherAppsRepository(context.unlauncherAppsStore, lifecycleScope)
val corePreferencesRepo =
CorePreferencesRepository(context.corePreferencesStore, lifecycleScope)
@Module
@InstallIn(SingletonComponent::class)
class DataStoreModule {
@Singleton
@Provides
fun provideQuickButtonPreferencesStore(
@ApplicationContext appContext: Context
): DataStore<QuickButtonPreferences> = appContext.quickButtonPreferencesStore

@Singleton
@Provides
fun provideUnlauncherAppsStore(
@ApplicationContext appContext: Context
): DataStore<UnlauncherApps> = appContext.unlauncherAppsStore

@Singleton
@Provides
fun provideCorePreferencesStore(
@ApplicationContext appContext: Context
): DataStore<CorePreferences> = appContext.corePreferencesStore
}
23 changes: 22 additions & 1 deletion app/src/main/java/com/sduduzog/slimlauncher/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@ import android.view.View
import androidx.annotation.StyleRes
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.datastore.core.DataStore
import androidx.navigation.NavController
import androidx.navigation.Navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.recyclerview.widget.RecyclerView
import com.jkuester.unlauncher.datastore.CorePreferences
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesRepository
import com.sduduzog.slimlauncher.utils.BaseFragment
import com.sduduzog.slimlauncher.utils.HomeWatcher
import com.sduduzog.slimlauncher.utils.IPublisher
import com.sduduzog.slimlauncher.utils.ISubscriber
import com.sduduzog.slimlauncher.utils.SystemUiManager
import com.sduduzog.slimlauncher.utils.WallpaperManager
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.android.components.ActivityComponent
import java.lang.reflect.Method
import javax.inject.Inject
import kotlin.math.absoluteValue
Expand All @@ -35,10 +42,21 @@ class MainActivity :
HomeWatcher.OnHomePressedListener,
IPublisher {

private val wallpaperManager = WallpaperManager(this)
@Inject
lateinit var corePresRepo: CorePreferencesRepository

@Inject
lateinit var systemUiManager: SystemUiManager

@Inject
lateinit var corePreferencesStore: DataStore<CorePreferences>

@EntryPoint
@InstallIn(ActivityComponent::class)
interface WallpaperManagerFactory {
fun getWallpaperManager(): WallpaperManager
}

private lateinit var settings: SharedPreferences
private lateinit var navigator: NavController
private lateinit var homeWatcher: HomeWatcher
Expand Down Expand Up @@ -121,6 +139,9 @@ class MainActivity :
first: Boolean
) {
super.onApplyThemeResource(theme, resid, first)
// This function is called too early in the lifecycle for normal injection so we do it the hard way
val factory = EntryPointAccessors.fromActivity(this, WallpaperManagerFactory::class.java)
val wallpaperManager = factory.getWallpaperManager()
wallpaperManager.onApplyThemeResource(theme, resid)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.RecyclerView
import com.jkuester.unlauncher.datastore.UnlauncherApp
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
import com.sduduzog.slimlauncher.datasource.apps.UnlauncherAppsRepository
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesRepository
import com.sduduzog.slimlauncher.ui.main.HomeFragment
import com.sduduzog.slimlauncher.utils.firstUppercase
import com.sduduzog.slimlauncher.utils.gravity

class AppDrawerAdapter(
private val listener: HomeFragment.AppDrawerListener,
lifecycleOwner: LifecycleOwner,
private val unlauncherDataSource: UnlauncherDataSource
unlauncherAppsRepo: UnlauncherAppsRepository,
private val corePreferencesRepo: CorePreferencesRepository
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private val workAppPrefix = "\uD83C\uDD46 " // Unicode for boxed w
Expand All @@ -29,13 +31,13 @@ class AppDrawerAdapter(
private var gravity = 3

init {
unlauncherDataSource.unlauncherAppsRepo.liveData().observe(
unlauncherAppsRepo.liveData().observe(
lifecycleOwner
) { unlauncherApps ->
apps = unlauncherApps.appsList
updateFilteredApps()
}
unlauncherDataSource.corePreferencesRepo.liveData().observe(lifecycleOwner) { corePrefs ->
corePreferencesRepo.liveData().observe(lifecycleOwner) { corePrefs ->
gravity = corePrefs.alignmentFormat.gravity()
updateFilteredApps()
}
Expand All @@ -60,9 +62,8 @@ class AppDrawerAdapter(
}
}

fun getFirstApp(): UnlauncherApp {
return filteredApps.filterIsInstance<AppDrawerRow.Item>().first().app
}
fun getFirstApp(): UnlauncherApp =
filteredApps.filterIsInstance<AppDrawerRow.Item>().first().app

override fun getItemViewType(position: Int): Int = filteredApps[position].rowType.ordinal

Expand All @@ -79,9 +80,8 @@ class AppDrawerAdapter(
}
}

private fun onlyFirstStringStartsWith(first: String, second: String, query: String): Boolean {
return first.startsWith(query, true) and !second.startsWith(query, true)
}
private fun onlyFirstStringStartsWith(first: String, second: String, query: String): Boolean =
first.startsWith(query, true) and !second.startsWith(query, true)

fun setAppFilter(query: String = "") {
val filterQuery = regex.replace(query, "")
Expand All @@ -90,13 +90,14 @@ class AppDrawerAdapter(

@SuppressLint("NotifyDataSetChanged")
private fun updateFilteredApps(filterQuery: String = "") {
val corePreferences = unlauncherDataSource.corePreferencesRepo.get()
val corePreferences = corePreferencesRepo.get()
val showDrawerHeadings = corePreferences.showDrawerHeadings
val searchAllApps = corePreferences.searchAllAppsInDrawer && filterQuery != ""
val displayableApps = apps
.filter { app ->
(app.displayInDrawer || searchAllApps) && regex.replace(app.displayName, "")
.contains(filterQuery, ignoreCase = true)
(app.displayInDrawer || searchAllApps) &&
regex.replace(app.displayName, "")
.contains(filterQuery, ignoreCase = true)
}

val includeHeadings = !showDrawerHeadings || filterQuery != ""
Expand Down Expand Up @@ -163,9 +164,7 @@ class AppDrawerAdapter(

val item: TextView = itemView.findViewById(R.id.aa_list_item_app_name)

override fun toString(): String {
return "${super.toString()} '${item.text}'"
}
override fun toString(): String = "${super.toString()} '${item.text}'"

fun bind(item: UnlauncherApp) {
this.item.text = item.displayName
Expand All @@ -176,9 +175,7 @@ class AppDrawerAdapter(
inner class HeaderViewHolder(headerView: View) : RecyclerView.ViewHolder(headerView) {
private val header: TextView = itemView.findViewById(R.id.aa_list_header_letter)

override fun toString(): String {
return "${super.toString()} '${header.text}'"
}
override fun toString(): String = "${super.toString()} '${header.text}'"

fun bind(letter: String) {
header.text = letter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sduduzog.slimlauncher.R
import com.sduduzog.slimlauncher.datasource.UnlauncherDataSource
import com.sduduzog.slimlauncher.datasource.coreprefs.CorePreferencesRepository
import com.sduduzog.slimlauncher.models.HomeApp
import com.sduduzog.slimlauncher.ui.main.HomeFragment
import com.sduduzog.slimlauncher.utils.gravity

class HomeAdapter(
private val listener: HomeFragment,
private val unlauncherDataSource: UnlauncherDataSource
private val corePreferencesRepo: CorePreferencesRepository
) : RecyclerView.Adapter<HomeAdapter.ViewHolder>() {

private var apps: List<HomeApp> = listOf()
Expand All @@ -31,7 +31,7 @@ class HomeAdapter(
holder.mLabelView.setOnClickListener {
listener.onLaunch(item, it)
}
unlauncherDataSource.corePreferencesRepo.liveData().observe(listener.viewLifecycleOwner) {
corePreferencesRepo.liveData().observe(listener.viewLifecycleOwner) {
holder.mLabelView.gravity = it.alignmentFormat.gravity()
}
}
Expand All @@ -47,8 +47,6 @@ class HomeAdapter(
inner class ViewHolder(mView: View) : RecyclerView.ViewHolder(mView) {
val mLabelView: TextView = mView.findViewById(R.id.home_fragment_list_item_app_name)

override fun toString(): String {
return super.toString() + " '" + mLabelView.text + "'"
}
override fun toString(): String = super.toString() + " '" + mLabelView.text + "'"
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package com.sduduzog.slimlauncher.datasource.apps

import android.app.Activity
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.datastore.core.DataStore
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.LiveData
import androidx.lifecycle.asLiveData
import androidx.lifecycle.lifecycleScope
import com.jkuester.unlauncher.datastore.UnlauncherApp
import com.jkuester.unlauncher.datastore.UnlauncherApps
import com.sduduzog.slimlauncher.data.model.App
import com.sduduzog.slimlauncher.models.HomeApp
import dagger.hilt.android.scopes.ActivityScoped
import java.io.IOException
import java.util.Locale
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch

class UnlauncherAppsRepository(
@ActivityScoped
class UnlauncherAppsRepository @Inject constructor(
private val unlauncherAppsStore: DataStore<UnlauncherApps>,
private val lifecycleScope: LifecycleCoroutineScope
activity: Activity
) {
private val lifecycleScope = (activity as ComponentActivity).lifecycleScope
private val unlauncherAppsFlow: Flow<UnlauncherApps> =
unlauncherAppsStore.data
.catch { exception ->
Expand Down
Loading

0 comments on commit 592dc54

Please sign in to comment.