Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement home screen components and UpcomingMovie components #89

Merged
merged 6 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/core-presentation/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
limitations under the License.
-->
<resources>
<string name="movie">Movie</string>
<string name="see_all">See All</string>
</resources>
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,42 @@ import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import com.maximillianleonov.cinemax.core.presentation.theme.CinemaxTheme
import com.maximillianleonov.cinemax.feature.home.presentation.components.UpcomingMoviesContainer

@Composable
fun HomeRoute(modifier: Modifier = Modifier) {
HomeScreen(modifier = modifier)
fun HomeRoute(
modifier: Modifier = Modifier,
viewModel: HomeViewModel = hiltViewModel()
) {
val uiState by viewModel.uiState.collectAsState()
HomeScreen(
uiState = uiState,
modifier = modifier
)
}

@Composable
internal fun HomeScreen(modifier: Modifier = Modifier) {
internal fun HomeScreen(
uiState: HomeUiState,
modifier: Modifier = Modifier
) {
LazyColumn(
modifier = modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(CinemaxTheme.spacing.extraMedium),
contentPadding = PaddingValues(vertical = CinemaxTheme.spacing.extraMedium)
) {
item {
@Suppress("ForbiddenComment")
UpcomingMoviesContainer(
movies = uiState.upcomingMovies,
isMoviesLoading = uiState.isUpcomingMoviesLoading,
onSeeAllClick = { /* TODO: Not yet implemented. */ }
)
}
}
}
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
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
)
}
}
)
}
}
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
Loading