mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 22:55:00 +00:00
[WIP]
This commit is contained in:
@@ -1,7 +1,42 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
import Display
|
||||
import MetalEngine
|
||||
|
||||
public final class PrivateCallScreen: UIView {
|
||||
public struct State: Equatable {
|
||||
public struct SignalInfo: Equatable {
|
||||
public var quality: Double
|
||||
|
||||
public init(quality: Double) {
|
||||
self.quality = quality
|
||||
}
|
||||
}
|
||||
|
||||
public enum LifecycleState: Equatable {
|
||||
case connecting
|
||||
case ringing
|
||||
case exchangingKeys
|
||||
case active(startTime: Double, signalInfo: SignalInfo)
|
||||
}
|
||||
|
||||
public var lifecycleState: LifecycleState
|
||||
|
||||
public init(lifecycleState: LifecycleState) {
|
||||
self.lifecycleState = lifecycleState
|
||||
}
|
||||
}
|
||||
|
||||
private struct Params: Equatable {
|
||||
var size: CGSize
|
||||
var insets: UIEdgeInsets
|
||||
|
||||
init(size: CGSize, insets: UIEdgeInsets) {
|
||||
self.size = size
|
||||
self.insets = insets
|
||||
}
|
||||
}
|
||||
|
||||
private let backgroundLayer: CallBackgroundLayer
|
||||
private let contentOverlayLayer: ContentOverlayLayer
|
||||
private let contentOverlayContainer: ContentOverlayContainer
|
||||
@@ -13,6 +48,18 @@ public final class PrivateCallScreen: UIView {
|
||||
|
||||
private let buttonGroupView: ButtonGroupView
|
||||
|
||||
public var state: State = State(lifecycleState: .connecting) {
|
||||
didSet {
|
||||
if self.state != oldValue {
|
||||
if let params = self.params {
|
||||
self.updateInternal(params: params, animated: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var params: Params?
|
||||
|
||||
public override init(frame: CGRect) {
|
||||
self.blurContentsLayer = SimpleLayer()
|
||||
|
||||
@@ -44,6 +91,26 @@ public final class PrivateCallScreen: UIView {
|
||||
|
||||
self.contentOverlayContainer.addSubview(self.buttonGroupView)
|
||||
|
||||
self.buttonGroupView.audioPressed = { [weak self] in
|
||||
guard let self else {
|
||||
return
|
||||
}
|
||||
|
||||
var state = self.state
|
||||
switch state.lifecycleState {
|
||||
case .connecting, .ringing, .exchangingKeys:
|
||||
state.lifecycleState = .active(startTime: CFAbsoluteTimeGetCurrent(), signalInfo: State.SignalInfo(quality: 1.0))
|
||||
case let .active(startTime, signalInfo):
|
||||
if signalInfo.quality == 1.0 {
|
||||
state.lifecycleState = .active(startTime: startTime, signalInfo: State.SignalInfo(quality: 0.2))
|
||||
} else if signalInfo.quality == 0.2 {
|
||||
state.lifecycleState = .connecting
|
||||
}
|
||||
|
||||
}
|
||||
self.state = state
|
||||
}
|
||||
|
||||
self.buttonGroupView.toggleVideo = { [weak self] in
|
||||
guard let self else {
|
||||
return
|
||||
@@ -65,9 +132,18 @@ public final class PrivateCallScreen: UIView {
|
||||
}
|
||||
|
||||
public func update(size: CGSize, insets: UIEdgeInsets) {
|
||||
let backgroundFrame = CGRect(origin: CGPoint(), size: size)
|
||||
let params = Params(size: size, insets: insets)
|
||||
if self.params == params {
|
||||
return
|
||||
}
|
||||
self.params = params
|
||||
self.updateInternal(params: params, animated: false)
|
||||
}
|
||||
|
||||
let aspect: CGFloat = size.width / size.height
|
||||
private func updateInternal(params: Params, animated: Bool) {
|
||||
let backgroundFrame = CGRect(origin: CGPoint(), size: params.size)
|
||||
|
||||
let aspect: CGFloat = params.size.width / params.size.height
|
||||
let sizeNorm: CGFloat = 64.0
|
||||
let renderingSize = CGSize(width: floor(sizeNorm * aspect), height: sizeNorm)
|
||||
let edgeSize: Int = 2
|
||||
@@ -77,18 +153,36 @@ public final class PrivateCallScreen: UIView {
|
||||
self.backgroundLayer.renderSpec = RenderLayerSpec(size: RenderSize(width: Int(renderingSize.width) + edgeSize * 2, height: Int(renderingSize.height) + edgeSize * 2))
|
||||
self.backgroundLayer.frame = visualBackgroundFrame
|
||||
|
||||
self.contentOverlayLayer.frame = CGRect(origin: CGPoint(), size: size)
|
||||
self.contentOverlayLayer.update(size: size, contentInsets: UIEdgeInsets())
|
||||
let backgroundStateIndex: Int
|
||||
switch self.state.lifecycleState {
|
||||
case .connecting:
|
||||
backgroundStateIndex = 0
|
||||
case .ringing:
|
||||
backgroundStateIndex = 0
|
||||
case .exchangingKeys:
|
||||
backgroundStateIndex = 0
|
||||
case let .active(_, signalInfo):
|
||||
if signalInfo.quality <= 0.2 {
|
||||
backgroundStateIndex = 2
|
||||
} else {
|
||||
backgroundStateIndex = 1
|
||||
}
|
||||
}
|
||||
self.backgroundLayer.update(stateIndex: backgroundStateIndex, animated: animated)
|
||||
|
||||
self.contentOverlayContainer.frame = CGRect(origin: CGPoint(), size: size)
|
||||
self.contentOverlayLayer.frame = CGRect(origin: CGPoint(), size: params.size)
|
||||
self.contentOverlayLayer.update(size: params.size, contentInsets: UIEdgeInsets())
|
||||
|
||||
self.contentOverlayContainer.frame = CGRect(origin: CGPoint(), size: params.size)
|
||||
|
||||
self.blurBackgroundLayer.renderSpec = RenderLayerSpec(size: RenderSize(width: Int(renderingSize.width) + edgeSize * 2, height: Int(renderingSize.height) + edgeSize * 2))
|
||||
self.blurBackgroundLayer.frame = visualBackgroundFrame
|
||||
self.blurBackgroundLayer.update(stateIndex: backgroundStateIndex, animated: animated)
|
||||
|
||||
self.buttonGroupView.frame = CGRect(origin: CGPoint(), size: size)
|
||||
self.buttonGroupView.update(size: size)
|
||||
self.buttonGroupView.frame = CGRect(origin: CGPoint(), size: params.size)
|
||||
self.buttonGroupView.update(size: params.size)
|
||||
|
||||
self.contentView.frame = CGRect(origin: CGPoint(), size: size)
|
||||
self.contentView.update(size: size, insets: insets)
|
||||
self.contentView.frame = CGRect(origin: CGPoint(), size: params.size)
|
||||
self.contentView.update(size: params.size, insets: params.insets, state: self.state)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user