Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow cache archive for offline read #211

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 8 additions & 54 deletions LANreader.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
E90576B629F6BF8700C3CE3F /* WrappingHStack.swift in Sources */ = {isa = PBXBuildFile; fileRef = E90576B529F6BF8700C3CE3F /* WrappingHStack.swift */; };
E90576BC29F7FB2900C3CE3F /* DraggableAndZoomableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E90576BB29F7FB2900C3CE3F /* DraggableAndZoomableView.swift */; };
E90576BF29F8C10700C3CE3F /* Puppy in Frameworks */ = {isa = PBXBuildFile; productRef = E90576BE29F8C10700C3CE3F /* Puppy */; };
E924CF762AEA70C1000AB76A /* Dependencies in Frameworks */ = {isa = PBXBuildFile; productRef = E924CF752AEA70C1000AB76A /* Dependencies */; };
E926CE7A2B1B5474000E0B10 /* NewCategory.swift in Sources */ = {isa = PBXBuildFile; fileRef = E926CE792B1B5474000E0B10 /* NewCategory.swift */; };
E94C1DDC2A204DDA00552ABD /* ImageService.swift in Sources */ = {isa = PBXBuildFile; fileRef = E94C1DDB2A204DDA00552ABD /* ImageService.swift */; };
E9623E6D2BF583FD005FD609 /* CacheView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9623E6C2BF583FD005FD609 /* CacheView.swift */; };
E98B23022942110100AE30C6 /* ArchiveIndexResponseNullTags.json in Resources */ = {isa = PBXBuildFile; fileRef = E98B23012942110100AE30C6 /* ArchiveIndexResponseNullTags.json */; };
E98B8B722A0A7B68004CBBC0 /* UniformTypeIdentifiers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E98B8B712A0A7B67004CBBC0 /* UniformTypeIdentifiers.framework */; };
E98B8B772A0A7B68004CBBC0 /* ActionRequestHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E98B8B762A0A7B68004CBBC0 /* ActionRequestHandler.swift */; };
Expand All @@ -69,9 +69,6 @@
E9CBC0F02A3DD62F00CCD592 /* LogFormatter.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9CBC0EF2A3DD62F00CCD592 /* LogFormatter.swift */; };
E9DAEC552B07295D00ADC215 /* ArchiveDetailsV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9DAEC542B07295D00ADC215 /* ArchiveDetailsV2.swift */; };
E9DCE8992AF0665900C17236 /* ArchiveListV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9DCE8982AF0665900C17236 /* ArchiveListV2.swift */; };
E9DCE89C2AF0672200C17236 /* Collections in Frameworks */ = {isa = PBXBuildFile; productRef = E9DCE89B2AF0672200C17236 /* Collections */; };
E9DCE89E2AF0672200C17236 /* DequeModule in Frameworks */ = {isa = PBXBuildFile; productRef = E9DCE89D2AF0672200C17236 /* DequeModule */; };
E9DCE8A02AF0672200C17236 /* OrderedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = E9DCE89F2AF0672200C17236 /* OrderedCollections */; };
E9DCE8A22AF0795600C17236 /* SearchViewV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9DCE8A12AF0795600C17236 /* SearchViewV2.swift */; };
E9E3631426F463F800C58D01 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 7D354CD024F1390400617F4A /* Alamofire */; };
E9E3631626F463F800C58D01 /* Logging in Frameworks */ = {isa = PBXBuildFile; productRef = 7D354CE124F15F1800617F4A /* Logging */; };
Expand Down Expand Up @@ -161,6 +158,7 @@
E90576BB29F7FB2900C3CE3F /* DraggableAndZoomableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DraggableAndZoomableView.swift; sourceTree = "<group>"; };
E926CE792B1B5474000E0B10 /* NewCategory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewCategory.swift; sourceTree = "<group>"; };
E94C1DDB2A204DDA00552ABD /* ImageService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageService.swift; sourceTree = "<group>"; };
E9623E6C2BF583FD005FD609 /* CacheView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CacheView.swift; sourceTree = "<group>"; };
E98B23012942110100AE30C6 /* ArchiveIndexResponseNullTags.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = ArchiveIndexResponseNullTags.json; sourceTree = "<group>"; };
E98B8B6F2A0A7B67004CBBC0 /* Action.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = Action.appex; sourceTree = BUILT_PRODUCTS_DIR; };
E98B8B712A0A7B67004CBBC0 /* UniformTypeIdentifiers.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UniformTypeIdentifiers.framework; path = System/Library/Frameworks/UniformTypeIdentifiers.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -192,14 +190,10 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
E9DCE8A02AF0672200C17236 /* OrderedCollections in Frameworks */,
E9E3631626F463F800C58D01 /* Logging in Frameworks */,
E9E3631826F463F800C58D01 /* GRDB in Frameworks */,
E90369C02AE72D64001BC558 /* ComposableArchitecture in Frameworks */,
E924CF762AEA70C1000AB76A /* Dependencies in Frameworks */,
E9E3631726F463F800C58D01 /* NotificationBannerSwift in Frameworks */,
E9DCE89C2AF0672200C17236 /* Collections in Frameworks */,
E9DCE89E2AF0672200C17236 /* DequeModule in Frameworks */,
E90576BF29F8C10700C3CE3F /* Puppy in Frameworks */,
E9E3631426F463F800C58D01 /* Alamofire in Frameworks */,
);
Expand Down Expand Up @@ -246,6 +240,7 @@
children = (
E997D8032AF3C9BA007C91F1 /* LibraryListV2.swift */,
E9B39A6D2B21E7A800700FE4 /* RandomView.swift */,
E9623E6C2BF583FD005FD609 /* CacheView.swift */,
);
path = Library;
sourceTree = "<group>";
Expand Down Expand Up @@ -476,10 +471,6 @@
7DF7EA792564E264003B5A5A /* GRDB */,
E90576BE29F8C10700C3CE3F /* Puppy */,
E90369BF2AE72D64001BC558 /* ComposableArchitecture */,
E924CF752AEA70C1000AB76A /* Dependencies */,
E9DCE89B2AF0672200C17236 /* Collections */,
E9DCE89D2AF0672200C17236 /* DequeModule */,
E9DCE89F2AF0672200C17236 /* OrderedCollections */,
);
productName = DuManga;
productReference = 7D354C9C24F1316800617F4A /* LANreader.app */;
Expand Down Expand Up @@ -564,8 +555,6 @@
7DF7EA782564E264003B5A5A /* XCRemoteSwiftPackageReference "GRDB" */,
E90576BD29F8C10700C3CE3F /* XCRemoteSwiftPackageReference "Puppy" */,
E90369BB2AE72B11001BC558 /* XCRemoteSwiftPackageReference "swift-composable-architecture" */,
E924CF742AEA70C1000AB76A /* XCRemoteSwiftPackageReference "swift-dependencies" */,
E9DCE89A2AF0672200C17236 /* XCRemoteSwiftPackageReference "swift-collections" */,
);
productRefGroup = 7D354C9D24F1316800617F4A /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -651,6 +640,7 @@
files = (
E9F3B0122B01A21C00F60F62 /* CategoryListV2.swift in Sources */,
7DF7EA742564E1D2003B5A5A /* AppDatabase.swift in Sources */,
E9623E6D2BF583FD005FD609 /* CacheView.swift in Sources */,
E9F3B0142B01D2DB00F60F62 /* CategoryArchiveListV2.swift in Sources */,
E9DCE8A22AF0795600C17236 /* SearchViewV2.swift in Sources */,
E926CE7A2B1B5474000E0B10 /* NewCategory.swift in Sources */,
Expand Down Expand Up @@ -874,7 +864,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 93;
CURRENT_PROJECT_VERSION = 94;
DEVELOPMENT_ASSET_PATHS = "\"LANreader/Preview Content\"";
DEVELOPMENT_TEAM = UUEBW58SA6;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -903,7 +893,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 93;
CURRENT_PROJECT_VERSION = 94;
DEVELOPMENT_ASSET_PATHS = "\"LANreader/Preview Content\"";
DEVELOPMENT_TEAM = UUEBW58SA6;
ENABLE_PREVIEWS = YES;
Expand Down Expand Up @@ -982,7 +972,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 93;
CURRENT_PROJECT_VERSION = 94;
DEVELOPMENT_TEAM = UUEBW58SA6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Action/Info.plist;
Expand Down Expand Up @@ -1011,7 +1001,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 93;
CURRENT_PROJECT_VERSION = 94;
DEVELOPMENT_TEAM = UUEBW58SA6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Action/Info.plist;
Expand Down Expand Up @@ -1131,22 +1121,6 @@
minimumVersion = 0.7.0;
};
};
E924CF742AEA70C1000AB76A /* XCRemoteSwiftPackageReference "swift-dependencies" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/pointfreeco/swift-dependencies";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.0;
};
};
E9DCE89A2AF0672200C17236 /* XCRemoteSwiftPackageReference "swift-collections" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/apple/swift-collections.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 1.0.5;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand Down Expand Up @@ -1185,26 +1159,6 @@
package = E90576BD29F8C10700C3CE3F /* XCRemoteSwiftPackageReference "Puppy" */;
productName = Puppy;
};
E924CF752AEA70C1000AB76A /* Dependencies */ = {
isa = XCSwiftPackageProductDependency;
package = E924CF742AEA70C1000AB76A /* XCRemoteSwiftPackageReference "swift-dependencies" */;
productName = Dependencies;
};
E9DCE89B2AF0672200C17236 /* Collections */ = {
isa = XCSwiftPackageProductDependency;
package = E9DCE89A2AF0672200C17236 /* XCRemoteSwiftPackageReference "swift-collections" */;
productName = Collections;
};
E9DCE89D2AF0672200C17236 /* DequeModule */ = {
isa = XCSwiftPackageProductDependency;
package = E9DCE89A2AF0672200C17236 /* XCRemoteSwiftPackageReference "swift-collections" */;
productName = DequeModule;
};
E9DCE89F2AF0672200C17236 /* OrderedCollections */ = {
isa = XCSwiftPackageProductDependency;
package = E9DCE89A2AF0672200C17236 /* XCRemoteSwiftPackageReference "swift-collections" */;
productName = OrderedCollections;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 7D354C9424F1316800617F4A /* Project object */;
Expand Down
7 changes: 6 additions & 1 deletion LANreader/Common/ArchiveGridV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import Logging
@Shared var archiveThumbnail: Data?

var id: String { self.archive.id }
let cached: Bool

init(archive: Shared<ArchiveItem>, archiveThumbnail: Data? = nil) {
init(archive: Shared<ArchiveItem>, archiveThumbnail: Data? = nil, cached: Bool = false) {
self._archive = archive
self._archiveThumbnail = Shared(
wrappedValue: archiveThumbnail,
Expand All @@ -22,6 +23,7 @@ import Logging
.appendingPathComponent(archive.id, conformingTo: .image)
)
)
self.cached = cached
}
}

