Skip to content

Commit 2bf1eb8

Browse files
committed
perf: use QS_TILE_URI
1 parent 6574f7b commit 2bf1eb8

File tree

8 files changed

+103
-92
lines changed

8 files changed

+103
-92
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@
189189
<meta-data
190190
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
191191
android:value="true" />
192+
<meta-data
193+
android:name="QS_TILE_URI"
194+
android:value="gkd://page" />
195+
192196
<intent-filter>
193197
<action android:name="android.service.quicksettings.action.QS_TILE" />
194198
</intent-filter>
@@ -199,6 +203,9 @@
199203
android:icon="@drawable/ic_capture"
200204
android:label="@string/capture_snapshot"
201205
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
206+
<meta-data
207+
android:name="QS_TILE_URI"
208+
android:value="gkd://page/2" />
202209
<intent-filter>
203210
<action android:name="android.service.quicksettings.action.QS_TILE" />
204211
</intent-filter>
@@ -212,6 +219,10 @@
212219
<meta-data
213220
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
214221
android:value="true" />
222+
<meta-data
223+
android:name="QS_TILE_URI"
224+
android:value="gkd://page/1" />
225+
215226
<intent-filter>
216227
<action android:name="android.service.quicksettings.action.QS_TILE" />
217228
</intent-filter>
@@ -225,6 +236,10 @@
225236
<meta-data
226237
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
227238
android:value="true" />
239+
<meta-data
240+
android:name="QS_TILE_URI"
241+
android:value="gkd://page/1" />
242+
228243
<intent-filter>
229244
<action android:name="android.service.quicksettings.action.QS_TILE" />
230245
</intent-filter>
@@ -238,6 +253,10 @@
238253
<meta-data
239254
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
240255
android:value="true" />
256+
<meta-data
257+
android:name="QS_TILE_URI"
258+
android:value="gkd://page?tab=1" />
259+
241260
<intent-filter>
242261
<action android:name="android.service.quicksettings.action.QS_TILE" />
243262
</intent-filter>
@@ -251,6 +270,10 @@
251270
<meta-data
252271
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
253272
android:value="true" />
273+
<meta-data
274+
android:name="QS_TILE_URI"
275+
android:value="gkd://page/1" />
276+
254277
<intent-filter>
255278
<action android:name="android.service.quicksettings.action.QS_TILE" />
256279
</intent-filter>

app/src/main/kotlin/li/songe/gkd/MainViewModel.kt

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.ramcosta.composedestinations.spec.Direction
1818
import io.ktor.client.request.get
1919
import io.ktor.client.statement.bodyAsText
2020
import kotlinx.coroutines.Dispatchers
21-
import kotlinx.coroutines.delay
2221
import kotlinx.coroutines.flow.MutableStateFlow
2322
import kotlinx.coroutines.flow.update
2423
import kotlinx.coroutines.launch
@@ -30,27 +29,17 @@ import li.songe.gkd.data.importData
3029
import li.songe.gkd.db.DbSet
3130
import li.songe.gkd.permission.AuthReason
3231
import li.songe.gkd.permission.shizukuOkState
33-
import li.songe.gkd.service.ButtonTileService
34-
import li.songe.gkd.service.HttpTileService
35-
import li.songe.gkd.service.MatchTileService
36-
import li.songe.gkd.service.RecordTileService
37-
import li.songe.gkd.service.SnapshotTileService
3832
import li.songe.gkd.shizuku.execCommandForResult
3933
import li.songe.gkd.store.createTextFlow
4034
import li.songe.gkd.ui.component.AlertDialogOptions
4135
import li.songe.gkd.ui.component.InputSubsLinkOption
4236
import li.songe.gkd.ui.component.RuleGroupState
4337
import li.songe.gkd.ui.component.UploadOptions
4438
import li.songe.gkd.ui.home.BottomNavItem
45-
import li.songe.gkd.ui.home.appListNav
46-
import li.songe.gkd.ui.home.controlNav
47-
import li.songe.gkd.ui.home.subsNav
4839
import li.songe.gkd.util.LOCAL_SUBS_ID
4940
import li.songe.gkd.util.UpdateStatus
5041
import li.songe.gkd.util.clearCache
5142
import li.songe.gkd.util.client
52-
import li.songe.gkd.util.componentName
53-
import li.songe.gkd.util.extraCptName
5443
import li.songe.gkd.util.launchTry
5544
import li.songe.gkd.util.openUri
5645
import li.songe.gkd.util.openWeChatScaner
@@ -167,16 +156,16 @@ class MainViewModel : ViewModel() {
167156
}
168157

