ReadViewSDK/Sources/RDReaderView/EPUBUI/RDEPUBReaderPersistence.swift
shen 54798ba578 refactor: 添加中文注释 + 优化模块结构
- 给全部 78 个 Swift 源文件添加详细的中文注释(文件级、类级、方法级)
- 删除 LegacyRDReaderController/ 死代码目录(16 文件 4592 行)
- 根目录翻页容器文件移入 ReaderView/ 目录
- Resources/ 移入 EPUBCore/Resources/(与使用者归属一致)
- RDEPUBTextIndexTable.swift 移入 EPUBTextRendering/(消除反向依赖)
- RDURLReaderController.swift 移入 EPUBUI/(入口控制器归入 UI 层)
- 更新 podspec 资源路径
2026-05-25 10:19:14 +08:00

156 lines
5.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Foundation
// MARK: -
///
///
///
public protocol RDEPUBReaderPersistence: AnyObject {
///
/// - Parameter bookIdentifier:
/// - Returns: nil
func loadLocation(for bookIdentifier: String) -> RDEPUBLocation?
///
/// - Parameters:
/// - location:
/// - bookIdentifier:
func saveLocation(_ location: RDEPUBLocation, for bookIdentifier: String)
///
func loadBookmarks(for bookIdentifier: String) -> [RDEPUBBookmark]
///
func saveBookmarks(_ bookmarks: [RDEPUBBookmark], for bookIdentifier: String)
///
func loadHighlights(for bookIdentifier: String) -> [RDEPUBHighlight]
///
func saveHighlights(_ highlights: [RDEPUBHighlight], for bookIdentifier: String)
///
func loadReaderSettings() -> RDEPUBReaderSettings?
///
func saveReaderSettings(_ settings: RDEPUBReaderSettings)
}
// MARK: -
/// //
public extension RDEPUBReaderPersistence {
func loadBookmarks(for bookIdentifier: String) -> [RDEPUBBookmark] {
_ = bookIdentifier
return []
}
func saveBookmarks(_ bookmarks: [RDEPUBBookmark], for bookIdentifier: String) {
_ = bookmarks
_ = bookIdentifier
}
func loadReaderSettings() -> RDEPUBReaderSettings? {
nil
}
func saveReaderSettings(_ settings: RDEPUBReaderSettings) {
_ = settings
}
}
// MARK: - UserDefaults
/// UserDefaults
/// JSON
/// SQLite
public final class RDEPUBUserDefaultsPersistence: RDEPUBReaderPersistence {
/// UserDefaults
private let defaults: UserDefaults
/// key
private let locationPrefix: String
/// key
private let bookmarksPrefix: String
/// key
private let highlightsPrefix: String
/// key
private let settingsKey: String
/// UserDefaults
/// - Parameters:
/// - defaults: UserDefaults .standard
/// - locationPrefix: key
/// - bookmarksPrefix: key
/// - highlightsPrefix: key
/// - settingsKey: key
public init(
defaults: UserDefaults = .standard,
locationPrefix: String = "ssreader.epub.location.",
bookmarksPrefix: String = "ssreader.epub.bookmarks.",
highlightsPrefix: String = "ssreader.epub.highlights.",
settingsKey: String = "ssreader.epub.settings"
) {
self.defaults = defaults
self.locationPrefix = locationPrefix
self.bookmarksPrefix = bookmarksPrefix
self.highlightsPrefix = highlightsPrefix
self.settingsKey = settingsKey
}
public func loadLocation(for bookIdentifier: String) -> RDEPUBLocation? {
guard let data = defaults.data(forKey: locationPrefix + bookIdentifier) else {
return nil
}
return try? JSONDecoder().decode(RDEPUBLocation.self, from: data)
}
public func saveLocation(_ location: RDEPUBLocation, for bookIdentifier: String) {
guard let data = try? JSONEncoder().encode(location) else {
return
}
defaults.set(data, forKey: locationPrefix + bookIdentifier)
}
public func loadBookmarks(for bookIdentifier: String) -> [RDEPUBBookmark] {
guard let data = defaults.data(forKey: bookmarksPrefix + bookIdentifier) else {
return []
}
return (try? JSONDecoder().decode([RDEPUBBookmark].self, from: data)) ?? []
}
public func saveBookmarks(_ bookmarks: [RDEPUBBookmark], for bookIdentifier: String) {
guard let data = try? JSONEncoder().encode(bookmarks) else {
return
}
defaults.set(data, forKey: bookmarksPrefix + bookIdentifier)
}
public func loadHighlights(for bookIdentifier: String) -> [RDEPUBHighlight] {
guard let data = defaults.data(forKey: highlightsPrefix + bookIdentifier) else {
return []
}
return (try? JSONDecoder().decode([RDEPUBHighlight].self, from: data)) ?? []
}
public func saveHighlights(_ highlights: [RDEPUBHighlight], for bookIdentifier: String) {
guard let data = try? JSONEncoder().encode(highlights) else {
return
}
defaults.set(data, forKey: highlightsPrefix + bookIdentifier)
}
public func loadReaderSettings() -> RDEPUBReaderSettings? {
guard let data = defaults.data(forKey: settingsKey) else {
return nil
}
return try? JSONDecoder().decode(RDEPUBReaderSettings.self, from: data)
}
public func saveReaderSettings(_ settings: RDEPUBReaderSettings) {
guard let data = try? JSONEncoder().encode(settings) else {
return
}
defaults.set(data, forKey: settingsKey)
}
}