Skip to content

Commit

Permalink
[feat]: moved apps chooser to another module and refactored to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Aug 14, 2024
1 parent 17a525f commit e912931
Show file tree
Hide file tree
Showing 30 changed files with 585 additions and 380 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation(libs.viewpump)
implementation(libs.gson)

implementation(libs.coil)
implementation(libs.glide)
ksp(libs.glide.compiler)

Expand Down
9 changes: 8 additions & 1 deletion app/src/main/kotlin/com/f0x1d/logfox/LogFoxApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationManagerCompat
import coil.ImageLoader
import coil.ImageLoaderFactory
import com.f0x1d.logfox.arch.LOGGING_STATUS_CHANNEL_ID
import com.f0x1d.logfox.arch.RECORDING_STATUS_CHANNEL_ID
import com.f0x1d.logfox.arch.notificationManagerCompat
Expand All @@ -14,11 +16,14 @@ import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject

@HiltAndroidApp
class LogFoxApp: Application() {
class LogFoxApp: Application(), ImageLoaderFactory {

@Inject
lateinit var appPreferences: AppPreferences

@Inject
lateinit var imageLoader: ImageLoader

override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(appPreferences.nightTheme)
Expand Down Expand Up @@ -51,4 +56,6 @@ class LogFoxApp: Application() {
)
}
}

override fun newImageLoader(): ImageLoader = imageLoader
}
49 changes: 49 additions & 0 deletions app/src/main/kotlin/com/f0x1d/logfox/coil/AppIconFetcher.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.f0x1d.logfox.coil

import android.content.Context
import coil.ImageLoader
import coil.decode.DataSource
import coil.fetch.DrawableResult
import coil.fetch.FetchResult
import coil.fetch.Fetcher
import coil.request.Options
import com.f0x1d.logfox.arch.di.IODispatcher
import com.f0x1d.logfox.model.InstalledApp
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton

class AppIconFetcher(
private val context: Context,
private val app: InstalledApp,
private val ioDispatcher: CoroutineDispatcher,
) : Fetcher {

override suspend fun fetch(): FetchResult = withContext(ioDispatcher) {
val appDrawable = context.packageManager.getApplicationIcon(app.packageName)

DrawableResult(
drawable = appDrawable,
isSampled = true,
dataSource = DataSource.DISK,
)
}

@Singleton
class Factory @Inject constructor(
@ApplicationContext private val context: Context,
@IODispatcher private val ioDispatcher: CoroutineDispatcher,
) : Fetcher.Factory<InstalledApp> {
override fun create(
data: InstalledApp,
options: Options,
imageLoader: ImageLoader,
): Fetcher = AppIconFetcher(
context = context,
app = data,
ioDispatcher = ioDispatcher,
)
}
}
27 changes: 27 additions & 0 deletions app/src/main/kotlin/com/f0x1d/logfox/di/CoilModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.f0x1d.logfox.di

import android.content.Context
import coil.ImageLoader
import com.f0x1d.logfox.coil.AppIconFetcher
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

