Skip to content

Commit 5a259b4

Browse files
committed
refactor: moving profile selection logic to viewModel
1 parent ea40a18 commit 5a259b4

File tree

4 files changed

+78
-87
lines changed

4 files changed

+78
-87
lines changed

feature-list-streams/src/main/java/com/codandotv/streamplayerapp/feature_list_streams/profile/data/ProfilePickerStreamRepository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,9 @@ private val mockProfiles = listOf(
4747
ProfileStream(
4848
name = "Random name 2",
4949
imageUrl = "https://raw.githubusercontent.com/git-jr/sample-files/main/profile%20pics/netflix_profile_pic_5.png"
50+
),
51+
ProfileStream(
52+
name = "CodandoTV",
53+
imageUrl = "https://raw.githubusercontent.com/git-jr/sample-files/main/profile%20pics/netflix_profile_pic_6.png"
5054
)
5155
)

feature-list-streams/src/main/java/com/codandotv/streamplayerapp/feature_list_streams/profile/presentation/screens/ProfilePickerStreamScreen.kt

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import androidx.compose.ui.graphics.graphicsLayer
3333
import androidx.compose.ui.platform.LocalContext
3434
import androidx.compose.ui.platform.LocalLifecycleOwner
3535
import androidx.compose.ui.res.painterResource
36+
import androidx.compose.ui.res.stringResource
3637
import androidx.compose.ui.unit.IntOffset
3738
import androidx.compose.ui.unit.dp
3839
import androidx.lifecycle.LifecycleOwner
@@ -67,14 +68,14 @@ fun ProfilePickerStreamScreen(
6768
SetupProfilePickerScreen(
6869
uiState = uiState,
6970
navController = navController,
70-
onChangeCenterImageAlpha = { viewModel.changeCenterImageAlpha(it) },
71-
onSetScreenSize = { width, height, widthPx, heightPx ->
72-
viewModel.changeScreenSize(width, height, widthPx, heightPx)
73-
},
71+
onSetCenterImageAlpha = { viewModel.setCenterImageAlpha(it) },
7472
onSetLastItemPositioned = { viewModel.setLastItemPositioned(it) },
7573
onSetHaltSizeImage = { viewModel.setHaltSizeImage(it) },
7674
onSetHalfExpandedSizeImage = { viewModel.setHalfExpandedSizeImage(it) },
77-
onClickSelectedProfile = { viewModel.clickSelectedProfile(it) }
75+
onClickSelectedProfile = { viewModel.moveSelectedProfileToCenterImage(it) },
76+
onSetScreenSize = { width, height, widthPx, heightPx ->
77+
viewModel.setScreenSize(width, height, widthPx, heightPx)
78+
},
7879
)
7980
}
8081
}
@@ -85,7 +86,7 @@ fun ProfilePickerStreamScreen(
8586
private fun SetupProfilePickerScreen(
8687
uiState: ProfilePickerStreamsUIState,
8788
navController: NavController,
88-
onChangeCenterImageAlpha: (Float) -> Unit = {},
89+
onSetCenterImageAlpha: (Float) -> Unit = {},
8990
onSetScreenSize: (Float, Float, Int, Int) -> Unit = { _, _, _, _ -> },
9091
onSetLastItemPositioned: (Boolean) -> Unit = {},
9192
onSetHaltSizeImage: (Int) -> Unit = { },
@@ -100,10 +101,10 @@ private fun SetupProfilePickerScreen(
100101
} else {
101102
Color.Transparent
102103
},
103-
label = "fundo da tela escurecendo",
104+
label = stringResource(R.string.profile_animation_background_opacity),
104105
animationSpec = tween(durationMillis = 1000),
105106
finishedListener = {
106-
onChangeCenterImageAlpha(0f)
107+
onSetCenterImageAlpha(0f)
107108
navController.navigateUp()
108109
}
109110
)
@@ -122,13 +123,13 @@ private fun SetupProfilePickerScreen(
122123

123124
val animatedSizeImage by animateDpAsState(
124125
targetValue = if (expandImage) expandedImageSize.dp else defaultImageSize.dp,
125-
label = "imagem de perfil selecionada aumentando de tamanho",
126+
label = stringResource(R.string.profile_animation_selected_image_size),
126127
animationSpec = tween(durationMillis = 1000),
127128
)
128129

129130
val animatedProfileAlpha: Float by animateFloatAsState(
130131
if (lastItemPositioned) 1f else 0f,
131-
label = "mostrando todos os perfis",
132+
label = stringResource(R.string.profile_animation_showing_all_profiles),
132133
)
133134

134135
val offsetSelectedProfileImage by animateIntOffsetAsState(
@@ -143,7 +144,7 @@ private fun SetupProfilePickerScreen(
143144
IntOffset(0, 0)
144145
}
145146
},
146-
label = "imagem de perfil selecionada se movendo para o centro",
147+
label = stringResource(R.string.profile_animation_selected_image_position),
147148
animationSpec = tween(durationMillis = if (!showCenterImage) 100 else 800)
148149
)
149150

@@ -181,7 +182,10 @@ private fun SetupProfilePickerScreen(
181182
AsyncImage(
182183
model = profile.imageUrl,
183184
placeholder = painterResource(id = R.drawable.image_placeholder),
184-
contentDescription = "image de perfil de ${profile.name}",
185+
contentDescription = stringResource(
186+
id = R.string.profile_current_profile_name,
187+
profile.name
188+
),
185189
modifier = Modifier
186190
.clip(RoundedCornerShape(5))
187191
.alpha(if (selectedItem == profile) selectedImageAlpha else 1f)
@@ -226,7 +230,10 @@ private fun SetupProfilePickerScreen(
226230
.crossfade(true)
227231
.build(),
228232
placeholder = painterResource(id = R.drawable.image_placeholder),
229-
contentDescription = "image de perfil de ${selectedItem.name}",
233+
contentDescription = stringResource(
234+
id = R.string.profile_current_profile_name,
235+
selectedItem.name
236+
),
230237
modifier = Modifier
231238
.clip(RoundedCornerShape(5))
232239
.size(animatedSizeImage)

feature-list-streams/src/main/java/com/codandotv/streamplayerapp/feature_list_streams/profile/presentation/screens/ProfilePickerStreamViewModel.kt

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class ProfilePickerStreamViewModel(
2525
initialValue = _uiState.value
2626
)
2727

28-
2928
override fun onCreate(owner: LifecycleOwner) {
3029
super.onCreate(owner)
3130

@@ -46,45 +45,6 @@ class ProfilePickerStreamViewModel(
4645
}
4746
}
4847

49-
private fun changeShowCenterImage(value: Boolean) {
50-
_uiState.value = _uiState.value.copy(
51-
showCenterImage = value
52-
)
53-
}
54-
55-
fun changeCenterImageAlpha(alpha: Float) {
56-
_uiState.value = _uiState.value.copy(
57-
centerImageAlpha = alpha
58-
)
59-
}
60-
61-
private fun changeSelectedProfile(profile: ProfileStream) {
62-
_uiState.value = _uiState.value.copy(
63-
selectedItem = profile
64-
)
65-
}
66-
67-
private fun changeCanMoveImageToCenter(value: Boolean) {
68-
_uiState.value = _uiState.value.copy(
69-
canMoveImageToCenter = value
70-
)
71-
}
72-
73-
private fun changeExpandImage(value: Boolean) {
74-
_uiState.value = _uiState.value.copy(
75-
expandImage = value
76-
)
77-
}
78-
79-
fun changeScreenSize(width: Float, height: Float, widthPx: Int, heightPx: Int) {
80-
_uiState.value = _uiState.value.copy(
81-
screenWidth = width,
82-
screenHeight = height
83-
)
84-
85-
calculateGridScreenSize(widthPx, heightPx)
86-
}
87-
8848
private fun calculateGridScreenSize(widthPx: Int, heightPx: Int) {
8949
val oneThirdOfWidthScreen = widthPx / 3
9050
val oneQuarterOfHeightScreen = heightPx / 4
@@ -107,20 +67,52 @@ class ProfilePickerStreamViewModel(
10767
oneThirdOfWidthScreen - haltSizeImage,
10868
oneQuarterOfHeightScreen * 2
10969
),
70+
Pair(
71+
oneThirdOfWidthScreen * 2 - haltSizeImage,
72+
oneQuarterOfHeightScreen * 2
73+
),
11074
)
11175

11276
_uiState.value = _uiState.value.copy(
11377
offsetProfiles = listOffsetProfiles
11478
)
11579
}
11680

81+
private fun calculateCenterScreen() {
82+
with(_uiState.value) {
83+
val newCenterScreen = Pair(
84+
oneThirdOfWidthScreen + oneThirdOfWidthScreen / 2 - halfExpandedSizeImage,
85+
oneQuarterOfHeightScreen
86+
)
87+
88+
_uiState.value = _uiState.value.copy(
89+
centerScreen = newCenterScreen
90+
)
91+
}
92+
93+
}
94+
95+
fun setCenterImageAlpha(alpha: Float) {
96+
_uiState.value = _uiState.value.copy(
97+
centerImageAlpha = alpha
98+
)
99+
}
100+
101+
fun setScreenSize(width: Float, height: Float, widthPx: Int, heightPx: Int) {
102+
_uiState.value = _uiState.value.copy(
103+
screenWidth = width,
104+
screenHeight = height
105+
)
106+
107+
calculateGridScreenSize(widthPx, heightPx)
108+
}
109+
117110
fun setLastItemPositioned(value: Boolean) {
118111
_uiState.value = _uiState.value.copy(
119112
lastItemPositioned = value
120113
)
121114
}
122115

123-
124116
fun setHaltSizeImage(size: Int) {
125117
_uiState.value = _uiState.value.copy(
126118
haltSizeImage = size
@@ -134,52 +126,33 @@ class ProfilePickerStreamViewModel(
134126
calculateCenterScreen()
135127
}
136128

137-
138-
private fun setSelectedImageAlpha(alpha: Float) {
139-
_uiState.value = _uiState.value.copy(
140-
selectedImageAlpha = alpha
141-
)
142-
}
143-
144-
private fun calculateCenterScreen() {
145-
with(_uiState.value) {
146-
val newCenterScreen = Pair(
147-
oneThirdOfWidthScreen + oneThirdOfWidthScreen / 2 - halfExpandedSizeImage,
148-
oneQuarterOfHeightScreen
149-
)
150-
151-
_uiState.value = _uiState.value.copy(
152-
centerScreen = newCenterScreen
153-
)
154-
}
155-
156-
}
157-
158-
fun clickSelectedProfile(profile: ProfileStream) {
129+
fun moveSelectedProfileToCenterImage(profile: ProfileStream) {
159130
viewModelScope.launch {
160131
with(_uiState.value) {
161132
// move hide image to the position of the clicked item
162-
changeSelectedProfile(profile)
133+
_uiState.value = _uiState.value.copy(
134+
selectedItem = profile
135+
)
163136

164-
// displacement to clicked item
137+
// delay displacement to clicked item
165138
delay(200)
166139

167140
// show hidden image
168-
changeShowCenterImage(!showCenterImage)
141+
_uiState.value = _uiState.value.copy(
142+
showCenterImage = !showCenterImage
143+
)
169144

170145
// maybe can remove this delay, when to join codes of this block
171-
delay(200)
146+
delay(100)
172147

173148
// hidden selected image
174-
// selectedImageAlpha = 0f
175-
setSelectedImageAlpha(0f)
176-
177149
// move the new image to the center screen
178-
// canMoveImageToCenter = !canMoveImageToCenter
179-
changeCanMoveImageToCenter(!canMoveImageToCenter)
180-
181150
// as increase in size
182-
changeExpandImage(!expandImage)
151+
_uiState.value = _uiState.value.copy(
152+
selectedImageAlpha = 0f,
153+
canMoveImageToCenter = !canMoveImageToCenter,
154+
expandImage = !expandImage
155+
)
183156
}
184157
}
185158
}

feature-list-streams/src/main/res/values/strings.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@
2222
<string name="list_highlight_banner_stream_ranking">Top 1 em %s hoje</string>
2323
<!-- endregion Highlight banner -->
2424

25+
<string name="profile_animation_background_opacity">Fundo da tela escurecendo</string>
26+
<string name="profile_animation_selected_image_size">Imagem de perfil selecionada aumentando de tamanho</string>
27+
<string name="profile_animation_selected_image_position"> "Imagem de perfil selecionada se movendo para o centro"</string>
28+
<string name="profile_animation_showing_all_profiles">Mostrando todos os perfis</string>
29+
<string name="profile_current_profile_name">Imagem de perfil de %s</string>
30+
31+
2532
</resources>

0 commit comments

Comments
 (0)