Expand Down Expand Up @@ -99,6 +101,9 @@ struct ArchiveGridV2: View {

func buildTitle(archive: ArchiveItem) -> String {
var title = archive.name
if store.cached {
return title
}
if archive.pagecount == archive.progress {
title = "👑 " + title
} else if archive.progress < 2 {
Expand Down
7 changes: 7 additions & 0 deletions LANreader/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ import Logging
case categoryArchiveList(CategoryArchiveListFeature)
case search(SearchFeature)
case random(RandomFeature)
case cache(CacheFeature)
}

@Reducer(state: .equatable)
Expand Down Expand Up @@ -230,6 +231,8 @@ struct ContentView: View {
SearchViewV2(store: store)
case let .random(store):
RandomView(store: store)
case let .cache(store):
CacheView(store: store)
}
}
.tabItem {
Expand All @@ -256,6 +259,8 @@ struct ContentView: View {
SearchViewV2(store: store)
case let .random(store):
RandomView(store: store)
case let .cache(store):
CacheView(store: store)
}
}
.tabItem {
Expand All @@ -282,6 +287,8 @@ struct ContentView: View {
SearchViewV2(store: store)
case let .random(store):
RandomView(store: store)
case let .cache(store):
CacheView(store: store)
}
}
.tabItem {
Expand Down
42 changes: 42 additions & 0 deletions LANreader/Database/AppDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ struct AppDatabase {
}
}

migrator.registerMigration("archiveCache") { database in
try database.create(table: "archiveCache") { table in
table.column("id", .text).primaryKey()
table.column("title", .text)
table.column("tags", .text)
table.column("thumbnail", .blob)
table.column("cached", .boolean)
table.column("totalPages", .integer)
table.column("lastUpdate", .datetime)
}
}

return migrator
}

Expand All @@ -95,6 +107,36 @@ struct AppDatabase {
}

extension AppDatabase {
func saveCache(_ cache: inout ArchiveCache) throws {
try dbWriter.write { database in
try cache.save(database)
}
}

func readAllCached() throws -> [ArchiveCache] {
try dbReader.read { database in
try ArchiveCache.fetchAll(database)
}
}

func readCache(_ id: String) throws -> ArchiveCache? {
try dbReader.read { database in
try ArchiveCache.fetchOne(database, key: id)
}
}

func existCache(_ id: String) throws -> Bool {
try dbReader.read { database in
try ArchiveCache.exists(database, key: id)
}
}

func deleteCache(_ id: String) throws -> Bool {
try dbWriter.write { database in
try ArchiveCache.deleteOne(database, id: id)
}
}

func saveArchive(_ archive: inout Archive) throws {
try dbWriter.write { database in
try archive.save(database)
Expand Down
22 changes: 22 additions & 0 deletions LANreader/Database/Record.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ struct ArchiveThumbnail: Identifiable, Equatable {
var lastUpdate: Date
}

struct ArchiveCache: Identifiable, Equatable {
var id: String
var title: String
var tags: String
var thumbnail: Data?
var cached: Bool
var totalPages: Int
var lastUpdate: Date
}

struct ArchiveImage: Identifiable, Equatable {
var id: String
var image: String
Expand Down Expand Up @@ -102,6 +112,18 @@ extension ArchiveThumbnail: Codable, FetchableRecord, MutablePersistableRecord {
}
}

extension ArchiveCache: Codable, FetchableRecord, MutablePersistableRecord {
fileprivate enum Columns {
static let id = Column(CodingKeys.id)
static let title = Column(CodingKeys.title)
static let tags = Column(CodingKeys.tags)
static let thumbnail = Column(CodingKeys.thumbnail)
static let cached = Column(CodingKeys.cached)
static let totalPages = Column(CodingKeys.totalPages)
static let lastUpdate = Column(CodingKeys.lastUpdate)
}
}

extension ArchiveImage: Codable, FetchableRecord, MutablePersistableRecord {
fileprivate enum Columns {
static let id = Column(CodingKeys.id)
Expand Down
11 changes: 3 additions & 8 deletions LANreader/LANreaderApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@ struct LANreaderApp: App {
let logFileURL = try FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("app.log", conformingTo: .log)
try? FileManager.default.removeItem(at: logFileURL)

do {
try FileManager.default.removeItem(at: logFileURL)
} catch {
// NOOP
}

if LANraragiService.downloadPath != nil {
if let path = LANraragiService.downloadPath {
if let sessionDownloadFolder = try? FileManager.default.contentsOfDirectory(
at: LANraragiService.downloadPath!,
at: path,
includingPropertiesForKeys: []
) {
sessionDownloadFolder.forEach { url in
Expand Down
Loading