Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // GroupViewController.m
- // BSUIR Schedule
- //
- // Created by Laba Kirill on 5/13/15.
- // Copyright (c) 2015 Laba Kirill. All rights reserved.
- //
- #import "GroupViewController.h"
- #import "Group.h"
- #import "Schedule.h"
- @interface GroupViewController ()
- @property (weak, nonatomic) IBOutlet UITableView *groupTable;
- @property (strong, nonatomic) NSMutableArray *groups;
- @property (assign, nonatomic) NSInteger selectedIndexCell;
- @end
- @implementation GroupViewController
- - (void)viewDidLoad {
- self.groupTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- NSManagedObjectContext *context = [self managedObjectContext];
- NSFetchRequest *request= [[NSFetchRequest alloc] init];
- NSEntityDescription *description = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
- [request setEntity:description];
- NSError *error = nil;
- self.groups = [[context executeFetchRequest:request error:&error] mutableCopy];
- self.selectedIndexCell = [self findSelectedCell];
- //
- // //print default
- // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- // NSLog(@"%@, %@", [defaults objectForKey:@"selectedGroup"], [defaults objectForKey:@"selectedSubgroup"]);
- [self.groupTable reloadData];
- }
- #pragma mark - Table Delegate
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.groups.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
- id object = self.groups[indexPath.row];
- Group *record = (Group *)object;
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- if([record.group isEqualToString:[defaults objectForKey:@"selectedGroup"]] && [record.subgroup isEqualToString:[defaults objectForKey:@"selectedSubgroup"]]) {
- cell.accessoryType = UITableViewCellAccessoryCheckmark;
- }
- else {
- cell.accessoryType = UITableViewCellAccessoryNone;
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@",record.group, record.subgroup];// record.group;
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- self.selectedIndexCell = [self findSelectedCell];
- NSIndexPath *index = [NSIndexPath indexPathForRow:self.selectedIndexCell inSection:0];
- [tableView cellForRowAtIndexPath:index].accessoryType = UITableViewCellAccessoryNone;
- [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- /* or
- selectedIndex = indexPath.row;
- [tableView reloadData];
- */
- id object = self.groups[indexPath.row];
- Group *record = (Group *)object;
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- [defaults setObject:record.group forKey:@"selectedGroup"];
- [defaults setObject:record.subgroup forKey:@"selectedSubgroup"];
- [defaults synchronize];
- }
- - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
- return YES;
- }
- -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
- return @"Удалить";
- }
- - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
- NSManagedObjectContext *context = [self managedObjectContext];
- if (editingStyle == UITableViewCellEditingStyleDelete) {
- if (self.groups.count != 1) {
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- Group *record = (Group *)[self.groups objectAtIndex:indexPath.row];
- if ([[defaults objectForKey:@"selectedGroup"] isEqual:record.group] && [[defaults objectForKey:@"selectedSubgroup"] isEqual:record.subgroup]) {
- NSIndexPath *indexPath;
- if (indexPath.row == 0) {
- record = (Group *)[self.groups objectAtIndex:1];
- indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
- } else {
- record = (Group *)[self.groups firstObject];
- indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
- }
- [defaults setObject:record.group forKey:@"selectedGroup"];
- [defaults setObject:record.subgroup forKey:@"selectedSubgroup"];
- [defaults synchronize];
- [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
- }
- if ([self getCountRecordsOfGroup:[self.groups objectAtIndex:indexPath.row]] == 1) {
- [self deleteLessonsForGroup:[self.groups objectAtIndex:indexPath.row]];
- }
- [context deleteObject:[self.groups objectAtIndex:indexPath.row]];
- NSError *error = nil;
- if (![context save:&error]) {
- NSLog(@"%@", [error localizedDescription]);
- }
- [self.groups removeObjectAtIndex:indexPath.row];
- [self.groupTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
- [self deleteEmptyEmployeeRecords];
- } else {
- UIAlertView *allert = [[UIAlertView alloc] initWithTitle:@"Упс..." message:@"Нельзя удалить единственное расписание!" delegate:self cancelButtonTitle:@"Ой, все!" otherButtonTitles:nil];
- [allert show];
- }
- }
- }
- - (NSInteger)findSelectedCell {
- NSInteger index;
- NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
- if (self.groups.count == 1) {
- id object = self.groups.firstObject;
- Group *record = (Group *)object;
- [defaults setObject:record.group forKey:@"selectedGroup"];
- [defaults setObject:record.subgroup forKey:@"selectedSubgroup"];
- [defaults synchronize];
- } else {
- for (int i = 0; i < self.groups.count; i++) {
- id object = self.groups[i];
- Group *record = (Group *)object;
- if ([record.group isEqualToString:[defaults objectForKey:@"selectedGroup"]] && [record.subgroup isEqualToString:[defaults objectForKey:@"selectedSubgroup"]]) {
- index = i;
- break;
- }
- }
- }
- return index;
- }
- #pragma mark - Core Data
- - (void)deleteEmptyEmployeeRecords {
- NSManagedObjectContext *context = [self managedObjectContext];
- NSFetchRequest *request= [[NSFetchRequest alloc] init];
- NSPredicate* predicate = [NSPredicate predicateWithFormat:@"lesson.@count == 0"];
- [request setPredicate:predicate];
- NSEntityDescription *description = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context];
- [request setEntity:description];
- NSError *error = nil;
- NSArray *allObjects = [context executeFetchRequest:request error:&error];
- for (id object in allObjects) {
- [context deleteObject:object];
- }
- if (![context save:&error]) {
- NSLog(@"%@", [error localizedDescription]);
- }
- }
- - (void)deleteLessonsForGroup:(id)group {
- Group *record = (Group *)group;
- NSManagedObjectContext *context = [self managedObjectContext];
- NSFetchRequest *request= [[NSFetchRequest alloc] init];
- NSPredicate* predicate =
- [NSPredicate predicateWithFormat:@"group = %@", record.group];
- [request setPredicate:predicate];
- NSEntityDescription *description = [NSEntityDescription entityForName:@"Lesson" inManagedObjectContext:context];
- [request setEntity:description];
- NSError *error = nil;
- NSArray *allObjects = [context executeFetchRequest:request error:&error];
- for (id object in allObjects) {
- [context deleteObject:object];
- }
- if (![context save:&error]) {
- NSLog(@"%@", [error localizedDescription]);
- }
- }
- - (NSInteger)getCountRecordsOfGroup:(id)group {
- Group *record = (Group *)group;
- NSManagedObjectContext *context = [self managedObjectContext];
- NSFetchRequest *request= [[NSFetchRequest alloc] init];
- NSPredicate* predicate =
- [NSPredicate predicateWithFormat:@"group = %@", record.group];
- [request setPredicate:predicate];
- NSEntityDescription *description = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
- [request setEntity:description];
- NSError *error = nil;
- NSArray *allObjects = [context executeFetchRequest:request error:&error];
- return allObjects.count;
- }
- - (NSManagedObjectContext *)managedObjectContext {
- NSManagedObjectContext *context = nil;
- id delegate = [[UIApplication sharedApplication] delegate];
- if ([delegate performSelector:@selector(managedObjectContext)]) {
- context = [delegate managedObjectContext];
- }
- return context;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement