Skip to content

Commit

Permalink
Fix memory keep growing (#217)
Browse files Browse the repository at this point in the history
* fix memory keep growing

* misc
  • Loading branch information
Doraemoe authored Jun 1, 2024
1 parent 1d2cfdb commit 725990e
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 131 deletions.
16 changes: 8 additions & 8 deletions LANreader.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 97;
CURRENT_PROJECT_VERSION = 98;
DEVELOPMENT_ASSET_PATHS = "\"LANreader/Preview Content\"";
DEVELOPMENT_TEAM = UUEBW58SA6;
ENABLE_PREVIEWS = YES;
Expand All @@ -876,7 +876,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.5.0;
MARKETING_VERSION = 2.5.1;
PRODUCT_BUNDLE_IDENTIFIER = com.jif.LANreader;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -893,7 +893,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 97;
CURRENT_PROJECT_VERSION = 98;
DEVELOPMENT_ASSET_PATHS = "\"LANreader/Preview Content\"";
DEVELOPMENT_TEAM = UUEBW58SA6;
ENABLE_PREVIEWS = YES;
Expand All @@ -905,7 +905,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.5.0;
MARKETING_VERSION = 2.5.1;
PRODUCT_BUNDLE_IDENTIFIER = com.jif.LANreader;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -972,7 +972,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 97;
CURRENT_PROJECT_VERSION = 98;
DEVELOPMENT_TEAM = UUEBW58SA6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Action/Info.plist;
Expand All @@ -984,7 +984,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 2.5.0;
MARKETING_VERSION = 2.5.1;
PRODUCT_BUNDLE_IDENTIFIER = com.jif.LANreader.Action;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -1001,7 +1001,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++20";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 97;
CURRENT_PROJECT_VERSION = 98;
DEVELOPMENT_TEAM = UUEBW58SA6;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Action/Info.plist;
Expand All @@ -1013,7 +1013,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 2.5.0;
MARKETING_VERSION = 2.5.1;
PRODUCT_BUNDLE_IDENTIFIER = com.jif.LANreader.Action;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-perception",
"state" : {
"revision" : "8e8ca36c02abc7775a3ab81f9468c0df5fe22c2c",
"version" : "1.1.7"
"revision" : "9b77fbd07b9529312f7e9adb10f5131acd9e2363",
"version" : "1.2.0"
}
},
{
Expand All @@ -177,8 +177,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swiftui-navigation",
"state" : {
"revision" : "2ec6c3a15293efff6083966b38439a4004f25565",
"version" : "1.3.0"
"revision" : "7ab04c6e2e6a73d34d5a762970ef88bf0aedb084",
"version" : "1.4.0"
}
},
{
Expand Down
149 changes: 73 additions & 76 deletions LANreader/Page/PageImageV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ import Logging
@SharedReader(.appStorage(SettingsKey.splitWideImage)) var splitImage = false
@SharedReader(.appStorage(SettingsKey.splitPiorityLeft)) var piorityLeft = false

var image: Data?
let pageId: String
let suffix: String
let pageNumber: Int
var loading: Bool = false
var progress: Double = 0
var errorMessage = ""
var pageMode: PageMode = .normal
var pageMode: PageMode
let cached: Bool

var id: String {
"\(pageId)-\(suffix)"
}

let folder: URL?
let path: URL?
let pathLeft: URL?
let pathRight: URL?

init(archiveId: String, pageId: String, pageNumber: Int, pageMode: PageMode = .normal, cached: Bool = false) {
init(archiveId: String, pageId: String, pageNumber: Int, pageMode: PageMode = .loading, cached: Bool = false) {
self.pageId = pageId
self.pageNumber = pageNumber
self.pageMode = pageMode
Expand All @@ -42,14 +42,12 @@ import Logging
} else {
LANraragiService.downloadPath
}
self.path = imagePath?
.appendingPathComponent(archiveId, conformingTo: .folder)
self.folder = imagePath?.appendingPathComponent(archiveId, conformingTo: .folder)
self.path = self.folder?
.appendingPathComponent("\(pageNumber)", conformingTo: .image)
self.pathLeft = imagePath?
.appendingPathComponent(archiveId, conformingTo: .folder)
self.pathLeft = self.folder?
.appendingPathComponent("\(pageNumber)-left", conformingTo: .image)
self.pathRight = imagePath?
.appendingPathComponent(archiveId, conformingTo: .folder)
self.pathRight = self.folder?
.appendingPathComponent("\(pageNumber)-right", conformingTo: .image)
}
}
Expand All @@ -60,7 +58,7 @@ import Logging
case subscribeToProgress(DownloadRequest)
case cancelSubscribeImageProgress
case setProgress(Double)
case setImage(Data, Data?, Data?)
case setImage(PageMode, Bool)
case setError(String)
case insertPage(PageMode)
}
Expand Down Expand Up @@ -93,26 +91,34 @@ import Logging
}
state.loading = true

let previousPageMode = state.pageMode

if force {
state.image = nil
} else {
switch state.pageMode {
case .normal:
if let path = state.path {
state.image = try? Data(contentsOf: path)
}
case .left:
if let path = state.pathLeft {
state.image = try? Data(contentsOf: path)
}
case .right:
if let path = state.pathRight {
state.image = try? Data(contentsOf: path)
state.pageMode = .loading
} else if state.pageMode == .loading {
if state.splitImage && !state.fallback {
if state.piorityLeft &&
FileManager.default.fileExists(
atPath: state.pathLeft?.path(percentEncoded: false) ?? ""
) {
state.pageMode = .left
return .send(.insertPage(.right))
} else if FileManager.default.fileExists(
atPath: state.pathRight?.path(percentEncoded: false) ?? ""
) {
state.pageMode = .right
return .send(.insertPage(.left))
}
}
if FileManager.default.fileExists(atPath: state.path?.path(percentEncoded: false) ?? "") {
state.pageMode = .normal
return .none
}
} else {
return .none
}

if state.image == nil {
if state.pageMode == .loading {
if state.cached {
state.loading = false
return .send(.setError(String(localized: "archive.cache.page.load.failed")))
Expand All @@ -121,38 +127,28 @@ import Logging
do {
let task = service.fetchArchivePage(page: state.pageId, pageNumber: state.pageNumber)
await send(.subscribeToProgress(task))
let imageData = try await task
.serializingData()
let imageUrl = try await task
.serializingDownloadedFileURL()
.value
await send(.cancelSubscribeImageProgress)

if !state.showOriginal {
await send(.setProgress(2.0))
}
let (processedImage, leftImage, rightImage) = imageService.resizeImage(
data: imageData,
let splitted = imageService.resizeImage(
imageUrl: imageUrl,
destinationUrl: state.folder!,
pageNumber: String(state.pageNumber),
split: state.splitImage && !state.fallback,
skip: state.showOriginal
)
await send(.setImage(processedImage, leftImage, rightImage))
await send(.setImage(previousPageMode, splitted))
} catch {
logger.error("failed to load image. \(error)")
}
await send(.setIsLoading(false))
}
}
} else {
if state.cached && state.pageMode == .normal && state.splitImage && !state.fallback {
if state.piorityLeft, let path = state.pathLeft, let leftImage = try? Data(contentsOf: path) {
state.pageMode = .left
state.image = leftImage
return .send(.insertPage(.right))
} else if let path = state.pathRight, let rightImage = try? Data(contentsOf: path) {
state.pageMode = .right
state.image = rightImage
return .send(.insertPage(.left))
}
}
}
state.loading = false
return .none
Expand All @@ -162,34 +158,23 @@ import Logging
case let .setProgress(progres):
state.progress = progres
return .none
case let .setImage(processedImage, leftImage, rightImage):
case let .setImage(previousPageMode, splitted):
state.progress = 0
state.loading = false
if leftImage != nil && rightImage != nil {
if let path = state.pathLeft {
try? leftImage!.write(to: path)
}
if let path = state.pathRight {
try? rightImage!.write(to: path)
if splitted {
if previousPageMode == .left || previousPageMode == .right {
state.pageMode = previousPageMode
return .none
}
switch state.pageMode {
case .normal:
if state.piorityLeft {
state.pageMode = .left
state.image = leftImage
return .send(.insertPage(.right))
} else {
state.pageMode = .right
state.image = rightImage
return .send(.insertPage(.left))
}
case .left:
state.image = leftImage
case .right:
state.image = rightImage
if state.piorityLeft {
state.pageMode = .left
return .send(.insertPage(.right))
} else {
state.pageMode = .right
return .send(.insertPage(.left))
}
} else {
state.image = processedImage
state.pageMode = .normal
}
return .none
case let .setError(message):
Expand All @@ -215,17 +200,7 @@ struct PageImageV2: View {
// If not wrapped in ZStack, TabView will render ALL pages when initial load
ZStack {
if visible {
if let imageData = store.image {
if let uiImage = UIImage(data: imageData) {
Image(uiImage: uiImage)
.resizable()
.aspectRatio(contentMode: .fit)
.draggableAndZoomable(contentSize: geometrySize)
} else {
Image(systemName: "rectangle.slash")
.frame(height: geometrySize.height)
}
} else {
if store.pageMode == .loading {
ProgressView(
value: store.progress > 1 ? 1 : store.progress,
total: 1
Expand All @@ -243,10 +218,31 @@ struct PageImageV2: View {
.task {
store.send(.load(false))
}
} else {
let contentPath = {
switch store.pageMode {
case .left:
return store.pathLeft
case .right:
return store.pathRight
default:
return store.path
}
}()
AsyncImage(url: contentPath) { image in
image
.resizable()
.aspectRatio(contentMode: .fit)
.draggableAndZoomable(contentSize: geometrySize)
} placeholder: {
Image(systemName: "rectangle.slash")
.frame(height: geometrySize.height)
}
}
} else {
Color.clear
}

}
.onAppear {
visible = true
Expand All @@ -258,6 +254,7 @@ struct PageImageV2: View {
}

enum PageMode: String {
case loading
case left
case right
case normal
Expand Down
Loading

0 comments on commit 725990e

Please sign in to comment.