1
1
package com.sunkensplashstudios.VRCRoboScout
2
2
3
3
import androidx.compose.foundation.clickable
4
+ import androidx.compose.foundation.interaction.FocusInteraction
4
5
import androidx.compose.foundation.interaction.MutableInteractionSource
5
6
import androidx.compose.foundation.interaction.PressInteraction
6
7
import androidx.compose.foundation.layout.Arrangement
@@ -96,7 +97,7 @@ class LookupViewModel : ViewModel() {
96
97
97
98
// EventLookup
98
99
var eventTextColor = mutableStateOf(Color .Gray )
99
- var eventName = mutableStateOf(" Event Name" )
100
+ var eventName = mutableStateOf(" Event Name\u200B " )
100
101
var events = mutableStateOf(listOf<Event >())
101
102
var page = mutableIntStateOf(1 )
102
103
var fetchedEvents = mutableStateOf(false )
@@ -285,6 +286,7 @@ fun TeamLookup(lookupViewModel: LookupViewModel, navController: NavController) {
285
286
286
287
val localContext = LocalContext .current
287
288
val userSettings = remember { UserSettings (localContext) }
289
+ val isFocused = remember { mutableStateOf(false ) }
288
290
289
291
Column (
290
292
horizontalAlignment = Alignment .CenterHorizontally ,
@@ -314,26 +316,29 @@ fun TeamLookup(lookupViewModel: LookupViewModel, navController: NavController) {
314
316
)
315
317
Spacer (modifier = Modifier .weight(1.0f ))
316
318
TextField (
317
- modifier = Modifier .sizeIn(
318
- maxWidth = 200 .dp,
319
- ),
319
+ modifier = Modifier .sizeIn(maxWidth = 200 .dp),
320
320
value = lookupViewModel.number.value,
321
- onValueChange = { lookupViewModel.number.value = it },
321
+ onValueChange = { lookupViewModel.number.value = it.trim() },
322
322
singleLine = true ,
323
323
interactionSource = remember { MutableInteractionSource () }
324
324
.also { interactionSource ->
325
325
LaunchedEffect (interactionSource) {
326
326
interactionSource.interactions.collect {
327
- if (it is PressInteraction .Release ) {
328
- lookupViewModel.number.value = " "
329
- lookupViewModel.fetchedTeams.value = false
327
+ when (it) {
328
+ is FocusInteraction .Focus -> isFocused.value = true
329
+ is FocusInteraction .Unfocus -> isFocused.value = false
330
+ is PressInteraction .Release -> {
331
+ lookupViewModel.number.value = " "
332
+ lookupViewModel.fetchedTeams.value = false
333
+ }
330
334
}
331
335
}
332
336
}
333
337
},
334
338
textStyle = LocalTextStyle .current.copy(
335
339
textAlign = TextAlign .Center ,
336
- fontSize = 34 .sp
340
+ fontSize = 34 .sp,
341
+ color = if (lookupViewModel.number.value.isEmpty() || lookupViewModel.number.value == " 229V\u200B " ) Color .Gray else MaterialTheme .colorScheme.onSurface
337
342
),
338
343
colors = TextFieldDefaults .colors(
339
344
focusedContainerColor = Color .Transparent ,
@@ -342,31 +347,51 @@ fun TeamLookup(lookupViewModel: LookupViewModel, navController: NavController) {
342
347
focusedIndicatorColor = Color .Transparent ,
343
348
unfocusedIndicatorColor = Color .Transparent ,
344
349
disabledIndicatorColor = Color .Transparent ,
345
- unfocusedTextColor = lookupViewModel.teamTextColor.value
350
+ unfocusedTextColor = lookupViewModel.teamTextColor.value,
346
351
),
347
352
keyboardOptions = KeyboardOptions (imeAction = ImeAction .Done ),
348
353
keyboardActions = KeyboardActions (
349
354
onDone = {
350
355
keyboardController?.hide()
351
356
lookupViewModel.fetchTeam()
352
- })
357
+ }),
358
+ placeholder = {
359
+ if (! isFocused.value && lookupViewModel.number.value.isEmpty()) {
360
+ Text (
361
+ " 229V\u200B " ,
362
+ modifier = Modifier .fillMaxWidth(),
363
+ style = LocalTextStyle .current.copy(
364
+ color = Color .Gray ,
365
+ fontSize = 34 .sp,
366
+ textAlign = TextAlign .Center ,
367
+ )
368
+ )
369
+ }
370
+ }
353
371
)
354
372
Spacer (modifier = Modifier .weight(1.0f ))
355
373
Box {
356
374
IconButton (onClick = {
357
375
favoriteTeams =
358
- if (lookupViewModel.number.value.isEmpty() || lookupViewModel.number.value == " 229V\u200B " || ! lookupViewModel.fetchedTeams.value ) {
376
+ if (lookupViewModel.number.value.isEmpty() || lookupViewModel.number.value == " 229V\u200B " ) {
359
377
return @IconButton
360
- } else if (favoriteTeams.contains(lookupViewModel.number.value.uppercase()) && lookupViewModel.teamTextColor .value != Color . Unspecified ) {
378
+ } else if (favoriteTeams.contains(lookupViewModel.number.value.uppercase()) && ! lookupViewModel.loadingTeams .value) {
361
379
userSettings.removeFavoriteTeam(lookupViewModel.number.value.uppercase())
362
380
userSettings.getData(" favoriteTeams" , " " ).replace(" [" , " " )
363
381
.replace(" ]" , " " )
364
382
.split(" , " )
365
383
} else {
366
- userSettings.addFavoriteTeam(lookupViewModel.number.value.uppercase())
367
- userSettings.getData(" favoriteTeams" , " " ).replace(" [" , " " )
368
- .replace(" ]" , " " )
369
- .split(" , " )
384
+ // allow adding to favorites only after fetching team data
385
+ if (! lookupViewModel.fetchedTeams.value) {
386
+ return @IconButton
387
+ }
388
+
389
+ else {
390
+ userSettings.addFavoriteTeam(lookupViewModel.number.value.uppercase())
391
+ userSettings.getData(" favoriteTeams" , " " ).replace(" [" , " " )
392
+ .replace(" ]" , " " )
393
+ .split(" , " )
394
+ }
370
395
}
371
396
}) {
372
397
if (favoriteTeams.contains(lookupViewModel.number.value.uppercase()) && lookupViewModel.number.value.isNotBlank()) {
@@ -676,6 +701,7 @@ fun TeamLookup(lookupViewModel: LookupViewModel, navController: NavController) {
676
701
fun EventLookup (lookupViewModel : LookupViewModel , navController : NavController ) {
677
702
678
703
val keyboardController = LocalSoftwareKeyboardController .current
704
+ val isFocused = remember { mutableStateOf(false ) }
679
705
680
706
Column (
681
707
horizontalAlignment = Alignment .CenterHorizontally ,
@@ -697,16 +723,21 @@ fun EventLookup(lookupViewModel: LookupViewModel, navController: NavController)
697
723
.also { interactionSource ->
698
724
LaunchedEffect (interactionSource) {
699
725
interactionSource.interactions.collect {
700
- if (it is PressInteraction .Release ) {
701
- lookupViewModel.eventName.value = " "
702
- lookupViewModel.fetchedEvents.value = false
726
+ when (it) {
727
+ is FocusInteraction .Focus -> isFocused.value = true
728
+ is FocusInteraction .Unfocus -> isFocused.value = false
729
+ is PressInteraction .Release -> {
730
+ lookupViewModel.eventName.value = " "
731
+ lookupViewModel.fetchedEvents.value = false
732
+ }
703
733
}
704
734
}
705
735
}
706
736
},
707
737
textStyle = LocalTextStyle .current.copy(
708
738
textAlign = TextAlign .Center ,
709
- fontSize = 34 .sp
739
+ fontSize = 34 .sp,
740
+ color = if (lookupViewModel.eventName.value.isEmpty() || lookupViewModel.eventName.value == " Event Name\u200B " ) Color .Gray else MaterialTheme .colorScheme.onSurface
710
741
),
711
742
colors = TextFieldDefaults .colors(
712
743
focusedContainerColor = Color .Transparent ,
@@ -723,7 +754,20 @@ fun EventLookup(lookupViewModel: LookupViewModel, navController: NavController)
723
754
keyboardController?.hide()
724
755
lookupViewModel.fetchEvents(name = lookupViewModel.eventName.value, page = 1 )
725
756
lookupViewModel.page.intValue = 1
726
- })
757
+ }),
758
+ placeholder = {
759
+ if (! isFocused.value && lookupViewModel.eventName.value.isEmpty()) {
760
+ Text (
761
+ " Event Name\u200B " ,
762
+ modifier = Modifier .fillMaxWidth(),
763
+ style = LocalTextStyle .current.copy(
764
+ color = Color .Gray ,
765
+ fontSize = 34 .sp,
766
+ textAlign = TextAlign .Center ,
767
+ )
768
+ )
769
+ }
770
+ }
727
771
)
728
772
}
729
773
if (lookupViewModel.loadingEvents.value) {
0 commit comments