-
-
Notifications
You must be signed in to change notification settings - Fork 70
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
601 bookmarks initial libkiwix implementation #679
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
AppStore/screenshots | ||
CoreKiwix.xcframework | ||
*.zim | ||
!Tests/TestResources/test_small.zim | ||
|
||
# Xcode | ||
# | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// LibBookmark.h | ||
// Kiwix | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface LibBookmark : NSObject | ||
|
||
@property std::string zimID_c; | ||
@property std::string url_c; | ||
@property std::string title_c; | ||
|
||
- (nonnull instancetype) init: (nonnull NSURL *) url inZIM: (nonnull NSUUID *) zimFileID withTitle: (nonnull NSString *) title; | ||
|
||
//- (const kiwix::Bookmark&) bridged; | ||
- (kiwix::Book) book; | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// LibBookmark.m | ||
// Kiwix | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdocumentation" | ||
#include "kiwix/bookmark.h" | ||
#include "kiwix/book.h" | ||
#include "zim/archive.h" | ||
#pragma clang diagnostic pop | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "LibBookmark.h" | ||
#import "ZimFileService.h" | ||
|
||
@interface LibBookmark() | ||
@property (nonatomic, strong) NSURL *_Nonnull url; | ||
@property (nonatomic, strong) NSUUID *_Nonnull zimFileID; | ||
@end | ||
|
||
@implementation LibBookmark | ||
|
||
- (instancetype) init: (NSURL *) url inZIM: (NSUUID *) zimFileID withTitle: (nonnull NSString *) title{ | ||
self = [super init]; | ||
if (self) { | ||
self.url = url; | ||
self.zimFileID = zimFileID; | ||
self.zimID_c = [[[zimFileID UUIDString] lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding]; | ||
self.url_c = [url fileSystemRepresentation]; | ||
self.title_c = [title cStringUsingEncoding:NSUTF8StringEncoding]; | ||
} | ||
return self; | ||
} | ||
|
||
- (kiwix::Book) book { | ||
return [ZimFileService.sharedInstance getBookBy: self.zimFileID]; | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// LibBookmarks.swift | ||
// Kiwix | ||
|
||
import Foundation | ||
|
||
struct LibBookmarks { | ||
|
||
static let shared = LibBookmarks() | ||
private let bridge = LibBookmarksBridge() | ||
|
||
private init() {} | ||
|
||
func isBookmarked(url: URL) -> Bool { | ||
guard let zimID = UUID(fromZimURL: url) else { return false } | ||
return bridge.__isBookmarked(url, inZIM: zimID) | ||
} | ||
|
||
func addBookmark(_ bookmark: LibBookmark) { | ||
bridge.__add(bookmark) | ||
} | ||
|
||
func removeBookmark(_ bookmark: LibBookmark) { | ||
bridge.__remove(bookmark) | ||
} | ||
} | ||
|
||
extension LibBookmark { | ||
convenience init?(withUrl url: URL, withTitle title: String) { | ||
guard let zimFileID = UUID(fromZimURL: url) else { return nil } | ||
self.init(url, inZIM: zimFileID, withTitle: title) | ||
} | ||
} | ||
|
||
extension UUID { | ||
init?(fromZimURL url: URL) { | ||
guard let uuid = UUID(uuidString: url.host ?? "") else { | ||
return nil | ||
} | ||
self = uuid | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// LibBookmarksBridge.h | ||
// Kiwix | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "kiwix/library.h" | ||
#import "kiwix/bookmark.h" | ||
#import "LibBookmark.h" | ||
|
||
@interface LibBookmarksBridge : NSObject | ||
|
||
- (nonnull instancetype) init; | ||
- (BOOL) isBookmarked: (nonnull NSURL *) url inZIM: (nonnull NSUUID *) zimFileID NS_REFINED_FOR_SWIFT; | ||
- (void) add: (nonnull LibBookmark *) bookmark NS_REFINED_FOR_SWIFT; | ||
- (void) remove: (nonnull LibBookmark *) bookmark NS_REFINED_FOR_SWIFT; | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// | ||
// LibBookmarksBridge.mm | ||
// Kiwix | ||
|
||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdocumentation" | ||
#include "kiwix/library.h" | ||
#include "kiwix/bookmark.h" | ||
#pragma clang diagnostic pop | ||
|
||
#import "LibBookmarksBridge.h" | ||
|
||
# pragma mark - Private properties | ||
@interface LibBookmarksBridge () | ||
|
||
@property kiwix::LibraryPtr library; | ||
|
||
@end | ||
|
||
|
||
# pragma mark - Implementation | ||
|
||
@implementation LibBookmarksBridge | ||
|
||
- (instancetype _Nonnull)init { | ||
self = [super init]; | ||
if (self) { | ||
self.library = kiwix::Library::create(); | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL) isBookmarked: (nonnull NSURL *) url inZIM: (nonnull NSUUID *) zimFileID { | ||
std::string url_c = [url fileSystemRepresentation]; | ||
std::string fileID_c = [[[zimFileID UUIDString] lowercaseString] cStringUsingEncoding:NSUTF8StringEncoding]; | ||
const std::vector<kiwix::Bookmark> bookmarks = self.library->getBookmarks(true); | ||
for (kiwix::Bookmark bookmark: bookmarks) { | ||
if (bookmark.getUrl() == url_c && bookmark.getBookId() == fileID_c) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
- (void) add: (LibBookmark *) bookmark { | ||
kiwix::Book book = [bookmark book]; | ||
self.library->addBook(book); | ||
kiwix::Bookmark bridgedBookmark = kiwix::Bookmark(book, bookmark.url_c, bookmark.title_c); | ||
self.library->addBookmark(bridgedBookmark); | ||
} | ||
|
||
- (void) remove: (LibBookmark *) bookmark { | ||
self.library->removeBookmark(bookmark.zimID_c, bookmark.url_c); | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,6 +115,16 @@ + (ZimFileMetaData *)getMetaDataWithFileURL:(NSURL *)url { | |
return metaData; | ||
} | ||
|
||
- (kiwix::Book) getBookBy:(nonnull NSUUID *) fileZimID { | ||
kiwix::Book book = kiwix::Book(); | ||
try { | ||
NSURL *fileURL = [ZimFileService.sharedInstance getFileURL: fileZimID]; | ||
book.update(zim::Archive([fileURL fileSystemRepresentation])); | ||
} catch (std::exception e) { } | ||
return book; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a lot of legwork at the moment:
Whereas all we want is to store:
This is to be compared with the 220 lines of code added in this PR... |
||
|
||
|
||
# pragma mark - URL Handling | ||
|
||
- (NSURL *)getFileURL:(NSUUID *)zimFileID { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// LibBookmarkTests.swift | ||
// UnitTests | ||
|
||
import XCTest | ||
@testable import Kiwix | ||
|
||
final class LibBookmarkTests: XCTestCase { | ||
|
||
private let testZimURL: URL = Bundle(for: LibBookmarkTests.self).url(forResource: "test_small", withExtension: "zim")! | ||
private let testTitle: String = "Dionysius of Halicarnassus On Literary Composition" | ||
private let bookmarkURL = URL(string: "kiwix://FF7D59DE-0FD8-F486-09FA-C57904646707/A/Dionysius%20of%20Halicarnassus%20On%20Literary%20Composition.50212.html")! | ||
|
||
func testIfZimFileExists() throws { | ||
XCTAssertNotNil(testZimURL) | ||
} | ||
|
||
func testAddingBookmark() { | ||
let fileURLData = ZimFileService.getFileURLBookmarkData(for: testZimURL)! | ||
try! ZimFileService.shared.open(fileURLBookmark: fileURLData) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit cumbersome, we have a hash of stored |
||
let bookmarks = LibBookmarks.shared | ||
XCTAssertFalse(bookmarks.isBookmarked(url: bookmarkURL)) | ||
|
||
let bookmark = LibBookmark(withUrl: bookmarkURL, withTitle: testTitle)! | ||
bookmarks.addBookmark(bookmark) | ||
|
||
XCTAssertTrue(bookmarks.isBookmarked(url: bookmarkURL)) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For comparison testing the pure swift implementation, does the same at the moment (or even more as it supports thumbnail URL and description):
Plus currently the |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if this call can be used without the argument and it works equally good:
self.library->getBookmarks();
The tricky part is, which is not much documented, is that the
kiwix::Book
needs to be added to the library,as on line 47.
If the
Book
is not added to thelibrary
, theself.library->getBookmarks();
won't return it, as it is an "invalid" bookmark..The other solution is not to add the Book to the library and call
getBookmarks(false)
.Well... without looking into the C++ code it is not obvious...