Skip to content

Commit 90e46da

Browse files
committed
Switch profiles from the sidebar and manage them in Who's watching
The bottom of the sidebar shows the active profile: its avatar when the rail is collapsed, a pill with the name, "Switch profiles" and a chevron when it's open. Pressing it reopens Who's watching, even with one profile, and Back leaves without switching. Who's watching gains the Aperture logo, an Add profile tile, and Edit profiles, which marks each avatar with a pencil and opens its editor.
1 parent f2854e3 commit 90e46da

5 files changed

Lines changed: 265 additions & 42 deletions

File tree

‎shared/src/commonMain/kotlin/me/xdan/aperture/data/repository/ProfileRepositoryImpl.kt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ class ProfileRepositoryImpl(
5858
private val _profileChosen = MutableStateFlow(false)
5959
override val profileChosen: StateFlow<Boolean> = _profileChosen
6060

61+
private val _pickerRequested = MutableStateFlow(false)
62+
override val pickerRequested: StateFlow<Boolean> = _pickerRequested
63+
64+
override fun openPicker() {
65+
_pickerRequested.value = true
66+
_profileChosen.value = false
67+
}
68+
69+
override fun dismissPicker() {
70+
if (!_pickerRequested.value) return
71+
_pickerRequested.value = false
72+
_profileChosen.value = true
73+
}
74+
6175
override suspend fun resolveLaunchChoice() {
6276
ensureDefaultProfile()
6377
val all = profileDao.getProfilesOnce()
@@ -71,6 +85,7 @@ class ProfileRepositoryImpl(
7185
if (profileDao.getProfile(profileId) == null) return
7286
preferences.setActiveProfileId(profileId)
7387
profileDao.markUsed(profileId, System.currentTimeMillis())
88+
_pickerRequested.value = false
7489
_profileChosen.value = true
7590
}
7691

‎shared/src/commonMain/kotlin/me/xdan/aperture/domain/repository/ProfileRepository.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ interface ProfileRepository {
1515
/** True once someone has picked a profile since launch, or when there's only one to pick. */
1616
val profileChosen: StateFlow<Boolean>
1717

18+
/** True while Who's watching was opened on purpose (from the sidebar), even with one profile. */
19+
val pickerRequested: StateFlow<Boolean>
20+
21+
/** Shows Who's watching again, e.g. to switch or add a profile. */
22+
fun openPicker()
23+
24+
/** Leaves a picker opened with [openPicker] without changing profile. */
25+
fun dismissPicker()
26+
1827
/** Run at launch: makes sure a profile is active, and skips the picker when there's only one. */
1928
suspend fun resolveLaunchChoice()
2029

‎shared/src/commonMain/kotlin/me/xdan/aperture/ui/ApertureApp.kt‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ fun ApertureApp(appState: ApertureAppState, onExit: () -> Unit) {
104104
// Keeps the active profile's Jellyfin index current while the app is open.
105105
val jellyfinWatcher: me.xdan.aperture.data.jellyfin.JellyfinLibraryWatcher = koinInject()
106106
LaunchedEffect(jellyfinWatcher) { jellyfinWatcher.run() }
107-
val choosingProfile = onboardingCompleted == true && !profileChosen && profiles.size > 1
107+
val pickerRequested by profileRepository.pickerRequested.collectAsState()
108+
// At launch only with several profiles; from the sidebar always, so a first extra profile can be added.
109+
val choosingProfile = onboardingCompleted == true && !profileChosen &&
110+
(profiles.size > 1 || pickerRequested) && profiles.isNotEmpty()
108111
// Read from long-lived effects (the companion collector), which would otherwise keep the first value.
109112
val choosingProfileNow by rememberUpdatedState(choosingProfile)
110113
val sponsorVerified by sponsorVerificationManager.isVerified.collectAsState()
@@ -254,7 +257,11 @@ fun ApertureApp(appState: ApertureAppState, onExit: () -> Unit) {
254257
me.xdan.aperture.ui.screen.profiles.ProfilePickerScreen(
255258
profiles = profiles,
256259
lastProfileId = activeProfileId,
257-
onSelect = profilesViewModel::select
260+
onSelect = profilesViewModel::select,
261+
onCreate = { name, color -> profilesViewModel.create(name, color) },
262+
onUpdate = profilesViewModel::update,
263+
onDelete = profilesViewModel::delete,
264+
onDismiss = if (pickerRequested) profileRepository::dismissPicker else null
258265
)
259266
} else if (appState.isAmbientActive) {
260267
BackHandler {

‎shared/src/commonMain/kotlin/me/xdan/aperture/ui/navigation/NavGraph.kt‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.tv.material3.pointerFocusRegion
66
import me.xdan.aperture.ui.platform.onMouseEnterExit
77
import me.xdan.aperture.ui.platform.usesMouse
88
import org.koin.compose.viewmodel.koinViewModel
9+
import androidx.compose.material.icons.rounded.ExpandMore
910

1011
import me.xdan.aperture.ui.platform.BackHandler
1112
import androidx.compose.animation.AnimatedVisibility
@@ -69,6 +70,10 @@ fun NavGraph(
6970
onPreviewAmbientMode: () -> Unit = {}
7071
) {
7172
val homeViewModel: HomeViewModel = koinViewModel()
73+
val profilesViewModel: me.xdan.aperture.ui.screen.profiles.ProfilesViewModel = koinViewModel()
74+
val profiles by profilesViewModel.profiles.collectAsState()
75+
val activeProfileId by profilesViewModel.activeProfileId.collectAsState()
76+
val profileRepository: me.xdan.aperture.domain.repository.ProfileRepository = org.koin.compose.koinInject()
7277
val mediaActionsViewModel: MediaActionsViewModel = koinViewModel()
7378
val mediaActionState by mediaActionsViewModel.state.collectAsState()
7479
val currentDestination = backstack.last()
@@ -620,6 +625,51 @@ fun NavGraph(
620625
Text("Settings")
621626
}
622627
}
628+
629+
// Who's watching, apart from the destinations at the bottom of the rail.
630+
Spacer(Modifier.weight(1f))
631+
val activeProfile = profiles.firstOrNull { it.id == activeProfileId }
632+
if (activeProfile != null) {
633+
NavigationDrawerItem(
634+
colors = drawerItemColors,
635+
selected = false,
636+
onClick = {
637+
closeDrawerAndRestoreFocus()
638+
profileRepository.openPicker()
639+
},
640+
modifier = Modifier
641+
.then(focusMotion.item("profile"))
642+
.focusProperties {
643+
canFocus = drawerCanReceiveFocus
644+
drawerReturnFocusRequester?.let { right = it }
645+
},
646+
leadingContent = {
647+
me.xdan.aperture.ui.screen.profiles.ProfileAvatar(
648+
activeProfile.name, activeProfile.colorId, 28.dp
649+
)
650+
},
651+
supportingContent = {
652+
DrawerFocusForeground(focusMotion, "profile", drawerValue == DrawerValue.Open) {
653+
Text("Switch profiles", maxLines = 1, style = MaterialTheme.typography.labelSmall)
654+
}
655+
},
656+
trailingContent = {
657+
DrawerFocusForeground(focusMotion, "profile", drawerValue == DrawerValue.Open) {
658+
Icon(Icons.Rounded.ExpandMore, contentDescription = null)
659+
}
660+
}
661+
) {
662+
DrawerFocusForeground(focusMotion, "profile", drawerValue == DrawerValue.Open) {
663+
// Two lines have to fit the drawer's fixed 56dp two-line item.
664+
Text(
665+
activeProfile.name,
666+
maxLines = 1,
667+
overflow = androidx.compose.ui.text.style.TextOverflow.Ellipsis,
668+
style = MaterialTheme.typography.titleSmall
669+
)
670+
}
671+
}
672+
}
623673
}
624674
}
625675
}

‎shared/src/commonMain/kotlin/me/xdan/aperture/ui/screen/profiles/Profiles.kt‎

Lines changed: 182 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -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
97100
fun 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
166308
fun ProfilesDialog(

0 commit comments

Comments
 (0)