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

Add missing PlayerView lifecycle handling (PiP #1) #85

Merged
merged 7 commits into from
Dec 22, 2023

Conversation

zigavehovec
Copy link
Contributor

@zigavehovec zigavehovec commented Dec 18, 2023

Description

Similar to RN wrapper before this PR the PlayerView's lifecycle handling is missing and so the player doesn't pause when app goes to the background.

This PR is also prework for PiP implementation, as we will need activity reference to implement it.

Changes

  • Add Context.requireActivity() to get activity reference from the Context
  • Add lifecycle handling using the LifecycleOwner pattern

Tests

Checklist (for PR submitters and reviewers)

  • 🗒 CHANGELOG.md entry for new/changed features, bug fixes or important code changes
  • 🧪 Tests added and/or updated
  • 📢 New public API is fully documented

@zigavehovec zigavehovec self-assigned this Dec 18, 2023
@zigavehovec zigavehovec marked this pull request as ready for review December 18, 2023 16:11
@zigavehovec zigavehovec changed the title Add missing PlayerView lifecycle handling Add missing PlayerView lifecycle handling (PiP 1) Dec 18, 2023
@zigavehovec zigavehovec changed the title Add missing PlayerView lifecycle handling (PiP 1) Add missing PlayerView lifecycle handling (PiP #1) Dec 18, 2023
Copy link
Contributor

@strangesource strangesource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Just some minor remarks.

Comment on lines 43 to 49
private val activityLifecycle =
activity.let { it as? FlutterActivity ?: it as? FlutterFragmentActivity }
?.lifecycle
?: error(
"Trying to create an instance of ${this::class.simpleName}" +
" while not attached to a FlutterActivity or FlutterFragmentActivity",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section Alternatives to FlutterActivity here indicate that a Flutter view can be part of different types of activities. While this is probably not the common use case we could just use

(activity as? LifecycleOwner)

instead to be more resilient.

Copy link
Contributor Author

@zigavehovec zigavehovec Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read through the docs and I don't expect any problems because of this. But I also see no issue in just using the LifecycleOwner observer cast, so let's do that just to be safe.

bd37436

Comment on lines 31 to 37

fun Context.requireActivity(): Activity =
when (this) {
is Activity -> this
is ContextWrapper -> baseContext.requireActivity()
else -> error("No activity found for context $this")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I am not a big fan of a single big util file. As it is - for now - only used once, can we just have it private in the FlutterPlayerView.kt? If you already have another use case for this in mind, please create a more specific file like e.g. ContextExtension.kt.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also have a slightly more verbose error message in case we don't have an activity. 🙂

Copy link
Contributor Author

@zigavehovec zigavehovec Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, big utils class aren't pretty. Not sure yet if we'll need this again so I made it private in the same file.
d315606

Copy link
Contributor

@krocard krocard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, just a few nits.

@@ -58,7 +85,7 @@ class FlutterPlayerView(
) = when (call.method) {
Methods.ENTER_FULLSCREEN -> playerView.enterFullscreen()
Methods.EXIT_FULLSCREEN -> playerView.exitFullscreen()
Methods.DESTROY_PLAYER_VIEW -> { /* no-op */ }
Methods.DESTROY_PLAYER_VIEW -> Unit // no-op
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like this style more 👍

?: error("Trying to create an instance of ${this::class.simpleName} while not attached to an Activity")

private val activityLifecycle =
(activity as? LifecycleOwner)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity should always be null or a LifecycleOwner, right?

Suggested change
(activity as? LifecycleOwner)
(activity as LifecycleOwner?)

Copy link
Contributor Author

@zigavehovec zigavehovec Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The activity should never be null at this point, but it might not extend LifecycleOwner.

when (this) {
is Activity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw instead of returning null? this could even be inlined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went for nullable approach to be able to provide a more descriptive error message where it's used (FlutterPlayerView.kt:46).
Since we might also need it somewhere else I decided to just leave it generic, so that it can just be extracted in that case.

Copy link
Contributor

@strangesource strangesource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@zigavehovec zigavehovec mentioned this pull request Dec 21, 2023
3 tasks
@zigavehovec zigavehovec merged commit bd2b533 into main Dec 22, 2023
3 checks passed
@zigavehovec zigavehovec deleted the PFL-83/add-lifecycle-handling branch December 22, 2023 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants