getsidetrack / swiftui-pipify

A SwiftUI component for launching custom picture-in-picture experiences
MIT License
155 stars 14 forks source link

Add Redraw Safety to Pipify Modifier #23

Open Sherlouk opened 2 years ago

Sherlouk commented 2 years ago

SwiftUI redraws views often, this is a big part of it's architecture to allow for quick and dynamic updates to views. Unfortunately, this causes issues with Pipify because it regenerates the PIP controller every time.

This has been seen in this Twitter thread (https://twitter.com/aoverholtzer/status/1546249575748358144) and my own experiences.

We need to ensure we protect against this.

Sherlouk commented 2 years ago

Not 100% sure on how to approach this so any input is value.

One potential idea I've been exploring (and relates partially to #3 and #13) is around having a single Pipify controller for the entire application.

We would simply switch the underlying view if the developer wants to support multiple (different) experiences. This could be done with a singleton quite trivially and would eliminate this issue entirely.

I also think by disconnecting the experience from the controller we can help with performance - this is because we wouldn't need to be constantly rendering the view in the background. We would only do this when the user wants to present PIP.

This helps with performance across the board (CPU, memory, battery impact), resolves the issue with multiple controllers, and keeps the simple API.

adamtow commented 2 years ago

I'd second the singleton controller. I've seen the reinitialization of the Pipify controller in my app. This might prevent the Pipify view from toggling on and off:

isPresented changed to true
2022-07-11 08:32:46.734661-0700 MixEffect[553:28587] [Pipify] activating audio session
2022-07-11 08:32:46.755616-0700 MixEffect[553:28587] [Pipify] starting picture in picture
2022-07-11 08:32:46.756007-0700 MixEffect[553:28587] [Pipify] failed to start: Failed to start picture in picture.
2022-07-11 08:32:46.760434-0700 MixEffect[553:28587] [Pipify] configuring audio session
2022-07-11 08:32:46.760749-0700 MixEffect[553:28587] [Pipify] creating pip controller
2022-07-11 08:32:46.769115-0700 MixEffect[553:28587] [Pipify] isPresented changed to false
2022-07-11 08:32:46.769139-0700 MixEffect[553:28587] [Pipify] stopping picture in picture
2022-07-11 08:32:46.769146-0700 MixEffect[553:28587] [Pipify] deactivating audio session