-
Notifications
You must be signed in to change notification settings - Fork 3
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
Conversation
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.
Nice catch! Just some minor remarks.
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", | ||
) |
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.
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.
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 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.
|
||
fun Context.requireActivity(): Activity = | ||
when (this) { | ||
is Activity -> this | ||
is ContextWrapper -> baseContext.requireActivity() | ||
else -> error("No activity found for context $this") | ||
} |
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.
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
.
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.
Maybe we can also have a slightly more verbose error message in case we don't have an activity. 🙂
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.
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
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.
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 |
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.
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) |
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.
The activity should always be null or a LifecycleOwner, right?
(activity as? LifecycleOwner) | |
(activity as LifecycleOwner?) |
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.
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 |
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.
throw instead of returning null? this could even be inlined.
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 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.
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.
Nice!
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
Context.requireActivity()
to get activity reference from the ContextTests
Checklist (for PR submitters and reviewers)
CHANGELOG.md
entry for new/changed features, bug fixes or important code changes