Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
kingslay committed Dec 2, 2023
1 parent d769c51 commit b3ab335
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Sources/KSPlayer/Subtitle/KSSubtitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ open class SubtitleModel: ObservableObject {
didSet {
oldValue?.isEnabled = false
selectedSubtitleInfo?.isEnabled = true
if let url, let info = selectedSubtitleInfo as? URLSubtitleInfo, !info.downloadURL.isFileURL, subtitleDataSouces.contains(where: { $0 === CacheDataSouce.singleton }) {
CacheDataSouce.singleton.addCache(fileURL: url, downloadURL: info.downloadURL)
if let url, let info = selectedSubtitleInfo as? URLSubtitleInfo, !info.downloadURL.isFileURL, let cache = subtitleDataSouces.first(where: { $0 is CacheSubtitleDataSouce }) as? CacheSubtitleDataSouce {
cache.addCache(fileURL: url, downloadURL: info.downloadURL)
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/KSPlayer/Subtitle/SubtitleDataSouce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public protocol FileURLSubtitleDataSouce: SubtitleDataSouce {
func searchSubtitle(fileURL: URL?) async throws
}

public protocol CacheSubtitleDataSouce: FileURLSubtitleDataSouce {
func addCache(fileURL: URL, downloadURL: URL)
}

public protocol SearchSubtitleDataSouce: SubtitleDataSouce {
func searchSubtitle(query: String?, languages: [String]) async throws
}
Expand All @@ -75,8 +79,8 @@ public extension KSOptions {
static var subtitleDataSouces: [SubtitleDataSouce] = [DirectorySubtitleDataSouce()]
}

public class CacheDataSouce: FileURLSubtitleDataSouce {
public static let singleton = CacheDataSouce()
public class PlistCacheSubtitleDataSouce: CacheSubtitleDataSouce {
public static let singleton = PlistCacheSubtitleDataSouce()
public var infos = [any SubtitleInfo]()
private let srtCacheInfoPath: String
// 因为plist不能保存URL
Expand Down

0 comments on commit b3ab335

Please sign in to comment.