Skip to content

Commit

Permalink
perf: remove useless code
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Sep 2, 2024
1 parent b12bf93 commit af97a9d
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 174 deletions.
105 changes: 66 additions & 39 deletions app/src/main/kotlin/li/songe/gkd/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import android.app.Activity
import android.app.ActivityManager
import android.content.ComponentName
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.animation.core.AnimationConstants
import androidx.compose.runtime.CompositionLocalProvider
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.rememberNavController
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ServiceUtils
import com.dylanc.activityresult.launcher.PickContentLauncher
import com.dylanc.activityresult.launcher.StartActivityLauncher
Expand All @@ -18,8 +26,6 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import li.songe.gkd.composition.CompositionActivity
import li.songe.gkd.composition.CompositionExt.useLifeCycleLog
import li.songe.gkd.debug.FloatingService
import li.songe.gkd.debug.HttpService
import li.songe.gkd.debug.ScreenshotService
Expand All @@ -31,58 +37,58 @@ import li.songe.gkd.service.updateLauncherAppId
import li.songe.gkd.ui.NavGraphs
import li.songe.gkd.ui.component.BuildDialog
import li.songe.gkd.ui.theme.AppTheme
import li.songe.gkd.util.LocalLauncher
import li.songe.gkd.util.LocalMainViewModel
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.LocalPickContentLauncher
import li.songe.gkd.util.UpgradeDialog
import li.songe.gkd.util.initFolder
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.map
import li.songe.gkd.util.storeFlow

