Advertisement
Guest User

Untitled

a guest
Jul 10th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  GroupSelectionViewController.m
  3. //  Pods
  4. //
  5. //  Created by Wojtek on 12.03.2015.
  6. //
  7. //
  8. #import "UIColor+AppColors.h"
  9. #import "GroupSelectionViewController.h"
  10. #import "GroupTableViewCell.h"
  11. #import "ViewHelper.h"
  12. //#import "configApi.h"
  13. #import "Group+CoreDataClass.h"
  14. #import "POA-Swift.h"
  15. static int const kHeaderSectionTag = 6900;
  16. @interface GroupSelectionViewController ()
  17.  
  18. @property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
  19. @property (assign) NSInteger expandedSectionHeaderNumber;
  20. @property (assign) UITableViewHeaderFooterView *expandedSectionHeader;
  21. @property (nonatomic,strong) NSMutableArray *sectionItems;
  22. @property (nonatomic,strong) NSArray *sectionNames;
  23.  
  24. @end
  25. @implementation GroupSelectionViewController
  26.  
  27. @synthesize tableViewOutlet, downloadFilesButton;
  28.  
  29. - (NSMutableArray *) sectionItems
  30. {
  31.     if (!_sectionItems) {
  32.         _sectionItems = [NSMutableArray new];
  33.     }
  34.     return _sectionItems;
  35. }
  36. - (NSArray *)_getNewTweets
  37. {
  38.  
  39.     NSArray * newTweets = [[self.fetchedResultsController fetchedObjects] valueForKey:@"title"]; //whatever magic to download new tweets
  40. //    NSArray * ary = [[NSMutableArray alloc] init];
  41. //    for (Group* elementCollectionKey in newTweets) {
  42. //         ary = [[ary  arrayByAddingObject:elementCollectionKey.title] mutableCopy];
  43. //    }
  44.     NSArray *arrayOfArrays = [NSArray arrayWithObjects:
  45.                               [NSArray arrayWithObjects:newTweets, nil], nil];
  46.     return arrayOfArrays;
  47. }
  48. - (void) fetchNewData
  49. {
  50.     [self.sectionItems addObjectsFromArray:[self _getNewTweets]];
  51.     [self.tableViewOutlet reloadData]; //animated would be prettier, but out of this scope
  52. }
  53. - (void)viewDidLoad {
  54.     [super viewDidLoad];
  55.    
  56.     appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
  57.    
  58.     //Initialize fetch controller
  59.     [self initializeFetchedResultsController];
  60.  
  61.     UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
  62.     [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
  63.     [tableViewOutlet addSubview:refreshControl];
  64.    
  65.  
  66.     [self setUpNotifications];
  67.     [self setUpView];
  68.     [self fetchNewData];
  69.     //Intialize the main Category
  70.    
  71.  
  72.     self.sectionNames = @[ @"iPhone", @"iPad", @"Apple Watch" ];
  73. //    self.sectionItems = @[ @[@"iPhone 5", @"iPhone 5s", @"iPhone 6", @"iPhone 6 Plus", @"iPhone 7", @"iPhone 7 Plus"],
  74. //                           @[@"iPad Mini", @"iPad Air 2", @"iPad Pro", @"iPad Pro 9.7"],
  75. //                           @[@"Apple Watch", @"Apple Watch 2", @"Apple Watch 2 (Nike)"]
  76. //                           ];
  77. //    NSMutableArray * gro = [[self.fetchedResultsController fetchedObjects] mutableCopy] ;
  78. //    self.sectionItems = [[[self.fetchedResultsController fetchedObjects] mutableCopy] valueForKey:@"title"];
  79. //    for (Group* elementCollectionKey in gro) {
  80. //
  81. //
  82. //        [[self.sectionItems  arrayByAddingObject:elementCollectionKey.title] mutableCopy];
  83. //    }
  84.    // [self setUpItems];
  85.    
  86.     // configure the tableview
  87.     self.tableViewOutlet.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  88.     self.tableViewOutlet.rowHeight = UITableViewAutomaticDimension;
  89.     self.tableViewOutlet.estimatedRowHeight = 100;
  90.     self.expandedSectionHeaderNumber = -1;
  91. }
  92. ////
  93. - (void) setUpItems {
  94.   //  id< NSFetchedResultsSectionInfo> sectionInfo = [[self fetchedResultsController] sections][0];
  95.    NSMutableArray * gro = [[self.fetchedResultsController fetchedObjects] mutableCopy] ;
  96.     for (Group* elementCollectionKey in gro) {
  97.         self.sectionItems = [[self.sectionItems  arrayByAddingObject:elementCollectionKey.title] mutableCopy];
  98.     }
  99. //    NSArray *someArray = ...;
  100. //    NSMutableArray* subArrayData = [someArray mutableCopy];
  101.    // self.sectionItems = [sectionInfo objects];
  102.  
  103. }
  104. #pragma mark - Table view data source
  105.  
  106. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  107.    
  108.     if (self.sectionNames.count > 0) {
  109.         self.tableViewOutlet.backgroundView = nil;
  110.         return self.sectionNames.count;
  111.     } else {
  112.         UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
  113.        
  114.         messageLabel.text = @"Retrieving data.\nPlease wait.";
  115.         messageLabel.numberOfLines = 0;
  116.         messageLabel.textAlignment = NSTextAlignmentCenter;
  117.         messageLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:20];
  118.         [messageLabel sizeToFit];
  119.         self.tableViewOutlet.backgroundView = messageLabel;
  120.        
  121.         return 0;
  122.     }
  123. }
  124.  
  125. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  126.     if (self.expandedSectionHeaderNumber == section) {
  127.         NSMutableArray *arrayOfItems = [[self.sectionItems objectAtIndex:section]objectAtIndex: section] ;
  128.         return arrayOfItems.count;
  129.     } else {
  130.         return 0;
  131.     }
  132.    
  133.    
  134. }
  135.  
  136. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  137.     if (self.sectionNames.count) {
  138.         return [self.sectionNames objectAtIndex:section];
  139.     }
  140.    
  141.     return @"";
  142. }
  143.  
  144. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; {
  145.     return 44.0;
  146. }
  147.  
  148. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
  149.     // recast your view as a UITableViewHeaderFooterView
  150.     UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  151.     header.contentView.backgroundColor = [UIColor colorWithHexString:@"#408000"];
  152.     header.textLabel.textColor = [UIColor whiteColor];
  153.     UIImageView *viewWithTag = [self.view viewWithTag:kHeaderSectionTag + section];
  154.     if (viewWithTag) {
  155.         [viewWithTag removeFromSuperview];
  156.     }
  157.     // add the arrow image
  158.     CGSize headerFrame = self.view.frame.size;
  159.     UIImageView *theImageView = [[UIImageView alloc] initWithFrame:CGRectMake(headerFrame.width - 32, 13, 18, 18)];
  160.     theImageView.image = [UIImage imageNamed:@"Chevron-Dn-Wht"];
  161.     theImageView.tag = kHeaderSectionTag + section;
  162.     [header addSubview:theImageView];
  163.    
  164.     // make headers touchable
  165.     header.tag = section;
  166.     UITapGestureRecognizer *headerTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderWasTouched:)];
  167.     [header addGestureRecognizer:headerTapGesture];
  168. }
  169.  
  170. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  171.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  172.     NSArray *section = [self.sectionItems objectAtIndex:indexPath.section];
  173.    
  174.     cell.textLabel.textColor = [UIColor blackColor];
  175.     cell.textLabel.text = @"1";//[section objectAtIndex:indexPath.row];
  176.     cell.textLabel.text = [[section objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  177.    
  178.     return cell;
  179. }
  180.  
  181. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  182.     [tableView deselectRowAtIndexPath:indexPath animated:YES];
  183. }
  184.  
  185. - (void)updateTableViewRowDisplay:(NSArray *)arrayOfIndexPaths {
  186.     [self.tableViewOutlet beginUpdates];
  187.     [self.tableViewOutlet deleteRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation: UITableViewRowAnimationFade];
  188.     [self.tableViewOutlet endUpdates];
  189. }
  190.  
  191. #pragma mark - Expand / Collapse Methods
  192.  
  193. - (void)sectionHeaderWasTouched:(UITapGestureRecognizer *)sender {
  194.     UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)sender.view;
  195.     NSInteger section = headerView.tag;
  196.     UIImageView *eImageView = (UIImageView *)[headerView viewWithTag:kHeaderSectionTag + section];
  197.     self.expandedSectionHeader = headerView;
  198.    
  199.     if (self.expandedSectionHeaderNumber == -1) {
  200.         self.expandedSectionHeaderNumber = section;
  201.         [self tableViewExpandSection:section withImage: eImageView];
  202.     } else {
  203.         if (self.expandedSectionHeaderNumber == section) {
  204.             [self tableViewCollapeSection:section withImage: eImageView];
  205.             self.expandedSectionHeader = nil;
  206.         } else {
  207.             UIImageView *cImageView  = (UIImageView *)[self.view viewWithTag:kHeaderSectionTag + self.expandedSectionHeaderNumber];
  208.             [self tableViewCollapeSection:self.expandedSectionHeaderNumber withImage: cImageView];
  209.             [self tableViewExpandSection:section withImage: eImageView];
  210.         }
  211.     }
  212. }
  213.  
  214. - (void)tableViewCollapeSection:(NSInteger)section withImage:(UIImageView *)imageView {
  215.     NSArray *sectionData = [[self.sectionItems objectAtIndex:section] objectAtIndex:section];
  216.    
  217.     self.expandedSectionHeaderNumber = -1;
  218.     if (sectionData.count == 0) {
  219.         return;
  220.     } else {
  221.         [UIView animateWithDuration:0.4 animations:^{
  222.             imageView.transform = CGAffineTransformMakeRotation((0.0 * M_PI) / 180.0);
  223.         }];
  224.         NSMutableArray *arrayOfIndexPaths = [NSMutableArray array];
  225.         for (int i=0; i< sectionData.count; i++) {
  226.             NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:section];
  227.             [arrayOfIndexPaths addObject:index];
  228.         }
  229.         [self.tableViewOutlet beginUpdates];
  230.         [self.tableViewOutlet deleteRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation: UITableViewRowAnimationFade];
  231.         [self.tableViewOutlet endUpdates];
  232.     }
  233. }
  234.  
  235. - (void)tableViewExpandSection:(NSInteger)section withImage:(UIImageView *)imageView {
  236.     NSArray *sectionData = [[self.sectionItems objectAtIndex:section] objectAtIndex:section];
  237.    
  238.     if (sectionData.count == 0) {
  239.         self.expandedSectionHeaderNumber = -1;
  240.         return;
  241.     } else {
  242.         [UIView animateWithDuration:0.4 animations:^{
  243.             imageView.transform = CGAffineTransformMakeRotation((180.0 * M_PI) / 180.0);
  244.         }];
  245.         NSMutableArray *arrayOfIndexPaths = [NSMutableArray array];
  246.         for (int i=0; i< sectionData.count; i++) {
  247.             NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:section];
  248.             [arrayOfIndexPaths addObject:index];
  249.         }
  250.         self.expandedSectionHeaderNumber = section;
  251.         [self.tableViewOutlet beginUpdates];
  252.         [self.tableViewOutlet insertRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation: UITableViewRowAnimationFade];
  253.         [self.tableViewOutlet endUpdates];
  254.     }
  255. }
  256.  
  257.  
  258. /////
  259. - (void) setUpNotifications {
  260.     [[NSNotificationCenter defaultCenter] addObserver:self
  261.                                              selector:@selector(refreshGroups:)
  262.                                                  name:MfRefreshGroups
  263.                                                object:nil];
  264. }
  265.  
  266. - (void)viewDidAppear:(BOOL)animated {
  267.     [[GroupsAPI shared] getAllGroups];
  268.      [self setUpNotifications];
  269. }
  270.  
  271. - (void)refresh:(UIRefreshControl *)refreshControl {
  272.  
  273.     [[GroupsAPI shared] getAllGroups];
  274.    
  275.     [refreshControl endRefreshing];
  276. }
  277.  
  278. - (void) refreshGroups: (NSNotification*) notifictation {
  279.  
  280.    
  281.    // tableData = [self populateTableData];
  282.     if (tableData) {
  283.         [alert dismissWithClickedButtonIndex:0 animated:YES];
  284.     }
  285.     [self setUpView];
  286.     [tableViewOutlet reloadData];
  287. }
  288.  
  289. - (void)viewWillLayoutSubviews {
  290.     //[self setUpView];
  291.     [super viewWillLayoutSubviews];
  292. }
  293.  
  294. - (void)didReceiveMemoryWarning {
  295.     [super didReceiveMemoryWarning];
  296.     // Dispose of any resources that can be recreated.
  297. }
  298.  
  299. #pragma mark - set up view
  300. -(void)setUpView {
  301.     [ViewHelper setUpNavigationBar:self.navigationController.navigationBar];
  302.    
  303.     UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 648, 1)];
  304.     lineView.backgroundColor = [UIColor lightGrayColor];
  305.    
  306.     /*
  307.     _totalSize = 0;
  308.    
  309.     for (Group* group in tableData) {
  310.         if ([[group selected] boolValue]) {
  311.             _totalSize += [group.fileSize floatValue];
  312.         }
  313.     }
  314.    
  315.     if (_totalSize > 0.0f) {
  316.          _totalSize /= 1 << 20;
  317.     }*/
  318.    
  319.    
  320.     _totalSize = [[GroupsAPI shared] getGroupFileSizeToDwonload];
  321.     _totalSize /= 1 << 20;
  322.    
  323.     [self.totalSizeLabel setText:[NSString stringWithFormat:@"%.1f MB", _totalSize]];
  324.  
  325. }
  326. -(void)setUpTableView {
  327.     tableViewOutlet.backgroundColor = [UIColor redColor];
  328.     tableViewOutlet.backgroundView.backgroundColor = [UIColor greenColor];
  329. }
  330.  
  331. -(void)setUpNavigationBar {
  332.     UINavigationBar *navigationBar = self.navigationController.navigationBar;
  333.     [navigationBar setBackgroundImage:[UIImage imageNamed:@"transparent"]
  334.                        forBarPosition:UIBarPositionAny
  335.                            barMetrics:UIBarMetricsDefault];
  336.    
  337.     [navigationBar setShadowImage:[UIImage new]];
  338. }
  339.  
  340. #pragma mark - TableView data source
  341. //-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  342. //
  343. //    id< NSFetchedResultsSectionInfo> sectionInfo = [[self fetchedResultsController] sections][section];
  344. //    if ([sectionInfo numberOfObjects] < 1){
  345. //        noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30,650, 20)];
  346. //        noDataLabel.backgroundColor = [UIColor clearColor];
  347. //        noDataLabel.textAlignment = NSTextAlignmentCenter;
  348. //        noDataLabel.textColor = [UIColor blackColor];
  349. //        noDataLabel.numberOfLines = 0;
  350. //        noDataLabel.text = @"Brak danych do pobrania";
  351. //         noDataLabel.center = self.view.center;
  352. //        [self.view addSubview:noDataLabel];
  353. //    }else{
  354. //        [self->noDataLabel removeFromSuperview];
  355. //    }
  356. //    return [sectionInfo numberOfObjects];
  357. //
  358. //}
  359.  
  360. //-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  361. //    if ([[[self fetchedResultsController] sections] count] < 1){
  362. //        noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30,650, 20)];
  363. //
  364. //        noDataLabel.backgroundColor = [UIColor clearColor];
  365. //        noDataLabel.textAlignment = NSTextAlignmentCenter;
  366. //        noDataLabel.textColor = [UIColor blackColor];
  367. //        noDataLabel.numberOfLines = 0;
  368. //        noDataLabel.text = @"Brak danych do pobrania";
  369. //        noDataLabel.center = self.view.center;
  370. //        [self.view addSubview:noDataLabel];
  371. //    }else{
  372. //       [self->noDataLabel removeFromSuperview];
  373. //    }
  374. //    return [[[self fetchedResultsController] sections] count];
  375. //}
  376.  
  377. //-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  378. //
  379. //    GroupTableViewCell *cell = (GroupTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
  380. //
  381. //    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
  382. //
  383. //    float fileSize = [[(Group*)object fileSize] floatValue];
  384. //    fileSize = fileSize / (float)(1 << 20);
  385. //    [cell setBackgroundColor:[UIColor clearColor]];
  386. //    [cell.activeSwitch setOn:[[(Group*)object selected] boolValue]];
  387. //    [cell.nameLabel setText:[(Group*)object title]];
  388. //
  389. //    [cell.sizeLabel setText:[NSString stringWithFormat:@"%.1f MB", fileSize]];
  390. //
  391. //    return cell;
  392. //}
  393.  
  394. #pragma mark - TableView delegate
  395.  
  396. //-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  397. //    if (section == 0) {
  398. //        return @"Wybierz foldery do pobrania:";
  399. //    }
  400. //
  401. //    return nil;
  402. //}
  403. //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  404. //
  405. //    return 50;
  406. //}
  407. //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
  408. //
  409. //    //Programatically create the view
  410. //    CGRect viewFrame = CGRectMake(0, 0, tableView.frame.size.width, 50);
  411. //    UIView *headerView = [[UIView alloc] initWithFrame:viewFrame];
  412. //    headerView.backgroundColor = [UIColor colorWithRed:76.0f/255.0f green:139.0f/255.0f blue:245.0f/255.0f alpha:1.0f];
  413. //
  414. //    //Crate label programatically
  415. //
  416. //    UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
  417. //    headerLbl.text = [mainCategory objectAtIndex:section];
  418. //    headerLbl.textAlignment = NSTextAlignmentCenter;
  419. //    [headerView addSubview:headerLbl];
  420. //
  421. //    UIButton *expandBtn = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width-50, 0, 50, 50)];
  422. //    expandBtn.titleLabel.text = @"+";
  423. //    expandBtn.backgroundColor = [UIColor redColor];
  424. //    [headerView addSubview:expandBtn];
  425. //    expandBtn.tag = section;
  426. //
  427. //    [expandBtn addTarget:self action:@selector(expandCell:) forControlEvents:UIControlEventTouchUpInside];
  428. //
  429. //    return headerView;
  430. //}
  431. //
  432. //- (void)expandCell:(UIButton*)button{
  433. //
  434. //    NSString * status = [expandStatusArray objectAtIndex:button.tag];
  435. //
  436. //    if ([status isEqualToString:@"1"]) {
  437. //
  438. //        //Replace with o
  439. //        [expandStatusArray replaceObjectAtIndex:button.tag withObject:@"0"];
  440. //    }else{
  441. //        //Replace with 1
  442. //        [expandStatusArray replaceObjectAtIndex:button.tag withObject:@"1"];
  443. //    }
  444. //    [tableViewOutlet reloadData];
  445. //
  446. //}
  447.  
  448. #pragma mark - Actions
  449. - (IBAction)downloadFilesButtonTouchUp:(id)sender {
  450.     [downloadFilesButton setEnabled:NO];
  451.     [[NSNotificationCenter defaultCenter] postNotificationName:MfApiStartDownload object:sender];
  452. }
  453.  
  454. - (IBAction)groupSwitchValueChanged:(UISwitch*)sender {
  455.     [downloadFilesButton setEnabled:YES];
  456.     UITableViewCell* cell = (UITableViewCell*)[[sender superview] superview];
  457.    
  458.     NSIndexPath* indexPath = [self.tableViewOutlet indexPathForCell:cell];
  459.     if (indexPath) {
  460.        
  461.         Group *group = [self.fetchedResultsController objectAtIndexPath:indexPath];
  462.         [[GroupsAPI shared] setSelectedForGroupId:group.identifier.integerValue selected:sender.on completionHandler:^(){
  463.            
  464.             dispatch_async(dispatch_get_main_queue(),^{
  465.                
  466.                 _totalSize = [[GroupsAPI shared] getGroupFileSizeToDwonload];
  467.                 _totalSize /= 1 << 20;
  468.                
  469.                 [self.totalSizeLabel setText:[NSString stringWithFormat:@"%.1f MB", _totalSize]];
  470.             });
  471.            
  472.         }];
  473.  
  474.         /*
  475.         CGFloat fileSize = [[(Group*)group fileSize] floatValue];
  476.         fileSize /= 1 << 20;
  477.         if(sender.on) {
  478.            
  479.             _totalSize += fileSize;
  480.         } else {
  481.             _totalSize -= fileSize;
  482.         }*/
  483.     }
  484.  
  485. }
  486.  
  487.  
  488. #pragma NSFetchControler
  489.  
  490. - (void)initializeFetchedResultsController{
  491.    
  492.     NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Group"];
  493.    
  494.     NSSortDescriptor *titleSort = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
  495.    
  496.     [request setSortDescriptors:@[titleSort]];
  497.     //CDStackSingleton.shared.mainContext
  498.     NSManagedObjectContext *moc = [[CDStackSingleton shared] mainContext]; //Retrieve the main queue NSManagedObjectContext
  499.    
  500.     [self setFetchedResultsController:[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:nil cacheName:nil]];
  501.     [[self fetchedResultsController] setDelegate:self];
  502.    
  503.     NSError *error = nil;
  504.     if (![[self fetchedResultsController] performFetch:&error]) {
  505.         NSLog(@"Failed to initialize FetchedResultsController: %@\n%@", [error localizedDescription], [error userInfo]);
  506.         abort();
  507.     }
  508. }
  509.  
  510. - (void) performFetch {
  511.     NSError *error = nil;
  512.     if (![[self fetchedResultsController] performFetch:&error]) {
  513.         NSLog(@"Failed to initialize FetchedResultsController: %@\n%@", [error localizedDescription], [error userInfo]);
  514.         abort();
  515.     }
  516.    
  517.     [[self tableViewOutlet] reloadData];
  518. }
  519.  
  520. #pragma mark - NSFetchedResultsControllerDelegate
  521. - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
  522. {
  523.     [[self tableViewOutlet] beginUpdates];
  524. }
  525.  
  526. - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
  527. {
  528.     switch(type) {
  529.         case NSFetchedResultsChangeInsert:
  530.             [[self tableViewOutlet] insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  531.             break;
  532.         case NSFetchedResultsChangeDelete:
  533.             [[self tableViewOutlet] deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
  534.             break;
  535.         case NSFetchedResultsChangeMove:
  536.         case NSFetchedResultsChangeUpdate:
  537.             break;
  538.     }
  539. }
  540.  
  541. - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
  542. {
  543.     switch(type) {
  544.         case NSFetchedResultsChangeInsert:
  545.             [[self tableViewOutlet] insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  546.             break;
  547.         case NSFetchedResultsChangeDelete:
  548.             [[self tableViewOutlet] deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  549.             break;
  550.         case NSFetchedResultsChangeUpdate:
  551.             [self.tableViewOutlet reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
  552.             break;
  553.         case NSFetchedResultsChangeMove:
  554.             [[self tableViewOutlet] deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  555.             [[self tableViewOutlet] insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
  556.             break;
  557.     }
  558. }
  559.  
  560. - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
  561. {
  562.    
  563.     [[self tableViewOutlet] endUpdates];
  564.      [self fetchNewData];
  565. }
  566.  
  567.  
  568.  
  569. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement