1
\$\begingroup\$

I have A page, where you can create an event using the name, location, enddate startend date, attendee, URL location, repeat options, alert options and etc. There is two scenario when the user can create this new event and can see event details as well.

I knew this below approach is bad. I am thinking to change this code using model class for that UI. is it correct? or can anyone can suggest best approach ? What he has done is code below:

     #pragma mark - Add Button Action Handler

     - (void)handleAdd:(id)sender {
         NSString *alertMessage = nil;
         if (!_isEditMode) {
             [self saveNewCalender];
         }
         else {
             NSString *title = [self.calenderDataSource.calenderDatas objectForKey:kTitleEvent];
             if (!IS_NULL(title)) {
                 title = [self.calenderDataSource.calenderDatas objectForKey:kTitleEvent];
             }else{
                 title = self.calender.title;
            }
             NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
             NSString *trimmedString = [title stringByTrimmingCharactersInSet:whitespace];
             if([trimmedString length] == 0) {
                 alertMessage = NSLocalizedString(kCALENDAR_TITLE_ALERT_MSG,
     kCALENDAR_TITLE_ALERT_MSG);
             }else{
                 UIColor *color = [self.calenderDataSource.calenderDatas objectForKey:kColor];
                 if ((self.calender.permissions != CalendarPermissionsNone) && (self.calender.permissions != CalendarPermissionsReadOnly)) {

                     for (ShareeUser *user in self.sharedUser) {
                         [self.calender addSharee:user];
                     }

                     for (ShareeUser *user in self.tobeRemoveUser) {
                         [self.calender removeSharee:user];
                     }

                     [self.calender setTitle:trimmedString];
                     BOOL isPublic = [[self.calenderDataSource.calenderDatas objectForKey:kisPublic]
     boolValue];
                     [self.calender setIsPublic:isPublic];
                     NSString *desc = [self.calenderDataSource.calenderDatas
     objectForKey:kCollection_Description];
                     if((!IS_NULL(desc)&& ![desc isEqualToString:self.calender.calendarDescription])){
                         [self.calender setCalendarDescription:desc];
                     }else{
                        //
                         [self.calender setCalendarDescription:self.calender.calendarDescription];
                     }


                 }
                 if (color != nil)
                     [self.calender setColor:color];

                 BOOL isCalendarSaved;
                 NSError *error;
                 @try {
                     isCalendarSaved = [[EventStore sharedEventStore] saveCalendar:self.calender
                                                                                error:&error];
                    [[[CalendarManager calendarSharedManager] calendarSession] refresh];

                 } @catch (NSException *exception) {
                     [Logger logException:exception sender:__PRETTY_FUNCTION__];
                 }

                 if(isCalendarSaved){

                     alertMessage = NSLocalizedString(kCALENDAR_MODIFIED_ALERT_MSG,
     kCALENDAR_MODIFIED_ALERT_MSG);
                 }else{

                     alertMessage = NSLocalizedString(kCALENDAR_CANT_SAVE_MSG, kCALENDAR_CANT_SAVE_MSG);
                 }

                 // save calendar in core data
                 NSPredicate *isCalendarExist = [NSPredicate predicateWithFormat:@"identifier ==%d",self.calender.identifier];
                 NSMutableArray *calendars = [[CalendarManager calendarSharedManager] fetchCalendarArrayFromCoreData];
                 if ([calendars count]>0) {
                     NSArray *duplicateCals = [calendars filteredArrayUsingPredicate:isCalendarExist];
                     Calendar *calendar = duplicateCals[0];
                     calendar.identifier = [NSNumber numberWithInt:self.calender.identifier];
                     calendar.isVisible = [NSNumber numberWithBool:YES];
                     [CoreDataManager saveWithError:error];
                 }
             }
             [AlertController showAlertViewIn:self
                                        withTitle:nil
                                withButtonsTitles:nil
                                       andMessage:alertMessage
                                cancelButtonTitle:NSLocalizedString(@"TitleOk", @"OK")
                           destructiveButtonTitle:nil
                                    clickedButton:nil];

             if ([trimmedString length]>0) {
                 [self dismissViewControllerAnimated:YES completion:nil];
             }
         } }

      #pragma mark - Save New Calender Action

      - (void) saveNewCalender {

         NSString *alertMessage = nil;
         NSString *title = [self.calenderDataSource.calenderDatas objectForKey:kTitleEvent];
         NSCharacterSet *whitespace = [NSCharacterSet whitespaceCharacterSet];
         NSString *trimmedString = [title stringByTrimmingCharactersInSet:whitespace];
         // title can not be nil
         if (title == nil || [title length]<=0) {
             alertMessage = NSLocalizedString(KCALENDAR_EMPTY_TITLE_ALERT_MSG,
     KCALENDAR_EMPTY_TITLE_ALERT_MSG);
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self showMessage:alertMessage withTitle:nil];
             });
         }else if([trimmedString length] == 0) {
             alertMessage = NSLocalizedString(kCALENDAR_TITLE_ALERT_MSG, kCALENDAR_TITLE_ALERT_MSG);
             dispatch_async(dispatch_get_main_queue(), ^{
                 [self showMessage:alertMessage withTitle:nil];
           });
       }else {

           @try {
               // checking duplicuate calendar if any
               NSArray *calendas = [[EventStore sharedEventStore] calendars];
               NSPredicate *isDuplicateCalendarPredicate = [NSPredicate predicateWithFormat:@"title==%@",trimmedString];
               NSArray *filterArray = [calendas filteredArrayUsingPredicate:isDuplicateCalendarPredicate];

               if ([filterArray count]>0) {
                   alertMessage = NSLocalizedString(KCALENDAR_ALREADY_EXIST_MSG,
   KCALENDAR_ALREADY_EXIST_MSG);
               }else {
                   // saving calendar if unique
                   UIColor *color = [self.calenderDataSource.calenderDatas objectForKey:kColor];
                   BOOL isPublicEnabled = [[self.calenderDataSource.calenderDatas objectForKey:kisPublic]
     boolValue];
                     Calendar* aCalendar = [[Calendar alloc] init];
                     [aCalendar setTitle:trimmedString];
                     [aCalendar setColor:color];
                     //            [aCalendar setTimeZone:timeZone];
                     [aCalendar setIsPublic:isPublicEnabled];
                     NSString *desc = [self.calenderDataSource.calenderDatas
     objectForKey:kCollection_Description];
                     (desc != nil && [desc length]>0)? ([aCalendar setCalendarDescription:desc]):([aCalendar
     setCalendarDescription:@""]);

                 for (ShareeUser *user in self.sharedUser) {
                     [aCalendar addSharee:user];
                 }

                 BOOL isCalendarSaved;
                 NSError *error;

                 @try {

                     isCalendarSaved = [[EventStore sharedEventStore] saveCalendar:aCalendar error:&error];

                 } @catch (NSException *exception) {
                     [Logger logException:exception sender:__PRETTY_FUNCTION__];
                 }

                 if(!isCalendarSaved){
                     //Handle Calendar Save Error
                     alertMessage = NSLocalizedString(kCALENDAR_CANT_SAVE_MSG, kCALENDAR_CANT_SAVE_MSG);
                         [[ErrorManager sharedManager] handleAppForErrorType:ErrorTypeCalendar controller:self
  errorCode:error.code errorMessage:error.localizedDescription
  errorManagerActionCallback:^(ErrorManagerActionType actionType,
  NSString * _Nullable title, NSString * _Nullable message) {
                          dispatch_async(dispatch_get_main_queue(), ^{
                              [self showMessage:message withTitle:title];
                          });
                      }];
                  }

                  // save calendar in core data
                  BOOL ifOnlyOneCalendar = [self checkIfOneCalendar];
                  if (ifOnlyOneCalendar ) {
                      [[EventStore sharedEventStore] setDefaultCalendar:aCalendar];
                  }
                  if (isCalendarSaved) {

                      NSManagedObjectContext *temporaryContext = [[CoreDataManager privateInstance]managedObjectContext];

                         Calendar *calendar = [Calendar insertInManagedObjectContext:temporaryContext];
                     calendar.identifier = [NSNumber numberWithInt:aCalendar.identifier];
                     calendar.date = [NSDate date];
                     calendar.isVisible = [NSNumber numberWithBool:YES];
                     calendar.isSubscribed = [NSNumber numberWithBool:NO];
                     if (ifOnlyOneCalendar)
                         calendar.isDefault = [NSNumber numberWithBool:YES];
                     else
                         calendar.isDefault = [NSNumber numberWithBool:NO];

                     [CoreDataManager saveWithError:error];
                 }
                 [[[CalendarManager calendarSharedManager] calendarSession] refresh];

             }
         } @catch (NSException *exception) {
             [Logger logException:exception sender:__PRETTY_FUNCTION__];
         }
     }
     [self dismissViewControllerAnimated:YES completion:nil];
      }
\$\endgroup\$
  • \$\begingroup\$ I own the code. \$\endgroup\$ – Sandy Mar 9 '17 at 9:30
  • \$\begingroup\$ Please fix your code as posted here. Surely the ">" at the start of every line isn't supposed to be there. \$\endgroup\$ – BCdotWEB Mar 9 '17 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.