From e9f2d28a96ff27865ab9291cd8e78f0d9368fab7 Mon Sep 17 00:00:00 2001 From: abyyxwang Date: Tue, 12 Nov 2024 17:03:28 +0800 Subject: [PATCH] [RTCCommon] update 1.1.0 --- .../Extension/CGFloat+scale375.swift | 128 ++++---- .../Extension/UIView+roundrect.swift | 130 ++++---- Sources/RTCCommon/Logger/TRTCLog.swift | 290 +++++++++--------- 3 files changed, 274 insertions(+), 274 deletions(-) diff --git a/Sources/RTCCommon/Extension/CGFloat+scale375.swift b/Sources/RTCCommon/Extension/CGFloat+scale375.swift index 432ee12..87beb4c 100644 --- a/Sources/RTCCommon/Extension/CGFloat+scale375.swift +++ b/Sources/RTCCommon/Extension/CGFloat+scale375.swift @@ -1,67 +1,67 @@ -//// -//// CGFloat+scale375.swift -//// RTCCommon -//// -//// Created by krabyu on 2023/10/16. -//// Copyright © 2023 Tencent. All rights reserved. -//// // -//import Foundation -//import UIKit +// CGFloat+scale375.swift +// RTCCommon // -//public let screenWidth = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) -//public let screenHeight = max(UIScreen.main.bounds.width,UIScreen.main.bounds.height) +// Created by krabyu on 2023/10/16. +// Copyright © 2023 Tencent. All rights reserved. // -//public let deviceIsiPhoneX : Bool = { -// if UIDevice.current.userInterfaceIdiom == .pad { -// return false -// } -// let size = UIScreen.main.bounds.size -// let notchValue = Int(size.width/size.height*100) -// if notchValue == 216 || notchValue == 46 { -// return true -// } -// return false -//}() -// -//public let deviceSafeBottomHeight : CGFloat = { -// if deviceIsiPhoneX { -// return 34 -// } -// else { -// return 0 -// } -//}() -// -//extension CGFloat { -// -// /// 375设计图中的尺寸 -// /// -// /// - Returns: 最终结果缩放结果 -// public func scale375Width(exceptPad: Bool = true) -> CGFloat { -// if UIDevice.current.userInterfaceIdiom == .pad { -// return exceptPad ? self * 1.5 : self * (screenWidth / 375.00) -// } -// return self * (screenWidth / 375.00) -// } -// -// public func scale375Height(exceptPad: Bool = true) -> CGFloat { -// if UIDevice.current.userInterfaceIdiom == .pad { -// return exceptPad ? self * 1.5 : self * (screenHeight / 812.00) -// } -// return self * (screenHeight / 812.00) -// } -//} -// -//extension Int { -// /// 375设计图中的尺寸 -// /// -// /// - Returns: 最终结果缩放结果 -// public func scale375Width(exceptPad: Bool = true) -> CGFloat { -// return CGFloat(self).scale375Width() -// } -// -// public func scale375Height(exceptPad: Bool = true) -> CGFloat { -// return CGFloat(self).scale375Height() -// } -//} + +import Foundation +import UIKit + +public let screenWidth = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height) +public let screenHeight = max(UIScreen.main.bounds.width,UIScreen.main.bounds.height) + +public let deviceIsiPhoneX : Bool = { + if UIDevice.current.userInterfaceIdiom == .pad { + return false + } + let size = UIScreen.main.bounds.size + let notchValue = Int(size.width/size.height*100) + if notchValue == 216 || notchValue == 46 { + return true + } + return false +}() + +public let deviceSafeBottomHeight : CGFloat = { + if deviceIsiPhoneX { + return 34 + } + else { + return 0 + } +}() + +extension CGFloat { + + /// 375设计图中的尺寸 + /// + /// - Returns: 最终结果缩放结果 + public func scale375Width(exceptPad: Bool = true) -> CGFloat { + if UIDevice.current.userInterfaceIdiom == .pad { + return exceptPad ? self * 1.5 : self * (screenWidth / 375.00) + } + return self * (screenWidth / 375.00) + } + + public func scale375Height(exceptPad: Bool = true) -> CGFloat { + if UIDevice.current.userInterfaceIdiom == .pad { + return exceptPad ? self * 1.5 : self * (screenHeight / 812.00) + } + return self * (screenHeight / 812.00) + } +} + +extension Int { + /// 375设计图中的尺寸 + /// + /// - Returns: 最终结果缩放结果 + public func scale375Width(exceptPad: Bool = true) -> CGFloat { + return CGFloat(self).scale375Width() + } + + public func scale375Height(exceptPad: Bool = true) -> CGFloat { + return CGFloat(self).scale375Height() + } +} diff --git a/Sources/RTCCommon/Extension/UIView+roundrect.swift b/Sources/RTCCommon/Extension/UIView+roundrect.swift index 3e5941e..138fc8d 100644 --- a/Sources/RTCCommon/Extension/UIView+roundrect.swift +++ b/Sources/RTCCommon/Extension/UIView+roundrect.swift @@ -1,67 +1,67 @@ -//// -//// UIView+roundrect.swift -//// TRTCScene -//// -//// Created by adams on 2021/4/8. -//// Copyright © 2022 Tencent. All rights reserved. // -//import Foundation -//import UIKit +// UIView+roundrect.swift +// TRTCScene // -//public extension UIView { -// func roundedRect(rect: CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize) { -// let maskPath = UIBezierPath(roundedRect: rect, byRoundingCorners: byRoundingCorners, cornerRadii: cornerRadii) -// let maskLayer = CAShapeLayer() -// maskLayer.frame = bounds -// maskLayer.path = maskPath.cgPath -// layer.mask = maskLayer -// } -// -// func roundedCircle(rect: CGRect) { -// roundedRect(rect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: bounds.size.width / 2, height: bounds.size.height / 2)) -// } -// -// func clearRoundedCorners() { -// layer.mask = nil -// } -//} -// -//public extension UIView { -// private struct AssociatedKeys { -// static var gradientLayerKey = "gradientLayerKey" -// } -// -// var gradientLayer: CAGradientLayer? { -// get { -// return objc_getAssociatedObject(self, &AssociatedKeys.gradientLayerKey) as? CAGradientLayer -// } -// set { -// objc_setAssociatedObject(self, &AssociatedKeys.gradientLayerKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) -// } -// } -// -// func removeGradientLayer() { -// guard let glayer = gradientLayer else { -// return -// } -// glayer.removeFromSuperlayer() -// gradientLayer = nil -// } -// -// @discardableResult -// func gradient(colors: [UIColor], bounds: CGRect = .zero, isVertical: Bool = false) -> CAGradientLayer { -// let gradientLayer = self.gradientLayer ?? CAGradientLayer() -// if isVertical { -// gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0) -// gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0) -// } else { -// gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5) -// gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5) -// } -// self.gradientLayer = gradientLayer -// gradientLayer.colors = colors.map { $0.cgColor } -// gradientLayer.frame = bounds == .zero ? self.bounds : bounds -// layer.insertSublayer(gradientLayer, at: 0) -// return gradientLayer -// } -//} +// Created by adams on 2021/4/8. +// Copyright © 2022 Tencent. All rights reserved. + +import Foundation +import UIKit + +public extension UIView { + func roundedRect(rect: CGRect, byRoundingCorners: UIRectCorner, cornerRadii: CGSize) { + let maskPath = UIBezierPath(roundedRect: rect, byRoundingCorners: byRoundingCorners, cornerRadii: cornerRadii) + let maskLayer = CAShapeLayer() + maskLayer.frame = bounds + maskLayer.path = maskPath.cgPath + layer.mask = maskLayer + } + + func roundedCircle(rect: CGRect) { + roundedRect(rect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: bounds.size.width / 2, height: bounds.size.height / 2)) + } + + func clearRoundedCorners() { + layer.mask = nil + } +} + +public extension UIView { + private struct AssociatedKeys { + static var gradientLayerKey = "gradientLayerKey" + } + + var gradientLayer: CAGradientLayer? { + get { + return objc_getAssociatedObject(self, &AssociatedKeys.gradientLayerKey) as? CAGradientLayer + } + set { + objc_setAssociatedObject(self, &AssociatedKeys.gradientLayerKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } + } + + func removeGradientLayer() { + guard let glayer = gradientLayer else { + return + } + glayer.removeFromSuperlayer() + gradientLayer = nil + } + + @discardableResult + func gradient(colors: [UIColor], bounds: CGRect = .zero, isVertical: Bool = false) -> CAGradientLayer { + let gradientLayer = self.gradientLayer ?? CAGradientLayer() + if isVertical { + gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0) + gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0) + } else { + gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5) + gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5) + } + self.gradientLayer = gradientLayer + gradientLayer.colors = colors.map { $0.cgColor } + gradientLayer.frame = bounds == .zero ? self.bounds : bounds + layer.insertSublayer(gradientLayer, at: 0) + return gradientLayer + } +} diff --git a/Sources/RTCCommon/Logger/TRTCLog.swift b/Sources/RTCCommon/Logger/TRTCLog.swift index 3b3c370..c75c3d4 100644 --- a/Sources/RTCCommon/Logger/TRTCLog.swift +++ b/Sources/RTCCommon/Logger/TRTCLog.swift @@ -1,148 +1,148 @@ -//// -//// TRTCLog.swift -//// TRTCVoiceRoomDemo -//// -//// Created by abyyxwang on 2020/6/10. -//// Copyright © 2020 tencent. All rights reserved. -//// // -//import Foundation +// TRTCLog.swift +// TRTCVoiceRoomDemo // -//import Foundation -//import os +// Created by abyyxwang on 2020/6/10. +// Copyright © 2020 tencent. All rights reserved. // -///// subsystem for current project. -///// - setup it when start a new project. -////private let subsystem = "com.slideart.photo2videomaker" -//private let subSystemIdentifier: String = Bundle.main.bundleIdentifier ?? "com.tencent.trtc" -//private let subsystem: String = "TRTC" -///// Custom log system. -//public class TRTCLog { -// public struct Output: OptionSet { -// public let rawValue: Int -// -// public init(rawValue: Int) { -// self.rawValue = rawValue -// } -// -// public static let info = Output(rawValue: 1 << 0) -// public static let debug = Output(rawValue: 1 << 1) -// public static let warning = Output(rawValue: 1 << 2) -// public static let error = Output(rawValue: 1 << 3) -// public static let print = Output(rawValue: 1 << 4) -// -// public static let all: Output = [.print, .info, .debug, .warning, .error] -// } -// -// /// setup log level and when does the log work. -// public static var output: Output = [.debug, .warning, .error, .print] -// -// @available(iOS 10.0, *) -// /// level info -// static let infoLog = OSLog(subsystem: subsystem, category: "INFO") -// /// info level -// /// -// /// - Parameters: -// /// - string: log info description -// /// - fileName: file name -// /// - methodName: method name -// /// - lineNumber: line number -// public static func info(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { -// #if DEBUG -// let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") -// let log = "\(file):line \(lineNumber) method:\(methodName):\(string)" -// if output.contains(.info) { -// if #available(iOS 10.0, *) { -// os_log("%@", log: infoLog, type: .info, log) -// } else { -// print(": %@", log) -// } -// } -// #endif -// } -// -// @available(iOS 10.0, *) -// /// level debug -// static let debugLog = OSLog(subsystem: subsystem, category: "DEBUG") -// /// debug level -// /// -// /// - Parameters: -// /// - string: log info description -// /// - fileName: file name -// /// - methodName: method name -// /// - lineNumber: line number -// public static func debug(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { -// #if DEBUG -// let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") -// let log = "\(file):line \(lineNumber) method:\(methodName):\(string)" -// if output.contains(.debug) { -// if #available(iOS 10.0, *) { -// os_log("%@", log: debugLog, type: .debug, log) -// } else { -// print(": %@", log) -// } -// } -// #endif -// } -// -// @available(iOS 10.0, *) -// /// level warning -// static let warningLog = OSLog(subsystem: subsystem, category: "WARNING") -// public static func warning(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { -// if output.contains(.warning) { -// let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") -// let log = "\(file):line \(lineNumber) method:\(methodName):\(string)" -// if #available(iOS 10.0, *) { -// os_log("%@", log: warningLog, type: .fault, log) -// } else { -// print(": %@", string) -// } -// } -// } -// -// @available(iOS 10.0, *) -// /// level error -// static let errorLog = OSLog(subsystem: subsystem, category: "ERROR") -// -// /// log error -// /// -// /// - Parameters: -// /// - string: message -// /// - fileName: filename -// /// - methodName: method -// /// - lineNumber: lineNumber -// public static func error(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { -// if output.contains(.error) { -// let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") -// let log = "\(file):line \(lineNumber) method:\(methodName):\n\(string)" -// if #available(iOS 10.0, *) { -// os_log("%@", log: errorLog, type: .error, log) -// } else { -// print(": %@", string) -// } -// #if DEBUG -//// assert(false, log) -// #endif -// } -// } -// -// /// just output in console. -// /// -// /// - Parameters: -// /// - message: output message. -// /// - fileName: file name. -// /// - methodName: method name. -// /// - lineNumber: line number. -// public static func out(message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { -// #if DEBUG -// if output.contains(.print) { -// let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") -// print("[PRINT]\(file):\(lineNumber) \(methodName):\(message)") -// } -// #endif -// } -// -// public static func out(_ message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { -// TRTCLog.out(message: message, fileName: fileName, methodName: methodName, lineNumber: lineNumber) -// } -//} + +import Foundation + +import Foundation +import os + +/// subsystem for current project. +/// - setup it when start a new project. +//private let subsystem = "com.slideart.photo2videomaker" +private let subSystemIdentifier: String = Bundle.main.bundleIdentifier ?? "com.tencent.trtc" +private let subsystem: String = "TRTC" +/// Custom log system. +public class TRTCLog { + public struct Output: OptionSet { + public let rawValue: Int + + public init(rawValue: Int) { + self.rawValue = rawValue + } + + public static let info = Output(rawValue: 1 << 0) + public static let debug = Output(rawValue: 1 << 1) + public static let warning = Output(rawValue: 1 << 2) + public static let error = Output(rawValue: 1 << 3) + public static let print = Output(rawValue: 1 << 4) + + public static let all: Output = [.print, .info, .debug, .warning, .error] + } + + /// setup log level and when does the log work. + public static var output: Output = [.debug, .warning, .error, .print] + + @available(iOS 10.0, *) + /// level info + static let infoLog = OSLog(subsystem: subsystem, category: "INFO") + /// info level + /// + /// - Parameters: + /// - string: log info description + /// - fileName: file name + /// - methodName: method name + /// - lineNumber: line number + public static func info(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { + #if DEBUG + let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") + let log = "\(file):line \(lineNumber) method:\(methodName):\(string)" + if output.contains(.info) { + if #available(iOS 10.0, *) { + os_log("%@", log: infoLog, type: .info, log) + } else { + print(": %@", log) + } + } + #endif + } + + @available(iOS 10.0, *) + /// level debug + static let debugLog = OSLog(subsystem: subsystem, category: "DEBUG") + /// debug level + /// + /// - Parameters: + /// - string: log info description + /// - fileName: file name + /// - methodName: method name + /// - lineNumber: line number + public static func debug(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { + #if DEBUG + let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") + let log = "\(file):line \(lineNumber) method:\(methodName):\(string)" + if output.contains(.debug) { + if #available(iOS 10.0, *) { + os_log("%@", log: debugLog, type: .debug, log) + } else { + print(": %@", log) + } + } + #endif + } + + @available(iOS 10.0, *) + /// level warning + static let warningLog = OSLog(subsystem: subsystem, category: "WARNING") + public static func warning(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { + if output.contains(.warning) { + let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") + let log = "\(file):line \(lineNumber) method:\(methodName):\(string)" + if #available(iOS 10.0, *) { + os_log("%@", log: warningLog, type: .fault, log) + } else { + print(": %@", string) + } + } + } + + @available(iOS 10.0, *) + /// level error + static let errorLog = OSLog(subsystem: subsystem, category: "ERROR") + + /// log error + /// + /// - Parameters: + /// - string: message + /// - fileName: filename + /// - methodName: method + /// - lineNumber: lineNumber + public static func error(_ string: String, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { + if output.contains(.error) { + let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") + let log = "\(file):line \(lineNumber) method:\(methodName):\n\(string)" + if #available(iOS 10.0, *) { + os_log("%@", log: errorLog, type: .error, log) + } else { + print(": %@", string) + } + #if DEBUG +// assert(false, log) + #endif + } + } + + /// just output in console. + /// + /// - Parameters: + /// - message: output message. + /// - fileName: file name. + /// - methodName: method name. + /// - lineNumber: line number. + public static func out(message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { + #if DEBUG + if output.contains(.print) { + let file = (fileName as NSString).lastPathComponent.replacingOccurrences(of: ".Swift", with: "") + print("[PRINT]\(file):\(lineNumber) \(methodName):\(message)") + } + #endif + } + + public static func out(_ message: N, fileName: String = #file, methodName: String = #function, lineNumber: Int = #line) { + TRTCLog.out(message: message, fileName: fileName, methodName: methodName, lineNumber: lineNumber) + } +}