169158
val appListKeyFlow = MutableStateFlow(0)
170-
val tabFlow = MutableStateFlow(controlNav)
159+
val tabFlow = MutableStateFlow(BottomNavItem.Control.key)
171160
private var lastClickTabTime = 0L
172161
fun updateTab(navItem: BottomNavItem) {
173-
if (navItem == appListNav && navItem == tabFlow.value) {
162+
if (navItem == BottomNavItem.AppList && navItem.key == tabFlow.value) {
174163
// double click
175164
if (System.currentTimeMillis() - lastClickTabTime < 500) {
176165
appListKeyFlow.update { it + 1 }
177166
}
178167
}
179-
tabFlow.value = navItem
168+
tabFlow.value = navItem.key
180169
lastClickTabTime = System.currentTimeMillis()
181170
}
182171

@@ -205,7 +194,13 @@ class MainViewModel : ViewModel() {
205194
val notFoundToast = { toast("未知URI\n${uri}") }
206195
when (uri.host) {
207196
"page" -> when (uri.path) {
208-
"" -> {}
197+
"" -> {
198+
val tab = uri.getQueryParameter("tab")?.toIntOrNull()
199+
if (tab != null && BottomNavItem.allSubObjects.any { it.key == tab }) {
200+
tabFlow.value = tab
201+
}
202+
}
203+
209204
"/1" -> navigatePage(AdvancedPageDestination)
210205
"/2" -> navigatePage(SnapshotPageDestination())
211206
else -> notFoundToast()
@@ -222,42 +217,14 @@ class MainViewModel : ViewModel() {
222217

223218
fun handleIntent(intent: Intent) = viewModelScope.launchTry {
224219
LogUtils.d("handleIntent", intent)
225-
val sourceName = intent.getStringExtra(activityNavSourceName)
226220
val uri = intent.data?.normalizeScheme()
227-
when (sourceName) {
228-
OpenSchemeActivity::class.jvmName -> {
229-
if (uri?.scheme == "gkd") {
230-
delay(200)
231-
handleGkdUri(uri)
232-
}
233-
}
234-
235-
OpenFileActivity::class.jvmName -> {
236-
if (uri != null) {
237-
toast("加载导入中...")
238-
tabFlow.value = subsNav
239-
withContext(Dispatchers.IO) { importData(uri) }
240-
}
241-
}
242-
243-
OpenTileActivity::class.jvmName -> {
244-
val qsTileCpt = intent.extraCptName
245-
when (qsTileCpt) {
246-
HttpTileService::class.componentName, ButtonTileService::class.componentName, RecordTileService::class.componentName -> {
247-
delay(200)
248-
navigatePage(AdvancedPageDestination)
249-
}
250-
251-
SnapshotTileService::class.componentName -> {
252-
delay(200)
253-
navigatePage(SnapshotPageDestination)
254-
}
255-
256-
MatchTileService::class.componentName -> {
257-
tabFlow.value = subsNav
258-
}
259-
}
260-
}
221+
val source = intent.getStringExtra(activityNavSourceName)
222+
if (uri?.scheme == "gkd") {
223+
handleGkdUri(uri)
224+
} else if (source == OpenFileActivity::class.jvmName && uri != null) {
225+
toast("加载导入中...")
226+
tabFlow.value = BottomNavItem.SubsManage.key
227+
withContext(Dispatchers.IO) { importData(uri) }
261228
}
262229
}
263230

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
package li.songe.gkd
22

33
import android.app.Activity
4+
import android.content.pm.PackageManager
45
import android.os.Bundle
6+
import androidx.core.net.toUri
7+
import li.songe.gkd.util.extraCptName
58

69
class OpenTileActivity : Activity() {
710
override fun onCreate(savedInstanceState: Bundle?) {
811
super.onCreate(savedInstanceState)
12+
val qsTileCpt = intent?.extraCptName
13+
if (qsTileCpt != null && intent.data == null) {
14+
val serviceInfo =
15+
app.packageManager.getServiceInfo(qsTileCpt, PackageManager.GET_META_DATA)
16+
val uriValue = serviceInfo.metaData.getString("QS_TILE_URI")
17+
if (uriValue != null) {
18+
intent.data = uriValue.toUri()
19+
}
20+
}
921
navToMainActivity()
1022
}
1123
}

app/src/main/kotlin/li/songe/gkd/ui/home/AppListPage.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import androidx.compose.foundation.lazy.LazyColumn
1616
import androidx.compose.foundation.lazy.items
1717
import androidx.compose.material.icons.Icons
1818
import androidx.compose.material.icons.automirrored.filled.Sort
19-
import androidx.compose.material.icons.filled.Apps
2019
import androidx.compose.material3.Checkbox
2120
import androidx.compose.material3.DropdownMenu
2221
import androidx.compose.material3.DropdownMenuItem
@@ -64,10 +63,6 @@ import li.songe.gkd.util.mapHashCode
6463
import li.songe.gkd.util.ruleSummaryFlow
6564
import li.songe.gkd.util.throttle
6665

67-
val appListNav = BottomNavItem(
68-
label = "应用", icon = Icons.Default.Apps
69-
)
70-
7166
@Composable
7267
fun useAppListPage(): ScaffoldExt {
7368
val context = LocalActivity.current as MainActivity
@@ -92,7 +87,7 @@ fun useAppListPage(): ScaffoldExt {
9287
val resetKey = orderedAppInfos.mapHashCode { it.id }
9388
val (scrollBehavior, listState) = useListScrollState(resetKey, appListKey)
9489
return ScaffoldExt(
95-
navItem = appListNav,
90+
navItem = BottomNavItem.AppList,
9691
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
9792
topBar = {
9893
DisposableEffect(null) {
@@ -126,7 +121,7 @@ fun useAppListPage(): ScaffoldExt {
126121
mainVm.appListKeyFlow.update { it + 1 }
127122
}
128123
),
129-
text = appListNav.label,
124+
text = BottomNavItem.AppList.label,
130125
)
131126
}
132127
}, actions = {

app/src/main/kotlin/li/songe/gkd/ui/home/ControlPage.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import androidx.compose.material.icons.automirrored.outlined.HelpOutline
2222
import androidx.compose.material.icons.filled.History
2323
import androidx.compose.material.icons.filled.Memory
2424
import androidx.compose.material.icons.outlined.Equalizer
25-
import androidx.compose.material.icons.outlined.Home
2625
import androidx.compose.material.icons.outlined.Layers
2726
import androidx.compose.material.icons.outlined.Notifications
2827
import androidx.compose.material.icons.outlined.RocketLaunch
@@ -72,8 +71,6 @@ import li.songe.gkd.util.SafeR
7271
import li.songe.gkd.util.launchAsFn
7372
import li.songe.gkd.util.throttle
7473

75-
val controlNav = BottomNavItem(label = "主页", icon = Icons.Outlined.Home)
76-
7774
@Composable
7875
fun useControlPage(): ScaffoldExt {
7976
val context = LocalActivity.current as MainActivity
@@ -83,7 +80,7 @@ fun useControlPage(): ScaffoldExt {
8380
val scrollState = rememberScrollState()
8481
val writeSecureSettings by writeSecureSettingsState.stateFlow.collectAsState()
8582
return ScaffoldExt(
86-
navItem = controlNav,
83+
navItem = BottomNavItem.Control,
8784
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
8885
topBar = {
8986
TopAppBar(scrollBehavior = scrollBehavior, title = {

app/src/main/kotlin/li/songe/gkd/ui/home/HomePage.kt

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package li.songe.gkd.ui.home
22

3+
import androidx.compose.material.icons.Icons
4+
import androidx.compose.material.icons.automirrored.filled.FormatListBulleted
5+
import androidx.compose.material.icons.filled.Apps
6+
import androidx.compose.material.icons.outlined.Home
7+
import androidx.compose.material.icons.outlined.Settings
38
import androidx.compose.material3.Icon
49
import androidx.compose.material3.NavigationBar
510
import androidx.compose.material3.NavigationBarItem
@@ -16,36 +21,58 @@ import com.ramcosta.composedestinations.annotation.RootGraph
1621
import li.songe.gkd.ui.share.LocalMainViewModel
1722
import li.songe.gkd.ui.style.ProfileTransitions
1823

19-
data class BottomNavItem(
24+
sealed class BottomNavItem(
25+
val key: Int,
2026
val label: String,
2127
val icon: ImageVector,
22-
)
28+
) {
29+
object Control : BottomNavItem(
30+
key = 0,
31+
label = "主页",
32+
icon = Icons.Outlined.Home,
33+
)
34+
35+
object SubsManage : BottomNavItem(
36+
key = 1,
37+
label = "订阅",
38+
icon = Icons.AutoMirrored.Filled.FormatListBulleted,
39+
)
40+
41+
object AppList : BottomNavItem(
42+
key = 2,
43+
label = "应用",
44+
icon = Icons.Default.Apps,
45+
)
46+
47+
object Settings : BottomNavItem(
48+
key = 3,
49+
label = "设置",
50+
icon = Icons.Outlined.Settings,
51+
)
52+
53+
companion object {
54+
val allSubObjects by lazy { arrayOf(Control, SubsManage, AppList, Settings) }
55+
}
56+
}
2357

2458
@Destination<RootGraph>(style = ProfileTransitions::class, start = true)
2559
@Composable
2660
fun HomePage() {
2761
val mainVm = LocalMainViewModel.current
28-
viewModel<HomeVm>()
62+
viewModel<HomeVm>() // init state
2963
val tab by mainVm.tabFlow.collectAsState()
30-
31-
val controlPage = useControlPage()
32-
val subsPage = useSubsManagePage()
33-
val appListPage = useAppListPage()
34-
val settingsPage = useSettingsPage()
35-
36-
val pages = arrayOf(controlPage, subsPage, appListPage, settingsPage)
37-
38-
val currentPage = pages.find { p -> p.navItem.label == tab.label } ?: controlPage
64+
val pages = arrayOf(useControlPage(), useSubsManagePage(), useAppListPage(), useSettingsPage())
65+
val page = pages.find { p -> p.navItem.key == tab } ?: pages.first()
3966

4067
Scaffold(
41-
modifier = currentPage.modifier,
42-
topBar = currentPage.topBar,
43-
floatingActionButton = currentPage.floatingActionButton,
68+
modifier = page.modifier,
69+
topBar = page.topBar,
70+
floatingActionButton = page.floatingActionButton,
4471
bottomBar = {
4572
NavigationBar {
4673
pages.forEach { page ->
4774
NavigationBarItem(
48-
selected = tab.label == page.navItem.label,
75+
selected = page.navItem.key == tab,
4976
modifier = Modifier,
5077
onClick = {
5178
mainVm.updateTab(page.navItem)
@@ -62,6 +89,6 @@ fun HomePage() {
6289
}
6390
}
6491
},
65-
content = currentPage.content
92+
content = page.content
6693
)
6794
}

0 commit comments

Comments
 (0)