Last active
August 29, 2015 14:08
-
-
Save paulw11/da11ab3c7688c6deb1d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
typedef NS_ENUM(NSInteger, DeliDay) { | |
DeliDaySunday=0, | |
DeliDayMonday=1, | |
DeliDayTuesday=2, | |
DeliDayWednesday=3, | |
DeliDayThursday=4, | |
DeliDayFriday=5, | |
DeliDaySaturday=6, | |
DeliDayUnknown=-1 | |
}; | |
@interface DeliData : NSObject | |
-(id)init:(NSData *)menuData; | |
-(NSArray *) itemsForMeal:(NSString *)meal onDay:(DeliDay)day atCounter:(NSString *)counter ; | |
-(NSArray *) mealsForDay:(DeliDay)day; | |
-(NSArray *) countersForMeal:(NSString *)meal onDay:(DeliDay)day; | |
+(DeliDay)deliDayForString:(NSString *)dayString; | |
+(NSString *)stringForDeliDay:(DeliDay)day; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "DeliData.h" | |
#import "TFHpple.h" | |
#import "Item.h" | |
@interface DeliData () | |
@property (strong,nonatomic) NSMutableArray *menuData; | |
@property (strong,nonatomic) NSMutableArray *dayMeals; | |
@end | |
@implementation DeliData | |
static NSArray *dayStrings=nil; | |
static dispatch_once_t onceToken; | |
-(id)init { | |
if (self=[super init]) { | |
[self populateMenuArray]; | |
} | |
return self; | |
} | |
-(id)init:(NSData *)menuData { | |
if (self=[super init]) { | |
[self populateMenuArray]; | |
[self parseXML:menuData]; | |
} | |
return self; | |
} | |
-(void)populateMenuArray { | |
self.menuData=[NSMutableArray arrayWithCapacity:7]; | |
self.dayMeals=[NSMutableArray arrayWithCapacity:7]; | |
for (int i=0;i<7;i++) { | |
[self.menuData addObject:[NSMutableDictionary new]]; | |
[self.dayMeals addObject:[NSMutableArray new]]; | |
} | |
} | |
-(void)parseXML:(NSData *)menuData { | |
TFHpple *Parser = [TFHpple hppleWithXMLData:menuData]; | |
NSString *XpathQueryString = @"//day"; | |
NSArray *Nodes = [Parser searchWithXPathQuery:XpathQueryString]; | |
for (TFHppleElement *element in Nodes) { | |
NSString *dayString=element.attributes[@"name"]; | |
DeliDay day=[DeliData deliDayForString:dayString]; | |
if (day != DeliDayUnknown) { | |
NSMutableDictionary *dayDict=(NSMutableDictionary *)self.menuData[day]; | |
NSMutableArray *dayMeals=self.dayMeals[day]; | |
NSArray *mealsArray=[element childrenWithTagName:@"meal"]; | |
for (TFHppleElement *mealElement in mealsArray) { | |
NSString *mealName=mealElement.attributes[@"name"]; | |
[dayMeals addObject:mealName]; | |
NSMutableDictionary *counterDict=(NSMutableDictionary *)dayDict[mealName]; | |
if (counterDict == nil) { | |
counterDict=[NSMutableDictionary new]; | |
dayDict[mealName]=counterDict; | |
} | |
NSArray *countersArray=[mealElement childrenWithTagName:@"counter"]; | |
for (TFHppleElement *counterElement in countersArray) { | |
NSString *counterName=counterElement.attributes[@"name"]; | |
if (counterName!=nil) { | |
NSMutableArray *itemsArray=(NSMutableArray *)counterDict[counterName]; | |
if (itemsArray == nil) { | |
itemsArray=[NSMutableArray new]; | |
counterDict[counterName]=itemsArray; | |
} | |
NSArray *dishArray=[counterElement childrenWithTagName:@"dish"]; | |
for (TFHppleElement *dishElement in dishArray) { | |
Item *newItem=[Item new]; | |
TFHppleElement *dishNameElement=[dishElement firstChildWithTagName:@"name"]; | |
NSString *text=[[dishNameElement firstTextChild].content stringByReplacingOccurrencesOfString:@"\n" withString:@""]; | |
newItem.title=text; | |
TFHppleElement *dishUrlElement=[dishElement firstChildWithTagName:@"url"]; | |
text=[[dishUrlElement firstTextChild].content stringByReplacingOccurrencesOfString:@"\n" withString:@""]; | |
newItem.url=text; | |
[itemsArray addObject:newItem]; | |
} | |
} | |
} | |
} | |
} | |
else { | |
NSLog(@"Invalid day name %@",dayString); | |
} | |
} | |
} | |
+(void)setupDayStrings { | |
dispatch_once(&onceToken, ^{ | |
dayStrings = @[@"Sunday",@"Monday",@"Tuesday",@"Wednesday",@"Thursday",@"Friday",@"Saturday"];; | |
}); | |
} | |
#pragma mark - Public methods | |
-(NSArray *) mealsForDay:(DeliDay)day { | |
return self.dayMeals[day]; | |
} | |
-(NSArray *) countersForMeal:(NSString *)meal onDay:(DeliDay)day { | |
NSDictionary *mealsDict=(NSDictionary *)self.menuData[day]; | |
NSDictionary *countersDict=(NSDictionary *)mealsDict[meal]; | |
return [countersDict allKeys]; | |
} | |
-(NSArray *) itemsForMeal:(NSString *)meal onDay:(DeliDay)day atCounter:(NSString *)counter | |
{ | |
NSDictionary *mealsDict=(NSDictionary *)self.menuData[day]; | |
NSDictionary *countersDict=(NSDictionary *)mealsDict[meal]; | |
NSArray *itemsArray=countersDict[counter]; | |
return itemsArray; | |
} | |
-(NSString *)description { | |
return self.menuData.description; | |
} | |
#pragma mark - Class methods | |
+(DeliDay)deliDayForString:(NSString *)dayString { | |
[DeliData setupDayStrings]; | |
for (NSInteger i=0;i<dayStrings.count;i++) { | |
if ([dayString caseInsensitiveCompare:dayStrings[i]]==NSOrderedSame) { | |
return i; | |
} | |
} | |
return DeliDayUnknown; | |
} | |
+(NSString *)stringForDeliDay:(DeliDay)day { | |
[DeliData setupDayStrings]; | |
return dayStrings[day]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface Item : NSObject | |
@property (nonatomic, copy) NSString *title; | |
@property (nonatomic, copy) NSString *url; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "Item.h" | |
@implementation Item | |
-(id) init { | |
return [super init]; | |
} | |
-(NSString *)description { | |
return [NSString stringWithFormat:@"%@ - %@",self.title,self.url]; | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
@interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import "DeliData.h" | |
#import "MenuTableViewController.h" | |
@interface ViewController () | |
@property (strong,nonatomic) DeliData *delidata; | |
@property (weak,nonatomic) IBOutlet UITableView *tableView; | |
@property (strong,nonatomic) NSMutableArray *dayOrder; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
NSString *file = [[NSBundle mainBundle] pathForResource:@"menu" ofType:@"xml"]; | |
NSString *str = [NSString stringWithContentsOfFile:file | |
encoding:NSUTF8StringEncoding error:NULL]; | |
NSLog(@"str: %@", str); | |
self.delidata=[[DeliData alloc] init:[str dataUsingEncoding:NSUTF16StringEncoding]]; | |
self.dayOrder=[NSMutableArray new]; | |
NSCalendar *cal=[NSCalendar currentCalendar]; | |
NSInteger dayNumber = [cal component:NSCalendarUnitWeekday fromDate:[NSDate date]]-1; // Sunday gives 0, | |
for (int i=0;i<7;i++) { | |
[self.dayOrder addObject:[NSNumber numberWithInteger:dayNumber]]; | |
dayNumber=(dayNumber+1)%6; | |
} | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 7; | |
} | |
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
NSNumber *dayNumber=self.dayOrder[section]; | |
return [self.delidata mealsForDay:[dayNumber integerValue]].count; | |
} | |
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { | |
NSNumber *dayNumber=self.dayOrder[section]; | |
return [DeliData stringForDeliDay:[dayNumber integerValue]]; | |
} | |
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; | |
NSNumber *dayNumber=self.dayOrder[indexPath.section]; | |
NSArray *meals=[self.delidata mealsForDay:[dayNumber integerValue]]; | |
cell.textLabel.text=meals[indexPath.row]; | |
return cell; | |
} | |
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { | |
if ([segue.identifier isEqualToString:@"detailSegue"]) { | |
MenuTableViewController *destVC=(MenuTableViewController *)segue.destinationViewController; | |
destVC.deliData=self.delidata; | |
NSIndexPath *selectedPath=[self.tableView indexPathForSelectedRow]; | |
destVC.day=selectedPath.section; | |
destVC.meal=[self.delidata mealsForDay:destVC.day][selectedPath.row]; | |
[self.tableView deselectRowAtIndexPath:selectedPath animated:NO]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment