Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit 127a46f

Browse files
committed
Optimize RepositoriesScreen
1 parent f1e80a3 commit 127a46f

File tree

4 files changed

+98
-120
lines changed

4 files changed

+98
-120
lines changed

app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/settings/repositories/RepositoriesList.kt

+7-54
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@ package dev.sanmer.mrepo.ui.screens.settings.repositories
33
import androidx.compose.foundation.layout.Arrangement
44
import androidx.compose.foundation.layout.PaddingValues
55
import androidx.compose.foundation.layout.fillMaxSize
6-
import androidx.compose.foundation.layout.requiredHeightIn
76
import androidx.compose.foundation.lazy.LazyColumn
87
import androidx.compose.foundation.lazy.LazyListState
98
import androidx.compose.foundation.lazy.items
10-
import androidx.compose.foundation.rememberScrollState
119
import androidx.compose.foundation.shape.RoundedCornerShape
12-
import androidx.compose.foundation.verticalScroll
1310
import androidx.compose.material3.AlertDialog
1411
import androidx.compose.material3.Text
1512
import androidx.compose.material3.TextButton
@@ -28,9 +25,9 @@ import dev.sanmer.mrepo.viewmodel.RepositoriesViewModel.RepoState
2825
fun RepositoriesList(
2926
list: List<RepoState>,
3027
state: LazyListState,
31-
update: (RepoState) -> Unit,
28+
insert: (RepoState) -> Unit,
3229
delete: (RepoState) -> Unit,
33-
getUpdate: (RepoState, (Throwable) -> Unit) -> Unit
30+
update: (RepoState) -> Unit
3431
) = LazyColumn(
3532
state = state,
3633
modifier = Modifier.fillMaxSize(),
@@ -43,8 +40,8 @@ fun RepositoriesList(
4340
) { repo ->
4441
RepositoryItem(
4542
repo = repo,
46-
toggle = { update(it) },
47-
onUpdate = getUpdate,
43+
toggle = { insert(it) },
44+
onUpdate = update,
4845
onDelete = delete,
4946
)
5047
}
@@ -54,7 +51,7 @@ fun RepositoriesList(
5451
private fun RepositoryItem(
5552
repo: RepoState,
5653
toggle: (RepoState) -> Unit,
57-
onUpdate: (RepoState, (Throwable) -> Unit) -> Unit,
54+
onUpdate: (RepoState) -> Unit,
5855
onDelete: (RepoState) -> Unit,
5956
) {
6057
var delete by remember { mutableStateOf(false) }
@@ -64,28 +61,10 @@ private fun RepositoryItem(
6461
onConfirm = { onDelete(repo) }
6562
)
6663

67-
var failure by remember { mutableStateOf(false) }
68-
var message: String by remember { mutableStateOf("") }
69-
if (failure) FailureDialog(
70-
name = repo.name,
71-
message = message,
72-
onClose = {
73-
failure = false
74-
message = ""
75-
}
76-
)
77-
7864
RepositoryItem(
7965
repo = repo,
80-
toggle = {
81-
toggle(repo.copy(enable = it))
82-
},
83-
update = {
84-
onUpdate(repo) {
85-
failure = true
86-
message = it.stackTraceToString()
87-
}
88-
},
66+
toggle = { toggle(repo.copy(enable = it)) },
67+
update = { onUpdate(repo) },
8968
delete = { delete = true }
9069
)
9170
}
@@ -119,30 +98,4 @@ private fun DeleteDialog(
11998
Text(text = stringResource(id = R.string.dialog_cancel))
12099
}
121100
}
122-
)
123-
124-
@Composable
125-
fun FailureDialog(
126-
name: String,
127-
message: String,
128-
onClose: () -> Unit
129-
) = AlertDialog(
130-
shape = RoundedCornerShape(20.dp),
131-
onDismissRequest = onClose,
132-
title = { Text(text = name) },
133-
text = {
134-
Text(
135-
text = message,
136-
modifier = Modifier
137-
.requiredHeightIn(max = 280.dp)
138-
.verticalScroll(rememberScrollState())
139-
)
140-
},
141-
confirmButton = {
142-
TextButton(
143-
onClick = onClose
144-
) {
145-
Text(text = stringResource(id = R.string.dialog_ok))
146-
}
147-
}
148101
)

app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/settings/repositories/RepositoriesScreen.kt