@Module
@InstallIn(SingletonComponent::class)
object CoilModule {

@Provides
@Singleton
fun provideImageLoader(
@ApplicationContext context: Context,
appIconFetcherFactory: AppIconFetcher.Factory,
): ImageLoader = ImageLoader.Builder(context)
.components {
add(appIconFetcherFactory)
}
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class MainActivity: BaseViewModelActivity<MainViewModel, ActivityMainBinding>(),
Directions.logsExtendedCopyFragment -> false
Directions.filtersFragment -> false
Directions.editFilterFragment -> false
Directions.chooseAppFragment -> false
Directions.appsPickerFragment -> false
Directions.appCrashesFragment -> false
Directions.crashDetailsFragment -> false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.android.build.api.dsl.CommonExtension
import com.android.build.api.dsl.LibraryExtension
import extensions.bundle
import extensions.implementation
import extensions.library
import extensions.pluginId
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -24,6 +25,7 @@ class AndroidComposeConventionPlugin : Plugin<Project> {
}

dependencies {
implementation(library("kotlinx-immutable-collections"))
implementation(bundle("androidx-compose"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ interface SimpleLifecycleOwner {
state: Lifecycle.State = Lifecycle.State.STARTED,
collector: FlowCollector<T>,
) {
val lifecycle = getViewLifecycleOwner()?.lifecycle ?: lifecycle
val viewLifecycleOwner = runCatching { getViewLifecycleOwner() }.getOrNull()
val lifecycle = viewLifecycleOwner?.lifecycle ?: lifecycle

lifecycle.coroutineScope.launch {
lifecycle.repeatOnLifecycle(state) {
Expand Down
10 changes: 5 additions & 5 deletions core/core-navigation/src/main/res/navigation/logs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@
android:defaultValue="-1L" />
</fragment>
<action
android:id="@+id/action_editFilterFragment_to_chooseAppFragment"
app:destination="@id/chooseAppFragment"
android:id="@+id/action_editFilterFragment_to_appsPickerFragment"
app:destination="@id/appsPickerFragment"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim"
app:popEnterAnim="@anim/nav_default_pop_enter_anim"
app:popExitAnim="@anim/nav_default_pop_exit_anim" />

<fragment
android:id="@+id/chooseAppFragment"
android:name="com.f0x1d.logfox.feature.filters.ui.fragment.ChooseAppFragment"
android:label="ChooseAppFragment" />
android:id="@+id/appsPickerFragment"
android:name="com.f0x1d.logfox.feature.apps.picker.ui.fragment.picker.AppsPickerFragment"
android:label="AppsPickerFragment" />
</navigation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.f0x1d.logfox.ui.compose.component.button

import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import com.f0x1d.logfox.ui.Icons
import com.f0x1d.logfox.ui.compose.preview.DayNightPreview
import com.f0x1d.logfox.ui.compose.theme.LogFoxTheme

@Composable
fun NavigationBackButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
) {
IconButton(
modifier = modifier,
onClick = onClick,
) {
Icon(
painter = painterResource(id = Icons.ic_arrow_back),
contentDescription = null,
)
}
}

@DayNightPreview
@Composable
private fun Preview() = LogFoxTheme {
NavigationBackButton(onClick = { })
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,15 @@ fun RichButton(

@DayNightPreview
@Composable
private fun RichButtonAdbPreview() {
LogFoxTheme {
RichButton(
text = { Text(text = "ADB") },
icon = {
Icon(
painter = painterResource(id = Icons.ic_adb),
contentDescription = null,
)
},
onClick = { },
)
}
private fun RichButtonAdbPreview() = LogFoxTheme {
RichButton(
text = { Text(text = "ADB") },
icon = {
Icon(
painter = painterResource(id = Icons.ic_adb),
contentDescription = null,
)
},
onClick = { },
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.f0x1d.logfox.model

data class InstalledApp(
val title: CharSequence,
val title: String,
val packageName: String,
) : Identifiable {
override val id: Any get() = packageName
Expand Down
1 change: 1 addition & 0 deletions feature/feature-apps-picker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
9 changes: 9 additions & 0 deletions feature/feature-apps-picker/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("logfox.android.feature.compose")
}

android.namespace = "com.f0x1d.logfox.feature.apps.picker"

dependencies {
implementation(libs.coil.compose)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.f0x1d.logfox.feature.apps.picker.ui.fragment.picker

import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.f0x1d.logfox.arch.ui.fragment.compose.BaseComposeViewModelFragment
import com.f0x1d.logfox.feature.apps.picker.ui.fragment.picker.compose.AppsPickerScreenContent
import com.f0x1d.logfox.feature.apps.picker.ui.fragment.picker.compose.AppsPickerScreenListener
import com.f0x1d.logfox.feature.apps.picker.viewmodel.AppsPickerViewModel
import com.f0x1d.logfox.feature.apps.picker.viewmodel.resultHandler
import com.f0x1d.logfox.ui.compose.theme.LogFoxTheme
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class AppsPickerFragment: BaseComposeViewModelFragment<AppsPickerViewModel>() {

override val viewModel by viewModels<AppsPickerViewModel>()

private val resultHandler by resultHandler()

private val listener by lazy {
AppsPickerScreenListener(
onBackClicked = {
viewModel.performBackAction(findNavController()::popBackStack)
},
onAppClicked = {
if (resultHandler?.onAppSelected(it) == true) {
findNavController().popBackStack()
}
},
onSearchActiveChanged = viewModel::changeSearchActive,
onQueryChanged = viewModel::updateQuery,
)
}

@Composable
override fun Content() {
LogFoxTheme {
val state by viewModel.uiState.collectAsState()

AppsPickerScreenContent(
state = state,
listener = listener,
)
}
}
}
Loading

0 comments on commit e912931

Please sign in to comment.