Guest User

flipview

a guest
Oct 31st, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  MapViewController.m
  3. //  CPOP Test 4
  4. //
  5. //  Created by Will Kerswell on 03/10/2012.
  6. //  Copyright (c) 2012 Will Kerswell. All rights reserved.
  7. //
  8.  
  9. #import "MapViewController.h"
  10. #import "UIDevice+Resolutions.h"
  11. #import "DataBase.h"
  12.  
  13. @implementation MapViewController
  14. @synthesize showMapView;
  15. @synthesize mapSgementedControl;
  16.  
  17.  
  18.  
  19. /////////////////////////////
  20. #pragma mark - Pin Display
  21. /////////////////////////////
  22.  
  23.  
  24. -(void)pins:(NSString *)name lat:(NSString *)lat lon:(NSString *)lon poiID:(NSString *)poiID catID:(NSString *)catID
  25. {
  26.    
  27.     NSLog(@"catids: %@",catID);
  28.    
  29.     //cast string to float
  30.     CGFloat Lat = (CGFloat)[lat floatValue];
  31.     CGFloat Lon = (CGFloat)[lon floatValue];
  32.    
  33.     //Set coordinates of pin
  34.     CLLocationCoordinate2D coordinate;
  35.     coordinate.latitude = Lat;
  36.     coordinate.longitude = Lon;
  37.    
  38.     //custom anotation class
  39.     MapAnnotation* annotation = [[MapAnnotation alloc]init];
  40.     annotation.coordinate = coordinate;
  41.     annotation.title = name;
  42.     annotation.categoryID = catID;
  43.     annotation.poiID = poiID;
  44.    
  45.  
  46.     // Add pin to map
  47.     [self.showMapView addAnnotation:annotation];
  48.    
  49. }
  50.  
  51. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
  52.     //create annotation
  53.     MKPinAnnotationView *pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pinView"];
  54.     if (!pinView) {
  55.         pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pinView"];
  56.        
  57.         pinView.animatesDrop = FALSE;
  58.         pinView.canShowCallout = YES;
  59.         NSString* catID =[[NSString alloc]init];
  60.         catID = [(MapAnnotation*)annotation categoryID];
  61.        
  62.         NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  63.         NSString  *documentsDirectory = [paths objectAtIndex:0];
  64.         //set the path where the file is
  65.         NSString  *filePath = [NSString stringWithFormat:@"%@/cat%@.png", documentsDirectory,catID];
  66.        
  67.         if([ catID isEqualToString: @"1"])
  68.         {
  69.            //pinView.pinColor = MKPinAnnotationColorGreen;
  70.             pinView.image = [UIImage imageWithContentsOfFile:filePath];
  71.            
  72.         }else if([catID isEqualToString: @"2"]){
  73.            
  74.             pinView.image = [UIImage imageWithContentsOfFile:filePath];
  75.             //pinView.pinColor = MKPinAnnotationColorPurple;
  76.            
  77.         }else if([catID isEqualToString: @"3"]){
  78.            
  79.             pinView.image = [UIImage imageWithContentsOfFile:filePath];
  80.             //pinView.pinColor = MKPinAnnotationColorRed;
  81.            
  82.         }else {
  83.            
  84.             pinView.image = [UIImage imageWithContentsOfFile:filePath];
  85.             //pinView.pinColor = MKPinAnnotationColorRed;
  86.         }
  87.        
  88.         //details button
  89.         UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
  90.         pinView.rightCalloutAccessoryView = rightButton;
  91.        
  92.     } else {
  93.         pinView.annotation = annotation;
  94.     }
  95.     return pinView;
  96. }
  97.  
  98.  
  99. //IMPLEMENT THIS ONE (WHEN DISCLOSURE BUTTON IS PRESSED):
  100. - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control{
  101.     //create and annotation
  102.     MapAnnotation *annotation = view.annotation;
  103.     //set global var eq to the id
  104.     self.POIID = annotation.poiID;
  105.     //NSLog(@"Pin button pressed: %@",temp);
  106.     [self performSegueWithIdentifier:@"showDetail" sender:self];
  107.    
  108. }
  109.  
  110. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  111.    
  112.     if ([[segue identifier] isEqualToString:@"showDetail"]) {
  113.        
  114.         //say where its going
  115.         DetailsViewController *detailViewController = [segue destinationViewController];
  116.         //set the poiID in the next view eq to the row pressed.
  117.         detailViewController.poiID = self.POIID;
  118.        
  119.     }
  120.     if ([[segue identifier] isEqualToString:@"showAlternate"]) {
  121.         [[segue destinationViewController] setDelegate:self];
  122.     }
  123.  
  124.  
  125. }
  126.  
  127.  
  128. /////////////////////////////
  129. #pragma mark - Map Didplay
  130. /////////////////////////////
  131.  
  132. - (IBAction)mapSize:(id)sender {
  133.     //Chane between local and wide view.
  134.     switch (self.mapSgementedControl.selectedSegmentIndex) {
  135.         case 0:
  136.            
  137.             NSLog(@"Wide");
  138.             [self wideMap];
  139.             break;
  140.         case 1:
  141.            
  142.            
  143.             NSLog(@"Local");
  144.             [self localMap];
  145.         default:
  146.             break;
  147.     }
  148. }
  149.  
  150. -(void)wideMap{
  151.     //zooms the map out so the user can see the surrondings
  152.     showMapView.mapType= MKMapTypeStandard;
  153.     MKCoordinateRegion newRegion;
  154.     newRegion.center.latitude = LAT_POSN ;
  155.     newRegion.center.longitude = LON_POSN;
  156.     newRegion.span.latitudeDelta = 0.181168;
  157.     newRegion.span.longitudeDelta = 0.43911;
  158.     [self.showMapView setRegion:newRegion animated:YES];
  159.    
  160. }
  161.  
  162. -(void) localMap{
  163.     //zooms the user into the local view so they can see the ground.
  164.  
  165.     showMapView.mapType= MKMapTypeStandard;
  166.     MKCoordinateRegion newRegion;
  167.     newRegion.center.latitude = LAT_POSN ;
  168.     newRegion.center.longitude = LON_POSN;
  169.     newRegion.span.latitudeDelta = 0.002053;
  170.     newRegion.span.longitudeDelta = 0.005747;
  171.     //old span
  172.     //newRegion.span.latitudeDelta = 0.011323;
  173.     //newRegion.span.longitudeDelta = 0.027444;
  174.  
  175.     [self.showMapView setRegion:newRegion animated:YES];
  176.    
  177. }
  178.  
  179. -(void)addPins{
  180.     //create an instance of the database oject
  181.     DataBase * dataBase = [[DataBase alloc] init];
  182.     //open the database connection
  183.     [dataBase openDB];
  184.     //get the user defualts
  185.     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  186.     // getting an NSString
  187.     NSDictionary *catToggle = [prefs dictionaryForKey:@"pinFilters"];
  188.    
  189.     //loop of the catids and toggles from the user defaults
  190.     for (NSString *keys in catToggle) {
  191.        
  192.             //get on or off
  193.             NSString *toggle = [[NSString alloc]init];
  194.             toggle = [catToggle objectForKey:keys];
  195.            
  196.            
  197.             if (toggle == @"ON"){
  198.                 NSMutableDictionary *poisForOneCat = [[NSMutableDictionary alloc]init ];
  199.                 //if on get the id name lat and long for the pois in that cat
  200.                 poisForOneCat = [dataBase getPOIInfoForMap:keys];
  201.            
  202.                
  203.                 for(NSString *key in poisForOneCat) {
  204.                     //loop over the pois in that cat can put them on the map!
  205.                     NSMutableArray *arrayOfPinInformation = [poisForOneCat objectForKey:key];
  206.                     NSLog(@"arrayOfPinInformation: %@",arrayOfPinInformation);
  207.                    
  208.                     [self pins:key lat:arrayOfPinInformation[1] lon:arrayOfPinInformation[2] poiID:arrayOfPinInformation[0] catID:keys] ;
  209.                
  210.             }
  211.         }
  212.     }
  213.    
  214.  
  215.    
  216. }
  217.  
  218. /////////////////////////////
  219. #pragma mark - View Licfcycle
  220. /////////////////////////////
  221.  
  222. - (void)viewDidLoad
  223. {
  224.     //makes the top title black in the more or the tabbar view
  225.     self.tabBarController.moreNavigationController.navigationBar.tintColor = [UIColor blackColor];
  226.    
  227.     //Test to see if the user is using an iPhone 5
  228.     //This can be to change things depending on the screen size    if ([UIDevice deviceType] & iPhone5){
  229.     NSLog(@"iPhone 5");
  230.  
  231.  
  232.  
  233. //initialise array
  234. self.daoDS = [[POIDataDAO alloc] init];
  235. //set array of ojects to loacl array
  236. //  self.ds = self.daoDS.PopulateDataSource;
  237.  
  238. //set map delegate
  239. showMapView.delegate = (id)self;
  240.  
  241. [self addPins];
  242.  
  243.  
  244.  
  245. //force the map to start on the main queue
  246. dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
  247.  
  248. [super viewDidLoad];
  249. }
  250.  
  251.  
  252. //other lifecycle methods.
  253.  
  254.  
  255. -(void)mapPopulate {
  256.     //initialise array
  257.     self.daoDS = [[POIDataDAO alloc] init];
  258.     //set array of ojects to loacl array
  259.     //  self.ds = self.daoDS.PopulateDataSource;
  260.    
  261.     //set map delegate
  262.     showMapView.delegate = (id)self;
  263.    
  264.     [self addPins];
  265.    
  266.     dispatch_async(dispatch_get_main_queue(), ^{[self localMap];});
  267. }
  268.  
  269. - (void)didReceiveMemoryWarning
  270. {
  271.     [super didReceiveMemoryWarning];
  272.     // Dispose of any resources that can be recreated.
  273. }
  274.  
  275. - (void)viewDidUnload {
  276.    
  277.     [super viewDidUnload];
  278. }
  279.  
  280. - (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers {
  281.     UIView *editView = [controller.view.subviews objectAtIndex:1];
  282.     UINavigationBar *modalNavBar = [editView.subviews objectAtIndex:0];
  283.     modalNavBar.tintColor = [UIColor blackColor];
  284. }
  285.  
  286. #pragma mark - Flipside View
  287.  
  288. - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
  289. {
  290.     [self dismissViewControllerAnimated:YES completion:nil];
  291. }
  292.  
  293. @end
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303. //
  304. //  FlipsideViewController.m
  305. //  lol
  306. //
  307. //  Created by Will Kerswell on 29/10/2012.
  308. //  Copyright (c) 2012 Will Kerswell. All rights reserved.
  309. //
  310.  
  311. #import "FlipsideViewController.h"
  312.  
  313. @interface FlipsideViewController ()
  314.  
  315. @end
  316.  
  317. @implementation FlipsideViewController
  318.  
  319. //////////////////////////////////
  320. #pragma mark - View Lifecycle
  321. //////////////////////////////////
  322.  
  323. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  324. {
  325.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  326.     if (self) {
  327.         // Custom initialization
  328.     }
  329.     return self;
  330. }
  331.  
  332. - (void)viewDidLoad
  333. {
  334.    
  335.     //Change the colour of the top navigation on the navigationViewController.
  336.     self.navigationController.navigationBar.tintColor = [UIColor blackColor];
  337.    
  338.     //create an instance of the database oject
  339.     DataBase * dataBase = [[DataBase alloc] init];
  340.    
  341.     //open the database connection
  342.     [dataBase openDB];
  343.     self.categoriesDic = [dataBase getCatNames];
  344.    
  345.     //create array for the names and ids
  346.     NSMutableArray *namesHolder = [[NSMutableArray alloc]init];
  347.     NSMutableArray *catIDsHolder = [[NSMutableArray alloc]init];
  348.    
  349.     //loop through dic and get names.
  350.     for(NSString *key in self.categoriesDic) {
  351.         //add the names to array
  352.         [namesHolder addObject:key];
  353.     }
  354.    
  355.     //sort the names
  356.     [namesHolder sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
  357.    
  358.     //get the id from the dic with the key
  359.     for (int i=0; i<namesHolder.count; i++) {
  360.         //add id to the holder using key from dic to make sure the order is correct
  361.         [catIDsHolder addObject:[self.categoriesDic objectForKey:namesHolder[i]]];
  362.     }
  363.    
  364.     //set global arrya = to loacl array
  365.     self.catNames = namesHolder;
  366.     self.catIDs =  catIDsHolder;
  367.    
  368.    
  369.     //get the user defualts
  370.     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  371.     //get dic
  372.     NSDictionary *Dic = [prefs dictionaryForKey:@"pinFilters"];
  373.     NSLog(@"This is what is stored in the users settings: %@",Dic);
  374.    
  375.     //create new dic with old dic content.
  376.     NSMutableDictionary *newDic = [[NSMutableDictionary alloc]init];
  377.    
  378.     for (NSString *keys in Dic) {
  379.         [newDic setObject:[Dic objectForKey:keys] forKey:keys];
  380.     }
  381.    
  382.     self.filterDic = newDic;
  383.    
  384.    
  385.     //create array for the pin toggle mode in sorted order like cat names and ids
  386.     NSMutableArray *toggleHolder = [[NSMutableArray alloc]init];
  387.    
  388.     for (int i=0; i<self.catIDs.count; i++) {
  389.         //add id to the holder using key from dic to make sure the order is correct
  390.         [toggleHolder addObject:[self.filterDic objectForKey:self.catIDs[i]]];
  391.     }
  392.    
  393.     self.toggleArray = toggleHolder;
  394.    
  395.     NSLog(@"Toggle array: %@",self.toggleArray);
  396.    
  397.    
  398.     [super viewDidLoad];
  399.     // Do any additional setup after loading the view.
  400. }
  401.  
  402. - (void)didReceiveMemoryWarning
  403. {
  404.     [super didReceiveMemoryWarning];
  405.     // Dispose of any resources that can be recreated.
  406. }
  407.  
  408.  
  409. //////////////////////////////////
  410. #pragma mark - Populate TableView
  411. //////////////////////////////////
  412.  
  413.  
  414. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  415.     return 1;
  416. }
  417.  
  418. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  419.    
  420.     return @"Categories";
  421. }
  422.  
  423. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  424.    
  425.     return self.catNames.count;
  426. }
  427.  
  428. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  429.     static NSString *CellIdentifier = @"POICell";
  430.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  431.    
  432.    
  433.     //set the cell text to the
  434.     cell.textLabel.text = [self.catNames objectAtIndex:[indexPath row]];
  435.     NSString *toggle = [[NSString alloc]init];
  436.     toggle = [self.toggleArray objectAtIndex:[indexPath row]];
  437.     //add switch
  438.     cell.selectionStyle = UITableViewCellSelectionStyleNone;
  439.    
  440.     UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero];
  441.     cell.accessoryView = switchView;
  442.    
  443.     if (toggle == @"OFF") {
  444.         [switchView setOn:NO animated:NO];
  445.     }else{
  446.         [switchView setOn:YES animated:NO];
  447.     }
  448.    
  449.    
  450.     [switchView addTarget:self action:@selector(switchChanged: ) forControlEvents:UIControlEventValueChanged];
  451.     // Configure the cell...
  452.    
  453.     return cell;
  454. }
  455.  
  456. - (void) switchChanged:(id)sender {
  457.     //get the switch that it was sent from
  458.     UISwitch *switchInCell = (UISwitch *)sender;
  459.     //get the cell it was sent from
  460.     UITableViewCell * cell = (UITableViewCell *) switchInCell.superview;
  461.     //get the row it was sent from
  462.     NSIndexPath * indexpath = [self.inputTableView indexPathForCell:cell];
  463.     //cast the indexpath to int
  464.     NSInteger variable = indexpath.row;
  465.     //set the catID from the row index
  466.     NSString *StrCatID =[[NSString alloc]init];
  467.     StrCatID = [self.catIDs objectAtIndex:variable];
  468.     //set the filter as off in the user defualts.
  469.     [self.filterDic setValue:switchInCell.on ? @"ON" : @"OFF" forKey:StrCatID];
  470.     //store the newdic in the user defualts
  471.     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  472.     //save the dic to user defaults
  473.     [prefs setObject:self.filterDic forKey:@"pinFilters"];
  474.    
  475.    
  476.     //NSLog(@"New dic Changed: %@",self.filterDic);
  477.     //NSLog( @"The switch for item %@ is %@",StrCatID, switchInCell.on ? @"ON" : @"OFF" );
  478. }
  479.  
  480.  
  481. #pragma mark - Actions
  482.  
  483. - (IBAction)done:(id)sender
  484. {
  485.     [self.delegate flipsideViewControllerDidFinish:self];
  486.  
  487. }
  488.  
  489. @end
Advertisement
Add Comment
Please, Sign In to add comment