Skip to content
This repository
branch: fastsearch
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 237 lines (191 sloc) 5.517 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
//
// IGKBackForwardManager.m
// Ingredients
//
// Created by Alex Gordon on 07/03/2010.
// Written in 2010 by Fileability.
//

#import "IGKBackForwardManager.h"


@interface IGKBackForwardManager ()

- (void)loadItem:(WebHistoryItem *)item;

@end


@implementation IGKBackForwardManager

@synthesize webView;
@synthesize delegate;
@synthesize menuStack;

- (id)init
{
if (self = [super init])
{
backStack = [[NSMutableArray alloc] init];
forwardStack = [[NSMutableArray alloc] init];

}
return self;
}


- (void)visitPage:(WebHistoryItem *)item
{
//If item has the same URL as currentItem, we ignore it
NSString *itemURL = [item URLString];
NSString *currentItemURL = [currentItem URLString];
NSURL *newURL = [NSURL URLWithString:itemURL];
if ([itemURL length] && [currentItemURL length] && ([newURL isEqual:[NSURL URLWithString:currentItemURL]] || [[newURL scheme] isEqual:@"file"]))
{
return;
}

//Push currentItem onto backStack, if it exists
if (currentItem)
{
[self willChangeValueForKey:@"backStack"];
[backStack addObject:currentItem];
[[WebHistory optionalSharedHistory] addItems:[NSArray arrayWithObject:item]];

[self didChangeValueForKey:@"backStack"];
}

currentItem = item;

//Dump whatever's in forwardStack
[self willChangeValueForKey:@"forwardStack"];
[forwardStack removeAllObjects];
[self didChangeValueForKey:@"forwardStack"];

if ([delegate respondsToSelector:@selector(backForwardManagerUpdatedLists:)])
[delegate backForwardManagerUpdatedLists:self];
}

- (void)loadItem:(WebHistoryItem *)item
{
NSURL *url = [NSURL URLWithString:[item URLString]];

if ([delegate respondsToSelector:@selector(loadURL:recordHistory:)])
[delegate loadURL:url recordHistory:NO];
}
- (BOOL)canGoBack
{
if ([backStack count] > 0)
return YES;

return NO;
}
- (BOOL)canGoForward
{
if ([forwardStack count] > 0)
return YES;

return NO;
}
- (IBAction)goBack:(id)sender
{
[self goBackBy:1];
}
- (IBAction)goForward:(id)sender
{
[self goForwardBy:1];
}


- (void)goBackBy:(NSInteger)amount
{
//Check that there's a page to go back to
if ([backStack count] == 0)
{
return;
}

//Check that amount is in range
if (amount < 1 || amount > [backStack count])
{
return;
}

if (currentItem || amount > 1)
{
[self willChangeValueForKey:@"forwardStack"];

//Push currentItem onto forwardStack, if it exists
if (currentItem)
[forwardStack addObject:currentItem];

//Push the last [amount] objects into forwardStack
if (amount > 1)
{
NSArray *subarray = [backStack subarrayWithRange:NSMakeRange([backStack count] - amount + 1, amount - 1)];
for (id item in [subarray reverseObjectEnumerator])
{
[forwardStack addObject:item];
}
}

[self didChangeValueForKey:@"forwardStack"];
}

//Pop amount objects off backStack and assign the last one to currentItem
currentItem = [backStack objectAtIndex:[backStack count] - amount];

[self willChangeValueForKey:@"backStack"];
[backStack removeObjectsInRange:NSMakeRange([backStack count] - amount, amount)];
[self didChangeValueForKey:@"backStack"];

if ([delegate respondsToSelector:@selector(backForwardManagerUpdatedLists:)])
[delegate backForwardManagerUpdatedLists:self];

//Load the page we're going back to
[self loadItem:currentItem];
}
- (void)goForwardBy:(NSInteger)amount
{
//Check that there's a page to go forward to
if ([forwardStack count] == 0)
{
return;
}

//Check that amount is in range
if (amount < 1 || amount > [forwardStack count])
{
return;
}

if (currentItem || amount > 1)
{
[self willChangeValueForKey:@"backStack"];

//Push currentItem onto forwardStack, if it exists
if (currentItem)
[backStack addObject:currentItem];

//Push the last [amount] objects into forwardStack
if (amount > 1)
{
NSArray *subarray = [forwardStack subarrayWithRange:NSMakeRange([forwardStack count] - amount + 1, amount - 1)];
for (id item in [subarray reverseObjectEnumerator])
{
[backStack addObject:item];
}
}

[self didChangeValueForKey:@"backStack"];
}

//Pop amount objects off forwardStack and assign the last one to currentItem
currentItem = [forwardStack objectAtIndex:[forwardStack count] - amount];

[self willChangeValueForKey:@"forwardStack"];
[forwardStack removeObjectsInRange:NSMakeRange([forwardStack count] - amount, amount)];
[self didChangeValueForKey:@"forwardStack"];

if ([delegate respondsToSelector:@selector(backForwardManagerUpdatedLists:)])
[delegate backForwardManagerUpdatedLists:self];

//Load the page we're going forward to
[self loadItem:currentItem];
}

- (NSArray *)backList
{
//Reverse backStack
NSMutableArray *reverseCopy = [[NSMutableArray alloc] initWithCapacity:[backStack count]];
for (id x in [backStack reverseObjectEnumerator])
{
[reverseCopy addObject:x];
}

return reverseCopy;
}
- (WebHistoryItem *)backItem
{
return [backStack lastObject];
}
- (WebHistoryItem *)currentItem
{
return currentItem;
}
- (WebHistoryItem *)forwardItem
{
return [forwardStack lastObject];
}
- (NSArray *)forwardList
{
//Reverse forwardStack
NSMutableArray *reverseCopy = [[NSMutableArray alloc] initWithCapacity:[forwardStack count]];
for (id x in [forwardStack reverseObjectEnumerator])
{
[reverseCopy addObject:x];
}

return reverseCopy;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"\thistory = \n\tbackStack = %@\n\tcurrent = %@\n\tforwardStack = %@\n\t", backStack, currentItem, forwardStack];
}

@end
Something went wrong with that request. Please try again.