Cotton code
120 lines · 4.3 KB · SwiftRaw
// The in-call screen: Loom's `call.telephone` face over the call as the core// holds it (`CallCenter.call`), standing over the app while a call is under// way and for a moment after it ends. Its words are the core's (`calling`// line set); the ring here is CallKit's screen, not this one.#if os(iOS)import CottonFacesimport CottonKitimport SwiftUIimport WebRTCstruct CallScreen: View {    let center: CallCenter    let video: CallVideo    /// The ended call's last words stay this long before the screen goes.    private static let endedFor: Duration = .seconds(2)    @State private var shownEnded: String?    @State private var now = Date()    var body: some View {        Group {            if let state = state() {                CallTelephoneFace(                    state: state,                    picture: nil,                    remote: state.remoteVideo ? AnyView(CallVideoView(track: video.remote)) : nil,                    local: center.call.sendingVideo ? AnyView(CallVideoView(track: video.local, mirrored: true)) : nil,                    acts: CallTelephoneActs(                        mute: { center.toggleMute() },                        speaker: { center.toggleSpeaker() },                        video: { center.toggleVideo() },                        end: { center.end() }                    )                )                .transition(.opacity)                .accessibilityIdentifier("call-screen")            }        }        .animation(.easeInOut(duration: 0.25), value: center.call.phase)        .task(id: center.call.phase) {            if center.call.phase == .ended {                shownEnded = center.call.uuid                try? await Task.sleep(for: Self.endedFor)                shownEnded = nil            }        }        .task(id: center.call.liveAt) {            // The length of a live call, a second at a time.            while center.call.phase == .live, !Task.isCancelled {                now = Date()                try? await Task.sleep(for: .seconds(1))            }        }    }    /// What the face draws, or nothing when no call stands here: idle, a ring    /// (CallKit's screen), or an end that has had its moment.    private func state() -> CallTelephoneState? {        let call = center.call        switch call.phase {        case .idle, .ringingIn: return nil        case .ended where shownEnded != call.uuid: return nil        default: break        }        return CallTelephoneState(            name: call.name ?? call.number ?? "",            status: status(call),            live: call.phase == .live,            muted: !call.sendingAudio,            speaker: center.speaker,            video: call.sendingVideo,            remoteVideo: call.video && video.remote != nil && call.phase == .live        )    }    private func status(_ call: CallState) -> String {        switch call.phase {        case .live:            let seconds = max(0, Int(now.timeIntervalSince(call.liveAt ?? now)))            return seconds >= 3600                ? String(format: "%d:%02d:%02d", seconds / 3600, seconds / 60 % 60, seconds % 60)                : String(format: "%02d:%02d", seconds / 60, seconds % 60)        case .ended:            return call.reason.map { ShellLines.line("calling", "ended.\($0)") } ?? ShellLines.line("calling", "phase.ended")        default:            return ShellLines.line("calling", "phase.\(call.phase.rawValue)")        }    }}/// A WebRTC video track drawn by Metal.struct CallVideoView: UIViewRepresentable {    let track: RTCVideoTrack?    var mirrored = false    final class Coordinator {        var drawn: RTCVideoTrack?    }    func makeCoordinator() -> Coordinator { Coordinator() }    func makeUIView(context: Context) -> RTCMTLVideoView {        let view = RTCMTLVideoView()        view.videoContentMode = .scaleAspectFill        view.transform = mirrored ? CGAffineTransform(scaleX: -1, y: 1) : .identity        return view    }    func updateUIView(_ view: RTCMTLVideoView, context: Context) {        guard context.coordinator.drawn !== track else { return }        context.coordinator.drawn?.remove(view)        track?.add(view)        context.coordinator.drawn = track    }    static func dismantleUIView(_ view: RTCMTLVideoView, coordinator: Coordinator) {        coordinator.drawn?.remove(view)    }}#endif