Implementing a Scrapbook

You may want to offer your users the ability to save a particular page for later review. Pugpig supports this concept by using a scrapbook. The scrapbook can contain as many pages as you like, and it's even possible to have multiple scrapbooks.

Scrapbooking will work with any documents that use an ATOM datasource. Pugpig uses the HTML manifests to determine which files are required by a specific page, so your manifests must be accurate.

Defining a scrapbook

Add a KGScrapbook object to your application delegate, and initialise it in your application:didFinishLaunchingWithOptions: like this:

self.scrapbook = [[KGScrapbook alloc] initWithPath:@"scrapbook"];

The path defines the name of the scrapbook; using a different name will give you a different scrapbook. For UX reasons, we tend to stick to having a single scrapbook.

Adding an item to the scrapbook

Once your scrapbook is ready, you can add items to it using

[scrapbook addPageNumber:pageNumber fromDocument:document];

Generally, we'll extend our document view controller to include a button that adds a given page to the scrapbook; usually the button action would look something like this:

[appDelegate.scrapbook addPageNumber:self.pageControl.pageNumber 

Copying the item to the scrapbook can take a second; you may want to show some sort of overlay to tell the user what you're doing.

Don't add the same page to the scrapbook more than once! This will cause undefined behaviour.

Checking if an item is in the scrapbook

You can use KGScrapbook's indexForPageNumber:inDocument: method to check if a given page is in the scrapbook. If the page is not in your scrapbook, this method returns -1. Otherwise, it returns the scrapbook page number of the page in question. In your document view controller, you might write something like this:

NSInteger scrapbookPageNumber = [appDelegate.scrapbook indexForPageNumber:self.pageControl.pageNumber 
if (scrapbookPageNumber == --1) {
// page is not yet in scrapbook
else {
// page exists in scrapbook

Removing an item from the scrapbook

KGScrapbook also provides a removePageAtIndex: method. You can use this to remove a specific page from the scrapbook. Trying to remove a page that does not exist will cause an error.

Viewing your scrapbook

You can use a Pugpig page control to view your scrapbook content, because the KGScrapbook class providers a dataSource and imageStore.

Define a new view controller (let's call it the scrapbook view controller) that is similar to your document view controller. Simply replace the imagestore and datasource used by the pageControl (usually set up in openDocument:atPosition) with the scrapbook's imagestore and datasource:

[pageControl setImageStore:[scrapbook imageStore]];
[pageControl setDataSource:[scrapbook dataSource]];

The page control will display the scrapbook as a normal document. You can extend your scrapbook view controller to let you remove items from the scrapbook, and even re-order the pages - look at the KGScrapbook interface to see what else you can do.


Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request


Powered by Zendesk