[iOS] reading data and back buttons
This commit is contained in:
parent
dc165d83fb
commit
a0673f3100
5 changed files with 91 additions and 16 deletions
|
@ -6,6 +6,7 @@
|
|||
|
||||
@interface ArticleVC : UITableViewController <UISearchBarDelegate, UITableViewDelegate>
|
||||
@property (nonatomic, assign) id <ArticleDelegate> delegate;
|
||||
@property (nonatomic, strong) NSMutableArray * loadedWebPages;
|
||||
//uses on start of application
|
||||
-(NSString *)getDefaultArticle;
|
||||
@end
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
@interface ArticleVC ()
|
||||
{
|
||||
/// @todo Replace on Storage
|
||||
StorageMock m_storage;
|
||||
Storage m_storage;
|
||||
vector<ArticleInfo> m_infos;
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,12 @@
|
|||
self.tableView.tableHeaderView = self.searchBar;
|
||||
self.searchBar.text = @"";
|
||||
[self searchBar:self.searchBar textDidChange:@""];
|
||||
|
||||
NSString * path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"dat" inDirectory:@"/data/"];
|
||||
m_storage.Load([path UTF8String]);
|
||||
m_storage.QueryArticleInfos(m_infos, "");
|
||||
|
||||
[self.tableView reloadData];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -97,8 +103,18 @@
|
|||
ArticleInfo const * info = [self infoByIndexPath:indexPath];
|
||||
|
||||
GuideVC * vc = [[GuideVC alloc] init];
|
||||
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
|
||||
{
|
||||
BOOL rootVC = [self.navigationController.viewControllers count] == 1 ? YES : NO;
|
||||
[self.navigationController pushViewController:vc animated:YES];
|
||||
if (rootVC)
|
||||
self.loadedWebPages = [[NSMutableArray alloc] init];
|
||||
else
|
||||
[vc clearPreviosViews];
|
||||
}
|
||||
|
||||
vc.webPages = self.loadedWebPages;
|
||||
[vc loadPage:[NSString stringWithUTF8String:info->m_url.c_str()]];
|
||||
[self.navigationController pushViewController:vc animated:YES];
|
||||
[self.delegate selectHtmlPageUrl:[NSString stringWithUTF8String:info->m_url.c_str()]];
|
||||
}
|
||||
|
||||
|
|
|
@ -3,5 +3,8 @@
|
|||
@interface GuideVC : UIViewController <UIWebViewDelegate, UIGestureRecognizerDelegate>
|
||||
|
||||
-(void)loadPage:(NSString *)pageUrl;
|
||||
@property (nonatomic, strong) NSMutableArray * webPages;
|
||||
|
||||
-(void)clearPreviosViews;
|
||||
|
||||
@end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#import "GuideVC.h"
|
||||
#import "ArticleVC.h"
|
||||
#import "MapsWithMeAPI.h"
|
||||
#import "../../std/algorithm.hpp"
|
||||
|
||||
|
@ -8,10 +9,10 @@
|
|||
{
|
||||
float m_webViewScale;
|
||||
float m_webViewScaleOnStart;
|
||||
NSString * m_guide;
|
||||
}
|
||||
|
||||
@property (nonatomic, strong) UIWebView * webView;
|
||||
@property (nonatomic, strong) NSString * m_guide;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -27,9 +28,11 @@
|
|||
UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(onPinch:)];
|
||||
pinch.delegate = self;
|
||||
[self.webView addGestureRecognizer:pinch];
|
||||
self.webView.delegate = self;
|
||||
self.view = self.webView;
|
||||
m_webViewScale = 1.0;
|
||||
m_webViewScaleOnStart = 0.0;
|
||||
_webPages = [[NSMutableArray alloc] initWithObjects:nil];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
@ -42,13 +45,12 @@
|
|||
|
||||
-(void)loadPage:(NSString *)pageUrl
|
||||
{
|
||||
m_guide = [pageUrl copy];
|
||||
NSRange r = [pageUrl rangeOfString:@"."];
|
||||
NSString * pageName = [pageUrl substringToIndex:r.location];
|
||||
NSString * pageType = [pageUrl substringFromIndex:r.location + 1];
|
||||
NSString * pathtoPage = [[NSBundle mainBundle] pathForResource:pageName ofType:pageType inDirectory:DATAFOLDER];
|
||||
|
||||
NSURL * url = [NSURL fileURLWithPath: pathtoPage isDirectory:NO];
|
||||
self.m_guide = [pageUrl copy];
|
||||
NSRange r = [pageUrl rangeOfString:@"." options:NSBackwardsSearch];
|
||||
NSString * pathToPage;
|
||||
if (r.length == 0)
|
||||
pathToPage = [[NSBundle mainBundle] pathForResource:pageUrl ofType:@"html" inDirectory:DATAFOLDER];
|
||||
NSURL * url = [NSURL fileURLWithPath:pathToPage isDirectory:NO];
|
||||
[self.webView loadRequest:[NSURLRequest requestWithURL: url]];
|
||||
}
|
||||
|
||||
|
@ -60,15 +62,20 @@
|
|||
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg_header.png"] forBarMetrics:UIBarMetricsLandscapePhone];
|
||||
|
||||
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
|
||||
{
|
||||
self.navigationItem.leftBarButtonItem = [self getCustomButtonWithImage:@"ic_back"];
|
||||
self.navigationItem.rightBarButtonItem = [self getCustomButtonWithImage:@"ic_articleselection"];
|
||||
}
|
||||
if ([self.webPages count])
|
||||
self.navigationItem.leftBarButtonItem = [self getCustomButtonWithImage:@"ic_back"];
|
||||
}
|
||||
|
||||
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
|
||||
navigationType:(UIWebViewNavigationType)navigationType
|
||||
{
|
||||
NSString * str = [self normalizeUrl:[[request URL] absoluteString]];
|
||||
[self.webPages addObject:str];
|
||||
if ([self.webPages count] > 1 && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
|
||||
self.navigationItem.leftBarButtonItem = [self getCustomButtonWithImage:@"ic_back"];
|
||||
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
|
||||
self.navigationItem.leftBarButtonItem = [self getCustomButtonWithImage:@"ic_back"];
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
@ -109,8 +116,28 @@ shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherG
|
|||
|
||||
-(void)back
|
||||
{
|
||||
//@todo
|
||||
[self.navigationController popToRootViewControllerAnimated:YES];
|
||||
[self.webPages removeLastObject];
|
||||
if ([self.webPages count] == 0)
|
||||
{
|
||||
[self.navigationController popToRootViewControllerAnimated:YES];
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
NSRange r = [[self.webPages lastObject] rangeOfString:@"." options:NSBackwardsSearch];
|
||||
if (r.length)
|
||||
{
|
||||
NSString * pathToPage = [[NSBundle mainBundle] pathForResource:[[self.webPages lastObject] substringToIndex:r.location] ofType:@"html" inDirectory:DATAFOLDER];
|
||||
NSURL * url = [NSURL fileURLWithPath:pathToPage isDirectory:NO];
|
||||
[self.webView loadRequest:[NSURLRequest requestWithURL: url]];
|
||||
}
|
||||
else
|
||||
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[self.webPages lastObject]]]];
|
||||
}
|
||||
if ([self.webPages count] == 1 && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
|
||||
self.navigationItem.leftBarButtonItem = nil;
|
||||
[self.webPages removeLastObject];
|
||||
|
||||
}
|
||||
|
||||
-(UIBarButtonItem *)getCustomButtonWithImage:(NSString *)name
|
||||
|
@ -121,8 +148,34 @@ shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherG
|
|||
[button setImage:backButton forState:UIControlStateNormal];
|
||||
button.frame = CGRectMake(0, 0, backButton.size.width, backButton.size.height);
|
||||
|
||||
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
|
||||
if ([name isEqualToString:@"ic_back"])
|
||||
[button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
|
||||
else
|
||||
[button addTarget:self action:@selector(goToMainMenu) forControlEvents:UIControlEventTouchUpInside];
|
||||
|
||||
return [[UIBarButtonItem alloc] initWithCustomView:button];
|
||||
}
|
||||
|
||||
-(NSString *)normalizeUrl:(NSString *)url
|
||||
{
|
||||
if ([url rangeOfString:@"offlineguides.app//data/"].location != NSNotFound)
|
||||
{
|
||||
NSRange r = [url rangeOfString:@"/" options:NSBackwardsSearch];
|
||||
NSString * s = [url substringFromIndex:r.location + 1];
|
||||
return s;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
-(void)clearPreviosViews
|
||||
{
|
||||
self.navigationController.viewControllers = @[[self.navigationController.viewControllers objectAtIndex:0], [self.navigationController.viewControllers lastObject]];
|
||||
}
|
||||
|
||||
-(void)goToMainMenu
|
||||
{
|
||||
ArticleVC * vc = [[ArticleVC alloc] init];
|
||||
vc.loadedWebPages = [NSMutableArray arrayWithArray:self.webPages];
|
||||
[self.navigationController pushViewController:vc animated:YES];
|
||||
}
|
||||
@end
|
||||
|
|
|
@ -40,6 +40,8 @@
|
|||
{
|
||||
UINavigationController * navVC = (UINavigationController*)[self.viewControllers objectAtIndex:1];
|
||||
GuideVC * g = (GuideVC *)navVC.visibleViewController;
|
||||
g.navigationItem.leftBarButtonItem = nil;
|
||||
g.webPages = [NSMutableArray arrayWithArray:@[]];
|
||||
[g loadPage:url];
|
||||
[self.pop dismissPopoverAnimated:YES];
|
||||
}
|
||||
|
|
Reference in a new issue