@@ -92,23 +92,68 @@ private fun RequestInitialFocus(requester: FocusRequester, key: Any? = Unit) {
9292 }
9393}
9494
95- /* * "Who's watching?" at launch when more than one profile exists. */
95+ /* *
96+ * "Who's watching?": at launch when there's more than one profile, and from the sidebar's profile
97+ * entry. Profiles can be added and edited here as well as in Settings.
98+ */
9699@Composable
97100fun ProfilePickerScreen (
98101 profiles : List <ProfileEntity >,
99102 lastProfileId : Long ,
100- onSelect : (Long ) -> Unit
103+ onSelect : (Long ) -> Unit ,
104+ onCreate : (name: String , colorId: String ) -> Unit ,
105+ onUpdate : (id: Long , name: String , colorId: String ) -> Unit ,
106+ onDelete : (Long ) -> Unit ,
107+ /* * Set when opened from the sidebar: Back leaves without switching. */
108+ onDismiss : (() -> Unit )? = null
101109) {
102- val initialRequester = remember { FocusRequester () }
103- val initialId = profiles.firstOrNull { it.id == lastProfileId }?.id ? : profiles.firstOrNull()?.id
104- if (profiles.isNotEmpty()) RequestInitialFocus (initialRequester, initialId)
110+ var editing by remember { mutableStateOf(false ) }
111+ var editingProfile by remember { mutableStateOf<ProfileEntity ?>(null ) }
112+ var adding by remember { mutableStateOf(false ) }
113+ val tileRequesters = remember { mutableMapOf<Long , FocusRequester >() }
114+ val addRequester = remember { FocusRequester () }
115+ val editButtonRequester = remember { FocusRequester () }
116+ // Where focus goes when the screen (or a dialog over it) settles.
117+ var focusTarget by remember { mutableStateOf<Long ?>(profiles.firstOrNull { it.id == lastProfileId }?.id ? : profiles.firstOrNull()?.id) }
118+ val countBeforeAdding = remember { mutableIntStateOf(profiles.size) }
105119
106- Box (
107- Modifier .fillMaxSize().background(MaterialTheme .colorScheme.background),
108- contentAlignment = Alignment .Center
109- ) {
110- Column (horizontalAlignment = Alignment .CenterHorizontally ) {
111- Text (" Who's watching?" , style = MaterialTheme .typography.displaySmall)
120+ // A newly added profile takes focus once it appears.
121+ LaunchedEffect (profiles.size) {
122+ if (profiles.size > countBeforeAdding.intValue) {
123+ focusTarget = profiles.maxByOrNull { it.createdAt }?.id
124+ countBeforeAdding.intValue = profiles.size
125+ }
126+ }
127+ val target = focusTarget?.let { id -> tileRequesters.getOrPut(id) { FocusRequester () } }
128+ if (target != null && editingProfile == null && ! adding) RequestInitialFocus (target, focusTarget to editing)
129+
130+ me.xdan.aperture.ui.platform.BackHandler (enabled = editing || onDismiss != null ) {
131+ if (editing) editing = false else onDismiss?.invoke()
132+ }
133+
134+ Box (Modifier .fillMaxSize().background(MaterialTheme .colorScheme.background)) {
135+ Row (
136+ Modifier .align(Alignment .TopStart ).padding(start = 56 .dp, top = 40 .dp),
137+ verticalAlignment = Alignment .CenterVertically
138+ ) {
139+ me.xdan.aperture.ui.component.ApertureBrandMark (modifier = Modifier .size(40 .dp))
140+ Spacer (Modifier .width(12 .dp))
141+ Text (
142+ " Aperture" ,
143+ style = MaterialTheme .typography.headlineSmall,
144+ fontFamily = me.xdan.aperture.ui.theme.ApertureBrandFontFamily ,
145+ fontWeight = FontWeight .SemiBold ,
146+ color = MaterialTheme .colorScheme.primary
147+ )
148+ }
149+ Column (
150+ Modifier .align(Alignment .Center ),
151+ horizontalAlignment = Alignment .CenterHorizontally
152+ ) {
153+ Text (
154+ if (editing) " Choose a profile to edit" else " Who's watching?" ,
155+ style = MaterialTheme .typography.displaySmall
156+ )
112157 Spacer (Modifier .height(48 .dp))
113158 LazyRow (
114159 horizontalArrangement = Arrangement .spacedBy(36 .dp),
@@ -117,50 +162,147 @@ fun ProfilePickerScreen(
117162 items(profiles, key = { it.id }) { profile ->
118163 ProfileTile (
119164 profile = profile,
120- onClick = { onSelect(profile.id) },
121- modifier = if (profile.id == initialId) Modifier .focusRequester(initialRequester) else Modifier
165+ editing = editing,
166+ onClick = {
167+ focusTarget = profile.id
168+ if (editing) editingProfile = profile else onSelect(profile.id)
169+ },
170+ modifier = Modifier .focusRequester(tileRequesters.getOrPut(profile.id) { FocusRequester () })
171+ )
172+ }
173+ item(key = " add" ) {
174+ AddProfileTile (
175+ onClick = {
176+ countBeforeAdding.intValue = profiles.size
177+ adding = true
178+ },
179+ modifier = Modifier .focusRequester(addRequester)
122180 )
123181 }
124182 }
183+ Spacer (Modifier .height(40 .dp))
184+ OutlinedButton (
185+ onClick = { editing = ! editing },
186+ modifier = Modifier .focusRequester(editButtonRequester)
187+ ) {
188+ Icon (
189+ if (editing) Icons .Rounded .Check else Icons .Rounded .Edit ,
190+ contentDescription = null ,
191+ modifier = Modifier .size(18 .dp)
192+ )
193+ Spacer (Modifier .width(8 .dp))
194+ Text (if (editing) " Done" else " Edit profiles" )
195+ }
196+ }
197+ }
198+
199+ if (adding) {
200+ ProfileEditorDialog (
201+ title = " Add profile" ,
202+ initialName = " " ,
203+ initialColorId = nextProfileColor(profiles),
204+ onSave = { name, color -> onCreate(name, color); adding = false },
205+ onDelete = null ,
206+ onDismiss = { adding = false }
207+ )
208+ }
209+ editingProfile?.let { profile ->
210+ ProfileEditorDialog (
211+ title = " Edit profile" ,
212+ initialName = profile.name,
213+ initialColorId = profile.colorId,
214+ onSave = { name, color -> onUpdate(profile.id, name, color); editingProfile = null },
215+ onDelete = if (profiles.size > 1 ) {
216+ {
217+ onDelete(profile.id)
218+ focusTarget = profiles.firstOrNull { it.id != profile.id }?.id
219+ editingProfile = null
220+ }
221+ } else null ,
222+ onDismiss = { editingProfile = null }
223+ )
224+ }
225+ }
226+
227+ @Composable
228+ private fun ProfileTile (profile : ProfileEntity , editing : Boolean , onClick : () -> Unit , modifier : Modifier = Modifier ) {
229+ var focused by remember { mutableStateOf(false ) }
230+ Column (horizontalAlignment = Alignment .CenterHorizontally , modifier = Modifier .width(172 .dp)) {
231+ TileCircle (onClick = onClick, modifier = modifier, onFocusChanged = { focused = it }) {
232+ Box (contentAlignment = Alignment .Center ) {
233+ ProfileAvatar (profile.name, profile.colorId, 148 .dp, Modifier .padding(10 .dp))
234+ if (editing) {
235+ // A dimmed avatar with a pencil, so it's clear what pressing it will do.
236+ Box (
237+ Modifier .size(148 .dp).clip(CircleShape ).background(Color .Black .copy(alpha = 0.62f )),
238+ contentAlignment = Alignment .Center
239+ ) {
240+ Icon (Icons .Rounded .Edit , contentDescription = " Edit ${profile.name} " , tint = Color .White , modifier = Modifier .size(44 .dp))
241+ }
242+ }
243+ }
125244 }
245+ Spacer (Modifier .height(14 .dp))
246+ TileLabel (profile.name, focused)
126247 }
127248}
128249
129250@Composable
130- private fun ProfileTile ( profile : ProfileEntity , onClick : () -> Unit , modifier : Modifier = Modifier ) {
251+ private fun AddProfileTile ( onClick : () -> Unit , modifier : Modifier = Modifier ) {
131252 var focused by remember { mutableStateOf(false ) }
132253 Column (horizontalAlignment = Alignment .CenterHorizontally , modifier = Modifier .width(172 .dp)) {
133- Surface (
134- onClick = onClick,
135- modifier = modifier.onFocusChanged { focused = it.isFocused },
136- shape = ClickableSurfaceDefaults .shape(CircleShape ),
137- colors = ClickableSurfaceDefaults .colors(
138- containerColor = Color .Transparent ,
139- focusedContainerColor = Color .Transparent
140- ),
141- scale = ClickableSurfaceDefaults .scale(focusedScale = 1.08f , pressedScale = 0.96f ),
142- border = ClickableSurfaceDefaults .border(
143- focusedBorder = Border (
144- border = BorderStroke (4 .dp, MaterialTheme .colorScheme.onSurface),
145- inset = 6 .dp,
146- shape = CircleShape
147- )
148- )
149- ) {
150- ProfileAvatar (profile.name, profile.colorId, 148 .dp, Modifier .padding(10 .dp))
254+ TileCircle (onClick = onClick, modifier = modifier, onFocusChanged = { focused = it }) {
255+ Box (
256+ Modifier .padding(10 .dp).size(148 .dp).clip(CircleShape )
257+ .background(MaterialTheme .colorScheme.surfaceVariant),
258+ contentAlignment = Alignment .Center
259+ ) {
260+ Icon (Icons .Rounded .Add , contentDescription = null , tint = MaterialTheme .colorScheme.onSurfaceVariant, modifier = Modifier .size(56 .dp))
261+ }
151262 }
152263 Spacer (Modifier .height(14 .dp))
153- Text (
154- profile.name,
155- style = MaterialTheme .typography.titleLarge,
156- color = if (focused) MaterialTheme .colorScheme.onSurface else MaterialTheme .colorScheme.onSurfaceVariant,
157- maxLines = 1 ,
158- overflow = TextOverflow .Ellipsis ,
159- textAlign = TextAlign .Center
160- )
264+ TileLabel (" Add profile" , focused)
161265 }
162266}
163267
268+ @Composable
269+ private fun TileCircle (
270+ onClick : () -> Unit ,
271+ modifier : Modifier ,
272+ onFocusChanged : (Boolean ) -> Unit ,
273+ content : @Composable () -> Unit
274+ ) {
275+ Surface (
276+ onClick = onClick,
277+ modifier = modifier.onFocusChanged { onFocusChanged(it.isFocused) },
278+ shape = ClickableSurfaceDefaults .shape(CircleShape ),
279+ colors = ClickableSurfaceDefaults .colors(
280+ containerColor = Color .Transparent ,
281+ focusedContainerColor = Color .Transparent
282+ ),
283+ scale = ClickableSurfaceDefaults .scale(focusedScale = 1.08f , pressedScale = 0.96f ),
284+ border = ClickableSurfaceDefaults .border(
285+ focusedBorder = Border (
286+ border = BorderStroke (4 .dp, MaterialTheme .colorScheme.onSurface),
287+ inset = 6 .dp,
288+ shape = CircleShape
289+ )
290+ )
291+ ) { content() }
292+ }
293+
294+ @Composable
295+ private fun TileLabel (text : String , focused : Boolean ) {
296+ Text (
297+ text,
298+ style = MaterialTheme .typography.titleLarge,
299+ color = if (focused) MaterialTheme .colorScheme.onSurface else MaterialTheme .colorScheme.onSurfaceVariant,
300+ maxLines = 1 ,
301+ overflow = TextOverflow .Ellipsis ,
302+ textAlign = TextAlign .Center
303+ )
304+ }
305+
164306/* * Settings › Profiles: switch, add and edit. */
165307@Composable
166308fun ProfilesDialog (
0 commit comments