-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement home screen components and UpcomingMovie components (#89)
* Implement Pager components * Implement UpcomingMovie components * Add HomeViewModel * Add HomeUiState * Add HomeEvent * Update HomeScreen
- Loading branch information
1 parent
2e7dfc8
commit 5f7cb87
Showing
8 changed files
with
506 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...tion/src/main/kotlin/com/maximillianleonov/cinemax/feature/home/presentation/HomeEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2022 Maximillian Leonov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.maximillianleonov.cinemax.feature.home.presentation | ||
|
||
import com.maximillianleonov.cinemax.core.presentation.common.Event | ||
|
||
sealed class HomeEvent : Event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...on/src/main/kotlin/com/maximillianleonov/cinemax/feature/home/presentation/HomeUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2022 Maximillian Leonov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.maximillianleonov.cinemax.feature.home.presentation | ||
|
||
import com.maximillianleonov.cinemax.core.presentation.common.State | ||
import com.maximillianleonov.cinemax.core.presentation.model.Movie | ||
|
||
data class HomeUiState( | ||
val upcomingMovies: List<Movie> = emptyList(), | ||
val isUpcomingMoviesLoading: Boolean = false, | ||
val error: Throwable? = null | ||
) : State |
73 changes: 73 additions & 0 deletions
73
.../src/main/kotlin/com/maximillianleonov/cinemax/feature/home/presentation/HomeViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2022 Maximillian Leonov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.maximillianleonov.cinemax.feature.home.presentation | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.maximillianleonov.cinemax.core.presentation.common.EventHandler | ||
import com.maximillianleonov.cinemax.core.presentation.mapper.toMovie | ||
import com.maximillianleonov.cinemax.core.presentation.util.handle | ||
import com.maximillianleonov.cinemax.domain.model.MovieModel | ||
import com.maximillianleonov.cinemax.domain.usecase.GetUpcomingMoviesUseCase | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class HomeViewModel @Inject constructor( | ||
private val getUpcomingMoviesUseCase: GetUpcomingMoviesUseCase | ||
) : ViewModel(), EventHandler<HomeEvent> { | ||
private val _uiState = MutableStateFlow(HomeUiState()) | ||
val uiState = _uiState.asStateFlow() | ||
|
||
@Suppress("UnusedPrivateMember") | ||
private var upcomingMoviesJob = loadUpcomingMovies() | ||
|
||
override fun onEvent(event: HomeEvent) = Unit | ||
|
||
private fun loadUpcomingMovies() = viewModelScope.launch { | ||
getUpcomingMoviesUseCase().handle( | ||
onLoading = { upcomingMovies -> | ||
_uiState.update { | ||
it.copy( | ||
upcomingMovies = upcomingMovies?.map(MovieModel::toMovie).orEmpty(), | ||
isUpcomingMoviesLoading = true | ||
) | ||
} | ||
}, | ||
onSuccess = { upcomingMovies -> | ||
_uiState.update { | ||
it.copy( | ||
upcomingMovies = upcomingMovies.map(MovieModel::toMovie), | ||
isUpcomingMoviesLoading = false | ||
) | ||
} | ||
}, | ||
onFailure = { throwable -> | ||
_uiState.update { | ||
it.copy( | ||
error = throwable, | ||
isUpcomingMoviesLoading = false | ||
) | ||
} | ||
} | ||
) | ||
} | ||
} |
140 changes: 140 additions & 0 deletions
140
...c/main/kotlin/com/maximillianleonov/cinemax/feature/home/presentation/components/Pager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright 2022 Maximillian Leonov | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.maximillianleonov.cinemax.feature.home.presentation.components | ||
|
||
import androidx.compose.animation.core.animateDpAsState | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.offset | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.shape.CircleShape | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.graphics.graphicsLayer | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.IntOffset | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.util.lerp | ||
import com.google.accompanist.pager.ExperimentalPagerApi | ||
import com.google.accompanist.pager.PagerScope | ||
import com.google.accompanist.pager.PagerState | ||
import com.google.accompanist.pager.calculateCurrentOffsetForPage | ||
import com.maximillianleonov.cinemax.core.presentation.theme.CinemaxTheme | ||
import kotlin.math.absoluteValue | ||
import kotlin.math.sign | ||
|
||
@OptIn(ExperimentalPagerApi::class) | ||
@Composable | ||
fun DefaultHorizontalPagerIndicator( | ||
pagerState: PagerState, | ||
modifier: Modifier = Modifier, | ||
pageCount: Int = pagerState.pageCount, | ||
pageIndexMapping: (Int) -> Int = { it }, | ||
activeColor: Color = CinemaxTheme.colors.primaryBlue, | ||
inactiveColor: Color = activeColor.copy(PagerIndicatorInactiveColorAlpha), | ||
activeIndicatorWidth: Dp = CinemaxTheme.spacing.extraMedium, | ||
indicatorWidth: Dp = CinemaxTheme.spacing.small, | ||
indicatorHeight: Dp = indicatorWidth, | ||
spacing: Dp = indicatorWidth, | ||
indicatorShape: Shape = CircleShape, | ||
) { | ||
val indicatorWidthPx = LocalDensity.current.run { indicatorWidth.roundToPx() } | ||
val spacingPx = LocalDensity.current.run { spacing.roundToPx() } | ||
|
||
Box( | ||
modifier = modifier, | ||
contentAlignment = Alignment.CenterStart | ||
) { | ||
Row( | ||
horizontalArrangement = Arrangement.spacedBy(spacing), | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
repeat(pageCount) { page -> | ||
val horizontalPadding by animateDpAsState( | ||
targetValue = if (page == pagerState.currentPage) spacing else 0.dp | ||
) | ||
Box( | ||
modifier = Modifier | ||
.padding(horizontal = horizontalPadding) | ||
.size(width = indicatorWidth, height = indicatorHeight) | ||
.background(color = inactiveColor, shape = indicatorShape) | ||
) | ||
} | ||
} | ||
|
||
Box( | ||
Modifier | ||
.offset { | ||
val position = pageIndexMapping(pagerState.currentPage) | ||
val offset = pagerState.currentPageOffset | ||
val next = pageIndexMapping(pagerState.currentPage + offset.sign.toInt()) | ||
val scrollPosition = ((next - position) * offset.absoluteValue + position) | ||
.coerceIn( | ||
minimumValue = 0f, | ||
maximumValue = (pageCount - 1) | ||
.coerceAtLeast(minimumValue = 0) | ||
.toFloat() | ||
) | ||
|
||
IntOffset( | ||
x = ((spacingPx + indicatorWidthPx) * scrollPosition).toInt(), | ||
y = 0 | ||
) | ||
} | ||
.size(width = activeIndicatorWidth, height = indicatorHeight) | ||
.background(color = activeColor, shape = indicatorShape) | ||
) | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalPagerApi::class) | ||
fun Modifier.pagerTransition( | ||
pagerScope: PagerScope, | ||
page: Int | ||
) = graphicsLayer { | ||
// Calculate the absolute offset for the current page from the | ||
// scroll position. We use the absolute value which allows us to mirror | ||
// any effects for both directions | ||
val pageOffset = pagerScope.calculateCurrentOffsetForPage(page).absoluteValue | ||
|
||
// We animate the scaleX + scaleY, between 85% and 100% | ||
lerp( | ||
start = 0.85f, | ||
stop = 1f, | ||
fraction = 1f - pageOffset.coerceIn(0f, 1f) | ||
).also { scale -> | ||
scaleX = scale | ||
scaleY = scale | ||
} | ||
|
||
// We animate the alpha, between 50% and 100% | ||
alpha = lerp( | ||
start = 0.5f, | ||
stop = 1f, | ||
fraction = 1f - pageOffset.coerceIn(0f, 1f) | ||
) | ||
} | ||
|
||
private const val PagerIndicatorInactiveColorAlpha = 0.32f |
Oops, something went wrong.