class MainActivity : CompositionActivity({
this as MainActivity
useLifeCycleLog()
val launcher = StartActivityLauncher(this)
val pickContentLauncher = PickContentLauncher(this)

lifecycleScope.launch {
storeFlow.map(lifecycleScope) { s -> s.excludeFromRecents }.collect {
(app.getSystemService(ACTIVITY_SERVICE) as ActivityManager).let { manager ->
manager.appTasks.forEach { task ->
task?.setExcludeFromRecents(it)
class MainActivity : ComponentActivity() {
val mainVm by viewModels<MainViewModel>()
val launcher by lazy { StartActivityLauncher(this) }
val pickContentLauncher by lazy { PickContentLauncher(this) }

override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
enableEdgeToEdge()
fixTopPadding()
super.onCreate(savedInstanceState)

lifecycleScope.launch {
storeFlow.map(lifecycleScope) { s -> s.excludeFromRecents }.collect {
(app.getSystemService(ACTIVITY_SERVICE) as ActivityManager).let { manager ->
manager.appTasks.forEach { task ->
task?.setExcludeFromRecents(it)
}
}
}
}
}

ManageService.autoStart(this)
mainVm

setContent {
val navController = rememberNavController()
AppTheme {
CompositionLocalProvider(
LocalLauncher provides launcher,
LocalPickContentLauncher provides pickContentLauncher,
LocalNavController provides navController,
LocalMainViewModel provides mainVm
) {
DestinationsNavHost(
navGraph = NavGraphs.root,
navController = navController
)
AuthDialog(mainVm.authReasonFlow)
BuildDialog(mainVm.dialogFlow)
if (BuildConfig.ENABLED_UPDATE) {
UpgradeDialog(mainVm.updateStatus)
mainVm
launcher
pickContentLauncher
ManageService.autoStart(this)

setContent {
val navController = rememberNavController()
AppTheme {
CompositionLocalProvider(
LocalNavController provides navController
) {
DestinationsNavHost(
navGraph = NavGraphs.root,
navController = navController
)
AuthDialog(mainVm.authReasonFlow)
BuildDialog(mainVm.dialogFlow)
if (BuildConfig.ENABLED_UPDATE) {
UpgradeDialog(mainVm.updateStatus)
}
}
}
}
}
}) {
val mainVm by viewModels<MainViewModel>()

override fun onResume() {
super.onResume()
Expand Down Expand Up @@ -154,3 +160,24 @@ private fun updateServiceRunning() {
ScreenshotService.isRunning.value = ServiceUtils.isServiceRunning(ScreenshotService::class.java)
HttpService.isRunning.value = ServiceUtils.isServiceRunning(HttpService::class.java)
}

private fun Activity.fixTopPadding() {
// 当调用系统分享时, 会导致状态栏区域消失, 应用整体上移, 设置一个 top padding 保证不上移
var tempTop: Int? = null
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { view, windowInsets ->
view.setBackgroundColor(Color.TRANSPARENT)
val statusBars = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
if (statusBars.top == 0) {
view.setPadding(
statusBars.left,
tempTop ?: BarUtils.getStatusBarHeight(),
statusBars.right,
statusBars.bottom
)
} else {
tempTop = statusBars.top
view.setPadding(statusBars.left, 0, statusBars.right, statusBars.bottom)
}
ViewCompat.onApplyWindowInsets(view, windowInsets)
}
}

This file was deleted.

3 changes: 0 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/composition/Typealias.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package li.songe.gkd.composition

import android.content.Context
import android.content.Intent
import com.torrydo.floatingbubbleview.service.expandable.BubbleBuilder

typealias StartCommandHook = (intent: Intent?, flags: Int, startId: Int) -> Unit

typealias onReceiveType = (Context?, Intent?) -> Unit

typealias ConfigBubbleHook = ((BubbleBuilder) -> Unit) -> Unit
4 changes: 1 addition & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/AdvancedPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ import li.songe.gkd.ui.destinations.SnapshotPageDestination
import li.songe.gkd.ui.style.EmptyHeight
import li.songe.gkd.ui.style.itemPadding
import li.songe.gkd.ui.style.titleItemPadding
import li.songe.gkd.util.LocalLauncher
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.appInfoCacheFlow
Expand All @@ -111,7 +110,6 @@ import rikka.shizuku.Shizuku
fun AdvancedPage() {
val context = LocalContext.current as MainActivity
val vm = viewModel<AdvancedVm>()
val launcher = LocalLauncher.current
val navController = LocalNavController.current
val store by storeFlow.collectAsState()
val snapshotCount by vm.snapshotCountFlow.collectAsState()
Expand Down Expand Up @@ -443,7 +441,7 @@ fun AdvancedPage() {
val mediaProjectionManager =
context.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
val activityResult =
launcher.launchForResult(mediaProjectionManager.createScreenCaptureIntent())
context.launcher.launchForResult(mediaProjectionManager.createScreenCaptureIntent())
if (activityResult.resultCode == Activity.RESULT_OK && activityResult.data != null) {
ScreenshotService.start(intent = activityResult.data!!)
}
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/AppConfigPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
Expand All @@ -52,6 +53,7 @@ import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph
import com.ramcosta.composedestinations.navigation.navigate
import kotlinx.coroutines.flow.update
import li.songe.gkd.MainActivity
import li.songe.gkd.data.ExcludeData
import li.songe.gkd.data.RawSubscription
import li.songe.gkd.data.SubsConfig
Expand All @@ -66,7 +68,6 @@ import li.songe.gkd.ui.style.itemVerticalPadding
import li.songe.gkd.ui.style.menuPadding
import li.songe.gkd.ui.style.titleItemPadding
import li.songe.gkd.util.LOCAL_SUBS_ID
import li.songe.gkd.util.LocalMainViewModel
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.ResolvedGroup
Expand Down Expand Up @@ -299,7 +300,7 @@ private fun AppGroupCard(
onClick: () -> Unit,
onCheckedChange: ((Boolean) -> Unit)?,
) {
val mainVm = LocalMainViewModel.current
val context = LocalContext.current as MainActivity
Row(
modifier = Modifier
.clickable(onClick = onClick)
Expand Down Expand Up @@ -361,7 +362,7 @@ private fun AppGroupCard(
interactionSource = remember { MutableInteractionSource() },
indication = null,
) {
mainVm.dialogFlow.updateDialogOptions(
context.mainVm.dialogFlow.updateDialogOptions(
title = "内置禁用",
text = "此规则组已经在其 apps 字段中配置对当前应用的禁用, 因此无法手动开启规则组\n\n提示: 这种情况一般在此全局规则无法适配/跳过适配/单独适配当前应用时出现",
)
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/kotlin/li/songe/gkd/ui/AppItemPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
Expand All @@ -61,6 +62,7 @@ import com.ramcosta.composedestinations.navigation.navigate
import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.json.JsonArray
import kotlinx.serialization.json.jsonObject
import li.songe.gkd.MainActivity
import li.songe.gkd.data.ExcludeData
import li.songe.gkd.data.RawSubscription
import li.songe.gkd.data.SubsConfig
Expand All @@ -71,7 +73,6 @@ import li.songe.gkd.ui.component.waitResult
import li.songe.gkd.ui.destinations.GroupImagePageDestination
import li.songe.gkd.ui.style.EmptyHeight
import li.songe.gkd.ui.style.itemPadding
import li.songe.gkd.util.LocalMainViewModel
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.appInfoCacheFlow
Expand All @@ -93,8 +94,8 @@ fun AppItemPage(
appId: String,
focusGroupKey: Int? = null, // 背景/边框高亮一下
) {
val context = LocalContext.current as MainActivity
val navController = LocalNavController.current
val mainVm = LocalMainViewModel.current
val vm = viewModel<AppItemVm>()
val subsItem = vm.subsItemFlow.collectAsState().value
val subsRaw = vm.subsRawFlow.collectAsState().value
Expand Down Expand Up @@ -293,7 +294,7 @@ fun AppItemPage(
onClick = {
expanded = false
vm.viewModelScope.launchTry {
mainVm.dialogFlow.waitResult(
context.mainVm.dialogFlow.waitResult(
title = "删除规则组",
text = "确定删除规则组 ${group.name} ?",
error = true,
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/kotlin/li/songe/gkd/ui/CategoryPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
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
import li.songe.gkd.MainActivity
import li.songe.gkd.data.CategoryConfig
import li.songe.gkd.data.RawSubscription
import li.songe.gkd.db.DbSet
Expand All @@ -56,7 +58,6 @@ import li.songe.gkd.ui.component.waitResult
import li.songe.gkd.ui.style.EmptyHeight
import li.songe.gkd.ui.style.itemPadding
import li.songe.gkd.util.EnableGroupOption
import li.songe.gkd.util.LocalMainViewModel
import li.songe.gkd.util.LocalNavController
import li.songe.gkd.util.ProfileTransitions
import li.songe.gkd.util.findOption
Expand All @@ -69,8 +70,8 @@ import li.songe.gkd.util.updateSubscription
@Destination(style = ProfileTransitions::class)
@Composable
fun CategoryPage(subsItemId: Long) {
val context = LocalContext.current as MainActivity
val navController = LocalNavController.current
val mainVm = LocalMainViewModel.current

val vm = viewModel<CategoryVm>()
val subsItem by vm.subsItemFlow.collectAsState()
Expand Down Expand Up @@ -107,7 +108,7 @@ fun CategoryPage(subsItemId: Long) {
)
}, actions = {
IconButton(onClick = throttle {
mainVm.dialogFlow.updateDialogOptions(
context.mainVm.dialogFlow.updateDialogOptions(
title = "开关优先级",
text = "规则手动配置 > 分类手动配置 > 分类默认 > 规则默认\n\n重置开关: 移除规则手动配置",
)
Expand Down Expand Up @@ -202,7 +203,7 @@ fun CategoryPage(subsItemId: Long) {
}, onClick = {
expanded = false
vm.viewModelScope.launchTry {
mainVm.dialogFlow.waitResult(
context.mainVm.dialogFlow.waitResult(
title = "删除类别",
text = "确定删除 ${category.name} ?",
error = true,
Expand Down
Loading

0 comments on commit af97a9d

Please sign in to comment.