-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature]Extract video mirroring and callSettings observation to view…
…Modifiers Allowing integrators to override their usage by using directly the VideoCallParticipantView without applying the modifiers or by overriding the ViewFactory method.
- Loading branch information
1 parent
3feeca9
commit 999851f
Showing
7 changed files
with
147 additions
and
17 deletions.
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
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
59 changes: 59 additions & 0 deletions
59
...es/StreamVideoSwiftUI/CallView/ViewModifiers/FromCameraUsageObservationViewModifier.swift
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,59 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamVideo | ||
import SwiftUI | ||
|
||
private struct FromCameraUsageObservationViewModifier: ViewModifier { | ||
|
||
/// Injects the StreamVideo instance from the environment to access the current active call and its state. | ||
@Injected(\.streamVideo) private var streamVideo | ||
|
||
/// Tracks whether the front camera is being used by the local user. | ||
@State var isUsingFrontCameraForLocalUser: Bool = false | ||
|
||
/// The current call whose state is being observed for camera settings. | ||
var call: Call? | ||
|
||
/// The participant whose view might need to observe front camera usage. | ||
var participant: CallParticipant | ||
|
||
/// Defines the body of the view modifier. | ||
/// - Parameter content: The content view that is being modified. | ||
/// - Returns: A view that updates its state based on the camera position of the local user. | ||
func body(content: Content) -> some View { | ||
if participant.id == streamVideo.state.activeCall?.state.localParticipant?.id { | ||
content | ||
// Observes changes to the call's settings and updates the `isUsingFrontCameraForLocalUser` state accordingly. | ||
.onReceive(call?.state.$callSettings) { | ||
self.isUsingFrontCameraForLocalUser = $0.cameraPosition == .front | ||
} | ||
} else { | ||
content | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
|
||
/// Observes whether the front camera is being used by the local user during a call. | ||
/// This modifier listens for updates to the camera settings in the call and updates the view. | ||
/// - Parameters: | ||
/// - call: The `Call` instance whose camera settings are being observed. | ||
/// - participant: The `CallParticipant` that might need to observe from camera usage. | ||
/// - Returns: A view that reflects changes based on the front camera usage. | ||
@ViewBuilder | ||
public func frontCameraUsageObservation( | ||
call: Call?, | ||
participant: CallParticipant | ||
) -> some View { | ||
// Applies the FromCameraUsageObservationViewModifier to observe the camera usage. | ||
modifier( | ||
FromCameraUsageObservationViewModifier( | ||
call: call, | ||
participant: participant | ||
) | ||
) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...ces/StreamVideoSwiftUI/CallView/ViewModifiers/LocalParticipantMirroringViewModifier.swift
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,59 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamVideo | ||
import SwiftUI | ||
|
||
private struct LocalParticipantMirroringViewModifier: ViewModifier { | ||
|
||
/// Injects the StreamVideo instance from the environment to access the current active call and its state. | ||
@Injected(\.streamVideo) private var streamVideo | ||
|
||
/// The participant whose view might need to be mirrored. | ||
var participant: CallParticipant | ||
|
||
/// The angle by which the content should be rotated if mirroring is applied. | ||
var angle: Angle | ||
|
||
/// The axis around which the content will be rotated in 3D space. | ||
var axis: (x: CGFloat, y: CGFloat, z: CGFloat) | ||
|
||
/// Defines the body of the view modifier. | ||
/// - Parameter content: The content view that is being modified. | ||
/// - Returns: A view that is conditionally mirrored if the participant is the local participant. | ||
func body(content: Content) -> some View { | ||
// If the participant is the local participant, apply a 3D rotation to mirror their view. | ||
if participant.id == streamVideo.state.activeCall?.state.localParticipant?.id { | ||
content.rotation3DEffect(angle, axis: axis) | ||
} else { | ||
// Otherwise, display the content without modification. | ||
content | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
|
||
/// Applies a mirroring effect to the view if the provided participant is the local participant. | ||
/// This is useful for displaying the local participant's video feed with a mirror effect in the UI. | ||
/// - Parameters: | ||
/// - participant: The `CallParticipant` that might need to be mirrored. | ||
/// - angle: The rotation angle (default is 180 degrees). | ||
/// - axis: The axis of rotation (default is around the Y-axis). | ||
/// - Returns: A view that conditionally applies the mirroring effect. | ||
public func localParticipantMirroring( | ||
participant: CallParticipant, | ||
angle: Angle = .degrees(180), | ||
axis: (x: CGFloat, y: CGFloat, z: CGFloat) = (x: 0, y: 1, z: 0) | ||
) -> some View { | ||
// Apply the LocalParticipantMirroringViewModifier with the specified participant, angle, and axis. | ||
modifier( | ||
LocalParticipantMirroringViewModifier( | ||
participant: participant, | ||
angle: angle, | ||
axis: axis | ||
) | ||
) | ||
} | ||
} |
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
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