Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GroupSelectionViewController.m
- // Pods
- //
- // Created by Wojtek on 12.03.2015.
- //
- //
- #import "UIColor+AppColors.h"
- #import "GroupSelectionViewController.h"
- #import "GroupTableViewCell.h"
- #import "ViewHelper.h"
- //#import "configApi.h"
- #import "Group+CoreDataClass.h"
- #import "POA-Swift.h"
- static int const kHeaderSectionTag = 6900;
- @interface GroupSelectionViewController ()
- @property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
- @property (assign) NSInteger expandedSectionHeaderNumber;
- @property (assign) UITableViewHeaderFooterView *expandedSectionHeader;
- @property (nonatomic,strong) NSMutableArray *sectionItems;
- @property (nonatomic,strong) NSArray *sectionNames;
- @end
- @implementation GroupSelectionViewController
- @synthesize tableViewOutlet, downloadFilesButton;
- - (NSMutableArray *) sectionItems
- {
- if (!_sectionItems) {
- _sectionItems = [NSMutableArray new];
- }
- return _sectionItems;
- }
- - (NSArray *)_getNewTweets
- {
- NSArray * newTweets = [[self.fetchedResultsController fetchedObjects] valueForKey:@"title"]; //whatever magic to download new tweets
- // NSArray * ary = [[NSMutableArray alloc] init];
- // for (Group* elementCollectionKey in newTweets) {
- // ary = [[ary arrayByAddingObject:elementCollectionKey.title] mutableCopy];
- // }
- NSArray *arrayOfArrays = [NSArray arrayWithObjects:
- [NSArray arrayWithObjects:newTweets, nil], nil];
- return arrayOfArrays;
- }
- - (void) fetchNewData
- {
- [self.sectionItems addObjectsFromArray:[self _getNewTweets]];
- [self.tableViewOutlet reloadData]; //animated would be prettier, but out of this scope
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
- //Initialize fetch controller
- [self initializeFetchedResultsController];
- UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
- [refreshControl addTarget:self action:@selector(refresh:) forControlEvents:UIControlEventValueChanged];
- [tableViewOutlet addSubview:refreshControl];
- [self setUpNotifications];
- [self setUpView];
- [self fetchNewData];
- //Intialize the main Category
- self.sectionNames = @[ @"iPhone", @"iPad", @"Apple Watch" ];
- // self.sectionItems = @[ @[@"iPhone 5", @"iPhone 5s", @"iPhone 6", @"iPhone 6 Plus", @"iPhone 7", @"iPhone 7 Plus"],
- // @[@"iPad Mini", @"iPad Air 2", @"iPad Pro", @"iPad Pro 9.7"],
- // @[@"Apple Watch", @"Apple Watch 2", @"Apple Watch 2 (Nike)"]
- // ];
- // NSMutableArray * gro = [[self.fetchedResultsController fetchedObjects] mutableCopy] ;
- // self.sectionItems = [[[self.fetchedResultsController fetchedObjects] mutableCopy] valueForKey:@"title"];
- // for (Group* elementCollectionKey in gro) {
- //
- //
- // [[self.sectionItems arrayByAddingObject:elementCollectionKey.title] mutableCopy];
- // }
- // [self setUpItems];
- // configure the tableview
- self.tableViewOutlet.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
- self.tableViewOutlet.rowHeight = UITableViewAutomaticDimension;
- self.tableViewOutlet.estimatedRowHeight = 100;
- self.expandedSectionHeaderNumber = -1;
- }
- ////
- - (void) setUpItems {
- // id< NSFetchedResultsSectionInfo> sectionInfo = [[self fetchedResultsController] sections][0];
- NSMutableArray * gro = [[self.fetchedResultsController fetchedObjects] mutableCopy] ;
- for (Group* elementCollectionKey in gro) {
- self.sectionItems = [[self.sectionItems arrayByAddingObject:elementCollectionKey.title] mutableCopy];
- }
- // NSArray *someArray = ...;
- // NSMutableArray* subArrayData = [someArray mutableCopy];
- // self.sectionItems = [sectionInfo objects];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- if (self.sectionNames.count > 0) {
- self.tableViewOutlet.backgroundView = nil;
- return self.sectionNames.count;
- } else {
- UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
- messageLabel.text = @"Retrieving data.\nPlease wait.";
- messageLabel.numberOfLines = 0;
- messageLabel.textAlignment = NSTextAlignmentCenter;
- messageLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:20];
- [messageLabel sizeToFit];
- self.tableViewOutlet.backgroundView = messageLabel;
- return 0;
- }
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if (self.expandedSectionHeaderNumber == section) {
- NSMutableArray *arrayOfItems = [[self.sectionItems objectAtIndex:section]objectAtIndex: section] ;
- return arrayOfItems.count;
- } else {
- return 0;
- }
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- if (self.sectionNames.count) {
- return [self.sectionNames objectAtIndex:section];
- }
- return @"";
- }
- - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; {
- return 44.0;
- }
- - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
- // recast your view as a UITableViewHeaderFooterView
- UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
- header.contentView.backgroundColor = [UIColor colorWithHexString:@"#408000"];
- header.textLabel.textColor = [UIColor whiteColor];
- UIImageView *viewWithTag = [self.view viewWithTag:kHeaderSectionTag + section];
- if (viewWithTag) {
- [viewWithTag removeFromSuperview];
- }
- // add the arrow image
- CGSize headerFrame = self.view.frame.size;
- UIImageView *theImageView = [[UIImageView alloc] initWithFrame:CGRectMake(headerFrame.width - 32, 13, 18, 18)];
- theImageView.image = [UIImage imageNamed:@"Chevron-Dn-Wht"];
- theImageView.tag = kHeaderSectionTag + section;
- [header addSubview:theImageView];
- // make headers touchable
- header.tag = section;
- UITapGestureRecognizer *headerTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderWasTouched:)];
- [header addGestureRecognizer:headerTapGesture];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- NSArray *section = [self.sectionItems objectAtIndex:indexPath.section];
- cell.textLabel.textColor = [UIColor blackColor];
- cell.textLabel.text = @"1";//[section objectAtIndex:indexPath.row];
- cell.textLabel.text = [[section objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- - (void)updateTableViewRowDisplay:(NSArray *)arrayOfIndexPaths {
- [self.tableViewOutlet beginUpdates];
- [self.tableViewOutlet deleteRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation: UITableViewRowAnimationFade];
- [self.tableViewOutlet endUpdates];
- }
- #pragma mark - Expand / Collapse Methods
- - (void)sectionHeaderWasTouched:(UITapGestureRecognizer *)sender {
- UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)sender.view;
- NSInteger section = headerView.tag;
- UIImageView *eImageView = (UIImageView *)[headerView viewWithTag:kHeaderSectionTag + section];
- self.expandedSectionHeader = headerView;
- if (self.expandedSectionHeaderNumber == -1) {
- self.expandedSectionHeaderNumber = section;
- [self tableViewExpandSection:section withImage: eImageView];
- } else {
- if (self.expandedSectionHeaderNumber == section) {
- [self tableViewCollapeSection:section withImage: eImageView];
- self.expandedSectionHeader = nil;
- } else {
- UIImageView *cImageView = (UIImageView *)[self.view viewWithTag:kHeaderSectionTag + self.expandedSectionHeaderNumber];
- [self tableViewCollapeSection:self.expandedSectionHeaderNumber withImage: cImageView];
- [self tableViewExpandSection:section withImage: eImageView];
- }
- }
- }
- - (void)tableViewCollapeSection:(NSInteger)section withImage:(UIImageView *)imageView {
- NSArray *sectionData = [[self.sectionItems objectAtIndex:section] objectAtIndex:section];
- self.expandedSectionHeaderNumber = -1;
- if (sectionData.count == 0) {
- return;
- } else {
- [UIView animateWithDuration:0.4 animations:^{
- imageView.transform = CGAffineTransformMakeRotation((0.0 * M_PI) / 180.0);
- }];
- NSMutableArray *arrayOfIndexPaths = [NSMutableArray array];
- for (int i=0; i< sectionData.count; i++) {
- NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:section];
- [arrayOfIndexPaths addObject:index];
- }
- [self.tableViewOutlet beginUpdates];
- [self.tableViewOutlet deleteRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation: UITableViewRowAnimationFade];
- [self.tableViewOutlet endUpdates];
- }
- }
- - (void)tableViewExpandSection:(NSInteger)section withImage:(UIImageView *)imageView {
- NSArray *sectionData = [[self.sectionItems objectAtIndex:section] objectAtIndex:section];
- if (sectionData.count == 0) {
- self.expandedSectionHeaderNumber = -1;
- return;
- } else {
- [UIView animateWithDuration:0.4 animations:^{
- imageView.transform = CGAffineTransformMakeRotation((180.0 * M_PI) / 180.0);
- }];
- NSMutableArray *arrayOfIndexPaths = [NSMutableArray array];
- for (int i=0; i< sectionData.count; i++) {
- NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:section];
- [arrayOfIndexPaths addObject:index];
- }
- self.expandedSectionHeaderNumber = section;
- [self.tableViewOutlet beginUpdates];
- [self.tableViewOutlet insertRowsAtIndexPaths:arrayOfIndexPaths withRowAnimation: UITableViewRowAnimationFade];
- [self.tableViewOutlet endUpdates];
- }
- }
- /////
- - (void) setUpNotifications {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(refreshGroups:)
- name:MfRefreshGroups
- object:nil];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [[GroupsAPI shared] getAllGroups];
- [self setUpNotifications];
- }
- - (void)refresh:(UIRefreshControl *)refreshControl {
- [[GroupsAPI shared] getAllGroups];
- [refreshControl endRefreshing];
- }
- - (void) refreshGroups: (NSNotification*) notifictation {
- // tableData = [self populateTableData];
- if (tableData) {
- [alert dismissWithClickedButtonIndex:0 animated:YES];
- }
- [self setUpView];
- [tableViewOutlet reloadData];
- }
- - (void)viewWillLayoutSubviews {
- //[self setUpView];
- [super viewWillLayoutSubviews];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - set up view
- -(void)setUpView {
- [ViewHelper setUpNavigationBar:self.navigationController.navigationBar];
- UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 648, 1)];
- lineView.backgroundColor = [UIColor lightGrayColor];
- /*
- _totalSize = 0;
- for (Group* group in tableData) {
- if ([[group selected] boolValue]) {
- _totalSize += [group.fileSize floatValue];
- }
- }
- if (_totalSize > 0.0f) {
- _totalSize /= 1 << 20;
- }*/
- _totalSize = [[GroupsAPI shared] getGroupFileSizeToDwonload];
- _totalSize /= 1 << 20;
- [self.totalSizeLabel setText:[NSString stringWithFormat:@"%.1f MB", _totalSize]];
- }
- -(void)setUpTableView {
- tableViewOutlet.backgroundColor = [UIColor redColor];
- tableViewOutlet.backgroundView.backgroundColor = [UIColor greenColor];
- }
- -(void)setUpNavigationBar {
- UINavigationBar *navigationBar = self.navigationController.navigationBar;
- [navigationBar setBackgroundImage:[UIImage imageNamed:@"transparent"]
- forBarPosition:UIBarPositionAny
- barMetrics:UIBarMetricsDefault];
- [navigationBar setShadowImage:[UIImage new]];
- }
- #pragma mark - TableView data source
- //-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- //
- // id< NSFetchedResultsSectionInfo> sectionInfo = [[self fetchedResultsController] sections][section];
- // if ([sectionInfo numberOfObjects] < 1){
- // noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30,650, 20)];
- // noDataLabel.backgroundColor = [UIColor clearColor];
- // noDataLabel.textAlignment = NSTextAlignmentCenter;
- // noDataLabel.textColor = [UIColor blackColor];
- // noDataLabel.numberOfLines = 0;
- // noDataLabel.text = @"Brak danych do pobrania";
- // noDataLabel.center = self.view.center;
- // [self.view addSubview:noDataLabel];
- // }else{
- // [self->noDataLabel removeFromSuperview];
- // }
- // return [sectionInfo numberOfObjects];
- //
- //}
- //-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- // if ([[[self fetchedResultsController] sections] count] < 1){
- // noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 30,650, 20)];
- //
- // noDataLabel.backgroundColor = [UIColor clearColor];
- // noDataLabel.textAlignment = NSTextAlignmentCenter;
- // noDataLabel.textColor = [UIColor blackColor];
- // noDataLabel.numberOfLines = 0;
- // noDataLabel.text = @"Brak danych do pobrania";
- // noDataLabel.center = self.view.center;
- // [self.view addSubview:noDataLabel];
- // }else{
- // [self->noDataLabel removeFromSuperview];
- // }
- // return [[[self fetchedResultsController] sections] count];
- //}
- //-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- //
- // GroupTableViewCell *cell = (GroupTableViewCell *) [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- //
- // NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
- //
- // float fileSize = [[(Group*)object fileSize] floatValue];
- // fileSize = fileSize / (float)(1 << 20);
- // [cell setBackgroundColor:[UIColor clearColor]];
- // [cell.activeSwitch setOn:[[(Group*)object selected] boolValue]];
- // [cell.nameLabel setText:[(Group*)object title]];
- //
- // [cell.sizeLabel setText:[NSString stringWithFormat:@"%.1f MB", fileSize]];
- //
- // return cell;
- //}
- #pragma mark - TableView delegate
- //-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- // if (section == 0) {
- // return @"Wybierz foldery do pobrania:";
- // }
- //
- // return nil;
- //}
- //- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
- //
- // return 50;
- //}
- //- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
- //
- // //Programatically create the view
- // CGRect viewFrame = CGRectMake(0, 0, tableView.frame.size.width, 50);
- // UIView *headerView = [[UIView alloc] initWithFrame:viewFrame];
- // headerView.backgroundColor = [UIColor colorWithRed:76.0f/255.0f green:139.0f/255.0f blue:245.0f/255.0f alpha:1.0f];
- //
- // //Crate label programatically
- //
- // UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
- // headerLbl.text = [mainCategory objectAtIndex:section];
- // headerLbl.textAlignment = NSTextAlignmentCenter;
- // [headerView addSubview:headerLbl];
- //
- // UIButton *expandBtn = [[UIButton alloc] initWithFrame:CGRectMake(tableView.frame.size.width-50, 0, 50, 50)];
- // expandBtn.titleLabel.text = @"+";
- // expandBtn.backgroundColor = [UIColor redColor];
- // [headerView addSubview:expandBtn];
- // expandBtn.tag = section;
- //
- // [expandBtn addTarget:self action:@selector(expandCell:) forControlEvents:UIControlEventTouchUpInside];
- //
- // return headerView;
- //}
- //
- //- (void)expandCell:(UIButton*)button{
- //
- // NSString * status = [expandStatusArray objectAtIndex:button.tag];
- //
- // if ([status isEqualToString:@"1"]) {
- //
- // //Replace with o
- // [expandStatusArray replaceObjectAtIndex:button.tag withObject:@"0"];
- // }else{
- // //Replace with 1
- // [expandStatusArray replaceObjectAtIndex:button.tag withObject:@"1"];
- // }
- // [tableViewOutlet reloadData];
- //
- //}
- #pragma mark - Actions
- - (IBAction)downloadFilesButtonTouchUp:(id)sender {
- [downloadFilesButton setEnabled:NO];
- [[NSNotificationCenter defaultCenter] postNotificationName:MfApiStartDownload object:sender];
- }
- - (IBAction)groupSwitchValueChanged:(UISwitch*)sender {
- [downloadFilesButton setEnabled:YES];
- UITableViewCell* cell = (UITableViewCell*)[[sender superview] superview];
- NSIndexPath* indexPath = [self.tableViewOutlet indexPathForCell:cell];
- if (indexPath) {
- Group *group = [self.fetchedResultsController objectAtIndexPath:indexPath];
- [[GroupsAPI shared] setSelectedForGroupId:group.identifier.integerValue selected:sender.on completionHandler:^(){
- dispatch_async(dispatch_get_main_queue(),^{
- _totalSize = [[GroupsAPI shared] getGroupFileSizeToDwonload];
- _totalSize /= 1 << 20;
- [self.totalSizeLabel setText:[NSString stringWithFormat:@"%.1f MB", _totalSize]];
- });
- }];
- /*
- CGFloat fileSize = [[(Group*)group fileSize] floatValue];
- fileSize /= 1 << 20;
- if(sender.on) {
- _totalSize += fileSize;
- } else {
- _totalSize -= fileSize;
- }*/
- }
- }
- #pragma NSFetchControler
- - (void)initializeFetchedResultsController{
- NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Group"];
- NSSortDescriptor *titleSort = [NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES];
- [request setSortDescriptors:@[titleSort]];
- //CDStackSingleton.shared.mainContext
- NSManagedObjectContext *moc = [[CDStackSingleton shared] mainContext]; //Retrieve the main queue NSManagedObjectContext
- [self setFetchedResultsController:[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:nil cacheName:nil]];
- [[self fetchedResultsController] setDelegate:self];
- NSError *error = nil;
- if (![[self fetchedResultsController] performFetch:&error]) {
- NSLog(@"Failed to initialize FetchedResultsController: %@\n%@", [error localizedDescription], [error userInfo]);
- abort();
- }
- }
- - (void) performFetch {
- NSError *error = nil;
- if (![[self fetchedResultsController] performFetch:&error]) {
- NSLog(@"Failed to initialize FetchedResultsController: %@\n%@", [error localizedDescription], [error userInfo]);
- abort();
- }
- [[self tableViewOutlet] reloadData];
- }
- #pragma mark - NSFetchedResultsControllerDelegate
- - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
- {
- [[self tableViewOutlet] beginUpdates];
- }
- - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
- {
- switch(type) {
- case NSFetchedResultsChangeInsert:
- [[self tableViewOutlet] insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeDelete:
- [[self tableViewOutlet] deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeMove:
- case NSFetchedResultsChangeUpdate:
- break;
- }
- }
- - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
- {
- switch(type) {
- case NSFetchedResultsChangeInsert:
- [[self tableViewOutlet] insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeDelete:
- [[self tableViewOutlet] deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- case NSFetchedResultsChangeUpdate:
- [self.tableViewOutlet reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
- break;
- case NSFetchedResultsChangeMove:
- [[self tableViewOutlet] deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
- [[self tableViewOutlet] insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
- break;
- }
- }
- - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
- {
- [[self tableViewOutlet] endUpdates];
- [self fetchNewData];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement