Skip to content

Commit

Permalink
perf: rm hilt
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Aug 18, 2024
1 parent b4474f5 commit dbc4a28
Show file tree
Hide file tree
Showing 31 changed files with 65 additions and 108 deletions.
5 changes: 0 additions & 5 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ plugins {
alias(libs.plugins.kotlin.serialization)
alias(libs.plugins.kotlin.compose)
alias(libs.plugins.google.ksp)
alias(libs.plugins.google.hilt)
alias(libs.plugins.rikka.refine)
}

Expand Down Expand Up @@ -243,10 +242,6 @@ dependencies {
implementation(libs.destinations.core)
ksp(libs.destinations.ksp)

implementation(libs.google.hilt.android)
ksp(libs.google.hilt.android.compiler)
implementation(libs.androidx.hilt.navigation.compose)

implementation(libs.reorderable)

implementation(libs.androidx.splashscreen)
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.hjq.toast.Toaster
import com.tencent.mmkv.MMKV
import dagger.hilt.android.HiltAndroidApp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import li.songe.gkd.debug.clearHttpSubs
Expand Down Expand Up @@ -38,7 +37,6 @@ val applicationInfo by lazy {

val channel by lazy { applicationInfo.metaData.getString("channel") }

@HiltAndroidApp
class App : Application() {
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.blankj.utilcode.util.ServiceUtils
import com.dylanc.activityresult.launcher.PickContentLauncher
import com.dylanc.activityresult.launcher.StartActivityLauncher
import com.ramcosta.composedestinations.DestinationsNavHost
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -42,7 +41,6 @@ import li.songe.gkd.util.launchTry
import li.songe.gkd.util.map
import li.songe.gkd.util.storeFlow

@AndroidEntryPoint
class MainActivity : CompositionActivity({
this as MainActivity
useLifeCycleLog()
Expand Down Expand Up @@ -120,7 +118,8 @@ class MainActivity : CompositionActivity({
activityVisibleFlow.update { it - 1 }
}

var lastBackPressedTime = 0L
private var lastBackPressedTime = 0L

@Suppress("OVERRIDE_DEPRECATION")
override fun onBackPressed() {
// onBackPressedDispatcher.addCallback is not work, it will be covered by compose navigation
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/ActivityLogPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import com.ramcosta.composedestinations.annotation.Destination
Expand All @@ -52,7 +52,7 @@ import li.songe.gkd.util.throttle
fun ActivityLogPage() {
val context = LocalContext.current as MainActivity
val mainVm = context.mainVm
val vm = hiltViewModel<ActivityLogVm>()
val vm = viewModel<ActivityLogVm>()
val navController = LocalNavController.current

val logCount by vm.logCountFlow.collectAsState()
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/kotlin/li/songe/gkd/ui/ActivityLogVm.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package li.songe.gkd.ui

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.db.DbSet
import javax.inject.Inject

@HiltViewModel
class ActivityLogVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
class ActivityLogVm : ViewModel() {
val pagingDataFlow = Pager(PagingConfig(pageSize = 100)) { DbSet.activityLogDao.pagingSource() }
.flow.cachedIn(viewModelScope)

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.blankj.utilcode.util.LogUtils
import com.dylanc.activityresult.launcher.launchForResult
import com.ramcosta.composedestinations.annotation.Destination
Expand Down Expand Up @@ -107,7 +107,7 @@ import rikka.shizuku.Shizuku
@Composable
fun AdvancedPage() {
val context = LocalContext.current as MainActivity
val vm = hiltViewModel<AdvancedVm>()
val vm = viewModel<AdvancedVm>()
val launcher = LocalLauncher.current
val navController = LocalNavController.current
val store by storeFlow.collectAsState()
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ package li.songe.gkd.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.db.DbSet
import li.songe.gkd.ui.component.UploadOptions
import javax.inject.Inject

@HiltViewModel
class AdvancedVm @Inject constructor() : ViewModel() {
class AdvancedVm : ViewModel() {
val snapshotCountFlow =
DbSet.snapshotDao.count().stateIn(viewModelScope, SharingStarted.Eagerly, 0)

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/AppConfigPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.navigate
Expand Down Expand Up @@ -80,7 +80,7 @@ import li.songe.gkd.util.throttle
@Composable
fun AppConfigPage(appId: String) {
val navController = LocalNavController.current
val vm = hiltViewModel<AppConfigVm>()
val vm = viewModel<AppConfigVm>()
val ruleSortType by vm.ruleSortTypeFlow.collectAsState()
val appInfoCache by appInfoCacheFlow.collectAsState()
val appInfo = appInfoCache[appId]
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/AppConfigVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package li.songe.gkd.ui
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand All @@ -20,10 +19,8 @@ import li.songe.gkd.util.collator
import li.songe.gkd.util.getGroupRawEnable
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import javax.inject.Inject

@HiltViewModel
class AppConfigVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
class AppConfigVm (stateHandle: SavedStateHandle) : ViewModel() {
private val args = AppConfigPageDestination.argsFrom(stateHandle)

private val latestGlobalLogsFlow = DbSet.clickLogDao.queryAppLatest(
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/AppItemPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.LogUtils
import com.ramcosta.composedestinations.annotation.Destination
Expand All @@ -75,7 +75,6 @@ import li.songe.gkd.util.LocalMainViewModel
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.appInfoCacheFlow
import li.songe.json5.encodeToJson5String
import li.songe.gkd.util.getGroupRawEnable
import li.songe.gkd.util.json
import li.songe.gkd.util.launchAsFn
Expand All @@ -84,6 +83,7 @@ import li.songe.gkd.util.throttle
import li.songe.gkd.util.toast
import li.songe.gkd.util.updateSubscription
import li.songe.json5.Json5
import li.songe.json5.encodeToJson5String

@RootNavGraph
@Destination(style = ProfileTransitions::class)
Expand All @@ -95,7 +95,7 @@ fun AppItemPage(
) {
val navController = LocalNavController.current
val mainVm = LocalMainViewModel.current
val vm = hiltViewModel<AppItemVm>()
val vm = viewModel<AppItemVm>()
val subsItem = vm.subsItemFlow.collectAsState().value
val subsRaw = vm.subsRawFlow.collectAsState().value
val subsConfigs by vm.subsConfigsFlow.collectAsState()
Expand Down Expand Up @@ -285,7 +285,7 @@ fun AppItemPage(
},
)
}
if (editable && subsRaw != null) {
if (editable && subsRaw != null && subsItem != null) {
DropdownMenuItem(
text = {
Text(text = "删除", color = MaterialTheme.colorScheme.error)
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/AppItemVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package li.songe.gkd.ui
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
Expand All @@ -13,10 +12,8 @@ import li.songe.gkd.ui.destinations.AppItemPageDestination
import li.songe.gkd.util.map
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import javax.inject.Inject

@HiltViewModel
class AppItemVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
class AppItemVm (stateHandle: SavedStateHandle) : ViewModel() {
private val args = AppItemPageDestination.argsFrom(stateHandle)

val subsItemFlow =
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/CategoryPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -72,7 +72,7 @@ fun CategoryPage(subsItemId: Long) {
val navController = LocalNavController.current
val mainVm = LocalMainViewModel.current

val vm = hiltViewModel<CategoryVm>()
val vm = viewModel<CategoryVm>()
val subsItem by vm.subsItemFlow.collectAsState()
val subsRaw by vm.subsRawFlow.collectAsState()
val categoryConfigs by vm.categoryConfigsFlow.collectAsState()
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/CategoryVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ package li.songe.gkd.ui
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.db.DbSet
import li.songe.gkd.ui.destinations.CategoryPageDestination
import li.songe.gkd.util.map
import li.songe.gkd.util.subsIdToRawFlow
import li.songe.gkd.util.subsItemsFlow
import javax.inject.Inject

@HiltViewModel
class CategoryVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
class CategoryVm (stateHandle: SavedStateHandle) : ViewModel() {
private val args = CategoryPageDestination.argsFrom(stateHandle)

val subsItemFlow =
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/ui/ClickLogPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.itemKey
import com.ramcosta.composedestinations.annotation.Destination
Expand Down Expand Up @@ -78,7 +78,7 @@ fun ClickLogPage() {
val context = LocalContext.current as MainActivity
val mainVm = context.mainVm
val navController = LocalNavController.current
val vm = hiltViewModel<ClickLogVm>()
val vm = viewModel<ClickLogVm>()
val clickLogCount by vm.clickLogCountFlow.collectAsState()
val clickDataItems = vm.pagingDataFlow.collectAsLazyPagingItems()
val appInfoCache by appInfoCacheFlow.collectAsState()
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/ClickLogVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import androidx.paging.map
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.stateIn
import li.songe.gkd.data.SubsConfig
import li.songe.gkd.data.Tuple3
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.subsIdToRawFlow
import javax.inject.Inject

@HiltViewModel
class ClickLogVm @Inject constructor() : ViewModel() {
class ClickLogVm : ViewModel() {

val pagingDataFlow = Pager(PagingConfig(pageSize = 100)) { DbSet.clickLogDao.pagingSource() }
.flow.cachedIn(viewModelScope)
Expand Down
24 changes: 11 additions & 13 deletions app/src/main/kotlin/li/songe/gkd/ui/GlobalRuleExcludePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
Expand All @@ -84,7 +84,7 @@ import li.songe.gkd.util.toast
@Composable
fun GlobalRuleExcludePage(subsItemId: Long, groupKey: Int) {
val navController = LocalNavController.current
val vm = hiltViewModel<GlobalRuleExcludeVm>()
val vm = viewModel<GlobalRuleExcludeVm>()
val rawSubs = vm.rawSubsFlow.collectAsState().value
val group = vm.groupFlow.collectAsState().value
val excludeData = vm.excludeDataFlow.collectAsState().value
Expand Down Expand Up @@ -221,17 +221,15 @@ fun GlobalRuleExcludePage(subsItemId: Long, groupKey: Int) {
)
DropdownMenuItem(
text = {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = showHiddenApp,
onCheckedChange = {
vm.showHiddenAppFlow.value =
!vm.showHiddenAppFlow.value
})
Text("显示隐藏应用")
}
Text("显示隐藏应用")
},
trailingIcon = {
Checkbox(
checked = showHiddenApp,
onCheckedChange = {
vm.showHiddenAppFlow.value =
!vm.showHiddenAppFlow.value
})
},
onClick = {
vm.showHiddenAppFlow.value = !vm.showHiddenAppFlow.value
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/GlobalRuleExcludeVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package li.songe.gkd.ui
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand All @@ -17,10 +16,8 @@ import li.songe.gkd.util.SortTypeOption
import li.songe.gkd.util.map
import li.songe.gkd.util.orderedAppInfosFlow
import li.songe.gkd.util.subsIdToRawFlow
import javax.inject.Inject

@HiltViewModel
class GlobalRuleExcludeVm @Inject constructor(stateHandle: SavedStateHandle) : ViewModel() {
class GlobalRuleExcludeVm (stateHandle: SavedStateHandle) : ViewModel() {
private val args = GlobalRuleExcludePageDestination.argsFrom(stateHandle)

val rawSubsFlow = subsIdToRawFlow.map(viewModelScope) { it[args.subsItemId] }
Expand Down
Loading

0 comments on commit dbc4a28

Please sign in to comment.