Skip to content

Commit 661e696

Browse files
Adressed Comments
1 parent 2de9a2d commit 661e696

4 files changed

Lines changed: 43 additions & 24 deletions

File tree

app/src/main/java/com/cornellappdev/uplift/data/repositories/ConfettiRepository.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cornellappdev.uplift.data.repositories
22

3+
import androidx.compose.ui.geometry.Rect
34
import com.cornellappdev.uplift.ui.viewmodels.profile.ConfettiViewModel
45
import com.cornellappdev.uplift.util.UIEvent
56
import kotlinx.coroutines.flow.MutableStateFlow
@@ -18,4 +19,11 @@ class ConfettiRepository @Inject constructor() {
1819
fun showConfetti (event: ConfettiViewModel.ConfettiUiState) {
1920
_showConfettiEvent.value = UIEvent(event)
2021
}
22+
23+
private val _confettiBounds = MutableStateFlow<Rect?>(null)
24+
val confettiBounds = _confettiBounds.asStateFlow()
25+
26+
fun setConfettiBounds(bounds: Rect?) {
27+
_confettiBounds.value = bounds
28+
}
2129
}

app/src/main/java/com/cornellappdev/uplift/ui/MainNavigationWrapper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fun MainNavigationWrapper(
9090
val systemUiController: SystemUiController = rememberSystemUiController()
9191

9292
val checkInUiState = checkInViewModel.collectUiStateValue()
93-
var confettiBounds by remember { mutableStateOf<Rect?>(null) }
93+
val confettiUiState = confettiViewModel.collectUiStateValue()
9494

9595
val yourShimmerTheme = defaultShimmerTheme.copy(
9696
shaderColors = listOf(
@@ -275,7 +275,7 @@ fun MainNavigationWrapper(
275275
){
276276
Box(
277277
modifier = Modifier.onGloballyPositioned { coords ->
278-
confettiBounds = coords.boundsInRoot()
278+
confettiViewModel.setConfettiBounds(coords.boundsInRoot())
279279
}
280280
){
281281
CheckInPopUp(
@@ -290,7 +290,7 @@ fun MainNavigationWrapper(
290290
}
291291
ConfettiBurst(
292292
confettiViewModel = confettiViewModel,
293-
originRectInRoot = confettiBounds,
293+
particleSpawningBounds = confettiUiState.confettiBounds,
294294
modifier = Modifier
295295
)
296296
}

app/src/main/java/com/cornellappdev/uplift/ui/components/general/ConfettiBurst.kt

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import androidx.compose.ui.graphics.drawscope.withTransform
1717
import com.cornellappdev.uplift.ui.theme.ConfettiColors
1818
import kotlinx.coroutines.delay
1919
import com.cornellappdev.uplift.ui.viewmodels.profile.ConfettiViewModel
20+
import com.google.android.play.core.integrity.x
21+
import com.google.android.play.integrity.internal.y
2022
import kotlin.math.cos
2123
import kotlin.math.sin
2224
import kotlin.random.Random
@@ -63,7 +65,7 @@ private data class ConfettiParticle2D(
6365
@Composable
6466
fun ConfettiBurst(
6567
confettiViewModel: ConfettiViewModel,
66-
originRectInRoot: Rect?,
68+
particleSpawningBounds: Rect?,
6769
modifier: Modifier = Modifier,
6870
particleCount: Int = 30,
6971
colors: List<Color> = listOf(
@@ -75,11 +77,11 @@ fun ConfettiBurst(
7577
) {
7678
val uiState = confettiViewModel.collectUiStateValue()
7779

78-
if (!uiState.showing || originRectInRoot == null) {
80+
if (!uiState.showing || particleSpawningBounds == null) {
7981
return
8082
}
8183

82-
val rect = originRectInRoot
84+
val rect = particleSpawningBounds
8385

8486
var started by remember(uiState.showing) { mutableStateOf(false) }
8587

@@ -105,7 +107,7 @@ fun ConfettiBurst(
105107
val angle = ((-90f + Random.nextFloat() * 110f) * Math.PI / 180f).toFloat()
106108
//initial speed
107109
val speed = Random.nextFloat() * 700f + 400f
108-
//velocity compontents
110+
//velocity components
109111
val vx = cos(angle) * speed
110112
val vy0 = sin(angle) * speed
111113
//size in px
@@ -131,48 +133,46 @@ fun ConfettiBurst(
131133
}
132134
}
133135

134-
//Renders ballisitc motion + fade out for each particle
136+
//Renders ballistic motion + fade out for each particle
135137
Canvas(modifier = modifier.fillMaxSize()) {
136-
//gravity and seconds t
137-
val g = 1750f
138-
val t = progress * 1.2f
138+
//gravity and seconds (time)
139+
val gravity = 1750f
140+
val time = progress * 1.2f
139141

140142
particles.forEach { particle ->
141-
//position at time t
142-
val x = particle.start.x + particle.vx * t
143-
val y = particle.start.y + (particle.vy0 * t + 0.5f * g * t * t)
143+
//position at time
144+
val posX = particle.start.x + particle.vx * time
145+
val posY = particle.start.y + (particle.vy0 * time + 0.5f * gravity * time * time)
144146

145147
// fade progress
146148
val alpha = 1f - progress
147149

148150
//gradient brush
149151
val brush = Brush.linearGradient(
150152
colors = colors,
151-
start = Offset(x - particle.size * 0.8f, y- particle.size * 0.8f),
152-
end = Offset(x + particle.size* 0.8f, y+ particle.size * 0.8f)
153+
start = Offset(posX - particle.size * 0.8f, posY- particle.size * 0.8f),
154+
end = Offset(posX + particle.size* 0.8f, posY + particle.size * 0.8f)
153155
)
154156

155157
when (particle.shape) {
156-
//draws circle
157158
ConfettiShape.CIRCLE -> {
158159
drawCircle(
159160
brush = brush,
160161
radius = particle.size.toFloat(),
161-
center = Offset(x, y),
162+
center = Offset(posX, posY),
162163
alpha = alpha
163164
)
164165
}
165166

166-
//draws rectangle
167167
ConfettiShape.RECTANGLE -> {
168168
withTransform({
169-
rotate(degrees = particle.rotation, pivot = Offset(x, y))
169+
rotate(degrees = particle.rotation, pivot = Offset(posX, posY))
170170
}) {
171171
drawRoundRect(
172172
brush = brush,
173173
topLeft = Offset(
174-
x - (particle.size / 8f),
175-
y - (particle.size / 2f)
174+
posX - (particle.size / 8f),
175+
posY - (particle.size / 2f)
176176
),
177177
size = Size(
178178
width = particle.size * 0.6f,

app/src/main/java/com/cornellappdev/uplift/ui/viewmodels/profile/ConfettiViewModel.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cornellappdev.uplift.ui.viewmodels.profile
22

3+
import androidx.compose.ui.geometry.Rect
34
import com.cornellappdev.uplift.data.repositories.ConfettiRepository
45
import com.cornellappdev.uplift.ui.viewmodels.UpliftViewModel
56
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -10,13 +11,14 @@ import javax.inject.Inject
1011
/** A [ConfettiViewModel] listens for confetti events and exposes animation state to the UI. */
1112
@HiltViewModel
1213
class ConfettiViewModel @Inject constructor(
13-
confettiRepository: ConfettiRepository
14+
private val confettiRepository: ConfettiRepository
1415
) :
1516
UpliftViewModel<ConfettiViewModel.ConfettiUiState>(
1617
initialUiState = ConfettiUiState()
1718
) {
1819
data class ConfettiUiState(
19-
val showing: Boolean = false
20+
val showing: Boolean = false,
21+
val confettiBounds: Rect? = null
2022
)
2123

2224
/** Sets UI state to show the confetti animation. */
@@ -29,6 +31,10 @@ class ConfettiViewModel @Inject constructor(
2931
applyMutation { copy(showing = false) }
3032
}
3133

34+
fun setConfettiBounds(bounds: Rect?) {
35+
confettiRepository.setConfettiBounds(bounds)
36+
}
37+
3238
init {
3339
asyncCollect(confettiRepository.showConfettiEvent) { event ->
3440
event?.consume {
@@ -39,5 +45,10 @@ class ConfettiViewModel @Inject constructor(
3945
}
4046
}
4147
}
48+
49+
asyncCollect(confettiRepository.confettiBounds) { rect ->
50+
applyMutation { copy(confettiBounds = rect) }
51+
52+
}
4253
}
4354
}

0 commit comments

Comments
 (0)