mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-22 22:25:57 +00:00
Voice Chat improvements
This commit is contained in:
@@ -16,9 +16,13 @@ public final class AnimationNode : ASDisplayNode {
|
||||
|
||||
private var colorCallbacks: [LOTColorValueCallback] = []
|
||||
|
||||
public var played = false
|
||||
public var didPlay = false
|
||||
public var completion: (() -> Void)?
|
||||
|
||||
public var isPlaying: Bool {
|
||||
return self.animationView()?.isAnimationPlaying ?? false
|
||||
}
|
||||
|
||||
public init(animation: String? = nil, colors: [String: UIColor]? = nil, scale: CGFloat = 1.0) {
|
||||
self.scale = scale
|
||||
|
||||
@@ -77,6 +81,15 @@ public final class AnimationNode : ASDisplayNode {
|
||||
|
||||
public func setAnimation(name: String) {
|
||||
if let url = getAppBundle().url(forResource: name, withExtension: "json"), let composition = LOTComposition(filePath: url.path) {
|
||||
self.didPlay = false
|
||||
self.animationView()?.sceneModel = composition
|
||||
}
|
||||
}
|
||||
|
||||
public func setAnimation(data: Data) {
|
||||
if let json = try? JSONSerialization.jsonObject(with: data, options: []) as? [AnyHashable: Any] {
|
||||
let composition = LOTComposition(json: json)
|
||||
self.didPlay = false
|
||||
self.animationView()?.sceneModel = composition
|
||||
}
|
||||
}
|
||||
@@ -90,8 +103,8 @@ public final class AnimationNode : ASDisplayNode {
|
||||
}
|
||||
|
||||
public func play() {
|
||||
if let animationView = animationView(), !animationView.isAnimationPlaying, !self.played {
|
||||
self.played = true
|
||||
if let animationView = animationView(), !animationView.isAnimationPlaying && !self.didPlay {
|
||||
self.didPlay = true
|
||||
animationView.play { [weak self] _ in
|
||||
self?.completion?()
|
||||
}
|
||||
@@ -106,8 +119,8 @@ public final class AnimationNode : ASDisplayNode {
|
||||
}
|
||||
|
||||
public func reset() {
|
||||
if self.played, let animationView = animationView() {
|
||||
self.played = false
|
||||
if self.didPlay, let animationView = animationView() {
|
||||
self.didPlay = false
|
||||
animationView.stop()
|
||||
}
|
||||
}
|
||||
@@ -120,3 +133,64 @@ public final class AnimationNode : ASDisplayNode {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private let colorKeyRegex = try? NSRegularExpression(pattern: "\"k\":\\[[\\d\\.]+\\,[\\d\\.]+\\,[\\d\\.]+\\,[\\d\\.]+\\]")
|
||||
|
||||
public func transformedWithColors(data: Data, colors: [(UIColor, UIColor)]) -> Data {
|
||||
if var string = String(data: data, encoding: .utf8) {
|
||||
let sourceColors: [UIColor] = colors.map { $0.0 }
|
||||
let replacementColors: [UIColor] = colors.map { $0.1 }
|
||||
|
||||
func colorToString(_ color: UIColor) -> String {
|
||||
var r: CGFloat = 0.0
|
||||
var g: CGFloat = 0.0
|
||||
var b: CGFloat = 0.0
|
||||
if color.getRed(&r, green: &g, blue: &b, alpha: nil) {
|
||||
return "\"k\":[\(r),\(g),\(b),1]"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func match(_ a: Double, _ b: Double, eps: Double) -> Bool {
|
||||
return abs(a - b) < eps
|
||||
}
|
||||
|
||||
var replacements: [(NSTextCheckingResult, String)] = []
|
||||
|
||||
if let colorKeyRegex = colorKeyRegex {
|
||||
let results = colorKeyRegex.matches(in: string, range: NSRange(string.startIndex..., in: string))
|
||||
for result in results.reversed() {
|
||||
if let range = Range(result.range, in: string) {
|
||||
let substring = String(string[range])
|
||||
let color = substring[substring.index(string.startIndex, offsetBy: "\"k\":[".count) ..< substring.index(before: substring.endIndex)]
|
||||
let components = color.split(separator: ",")
|
||||
if components.count == 4, let r = Double(components[0]), let g = Double(components[1]), let b = Double(components[2]), let a = Double(components[3]) {
|
||||
if match(a, 1.0, eps: 0.01) {
|
||||
for i in 0 ..< sourceColors.count {
|
||||
let color = sourceColors[i]
|
||||
var cr: CGFloat = 0.0
|
||||
var cg: CGFloat = 0.0
|
||||
var cb: CGFloat = 0.0
|
||||
if color.getRed(&cr, green: &cg, blue: &cb, alpha: nil) {
|
||||
if match(r, Double(cr), eps: 0.01) && match(g, Double(cg), eps: 0.01) && match(b, Double(cb), eps: 0.01) {
|
||||
replacements.append((result, colorToString(replacementColors[i])))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (result, text) in replacements {
|
||||
if let range = Range(result.range, in: string) {
|
||||
string = string.replacingCharacters(in: range, with: text)
|
||||
}
|
||||
}
|
||||
|
||||
return string.data(using: .utf8) ?? data
|
||||
} else {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user