Without looking at the code, here's an idea: on every update you want to transform the camera in some way, like moving, rotating or scaling it. If the camera + that transformation will cause the object to go out of the screen, then don't transform the camera on that update.
I think this type of logic is called "filtering". Using any tests you want, you are filtering out any transformations that will cause the camera to look away from one or more objects before applying the transformation.
(This works only if the objects are static.)
Another solution is to compensate \ adjust after the fact: do move the camera every update, but in case one or more objects go out of frame, compute a movement or rotation for that camera that will cause it to bring them all back into view (eg aim the camera at the median* of all object locations, then dolly the camera back until all objects fit within the visible volume).
*Use the median of their locations (the center of the sum of their bounds), rather than using their average locations (sum locations and divide by n), because if you use the average the camera will always focus on clustered objects rather than every object equally.