Skip to content

Commit

Permalink
优化内存
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Sep 23, 2023
1 parent f7847f3 commit dfb887e
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Demo/SwiftUI/TracyPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@
AC77DFAB26402327001351AE /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1310;
LastUpgradeCheck = 1420;
LastUpgradeCheck = 1500;
TargetAttributes = {
AC77DFB626402329001351AE = {
CreatedOnToolsVersion = 12.4;
Expand Down Expand Up @@ -212,6 +213,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
Expand Down Expand Up @@ -246,6 +248,7 @@
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -279,6 +282,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
Expand Down Expand Up @@ -313,6 +317,7 @@
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_USER_SCRIPT_SANDBOXING = YES;
GCC_C_LANGUAGE_STANDARD = gnu11;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1420"
LastUpgradeVersion = "1500"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
11 changes: 10 additions & 1 deletion Sources/KSPlayer/AVPlayer/KSVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public typealias UIViewRepresentable = NSViewRepresentable
#endif

public struct KSVideoPlayer {
public let coordinator: Coordinator
@ObservedObject
public private(set) var coordinator: Coordinator
public let url: URL
public let options: KSOptions
public init(coordinator: Coordinator, url: URL, options: KSOptions) {
Expand Down Expand Up @@ -286,6 +287,14 @@ public extension KSVideoPlayer {
#endif
}

extension View {
func then(_ body: (inout Self) -> Void) -> Self {
var result = self
body(&result)
return result
}
}

/// 这是一个频繁变化的model。View要少用这个
public class ControllerTimeModel: ObservableObject {
// 改成int才不会频繁更新
Expand Down
10 changes: 4 additions & 6 deletions Sources/KSPlayer/Subtitle/KSSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,10 @@ open class SubtitleModel: ObservableObject {

public func addSubtitle(dataSouce: SubtitleDataSouce) {
if let dataSouce = dataSouce as? SearchSubtitleDataSouce {
if let url {
Task {
try? await dataSouce.searchSubtitle(url: url)
dataSouce.infos.forEach { info in
addSubtitle(info: info)
}
Task { @MainActor in
try? await dataSouce.searchSubtitle(url: url)
dataSouce.infos.forEach { info in
addSubtitle(info: info)
}
}
} else {
Expand Down
22 changes: 17 additions & 5 deletions Sources/KSPlayer/Subtitle/SubtitleDataSouce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public protocol SubtitleDataSouce: AnyObject {
}

public protocol SearchSubtitleDataSouce: SubtitleDataSouce {
func searchSubtitle(url: URL) async throws
func searchSubtitle(url: URL?) async throws
}

public extension KSOptions {
Expand All @@ -90,8 +90,11 @@ public class CacheDataSouce: SearchSubtitleDataSouce {
srtCacheInfoPath = (cacheFolder as NSString).appendingPathComponent("KSSrtInfo.plist")
}

public func searchSubtitle(url: URL) async throws {
public func searchSubtitle(url: URL?) async throws {
infos.removeAll()
guard let url else {
return
}
srtCacheInfoPath = (cacheFolder as NSString).appendingPathComponent("KSSrtInfo_\(url.lastPathComponent).plist")
if FileManager.default.fileExists(atPath: srtCacheInfoPath), let files = NSMutableDictionary(contentsOfFile: srtCacheInfoPath) as? [String: String] {
srtInfoCaches = files.filter { FileManager.default.fileExists(atPath: $1) }
Expand Down Expand Up @@ -131,8 +134,11 @@ public class DirectorySubtitleDataSouce: SearchSubtitleDataSouce {
public var infos = [any SubtitleInfo]()
public init() {}

public func searchSubtitle(url: URL) async throws {
public func searchSubtitle(url: URL?) async throws {
infos.removeAll()
guard let url else {
return
}
if url.isFileURL {
let subtitleURLs: [URL] = (try? FileManager.default.contentsOfDirectory(at: url.deletingLastPathComponent(), includingPropertiesForKeys: nil).filter(\.isSubtitle)) ?? []
let contents = subtitleURLs.map { URLSubtitleInfo(url: $0) }.sorted { left, right in
Expand All @@ -146,8 +152,11 @@ public class DirectorySubtitleDataSouce: SearchSubtitleDataSouce {
public class ShooterSubtitleDataSouce: SearchSubtitleDataSouce {
public var infos = [any SubtitleInfo]()
public init() {}
public func searchSubtitle(url: URL) async throws {
public func searchSubtitle(url: URL?) async throws {
infos.removeAll()
guard let url else {
return
}
guard url.isFileURL, let url = URL(string: "https://www.shooter.cn/api/subapi.php")?
.add(queryItems: ["format": "json", "pathinfo": url.path, "filehash": url.shooterFilehash])
else {
Expand Down Expand Up @@ -183,8 +192,11 @@ public class AssrtSubtitleDataSouce: SearchSubtitleDataSouce {
self.token = token
}

public func searchSubtitle(url: URL) async throws {
public func searchSubtitle(url: URL?) async throws {
infos.removeAll()
guard let url else {
return
}
let query = url.deletingPathExtension().lastPathComponent
guard let searchApi = URL(string: "https://api.assrt.net/v1/sub/search")?.add(queryItems: ["q": query]) else {
return
Expand Down

0 comments on commit dfb887e

Please sign in to comment.