+42-11
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import androidx.compose.foundation.layout.Box
88
import androidx.compose.foundation.layout.WindowInsets
99
import androidx.compose.foundation.layout.fillMaxWidth
1010
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.layout.requiredHeightIn
1112
import androidx.compose.foundation.lazy.rememberLazyListState
13+
import androidx.compose.foundation.rememberScrollState
1214
import androidx.compose.foundation.shape.RoundedCornerShape
1315
import androidx.compose.foundation.text.KeyboardActions
1416
import androidx.compose.foundation.text.KeyboardOptions
17+
import androidx.compose.foundation.verticalScroll
18+
import androidx.compose.material3.AlertDialog
1519
import androidx.compose.material3.FloatingActionButton
1620
import androidx.compose.material3.Icon
1721
import androidx.compose.material3.IconButton
@@ -41,8 +45,6 @@ import androidx.hilt.navigation.compose.hiltViewModel
4145
import androidx.lifecycle.compose.collectAsStateWithLifecycle
4246
import androidx.navigation.NavController
4347
import dev.sanmer.mrepo.R
44-
import dev.sanmer.mrepo.ui.animate.slideInTopToBottom
45-
import dev.sanmer.mrepo.ui.animate.slideOutBottomToTop
4648
import dev.sanmer.mrepo.ui.component.Loading
4749
import dev.sanmer.mrepo.ui.component.NavigateUpTopBar
4850
import dev.sanmer.mrepo.ui.component.PageIndicator
@@ -78,11 +80,11 @@ fun RepositoriesScreen(
7880
var add by remember { mutableStateOf(false) }
7981
if (add) AddDialog(
8082
onClose = { add = false },
81-
onAdd = {
82-
repoUrl = it
83-
viewModel.insert(it) { e ->
83+
onAdd = { url ->
84+
repoUrl = url
85+
viewModel.insert(url) {
8486
failure = true
85-
message = e.stackTraceToString()
87+
message = it.stackTraceToString()
8688
}
8789
}
8890
)
@@ -130,15 +132,18 @@ fun RepositoriesScreen(
130132
RepositoriesList(
131133
list = list,
132134
state = listSate,
133-
update = viewModel::update,
135+
insert = viewModel::insert,
134136
delete = viewModel::delete,
135-
getUpdate = viewModel::getUpdate
137+
update = { repo ->
138+
viewModel.update(repo) {
139+
failure = true
140+
message = it.stackTraceToString()
141+
}
142+
}
136143
)
137144

138145
AnimatedVisibility(
139-
visible = viewModel.progress,
140-
enter = slideInTopToBottom(),
141-
exit = slideOutBottomToTop()
146+
visible = viewModel.progress
142147
) {
143148
LinearProgressIndicator(
144149
modifier = Modifier.fillMaxWidth(),
@@ -200,6 +205,32 @@ private fun AddDialog(
200205
}
201206
}
202207

208+
@Composable
209+
private fun FailureDialog(
210+
name: String,
211+
message: String,
212+
onClose: () -> Unit
213+
) = AlertDialog(
214+
shape = RoundedCornerShape(20.dp),
215+
onDismissRequest = onClose,
216+
title = { Text(text = name) },
217+
text = {
218+
Text(
219+
text = message,
220+
modifier = Modifier
221+
.requiredHeightIn(max = 280.dp)
222+
.verticalScroll(rememberScrollState())
223+
)
224+
},
225+
confirmButton = {
226+
TextButton(
227+
onClick = onClose
228+
) {
229+
Text(text = stringResource(id = R.string.dialog_ok))
230+
}
231+
}
232+
)
233+
203234
@Composable
204235
private fun TopBar(
205236
scrollBehavior: TopAppBarScrollBehavior,

app/src/main/kotlin/dev/sanmer/mrepo/ui/screens/settings/repositories/RepositoryItem.kt

+16-38
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package dev.sanmer.mrepo.ui.screens.settings.repositories
22

33
import androidx.annotation.DrawableRes
44
import androidx.annotation.StringRes
5-
import androidx.compose.animation.Crossfade
65
import androidx.compose.foundation.layout.Arrangement
76
import androidx.compose.foundation.layout.Column
87
import androidx.compose.foundation.layout.PaddingValues
98
import androidx.compose.foundation.layout.Row
109
import androidx.compose.foundation.layout.Spacer
1110
import androidx.compose.foundation.layout.WindowInsets
1211
import androidx.compose.foundation.layout.fillMaxWidth
12+
import androidx.compose.foundation.layout.navigationBars
1313
import androidx.compose.foundation.layout.padding
1414
import androidx.compose.foundation.layout.size
1515
import androidx.compose.foundation.layout.width
@@ -36,12 +36,10 @@ import androidx.compose.ui.platform.LocalContext
3636
import androidx.compose.ui.res.painterResource
3737
import androidx.compose.ui.res.stringResource
3838
import androidx.compose.ui.text.font.FontWeight
39-
import androidx.compose.ui.text.style.TextDecoration
4039
import androidx.compose.ui.text.style.TextOverflow
4140
import androidx.compose.ui.unit.dp
4241
import dev.sanmer.mrepo.R
4342
import dev.sanmer.mrepo.ui.component.LabelItem
44-
import dev.sanmer.mrepo.ui.component.NavigationBarsSpacer
4543
import dev.sanmer.mrepo.ui.utils.expandedShape
4644
import dev.sanmer.mrepo.utils.extensions.shareText
4745
import dev.sanmer.mrepo.utils.extensions.toDateTime
@@ -59,11 +57,6 @@ fun RepositoryItem(
5957
tonalElevation = 1.dp,
6058
onClick = { toggle(!repo.enable) },
6159
) {
62-
val (alpha, textDecoration) = when {
63-
!repo.enable -> 0.5f to TextDecoration.None
64-
else -> 1f to TextDecoration.None
65-
}
66-
6760
Column(
6861
modifier = Modifier
6962
.padding(all = 15.dp)
@@ -72,49 +65,36 @@ fun RepositoryItem(
7265
Row(
7366
verticalAlignment = Alignment.Top
7467
) {
75-
Crossfade(
76-
targetState = repo.enable,
77-
label = "RepositoryItem"
78-
) {
79-
if (it) {
80-
Icon(
81-
modifier = Modifier.size(32.dp),
82-
painter = painterResource(id = R.drawable.circle_check_filled),
83-
contentDescription = null,
84-
tint = MaterialTheme.colorScheme.secondary
85-
)
68+
Icon(
69+
modifier = Modifier.size(32.dp),
70+
painter = painterResource(id = if (repo.enable) {
71+
R.drawable.circle_check_filled
8672
} else {
87-
Icon(
88-
modifier = Modifier.size(32.dp),
89-
painter = painterResource(id = R.drawable.circle_x_filled),
90-
contentDescription = null,
91-
tint = MaterialTheme.colorScheme.secondary
92-
)
93-
}
94-
}
73+
R.drawable.circle_x_filled
74+
}),
75+
contentDescription = null,
76+
tint = MaterialTheme.colorScheme.secondary
77+
)
9578

9679
Spacer(modifier = Modifier.width(12.dp))
9780
Column(
9881
modifier = Modifier
9982
.weight(1f)
100-
.alpha(alpha),
83+
.alpha(if (repo.enable) 1f else 0.5f),
10184
verticalArrangement = Arrangement.spacedBy(2.dp)
10285
) {
10386
Text(
10487
text = repo.name,
10588
style = MaterialTheme.typography.titleSmall
10689
.copy(fontWeight = FontWeight.Bold),
10790
maxLines = 2,
108-
overflow = TextOverflow.Ellipsis,
109-
textDecoration = textDecoration
91+
overflow = TextOverflow.Ellipsis
11092
)
11193

11294
Text(
113-
text = stringResource(id = R.string.module_update_at,
114-
repo.timestamp.toDateTime()),
95+
text = stringResource(id = R.string.module_update_at, repo.timestamp.toDateTime()),
11596
style = MaterialTheme.typography.bodySmall,
116-
color = MaterialTheme.colorScheme.outline,
117-
textDecoration = textDecoration
97+
color = MaterialTheme.colorScheme.outline
11898
)
11999
}
120100

@@ -163,7 +143,7 @@ private fun BottomSheet(
163143
onDismissRequest = onClose,
164144
sheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true),
165145
shape = BottomSheetDefaults.expandedShape(15.dp),
166-
windowInsets = WindowInsets(0)
146+
windowInsets = WindowInsets.navigationBars
167147
) {
168148
val context = LocalContext.current
169149

@@ -230,8 +210,6 @@ private fun BottomSheet(
230210
)
231211
}
232212
}
233-
234-
NavigationBarsSpacer()
235213
}
236214

237215
@Composable
@@ -260,7 +238,7 @@ private fun ButtonItem(
260238
onClick = onClick,
261239
) {
262240
Icon(
263-
modifier = Modifier.size(22.dp),
241+
modifier = Modifier.size(20.dp),
264242
painter = painterResource(id = icon),
265243
contentDescription = null
266244
)

0 commit comments

Comments
 (0)