-
Notifications
You must be signed in to change notification settings - Fork 226
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
End of Year: Add stories datasource + gestures #486
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c0287fc
Add stories vm
ashiagr 05ff9e9
Add loading and error views
ashiagr d8f6279
Get fake stories from data source
ashiagr c8168ab
Get progress from vm
ashiagr 1044a5d
Add gestures and prev/ next actions
ashiagr 8963b0e
Add tests
ashiagr 1837925
Make loadStories a suspend function
ashiagr 3f138ea
Add delay to show loading
ashiagr 930cf6e
Improve stories visibility
ashiagr 50b61a8
Replace timer cancelled with timer null check
ashiagr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 13 additions & 0 deletions
13
...res/endofyear/src/main/java/au/com/shiftyjelly/pocketcasts/endofyear/StoriesDataSource.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,13 @@ | ||
package au.com.shiftyjelly.pocketcasts.endofyear | ||
|
||
import au.com.shiftyjelly.pocketcasts.endofyear.stories.Story | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface StoriesDataSource { | ||
var stories: List<Story> | ||
val totalLengthInMs | ||
get() = stories.sumOf { it.storyLength } | ||
|
||
suspend fun loadStories(): Flow<List<Story>> | ||
fun storyAt(index: Int): Story? | ||
} |
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
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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
package au.com.shiftyjelly.pocketcasts.endofyear | ||
|
||
import androidx.compose.animation.core.LinearEasing | ||
import androidx.compose.animation.core.animateFloatAsState | ||
import androidx.compose.animation.core.tween | ||
import androidx.annotation.FloatRange | ||
import androidx.compose.foundation.BorderStroke | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.gestures.detectTapGestures | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
|
@@ -14,68 +13,125 @@ import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.ButtonDefaults | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.IconButton | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.Share | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.input.pointer.pointerInput | ||
import androidx.compose.ui.layout.onGloballyPositioned | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.tooling.preview.PreviewParameter | ||
import androidx.compose.ui.unit.dp | ||
import au.com.shiftyjelly.pocketcasts.compose.AppTheme | ||
import au.com.shiftyjelly.pocketcasts.compose.bars.NavigationButton | ||
import au.com.shiftyjelly.pocketcasts.compose.buttons.RowOutlinedButton | ||
import au.com.shiftyjelly.pocketcasts.compose.components.TextP50 | ||
import au.com.shiftyjelly.pocketcasts.compose.preview.ThemePreviewParameterProvider | ||
import au.com.shiftyjelly.pocketcasts.endofyear.StoriesViewModel.State | ||
import au.com.shiftyjelly.pocketcasts.endofyear.stories.Story | ||
import au.com.shiftyjelly.pocketcasts.endofyear.stories.StoryFake1 | ||
import au.com.shiftyjelly.pocketcasts.ui.theme.Theme | ||
import au.com.shiftyjelly.pocketcasts.localization.R as LR | ||
|
||
private const val ProgressDurationMs = 5_000 | ||
private val ShareButtonStrokeWidth = 2.dp | ||
private val StoryViewCornerSize = 10.dp | ||
private const val NumberOfSegments = 2 | ||
|
||
@Composable | ||
fun StoriesScreen( | ||
viewModel: StoriesViewModel, | ||
onCloseClicked: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
var running by remember { mutableStateOf(false) } | ||
val progress: Float by animateFloatAsState( | ||
if (running) 1f else 0f, | ||
animationSpec = tween( | ||
durationMillis = ProgressDurationMs, | ||
easing = LinearEasing | ||
val state: State by viewModel.state.collectAsState() | ||
val progress: Float by viewModel.progress.collectAsState() | ||
when (state) { | ||
is State.Loaded -> StoriesView( | ||
state = state as State.Loaded, | ||
progress = progress, | ||
onSkipPrevious = { viewModel.skipPrevious() }, | ||
onSkipNext = { viewModel.skipNext() }, | ||
onPause = { viewModel.pause() }, | ||
onStart = { viewModel.start() }, | ||
onCloseClicked = onCloseClicked | ||
) | ||
) | ||
Box(modifier = modifier.background(color = Color.Black)) { | ||
StoryView(color = Color.Gray) | ||
State.Loading -> StoriesLoadingView(onCloseClicked) | ||
State.Error -> StoriesErrorView(onCloseClicked) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun StoriesView( | ||
state: State.Loaded, | ||
@FloatRange(from = 0.0, to = 1.0) progress: Float, | ||
onSkipPrevious: () -> Unit, | ||
onSkipNext: () -> Unit, | ||
onPause: () -> Unit, | ||
onStart: () -> Unit, | ||
onCloseClicked: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
var screenWidth by remember { mutableStateOf(1) } | ||
var isPaused by remember { mutableStateOf(false) } | ||
Box( | ||
modifier = modifier | ||
.fillMaxSize() | ||
.background(color = Color.Black) | ||
.onGloballyPositioned { | ||
screenWidth = it.size.width | ||
} | ||
.pointerInput(Unit) { | ||
detectTapGestures( | ||
onTap = { | ||
if (!isPaused) { | ||
if (it.x > screenWidth / 2) { | ||
onSkipNext() | ||
} else { | ||
onSkipPrevious() | ||
} | ||
} | ||
}, | ||
onLongPress = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used |
||
isPaused = true | ||
onPause() | ||
}, | ||
onPress = { | ||
awaitRelease() | ||
if (isPaused) { | ||
onStart() | ||
isPaused = false | ||
} | ||
} | ||
) | ||
} | ||
) { | ||
state.currentStory?.let { | ||
StoryView(it) | ||
} | ||
SegmentedProgressIndicator( | ||
progress = progress, | ||
numberOfSegments = NumberOfSegments, | ||
numberOfSegments = state.numberOfStories, | ||
modifier = modifier | ||
.padding(8.dp) | ||
.fillMaxWidth(), | ||
) | ||
CloseButtonView(onCloseClicked) | ||
} | ||
|
||
LaunchedEffect(Unit) { | ||
running = true | ||
} | ||
} | ||
|
||
@Composable | ||
private fun StoryView( | ||
color: Color, | ||
story: Story, | ||
modifier: Modifier = Modifier, | ||
) { | ||
Column { | ||
|
@@ -84,7 +140,7 @@ private fun StoryView( | |
.fillMaxSize() | ||
.weight(weight = 1f, fill = true) | ||
.clip(RoundedCornerShape(StoryViewCornerSize)) | ||
.background(color = color) | ||
.background(color = story.backgroundColor) | ||
) {} | ||
ShareButton() | ||
} | ||
|
@@ -128,13 +184,93 @@ private fun CloseButtonView( | |
} | ||
} | ||
|
||
@Composable | ||
private fun StoriesLoadingView( | ||
onCloseClicked: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
StoriesEmptyView( | ||
content = { CircularProgressIndicator(color = Color.White) }, | ||
onCloseClicked = onCloseClicked, | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Composable | ||
private fun StoriesErrorView( | ||
onCloseClicked: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
) { | ||
StoriesEmptyView( | ||
content = { | ||
TextP50( | ||
text = "Failed to load stories.", // TODO: replace hardcoded text | ||
ashiagr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
color = Color.White, | ||
) | ||
}, | ||
onCloseClicked = onCloseClicked, | ||
modifier = modifier | ||
) | ||
} | ||
|
||
@Composable | ||
private fun StoriesEmptyView( | ||
onCloseClicked: () -> Unit, | ||
modifier: Modifier = Modifier, | ||
content: @Composable () -> Unit = {}, | ||
) { | ||
Box( | ||
modifier = modifier | ||
.fillMaxSize() | ||
.background(color = Color.Black) | ||
) { | ||
CloseButtonView(onCloseClicked) | ||
Box( | ||
contentAlignment = Alignment.Center, | ||
modifier = modifier.fillMaxSize() | ||
) { | ||
content() | ||
} | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun StoriesScreenPreview( | ||
@PreviewParameter(ThemePreviewParameterProvider::class) themeType: Theme.ThemeType, | ||
) { | ||
AppTheme(themeType) { | ||
StoriesScreen( | ||
StoriesView( | ||
state = State.Loaded(currentStory = StoryFake1(), numberOfStories = 2), | ||
progress = 0.5f, | ||
onSkipPrevious = {}, | ||
onSkipNext = {}, | ||
onPause = {}, | ||
onStart = {}, | ||
onCloseClicked = {} | ||
) | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun StoriesLoadingViewPreview( | ||
@PreviewParameter(ThemePreviewParameterProvider::class) themeType: Theme.ThemeType, | ||
) { | ||
AppTheme(themeType) { | ||
StoriesLoadingView( | ||
onCloseClicked = {} | ||
) | ||
} | ||
} | ||
|
||
@Preview(showBackground = true) | ||
@Composable | ||
private fun StoriesErrorViewPreview( | ||
@PreviewParameter(ThemePreviewParameterProvider::class) themeType: Theme.ThemeType, | ||
) { | ||
AppTheme(themeType) { | ||
StoriesErrorView( | ||
onCloseClicked = {} | ||
) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't able to find a way to trigger
onTap
whileonPause
wastrue
. This obviously isn't doing any harm, but maybe with the press and long press handling we don't need to track theonPause
state manually anymore? Or maybe I just didn't find the right way to trigger it. πThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
onPress
gets triggered for bothonTap
andonLongPress
and I want to re-start the timer only whenonLongPress
is triggered which is why I'm manually trackingonPause
state. I'll see if I can improve this behavior as I noted here.Thank you for finding all these discussion points!