1
+ @file:OptIn(ExperimentalMaterial3Api ::class )
2
+
1
3
package com.huhx.picker.view
2
4
3
5
import android.os.Build
4
6
import androidx.compose.foundation.background
5
7
import androidx.compose.foundation.layout.Arrangement
6
8
import androidx.compose.foundation.layout.Box
9
+ import androidx.compose.foundation.layout.Column
7
10
import androidx.compose.foundation.layout.PaddingValues
8
11
import androidx.compose.foundation.layout.Row
9
12
import androidx.compose.foundation.layout.Spacer
10
13
import androidx.compose.foundation.layout.defaultMinSize
11
14
import androidx.compose.foundation.layout.fillMaxSize
12
15
import androidx.compose.foundation.layout.fillMaxWidth
13
16
import androidx.compose.foundation.layout.padding
17
+ import androidx.compose.foundation.layout.size
14
18
import androidx.compose.foundation.layout.statusBarsPadding
15
19
import androidx.compose.foundation.layout.width
20
+ import androidx.compose.foundation.lazy.LazyRow
21
+ import androidx.compose.foundation.lazy.itemsIndexed
16
22
import androidx.compose.foundation.pager.HorizontalPager
17
23
import androidx.compose.foundation.pager.PagerState
18
24
import androidx.compose.foundation.pager.rememberPagerState
@@ -24,12 +30,14 @@ import androidx.compose.material3.CenterAlignedTopAppBar
24
30
import androidx.compose.material3.ExperimentalMaterial3Api
25
31
import androidx.compose.material3.Icon
26
32
import androidx.compose.material3.IconButton
33
+ import androidx.compose.material3.MaterialTheme
27
34
import androidx.compose.material3.Scaffold
28
35
import androidx.compose.material3.Text
29
36
import androidx.compose.material3.TopAppBarDefaults
30
37
import androidx.compose.runtime.Composable
31
38
import androidx.compose.runtime.DisposableEffect
32
39
import androidx.compose.runtime.remember
40
+ import androidx.compose.runtime.rememberCoroutineScope
33
41
import androidx.compose.runtime.snapshots.SnapshotStateList
34
42
import androidx.compose.ui.Alignment
35
43
import androidx.compose.ui.Modifier
@@ -52,22 +60,64 @@ import coil.decode.GifDecoder
52
60
import coil.decode.ImageDecoderDecoder
53
61
import coil.request.ImageRequest
54
62
import com.huhx.picker.R
63
+ import com.huhx.picker.component.SelectedAssetImageItem
55
64
import com.huhx.picker.model.AssetInfo
65
+ import kotlinx.coroutines.launch
56
66
57
67
@UnstableApi
58
68
@Composable
59
- internal fun AssetPreviewScreen (
69
+ fun AssetPreviewScreen (
60
70
index : Int ,
61
71
assets : List <AssetInfo >,
62
72
navigateUp : () -> Unit ,
63
73
selectedList : SnapshotStateList <AssetInfo >,
64
74
) {
65
75
val pageState = rememberPagerState(initialPage = index, pageCount = assets::size)
76
+ val scope = rememberCoroutineScope()
77
+ val titleString = if (assets[pageState.currentPage].isImage()) " 图片" else " 视频"
66
78
67
79
Scaffold (
68
- topBar = { PreviewTopAppBar (navigateUp = navigateUp) },
80
+ topBar = {
81
+ CenterAlignedTopAppBar (
82
+ modifier = Modifier .statusBarsPadding(),
83
+ title = {
84
+ Column (horizontalAlignment = Alignment .CenterHorizontally ) {
85
+ Text (
86
+ text = titleString,
87
+ style = MaterialTheme .typography.bodyLarge.copy(fontSize = 18 .sp, color = Color .White )
88
+ )
89
+ Text (
90
+ text = assets[pageState.currentPage].dateString,
91
+ style = MaterialTheme .typography.bodyMedium.copy(color = Color .Gray )
92
+ )
93
+ }
94
+ },
95
+ colors = TopAppBarDefaults .centerAlignedTopAppBarColors(containerColor = Color .Black ),
96
+ navigationIcon = {
97
+ IconButton (onClick = navigateUp) {
98
+ Icon (Icons .AutoMirrored .Filled .ArrowBack , tint = Color .White , contentDescription = " " )
99
+ }
100
+ },
101
+ actions = {
102
+ Text (
103
+ modifier = Modifier .padding(horizontal = 4 .dp),
104
+ text = " ${pageState.currentPage + 1 } /${assets.size} " ,
105
+ style = MaterialTheme .typography.bodyLarge.copy(fontSize = 18 .sp, color = Color .White )
106
+ )
107
+ },
108
+ )
109
+ },
69
110
bottomBar = {
70
- SelectorBottomBar (selectedList = selectedList, assetInfo = assets[pageState.currentPage]) {
111
+ SelectorBottomBar (
112
+ selectedList = selectedList,
113
+ assetInfo = assets[pageState.currentPage],
114
+ onSelectedClick = { asset ->
115
+ val selectedIndex = assets.indexOfFirst { it.id == asset.id }
116
+ scope.launch {
117
+ pageState.animateScrollToPage(selectedIndex)
118
+ }
119
+ }
120
+ ) {
71
121
navigateUp()
72
122
if (selectedList.isEmpty()) selectedList.add(it)
73
123
}
@@ -83,54 +133,62 @@ internal fun AssetPreviewScreen(
83
133
}
84
134
}
85
135
86
- @OptIn(ExperimentalMaterial3Api ::class )
87
- @Composable
88
- private fun PreviewTopAppBar (navigateUp : () -> Unit ) {
89
- CenterAlignedTopAppBar (
90
- modifier = Modifier .statusBarsPadding(),
91
- title = {},
92
- colors = TopAppBarDefaults .centerAlignedTopAppBarColors(containerColor = Color .Black ),
93
- navigationIcon = {
94
- IconButton (onClick = navigateUp) {
95
- Icon (Icons .AutoMirrored .Filled .ArrowBack , tint = Color .White , contentDescription = " " )
96
- }
97
- }
98
- )
99
- }
100
-
136
+ @UnstableApi
101
137
@Composable
102
- private fun SelectorBottomBar (
138
+ fun SelectorBottomBar (
103
139
assetInfo : AssetInfo ,
104
140
selectedList : SnapshotStateList <AssetInfo >,
141
+ onSelectedClick : (AssetInfo ) -> Unit ,
105
142
onClick : (AssetInfo ) -> Unit ,
106
143
) {
107
- Row (
108
- modifier = Modifier
109
- .fillMaxWidth()
110
- .background(color = Color . Black .copy(alpha = 0.9F ))
111
- .padding(horizontal = 10 .dp, vertical = 8 .dp),
112
- horizontalArrangement = Arrangement . SpaceBetween ,
113
- verticalAlignment = Alignment . CenterVertically
114
- ) {
115
- Row (verticalAlignment = Alignment . CenterVertically ) {
116
- AssetImageIndicator (
117
- assetInfo = assetInfo ,
118
- selected = selectedList.any { it == assetInfo } ,
119
- assetSelected = selectedList ,
120
- fontSize = 14 .sp ,
121
- size = 22 .dp
122
- )
123
- Spacer (modifier = Modifier .width( 6 .dp))
124
- Text (text = stringResource( R .string.text_asset_select), color = Color . White , fontSize = 14 .sp)
144
+ Column {
145
+ if (selectedList.isNotEmpty()) {
146
+ LazyRow (
147
+ modifier = Modifier
148
+ .fillMaxWidth()
149
+ .background( Color . Black .copy(alpha = 0.8F ))
150
+ .padding(horizontal = 2 .dp, vertical = 2 .dp)
151
+ ) {
152
+ itemsIndexed(selectedList ) { _, resource ->
153
+ SelectedAssetImageItem (
154
+ assetInfo = resource ,
155
+ isSelected = resource.id == assetInfo.id ,
156
+ resourceType = resource.resourceType ,
157
+ modifier = Modifier .size( 64 .dp) ,
158
+ onClick = onSelectedClick
159
+ )
160
+ }
161
+ }
125
162
}
126
- Button (
127
- modifier = Modifier .defaultMinSize(minHeight = 1 .dp, minWidth = 1 .dp),
128
- shape = RoundedCornerShape (5 .dp),
129
- enabled = true ,
130
- contentPadding = PaddingValues (horizontal = 20 .dp, vertical = 6 .dp),
131
- onClick = { onClick(assetInfo) }
163
+
164
+ Row (
165
+ modifier = Modifier
166
+ .fillMaxWidth()
167
+ .background(color = Color .Black .copy(alpha = 0.9F ))
168
+ .padding(horizontal = 10 .dp, vertical = 8 .dp),
169
+ horizontalArrangement = Arrangement .SpaceBetween ,
170
+ verticalAlignment = Alignment .CenterVertically
132
171
) {
133
- Text (stringResource(R .string.text_done), color = Color .White , fontSize = 15 .sp)
172
+ Row (verticalAlignment = Alignment .CenterVertically ) {
173
+ AssetImageIndicator (
174
+ assetInfo = assetInfo,
175
+ selected = selectedList.any { it == assetInfo },
176
+ assetSelected = selectedList,
177
+ fontSize = 14 .sp,
178
+ size = 22 .dp
179
+ )
180
+ Spacer (modifier = Modifier .width(6 .dp))
181
+ Text (text = stringResource(R .string.text_asset_select), color = Color .White , fontSize = 14 .sp)
182
+ }
183
+ Button (
184
+ modifier = Modifier .defaultMinSize(minHeight = 1 .dp, minWidth = 1 .dp),
185
+ shape = RoundedCornerShape (5 .dp),
186
+ enabled = true ,
187
+ contentPadding = PaddingValues (horizontal = 20 .dp, vertical = 6 .dp),
188
+ onClick = { onClick(assetInfo) }
189
+ ) {
190
+ Text (stringResource(R .string.text_done), color = Color .White , fontSize = 15 .sp)
191
+ }
134
192
}
135
193
}
136
194
}
0 commit comments