Skip to content

Commit

Permalink
Try catch random page function (#424)
Browse files Browse the repository at this point in the history
* code

* project file
  • Loading branch information
automactic authored Dec 7, 2021
1 parent c93c7a6 commit ca6b354
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
16 changes: 8 additions & 8 deletions Kiwix.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@
CODE_SIGN_ENTITLEMENTS = iOS/Support/Kiwix.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 81;
CURRENT_PROJECT_VERSION = 84;
DEVELOPMENT_TEAM = L7HWM3SP3L;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = iOS/Support/Info.plist;
Expand All @@ -1710,7 +1710,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.15;
MARKETING_VERSION = 1.15.2;
OTHER_LDFLAGS = (
"-weak_framework",
SwiftUI,
Expand All @@ -1737,7 +1737,7 @@
CODE_SIGN_ENTITLEMENTS = iOS/Support/Kiwix.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 81;
CURRENT_PROJECT_VERSION = 84;
DEVELOPMENT_TEAM = L7HWM3SP3L;
ENABLE_BITCODE = YES;
INFOPLIST_FILE = iOS/Support/Info.plist;
Expand All @@ -1746,7 +1746,7 @@
"$(inherited)",
"@executable_path/Frameworks",
);
MARKETING_VERSION = 1.15;
MARKETING_VERSION = 1.15.2;
OTHER_LDFLAGS = (
"-weak_framework",
SwiftUI,
Expand Down Expand Up @@ -1962,7 +1962,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 79;
CURRENT_PROJECT_VERSION = 84;
DEVELOPMENT_TEAM = L7HWM3SP3L;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = iOS/QuickLookPreview/Info.plist;
Expand All @@ -1974,7 +1974,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.15;
MARKETING_VERSION = 1.15.2;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = self.Kiwix.QuickLook;
Expand All @@ -1997,7 +1997,7 @@
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 79;
CURRENT_PROJECT_VERSION = 84;
DEVELOPMENT_TEAM = L7HWM3SP3L;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = iOS/QuickLookPreview/Info.plist;
Expand All @@ -2009,7 +2009,7 @@
"@executable_path/Frameworks",
"@executable_path/../../Frameworks",
);
MARKETING_VERSION = 1.15;
MARKETING_VERSION = 1.15.2;
MTL_FAST_MATH = YES;
PRODUCT_BUNDLE_IDENTIFIER = self.Kiwix.QuickLook;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
12 changes: 8 additions & 4 deletions Model/Services/ZimFileService/ZimFileService.mm
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,14 @@ - (NSString *)getRandomPagePath:(NSString *)zimFileID {
if (found == self.readers->end()) {
return nil;
} else {
std::shared_ptr<kiwix::Reader> reader = found->second;
kiwix::Entry entry = reader->getRandomPage();
std::string path = entry.getPath();
return [NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding];
try {
std::shared_ptr<kiwix::Reader> reader = found->second;
kiwix::Entry entry = reader->getRandomPage();
std::string path = entry.getPath();
return [NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding];
} catch (std::exception) {
return nil;
}
}
}

Expand Down
12 changes: 9 additions & 3 deletions iOS/Controller/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,15 @@ class RootViewController: UIViewController, UISearchControllerDelegate, UISplitV
}

func openRandomPage(zimFileID: String? = nil) {
guard let zimFileID = zimFileID ?? onDeviceZimFiles?.map({ $0.fileID }).randomElement(),
let url = ZimFileService.shared.getRandomPageURL(zimFileID: zimFileID) else { return }
openURL(url)
if let zimFileID = zimFileID ?? onDeviceZimFiles?.map({ $0.fileID }).randomElement(),
let url = ZimFileService.shared.getRandomPageURL(zimFileID: zimFileID) {
openURL(url)
} else {
let message = "Unable to find a random article. Please try again or try use a different zim file."
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
}

// MARK: - Setup & Configurations
Expand Down

0 comments on commit ca6b354

Please sign in to comment.