@@ -9,6 +9,7 @@ import android.graphics.Canvas
9
9
import android.graphics.drawable.BitmapDrawable
10
10
import android.net.Uri
11
11
import android.provider.Settings
12
+ import androidx.compose.foundation.ExperimentalFoundationApi
12
13
import androidx.compose.foundation.Image
13
14
import androidx.compose.foundation.layout.Box
14
15
import androidx.compose.foundation.layout.Column
@@ -18,6 +19,8 @@ import androidx.compose.foundation.layout.padding
18
19
import androidx.compose.foundation.layout.size
19
20
import androidx.compose.foundation.lazy.LazyColumn
20
21
import androidx.compose.foundation.lazy.items
22
+ import androidx.compose.foundation.pager.HorizontalPager
23
+ import androidx.compose.foundation.pager.rememberPagerState
21
24
import androidx.compose.foundation.shape.RoundedCornerShape
22
25
import androidx.compose.material.icons.Icons
23
26
import androidx.compose.material.icons.outlined.MoreVert
@@ -36,9 +39,9 @@ import androidx.compose.runtime.Composable
36
39
import androidx.compose.runtime.LaunchedEffect
37
40
import androidx.compose.runtime.collectAsState
38
41
import androidx.compose.runtime.getValue
39
- import androidx.compose.runtime.mutableIntStateOf
40
42
import androidx.compose.runtime.mutableStateOf
41
43
import androidx.compose.runtime.remember
44
+ import androidx.compose.runtime.rememberCoroutineScope
42
45
import androidx.compose.runtime.setValue
43
46
import androidx.compose.ui.Alignment
44
47
import androidx.compose.ui.Modifier
@@ -54,22 +57,22 @@ import androidx.lifecycle.viewmodel.compose.viewModel
54
57
import com.d4rk.cleaner.R
55
58
import com.d4rk.cleaner.data.model.ui.ApkInfo
56
59
import com.d4rk.cleaner.utils.PermissionsUtils
60
+ import kotlinx.coroutines.launch
57
61
import java.io.File
58
62
59
63
/* *
60
64
* Composable function for managing and displaying different app categories.
61
65
*/
66
+ @OptIn(ExperimentalFoundationApi ::class )
62
67
@Composable
63
68
fun AppManagerComposable () {
64
-
65
69
val viewModel: AppManagerViewModel = viewModel(
66
70
factory = AppManagerViewModelFactory (LocalContext .current.applicationContext as Application )
67
71
)
68
72
val context = LocalContext .current
69
73
val tabs = listOf (" Installed Apps" , " System Apps" , " App Install Files" )
70
- var selectedIndex by remember { mutableIntStateOf(0 ) }
71
- val installedApps by viewModel.installedApps.collectAsState()
72
- val apkFiles by viewModel.apkFiles.collectAsState()
74
+ val pagerState = rememberPagerState(pageCount = { tabs.size })
75
+ val coroutineScope = rememberCoroutineScope()
73
76
74
77
LaunchedEffect (context) {
75
78
if (! PermissionsUtils .hasStoragePermissions(context)) {
@@ -79,11 +82,11 @@ fun AppManagerComposable() {
79
82
80
83
Column {
81
84
TabRow (
82
- selectedTabIndex = selectedIndex ,
85
+ selectedTabIndex = pagerState.currentPage ,
83
86
indicator = { tabPositions ->
84
- if (selectedIndex < tabPositions.size) {
87
+ if (pagerState.currentPage < tabPositions.size) {
85
88
TabRowDefaults .PrimaryIndicator (
86
- modifier = Modifier .tabIndicatorOffset(tabPositions[selectedIndex] ),
89
+ modifier = Modifier .tabIndicatorOffset(tabPositions[pagerState.currentPage]).fillMaxWidth( ),
87
90
shape = RoundedCornerShape (
88
91
topStart = 3 .dp, topEnd = 3 .dp, bottomEnd = 0 .dp, bottomStart = 0 .dp
89
92
),
@@ -92,22 +95,37 @@ fun AppManagerComposable() {
92
95
},
93
96
) {
94
97
tabs.forEachIndexed { index, title ->
95
- Tab (text = {
96
- Text (
97
- text = title,
98
- maxLines = 1 ,
99
- overflow = TextOverflow .Ellipsis ,
100
- color = MaterialTheme .colorScheme.onSurface
101
- )
102
- }, selected = selectedIndex == index, onClick = { selectedIndex = index })
98
+ Tab (
99
+ text = {
100
+ Text (
101
+ text = title,
102
+ maxLines = 1 ,
103
+ overflow = TextOverflow .Ellipsis ,
104
+ color = MaterialTheme .colorScheme.onSurface
105
+ )
106
+ },
107
+ selected = pagerState.currentPage == index,
108
+ onClick = {
109
+ coroutineScope.launch {
110
+ pagerState.animateScrollToPage(index)
111
+ }
112
+ }
113
+ )
103
114
}
104
115
}
105
- when (selectedIndex) {
106
- 0 -> AppsComposable (
107
- apps = installedApps.filter { it.flags and ApplicationInfo .FLAG_SYSTEM == 0 })
108
- 1 -> AppsComposable (
109
- apps = installedApps.filter { it.flags and ApplicationInfo .FLAG_SYSTEM != 0 })
110
- 2 -> ApksComposable (apkFiles = apkFiles)
116
+
117
+ HorizontalPager (
118
+ state = pagerState, // Only provide pagerState here
119
+ ) { page ->
120
+ when (page) {
121
+ 0 -> AppsComposable (
122
+ apps = viewModel.installedApps.collectAsState().value.filter { it.flags and ApplicationInfo .FLAG_SYSTEM == 0 }
123
+ )
124
+ 1 -> AppsComposable (
125
+ apps = viewModel.installedApps.collectAsState().value.filter { it.flags and ApplicationInfo .FLAG_SYSTEM != 0 }
126
+ )
127
+ 2 -> ApksComposable (apkFiles = viewModel.apkFiles.collectAsState().value)
128
+ }
111
129
}
112
130
}
113
131
}
0 commit comments