-
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.
- Loading branch information
1 parent
e6c604d
commit be9caef
Showing
10 changed files
with
412 additions
and
133 deletions.
There are no files selected for viewing
105 changes: 0 additions & 105 deletions
105
DemoApp/Sources/Components/Feedback/CallEndedViewModifier.swift
This file was deleted.
Oops, something went wrong.
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
195 changes: 195 additions & 0 deletions
195
Sources/StreamVideoSwiftUI/CallView/ViewModifiers/CallEndedViewModifier.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,195 @@ | ||
// | ||
// Copyright © 2024 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Combine | ||
import Foundation | ||
import StreamVideo | ||
import SwiftUI | ||
|
||
private final class CallEndedViewModifierViewModel: ObservableObject { | ||
|
||
@Injected(\.streamVideo) private var streamVideo | ||
|
||
@Published var activeCall: Call? | ||
@Published var lastCall: Call? | ||
@Published var isPresentingSubview: Bool = false | ||
@Published var maxParticipantsCount: Int = 0 | ||
|
||
private var observationCancellable: AnyCancellable? | ||
|
||
init() { | ||
observationCancellable = streamVideo | ||
.state | ||
.$activeCall | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] in self?.activeCall = $0 } | ||
} | ||
} | ||
|
||
@available(iOS 14.0, *) | ||
private struct CallEndedViewModifier<Subview: View>: ViewModifier { | ||
|
||
private var subviewProvider: (Call?, @escaping () -> Void) -> Subview | ||
|
||
@StateObject private var viewModel: CallEndedViewModifierViewModel | ||
|
||
init( | ||
@ViewBuilder subviewProvider: @escaping (Call?, @escaping () -> Void) -> Subview | ||
) { | ||
self.subviewProvider = subviewProvider | ||
_viewModel = .init(wrappedValue: .init()) | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.sheet(isPresented: $viewModel.isPresentingSubview) { | ||
subviewProvider(viewModel.lastCall) { | ||
viewModel.lastCall = nil | ||
viewModel.isPresentingSubview = false | ||
} | ||
} | ||
.onReceive(viewModel.$activeCall) { call in | ||
log | ||
.debug( | ||
"CallEnded view modifier received newValue:\(call?.cId ?? "nil") oldValue:\(viewModel.lastCall?.cId ?? "nil") isPresentingSubview:\(viewModel.isPresentingSubview) maxParticipantsCount:\(viewModel.maxParticipantsCount)." | ||
) | ||
|
||
switch (call, viewModel.lastCall, viewModel.isPresentingSubview) { | ||
case (nil, let activeCall, false) where activeCall != nil && viewModel.maxParticipantsCount > 1: | ||
/// The following presentation criteria are required: | ||
/// - The activeCall was ended. | ||
/// - Participants, during call's duration, grew to more than one. | ||
viewModel.isPresentingSubview = true | ||
|
||
case let (newActiveCall, activeCall, _) where newActiveCall != nil && activeCall != nil: | ||
/// The activeCall was replaced with another call. We should not present the | ||
/// subview. We will also hide any modals if any is visible. | ||
viewModel.lastCall = newActiveCall | ||
viewModel.isPresentingSubview = false | ||
viewModel.maxParticipantsCount = 0 | ||
|
||
case (let newActiveCall, nil, _) where newActiveCall != nil: | ||
/// A new call has started. We should not present the subview. We will also hide | ||
/// any modals if any is visible. | ||
viewModel.lastCall = newActiveCall | ||
viewModel.isPresentingSubview = false | ||
viewModel.maxParticipantsCount = 0 | ||
|
||
default: | ||
/// For every other case we won't perform any action. | ||
break | ||
} | ||
} | ||
.onReceive(viewModel.activeCall?.state.$participants) { | ||
/// Every time participants update, we store the maximum number of participants in | ||
/// the call (during call's duration). | ||
let newMaxParticipantsCount = max(viewModel.maxParticipantsCount, $0.count) | ||
if newMaxParticipantsCount != viewModel.maxParticipantsCount { | ||
log | ||
.debug( | ||
"CallEnded view modifier updated maxParticipantsCount:\(viewModel.maxParticipantsCount) → \(newMaxParticipantsCount)" | ||
) | ||
viewModel.maxParticipantsCount = newMaxParticipantsCount | ||
} | ||
} | ||
} | ||
} | ||
|
||
@available(iOS, introduced: 13, obsoleted: 14) | ||
private struct CallEndedViewModifier_iOS13<Subview: View>: ViewModifier { | ||
|
||
private var subviewProvider: (Call?, @escaping () -> Void) -> Subview | ||
|
||
@BackportStateObject private var viewModel: CallEndedViewModifierViewModel = .init() | ||
|
||
init( | ||
@ViewBuilder subviewProvider: @escaping (Call?, @escaping () -> Void) -> Subview | ||
) { | ||
self.subviewProvider = subviewProvider | ||
} | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.sheet(isPresented: $viewModel.isPresentingSubview) { | ||
subviewProvider(viewModel.lastCall) { | ||
viewModel.lastCall = nil | ||
viewModel.isPresentingSubview = false | ||
} | ||
} | ||
.onReceive(viewModel.$activeCall) { call in | ||
log | ||
.debug( | ||
"CallEnded view modifier received newValue:\(call?.cId ?? "nil") oldValue:\(viewModel.lastCall?.cId ?? "nil") isPresentingSubview:\(viewModel.isPresentingSubview) maxParticipantsCount:\(viewModel.maxParticipantsCount)." | ||
) | ||
|
||
switch (call, viewModel.lastCall, viewModel.isPresentingSubview) { | ||
case (nil, let activeCall, false) where activeCall != nil && viewModel.maxParticipantsCount > 1: | ||
/// The following presentation criteria are required: | ||
/// - The activeCall was ended. | ||
/// - Participants, during call's duration, grew to more than one. | ||
viewModel.isPresentingSubview = true | ||
|
||
case let (newActiveCall, activeCall, _) where newActiveCall != nil && activeCall != nil: | ||
/// The activeCall was replaced with another call. We should not present the | ||
/// subview. We will also hide any modals if any is visible. | ||
viewModel.lastCall = newActiveCall | ||
viewModel.isPresentingSubview = false | ||
viewModel.maxParticipantsCount = 0 | ||
|
||
case (let newActiveCall, nil, _) where newActiveCall != nil: | ||
/// The activeCall was replaced with another call. We should not present the | ||
/// subview. We will also hide any modals if any is visible. | ||
viewModel.lastCall = newActiveCall | ||
viewModel.isPresentingSubview = false | ||
viewModel.maxParticipantsCount = 0 | ||
|
||
default: | ||
/// For every other case we won't perform any action. | ||
break | ||
} | ||
} | ||
.onReceive(viewModel.activeCall?.state.$participants) { | ||
/// Every time participants update, we store the maximum number of participants in | ||
/// the call (during call's duration). | ||
let newMaxParticipantsCount = max(viewModel.maxParticipantsCount, $0.count) | ||
if newMaxParticipantsCount != viewModel.maxParticipantsCount { | ||
log | ||
.debug( | ||
"CallEnded view modifier updated maxParticipantsCount:\(viewModel.maxParticipantsCount) → \(newMaxParticipantsCount)" | ||
) | ||
viewModel.maxParticipantsCount = newMaxParticipantsCount | ||
} | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
|
||
/// A viewModifier that observes callState from StreamVideo. Once the following criteria are being | ||
/// fulfilled, presents a modal with the provided content. | ||
/// Activation criteria: | ||
/// - Active call was ended. | ||
/// - Participants, during call's duration, grew to more than one. | ||
/// | ||
/// - Parameter content: A viewBuilder that returns the modal's content. The viewModifier | ||
/// will provide a dismiss closure that can be called from the content to close the modal. | ||
@ViewBuilder | ||
public func onCallEnded( | ||
@ViewBuilder _ content: @escaping (Call?, @escaping () -> Void) -> some View | ||
) -> some View { | ||
if #available(iOS 14.0, *) { | ||
modifier( | ||
CallEndedViewModifier( | ||
subviewProvider: content | ||
) | ||
) | ||
} else { | ||
modifier( | ||
CallEndedViewModifier_iOS13( | ||
subviewProvider: content | ||
) | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.