Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. //
  2. // GroupViewController.m
  3. // BSUIR Schedule
  4. //
  5. // Created by Laba Kirill on 5/13/15.
  6. // Copyright (c) 2015 Laba Kirill. All rights reserved.
  7. //
  8.  
  9. #import "GroupViewController.h"
  10. #import "Group.h"
  11. #import "Schedule.h"
  12.  
  13. @interface GroupViewController ()
  14.  
  15. @property (weak, nonatomic) IBOutlet UITableView *groupTable;
  16.  
  17. @property (strong, nonatomic) NSMutableArray *groups;
  18. @property (assign, nonatomic) NSInteger selectedIndexCell;
  19.  
  20. @end
  21.  
  22. @implementation GroupViewController
  23.  
  24. - (void)viewDidLoad {
  25. self.groupTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  26. }
  27.  
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30.  
  31. NSManagedObjectContext *context = [self managedObjectContext];
  32. NSFetchRequest *request= [[NSFetchRequest alloc] init];
  33. NSEntityDescription *description = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
  34.  
  35. [request setEntity:description];
  36.  
  37.  
  38. NSError *error = nil;
  39. self.groups = [[context executeFetchRequest:request error:&error] mutableCopy];
  40. self.selectedIndexCell = [self findSelectedCell];
  41.  
  42. //
  43. // //print default
  44. // NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  45. // NSLog(@"%@, %@", [defaults objectForKey:@"selectedGroup"], [defaults objectForKey:@"selectedSubgroup"]);
  46.  
  47. [self.groupTable reloadData];
  48. }
  49.  
  50. #pragma mark - Table Delegate
  51.  
  52. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  53. return 1;
  54. }
  55.  
  56. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  57. return self.groups.count;
  58. }
  59.  
  60. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  61. static NSString *CellIdentifier = @"Cell";
  62.  
  63. UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  64.  
  65. id object = self.groups[indexPath.row];
  66. Group *record = (Group *)object;
  67.  
  68. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  69. if([record.group isEqualToString:[defaults objectForKey:@"selectedGroup"]] && [record.subgroup isEqualToString:[defaults objectForKey:@"selectedSubgroup"]]) {
  70. cell.accessoryType = UITableViewCellAccessoryCheckmark;
  71. }
  72. else {
  73. cell.accessoryType = UITableViewCellAccessoryNone;
  74. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  75. }
  76.  
  77. cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@",record.group, record.subgroup];// record.group;
  78.  
  79. return cell;
  80. }
  81.  
  82. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. self.selectedIndexCell = [self findSelectedCell];
  85.  
  86. NSIndexPath *index = [NSIndexPath indexPathForRow:self.selectedIndexCell inSection:0];
  87. [tableView cellForRowAtIndexPath:index].accessoryType = UITableViewCellAccessoryNone;
  88. [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
  89. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  90.  
  91. /* or
  92. selectedIndex = indexPath.row;
  93. [tableView reloadData];
  94. */
  95.  
  96. id object = self.groups[indexPath.row];
  97. Group *record = (Group *)object;
  98.  
  99. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  100. [defaults setObject:record.group forKey:@"selectedGroup"];
  101. [defaults setObject:record.subgroup forKey:@"selectedSubgroup"];
  102. [defaults synchronize];
  103. }
  104.  
  105. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  106. return YES;
  107. }
  108.  
  109. -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
  110. return @"Удалить";
  111. }
  112.  
  113. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  114. NSManagedObjectContext *context = [self managedObjectContext];
  115.  
  116. if (editingStyle == UITableViewCellEditingStyleDelete) {
  117. if (self.groups.count != 1) {
  118. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  119.  
  120. Group *record = (Group *)[self.groups objectAtIndex:indexPath.row];
  121. if ([[defaults objectForKey:@"selectedGroup"] isEqual:record.group] && [[defaults objectForKey:@"selectedSubgroup"] isEqual:record.subgroup]) {
  122.  
  123. NSIndexPath *indexPath;
  124. if (indexPath.row == 0) {
  125. record = (Group *)[self.groups objectAtIndex:1];
  126. indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
  127. } else {
  128. record = (Group *)[self.groups firstObject];
  129. indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  130. }
  131.  
  132. [defaults setObject:record.group forKey:@"selectedGroup"];
  133. [defaults setObject:record.subgroup forKey:@"selectedSubgroup"];
  134. [defaults synchronize];
  135.  
  136. [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
  137. }
  138.  
  139. if ([self getCountRecordsOfGroup:[self.groups objectAtIndex:indexPath.row]] == 1) {
  140. [self deleteLessonsForGroup:[self.groups objectAtIndex:indexPath.row]];
  141. }
  142.  
  143. [context deleteObject:[self.groups objectAtIndex:indexPath.row]];
  144.  
  145. NSError *error = nil;
  146. if (![context save:&error]) {
  147. NSLog(@"%@", [error localizedDescription]);
  148. }
  149.  
  150. [self.groups removeObjectAtIndex:indexPath.row];
  151.  
  152. [self.groupTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
  153.  
  154. [self deleteEmptyEmployeeRecords];
  155. } else {
  156. UIAlertView *allert = [[UIAlertView alloc] initWithTitle:@"Упс..." message:@"Нельзя удалить единственное расписание!" delegate:self cancelButtonTitle:@"Ой, все!" otherButtonTitles:nil];
  157. [allert show];
  158. }
  159. }
  160. }
  161.  
  162. - (NSInteger)findSelectedCell {
  163. NSInteger index;
  164. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  165.  
  166. if (self.groups.count == 1) {
  167. id object = self.groups.firstObject;
  168. Group *record = (Group *)object;
  169.  
  170. [defaults setObject:record.group forKey:@"selectedGroup"];
  171. [defaults setObject:record.subgroup forKey:@"selectedSubgroup"];
  172. [defaults synchronize];
  173. } else {
  174. for (int i = 0; i < self.groups.count; i++) {
  175. id object = self.groups[i];
  176. Group *record = (Group *)object;
  177.  
  178. if ([record.group isEqualToString:[defaults objectForKey:@"selectedGroup"]] && [record.subgroup isEqualToString:[defaults objectForKey:@"selectedSubgroup"]]) {
  179. index = i;
  180. break;
  181. }
  182. }
  183. }
  184.  
  185. return index;
  186. }
  187.  
  188. #pragma mark - Core Data
  189.  
  190. - (void)deleteEmptyEmployeeRecords {
  191. NSManagedObjectContext *context = [self managedObjectContext];
  192. NSFetchRequest *request= [[NSFetchRequest alloc] init];
  193. NSPredicate* predicate = [NSPredicate predicateWithFormat:@"lesson.@count == 0"];
  194. [request setPredicate:predicate];
  195.  
  196. NSEntityDescription *description = [NSEntityDescription entityForName:@"Employee" inManagedObjectContext:context];
  197. [request setEntity:description];
  198.  
  199. NSError *error = nil;
  200. NSArray *allObjects = [context executeFetchRequest:request error:&error];
  201.  
  202. for (id object in allObjects) {
  203. [context deleteObject:object];
  204. }
  205.  
  206. if (![context save:&error]) {
  207. NSLog(@"%@", [error localizedDescription]);
  208. }
  209. }
  210.  
  211. - (void)deleteLessonsForGroup:(id)group {
  212. Group *record = (Group *)group;
  213.  
  214. NSManagedObjectContext *context = [self managedObjectContext];
  215. NSFetchRequest *request= [[NSFetchRequest alloc] init];
  216. NSPredicate* predicate =
  217. [NSPredicate predicateWithFormat:@"group = %@", record.group];
  218. [request setPredicate:predicate];
  219.  
  220. NSEntityDescription *description = [NSEntityDescription entityForName:@"Lesson" inManagedObjectContext:context];
  221. [request setEntity:description];
  222.  
  223. NSError *error = nil;
  224. NSArray *allObjects = [context executeFetchRequest:request error:&error];
  225.  
  226. for (id object in allObjects) {
  227. [context deleteObject:object];
  228. }
  229.  
  230. if (![context save:&error]) {
  231. NSLog(@"%@", [error localizedDescription]);
  232. }
  233. }
  234.  
  235. - (NSInteger)getCountRecordsOfGroup:(id)group {
  236. Group *record = (Group *)group;
  237.  
  238. NSManagedObjectContext *context = [self managedObjectContext];
  239. NSFetchRequest *request= [[NSFetchRequest alloc] init];
  240. NSPredicate* predicate =
  241. [NSPredicate predicateWithFormat:@"group = %@", record.group];
  242. [request setPredicate:predicate];
  243.  
  244. NSEntityDescription *description = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
  245. [request setEntity:description];
  246.  
  247. NSError *error = nil;
  248. NSArray *allObjects = [context executeFetchRequest:request error:&error];
  249.  
  250. return allObjects.count;
  251. }
  252.  
  253. - (NSManagedObjectContext *)managedObjectContext {
  254. NSManagedObjectContext *context = nil;
  255. id delegate = [[UIApplication sharedApplication] delegate];
  256.  
  257. if ([delegate performSelector:@selector(managedObjectContext)]) {
  258. context = [delegate managedObjectContext];
  259. }
  260.  
  261. return context;
  262. }
  263.  
  264. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement