Skip to content

Commit

Permalink
V3.0 bug fixes (#472)
Browse files Browse the repository at this point in the history
* fix: bookmarks not removed when unlinking zim file

* fix: zim file not deleted when deleting zim file

* fix: pop view on unlink / delete

* build number

* webview loading concurrency issue

* build number

* fix: search text & results cleared when hiding keyboard

* fix: some zim files (e.g. ifixit) are missing category

* disable random & main article button when no opened zim file

* iPadOS: multi window

* build number

* macOS: navigation item keyboard shortcut

* build number

* another attempt at fixing crashes when attempting to sending data back at WKURLSchemeTask

* build number

* Revert "another attempt at fixing crashes when attempting to sending data back at WKURLSchemeTask"

This reverts commit cf69848.

* ignore NSExceptions

* resolve compile warning

* update to xcode recommended project settings

* build number

* more deterministic sorting

* remove duplicated search texts

* build number

* build number
  • Loading branch information
automactic authored Nov 13, 2022
1 parent 70a5712 commit baeb36e
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 96 deletions.
42 changes: 22 additions & 20 deletions Kiwix.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
97F425C227151A0D00D0F738 /* QuickLook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 97F425C127151A0D00D0F738 /* QuickLook.framework */; };
97F425C527151A0D00D0F738 /* PreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F425C427151A0D00D0F738 /* PreviewViewController.swift */; };
97F425CA27151A0D00D0F738 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97F425C827151A0D00D0F738 /* MainInterface.storyboard */; };
97F425CE27151A0D00D0F738 /* QuickLookPreview.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = 97F425C027151A0D00D0F738 /* QuickLookPreview.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
97F425CE27151A0D00D0F738 /* QuickLookPreview.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 97F425C027151A0D00D0F738 /* QuickLookPreview.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
97F425D327151E9C00D0F738 /* ZimFileService.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9779A5A92456793500F6F6FF /* ZimFileService.mm */; };
97F425D427151E9C00D0F738 /* ZimFileService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9779A5AA2456793500F6F6FF /* ZimFileService.swift */; };
97F425D527151EF800D0F738 /* ZimFileMetaData.mm in Sources */ = {isa = PBXBuildFile; fileRef = 9779A5AC2456793600F6F6FF /* ZimFileMetaData.mm */; };
Expand Down Expand Up @@ -374,15 +374,15 @@
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
};
97C57602202CB0E900E37502 /* Embed App Extensions */ = {
97C57602202CB0E900E37502 /* Embed Foundation Extensions */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "";
dstSubfolderSpec = 13;
files = (
97F425CE27151A0D00D0F738 /* QuickLookPreview.appex in Embed App Extensions */,
97F425CE27151A0D00D0F738 /* QuickLookPreview.appex in Embed Foundation Extensions */,
);
name = "Embed App Extensions";
name = "Embed Foundation Extensions";
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -1206,7 +1206,7 @@
97A36C261F8C21210079B452 /* Sources */,
97A36C341F8C21210079B452 /* Frameworks */,
97A36C431F8C21210079B452 /* Resources */,
97C57602202CB0E900E37502 /* Embed App Extensions */,
97C57602202CB0E900E37502 /* Embed Foundation Extensions */,
977BD604276EA929004D2C32 /* Embed Frameworks */,
);
buildRules = (
Expand Down Expand Up @@ -1287,7 +1287,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1330;
LastUpgradeCheck = 1310;
LastUpgradeCheck = 1410;
ORGANIZATIONNAME = "Chris Li";
TargetAttributes = {
970EC3F023BE8E20008DCA27 = {
Expand Down Expand Up @@ -1900,7 +1900,7 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 99;
CURRENT_PROJECT_VERSION = 107;
DEVELOPMENT_ASSET_PATHS = "\"SwiftUI/Support\"";
DEVELOPMENT_TEAM = L7HWM3SP3L;
ENABLE_PREVIEWS = YES;
Expand All @@ -1918,7 +1918,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 3.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = self.Kiwix;
Expand All @@ -1940,7 +1940,7 @@
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 99;
CURRENT_PROJECT_VERSION = 107;
DEVELOPMENT_ASSET_PATHS = "\"SwiftUI/Support\"";
DEVELOPMENT_TEAM = L7HWM3SP3L;
ENABLE_PREVIEWS = YES;
Expand All @@ -1958,7 +1958,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 3.0;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = self.Kiwix;
PRODUCT_NAME = Kiwix;
Expand Down Expand Up @@ -1987,7 +1987,7 @@
"$(LD_RUNPATH_SEARCH_PATHS_$(IS_MACCATALYST))",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
MARKETING_VERSION = 2.1.2;
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = org.kiwix.KiwixMacOS;
Expand Down Expand Up @@ -2019,7 +2019,7 @@
"$(LD_RUNPATH_SEARCH_PATHS_$(IS_MACCATALYST))",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.13;
MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
MARKETING_VERSION = 2.1.2;
OTHER_LDFLAGS = "$(inherited)";
PRODUCT_BUNDLE_IDENTIFIER = org.kiwix.KiwixMacOS;
Expand Down Expand Up @@ -2080,8 +2080,8 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
MACOSX_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
OTHER_SWIFT_FLAGS = "";
Expand Down Expand Up @@ -2135,8 +2135,8 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
MACOSX_DEPLOYMENT_TARGET = 11.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
MACOSX_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
Expand Down Expand Up @@ -2287,7 +2287,8 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 99;
CURRENT_PROJECT_VERSION = 107;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"SwiftUI/Support\"";
DEVELOPMENT_TEAM = L7HWM3SP3L;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -2301,7 +2302,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 3.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = self.Kiwix;
Expand All @@ -2327,7 +2328,8 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 99;
CURRENT_PROJECT_VERSION = 107;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"SwiftUI/Support\"";
DEVELOPMENT_TEAM = L7HWM3SP3L;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -2341,7 +2343,7 @@
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 2.0;
MARKETING_VERSION = 3.0;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = self.Kiwix;
PRODUCT_NAME = Kiwix;
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 = "1330"
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "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 = "1310"
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "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 = "1310"
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "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 = "1310"
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "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 = "1310"
LastUpgradeVersion = "1410"
wasCreatedForAppExtension = "YES"
version = "2.0">
<BuildAction
Expand Down
2 changes: 1 addition & 1 deletion Kiwix.xcodeproj/xcshareddata/xcschemes/WikiMed.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1330"
LastUpgradeVersion = "1410"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Model/Realm/RealmConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ extension Realm {
guard let zimFileData = existingZimFiles.popLast() else { continue }

zimFile.articleCount = zimFileData.articleCount
zimFile.category = zimFileData.category
zimFile.category = (Category(rawValue: zimFileData.category) ?? .other).rawValue
zimFile.created = zimFileData.created
zimFile.downloadURL = zimFileData.downloadURL
zimFile.faviconData = zimFileData.faviconData
Expand Down
11 changes: 11 additions & 0 deletions Model/Support/Kiwix-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@
#import "ZimFileMetaData.h"
#import "SearchOperation.h"
#import "SearchResult.h"


NS_INLINE NSException * _Nullable objCTryBlock(void(^_Nonnull tryBlock)(void)) {
@try {
tryBlock();
return nil;
}
@catch (NSException *exception) {
return exception;
}
}
81 changes: 38 additions & 43 deletions Model/Utilities/WebKitHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,54 @@ import os
import WebKit

class KiwixURLSchemeHandler: NSObject, WKURLSchemeHandler {
private var activeRequests = Set<URLRequest>()
private let activeRequestsSemaphore = DispatchSemaphore(value: 1)
private let dataFetchingSemaphore = DispatchSemaphore(value: ProcessInfo.processInfo.activeProcessorCount)
private var urls = Set<URL>()
private var queue = DispatchQueue(label: "org.kiwix.webContent", qos: .userInitiated)

func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) {
// unpack zimFileID and content path from the url
guard let url = urlSchemeTask.request.url,
url.isKiwixURL else {
queue.async {
// unpack zimFileID and content path from the url
guard let url = urlSchemeTask.request.url, url.isKiwixURL else {
urlSchemeTask.didFailWithError(URLError(.unsupportedURL))
return
}

// remember this active url scheme task
activeRequestsSemaphore.wait()
activeRequests.insert(urlSchemeTask.request)
activeRequestsSemaphore.signal()

// fetch data and send response
DispatchQueue.global(qos: .userInitiated).async {
// fetch data
self.dataFetchingSemaphore.wait()
let content = ZimFileService.shared.getURLContent(url: url)
self.dataFetchingSemaphore.signal()

// check the url scheme task is not stopped
self.activeRequestsSemaphore.wait()
guard let _ = self.activeRequests.remove(urlSchemeTask.request) else {
self.activeRequestsSemaphore.signal()
return
}
self.activeRequestsSemaphore.signal()

// assemble and send response
if let content = content,
let response = HTTPURLResponse(
url: url,
statusCode: 200,
httpVersion: "HTTP/1.1",
headerFields: ["Content-Type": content.mime, "Content-Length": "\(content.length)"])
{
urlSchemeTask.didReceive(response)
urlSchemeTask.didReceive(content.data)
urlSchemeTask.didFinish()
} else {
os_log("Resource not found for url: %s.", log: Log.URLSchemeHandler, type: .info, url.absoluteString)
urlSchemeTask.didFailWithError(URLError(.resourceUnavailable, userInfo: ["url": url]))
// fetch url content and return data to the task
self.urls.insert(url)
DispatchQueue.global(qos: .userInitiated).async {
let content = ZimFileService.shared.getURLContent(url: url)
self.queue.async {
guard self.urls.contains(url) else { return }
if let content = content,
let response = HTTPURLResponse(
url: url,
statusCode: 200,
httpVersion: "HTTP/1.1",
headerFields: ["Content-Type": content.mime, "Content-Length": "\(content.length)"])
{
objCTryBlock {
urlSchemeTask.didReceive(response)
urlSchemeTask.didReceive(content.data)
urlSchemeTask.didFinish()
}
} else {
os_log(
"Resource not found for url: %s.",
log: Log.URLSchemeHandler,
type: .info,
url.absoluteString
)
urlSchemeTask.didFailWithError(URLError(.resourceUnavailable, userInfo: ["url": url]))
}
self.urls.remove(url)
}
}
}
}

func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
activeRequestsSemaphore.wait()
activeRequests.remove(urlSchemeTask.request)
activeRequestsSemaphore.signal()
queue.async {
guard let url = urlSchemeTask.request.url else { return }
self.urls.remove(url)
}
}
}
7 changes: 7 additions & 0 deletions SwiftUI/Library/ZimFileDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Defaults
struct ZimFileDetail: View {
@Binding var url: URL?
@Default(.downloadUsingCellular) private var downloadUsingCellular
@Environment(\.presentationMode) var presentationMode
@EnvironmentObject private var viewModel: ViewModel
@EnvironmentObject private var libraryViewModel: LibraryViewModel
@ObservedObject var zimFile: ZimFile
Expand Down Expand Up @@ -126,6 +127,9 @@ struct ZimFileDetail: View {
"""),
primaryButton: .destructive(Text("Unlink")) {
LibraryOperations.unlink(zimFileID: zimFile.fileID)
#if os(iOS)
presentationMode.wrappedValue.dismiss()
#endif
},
secondaryButton: .cancel()
)
Expand All @@ -141,6 +145,9 @@ struct ZimFileDetail: View {
message: Text("The zim file and all bookmarked articles linked to this zim file will be deleted."),
primaryButton: .destructive(Text("Delete")) {
LibraryOperations.delete(zimFileID: zimFile.fileID)
#if os(iOS)
presentationMode.wrappedValue.dismiss()
#endif
},
secondaryButton: .cancel()
)
Expand Down
2 changes: 1 addition & 1 deletion SwiftUI/Model/Entities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ZimFile: NSManagedObject, Identifiable {
@NSManaged var size: Int64

@NSManaged var downloadTask: DownloadTask?
@NSManaged var bookmarks: [Bookmark]
@NSManaged var bookmarks: Set<Bookmark>

static var openedPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
NSPredicate(format: "fileURLBookmark != nil"),
Expand Down
Loading

0 comments on commit baeb36e

Please sign in to comment.