proceeding with site image

mail@pastecode.io avatar
unknown
objectivec
7 months ago
13 kB
2
Indexable
Never
- (IBAction)proceedBtnTapped:(UIBarButtonItem *)sender {
    @try {
   
        NSDictionary *lcoationDic;
        CLLocationCoordinate2D coordinate = [self.mapview.projection coordinateForPoint:self.mapview.center];
        marker.map = nil;
        marker= nil;
        _mapview.myLocationEnabled = NO;
        
        UIGraphicsBeginImageContextWithOptions(_mapview.bounds.size, NO, 1.0);
        [_mapview.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        
        if(_placemark){
            
            if (_updatedSiteDicitonary) {
                _siteDicitonary = [_updatedSiteDicitonary mutableCopy];
                //WDDocument *document = [[WDDrawingManager sharedInstance] openMyDocumentWithName:[NSString stringWithFormat:@"%@.inkpad",_siteDicitonary[@"name"]] withCompletionHandler:nil];
                //[[WDDrawingManager sharedInstance] deleteDrawing:document];
                NSDictionary *locDict = [_updatedSiteDicitonary[@"locations"] lastObject];
                
                lcoationDic = @{@"id":locDict[@"id"]?locDict[@"id"]:[NSNull null],
                                @"Street":locDict[@"street"],
                                @"Latitude":[NSNumber numberWithDouble:coordinate.latitude],
                                @"Longitude":[NSNumber numberWithDouble:coordinate.longitude],
                                @"Country":locDict[@"country"],
                                @"City":locDict[@"city"],
                                @"ZoomScale":[NSNumber numberWithFloat:_mapview.camera.zoom],
                                @"Postcode":locDict[@"postcode"]};
                [_siteDicitonary removeObjectForKey:@"locations"];
                [_siteDicitonary removeObjectForKey:@"width"];
                [_siteDicitonary removeObjectForKey:@"height"];
                [_siteDicitonary removeObjectForKey:@"drawingImageOut"];
                [_siteDicitonary removeObjectForKey:@"drawingImage"];
                [_siteDicitonary removeObjectForKey:@"impactFileOut"];
            }else{
                
                if(_placemark.thoroughfare){
                    lcoationDic = @{@"Street":_placemark.thoroughfare?_placemark.thoroughfare:@"N/A",
                                    @"Latitude":[NSNumber numberWithDouble:coordinate.latitude],
                                    @"Longitude":[NSNumber numberWithDouble:coordinate.longitude],
                                    @"Country":_placemark.country,
                                    @"City":_placemark.locality?_placemark.locality:@"",
                                    @"ZoomScale":[NSNumber numberWithFloat:_mapview.camera.zoom],
                                    @"Postcode":_placemark.postalCode?_placemark.postalCode:@"N/A"};
                }
                
                else lcoationDic = @{@"Latitude":[NSNumber numberWithDouble:coordinate.latitude],
                                     @"Longitude":[NSNumber numberWithDouble:coordinate.longitude]};
            }
            //NSString *imageName = [NSString stringWithFormat:@"%@",_siteDicitonary[@"name"]];
            
            //_document = [[WDDrawingManager sharedInstance] drawingWithImage:newImage imageName:imageName drawingName:[NSString stringWithFormat:@"%@.inkpad",imageName]];
            
            
            
            [_siteDicitonary setValue:[NSNumber numberWithDouble:newImage.size.height] forKey:@"Height"];
            [_siteDicitonary setValue:[NSNumber numberWithDouble:newImage.size.width] forKey:@"Width"];
            [_siteDicitonary setValue:@[lcoationDic] forKey:@"Locations"];
            
            
            //NSData *drawingImageData = UIImageJPEGRepresentation([newImage resizedImage:CGSizeMake(100, 80) interpolationQuality:0], .9);
            NSData *drawingImageData = UIImageJPEGRepresentation(newImage, .9);
            NSString *base64Encoded = [drawingImageData base64EncodedStringWithOptions:0];
            
            [_siteDicitonary setValue:base64Encoded forKey:@"DrawingImage"];
            [self startActivity];
            
            [[ESServiceManager sharedInstance] createSite:_siteDicitonary withCompletionBlock:^(id object, NSError *error){
                
                if (!error) {
                    [self.delegate siteImageAdded:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                    NSNumber *siteId = object[@"id"];
                    __block int callindex = 0;
                    //creating default job
                    NSString *jobprefix = [[ESServiceManager sharedInstance] getDeviceJobSerialNo];
                    if (jobprefix && self->_siteDicitonary[@"id"] == nil) {
                        NSDictionary *jobDicitonary = @{@"siteId":siteId,@"name":jobprefix,@"assignTo":@"",@"jobProducts":@[]};
                        [[ESServiceManager sharedInstance] createJob:jobDicitonary withCompletionBlock:^(id object, NSError *error){
                            [[NSUserDefaults standardUserDefaults] setValue:jobprefix forKey:[siteId stringValue]];
                            [[NSUserDefaults standardUserDefaults] synchronize];
                        }];
                        for (NSDictionary *contactDic in self->_siteContacts) {
                            NSMutableDictionary *siteDic = [NSMutableDictionary dictionaryWithDictionary:contactDic];
                            siteDic[@"SiteId"] = siteId;
                            [[ESServiceManager sharedInstance] createContact:siteDic withCompletionBlock:^(id object, NSError *error){
                                callindex++;
                                if (callindex == self->_siteContacts.count) {
                                    [self stopActivity];
                                    if (self->_updatedSiteDicitonary) {
                                        [self removeDrawingFile];
                                    }
                                    [self.navigationController popViewControllerAnimated:YES];
                                }
                            }];
                        }
                    }
                    
                }else{
                    [self stopActivity];
                    //                [[[UIAlertView alloc] initWithTitle:@"Error occurred" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil] show];
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error occurred" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:cancel];
                    
                    [self presentViewController:alert animated:YES completion:nil];
                }
                
            }];
            
            
        }else {
            if (_updatedSiteDicitonary) {
                _siteDicitonary = [_updatedSiteDicitonary mutableCopy];
                //WDDocument *document = [[WDDrawingManager sharedInstance] openMyDocumentWithName:[NSString stringWithFormat:@"%@.inkpad",_siteDicitonary[@"name"]] withCompletionHandler:nil];
                //[[WDDrawingManager sharedInstance] deleteDrawing:document];
                NSDictionary *locDict = [_updatedSiteDicitonary[@"locations"] lastObject];
                
                lcoationDic = @{@"id":locDict[@"id"]?locDict[@"id"]:[NSNull null],
                                @"Street":locDict[@"street"],
                                @"Latitude":[NSNumber numberWithDouble:coordinate.latitude],
                                @"Longitude":[NSNumber numberWithDouble:coordinate.longitude],
                                @"Country":locDict[@"country"],
                                @"City":locDict[@"city"],
                                @"ZoomScale":[NSNumber numberWithFloat:_mapview.camera.zoom],
                                @"Postcode":locDict[@"postcode"]};
                [_siteDicitonary removeObjectForKey:@"locations"];
                [_siteDicitonary removeObjectForKey:@"width"];
                [_siteDicitonary removeObjectForKey:@"height"];
                [_siteDicitonary removeObjectForKey:@"drawingImageOut"];
                [_siteDicitonary removeObjectForKey:@"drawingImage"];
                [_siteDicitonary removeObjectForKey:@"impactFileOut"];
            }else{
                lcoationDic = @{@"Street":_locationTxtField.text,
                                @"Latitude":[NSNumber numberWithDouble:coordinate.latitude],
                                @"Longitude":[NSNumber numberWithDouble:coordinate.longitude],
                                @"Country":_countryTxtField.text,
                                @"City":@"",
                                @"ZoomScale":[NSNumber numberWithFloat:_mapview.camera.zoom],
                                @"Postcode":_postalTxtField.text};
            }
            
            [_siteDicitonary setValue:[NSNumber numberWithDouble:newImage.size.height] forKey:@"Height"];
            [_siteDicitonary setValue:[NSNumber numberWithDouble:newImage.size.width] forKey:@"Width"];
            [_siteDicitonary setValue:@[lcoationDic] forKey:@"Locations"];
            
            
            //NSData *drawingImageData = UIImageJPEGRepresentation([newImage resizedImage:CGSizeMake(100, 80) interpolationQuality:0], .9);
            NSData *drawingImageData = UIImageJPEGRepresentation(newImage, .9);
            NSString *base64Encoded = [drawingImageData base64EncodedStringWithOptions:0];
            
            [_siteDicitonary setValue:base64Encoded forKey:@"DrawingImage"];
            [self startActivity];
            
            [[ESServiceManager sharedInstance] createSite:_siteDicitonary withCompletionBlock:^(id object, NSError *error){
                
                if (!error) {
                    [self.delegate siteImageAdded:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                    NSNumber *siteId = object[@"id"];
                    __block int callindex = 0;
                    //creating default job
                    NSString *jobprefix = [[ESServiceManager sharedInstance] getDeviceJobSerialNo];
                    if (jobprefix && self->_siteDicitonary[@"id"] == nil) {
                        NSDictionary *jobDicitonary = @{@"siteId":siteId,@"name":jobprefix,@"assignTo":@"",@"jobProducts":@[]};
                        [[ESServiceManager sharedInstance] createJob:jobDicitonary withCompletionBlock:^(id object, NSError *error){
                            [[NSUserDefaults standardUserDefaults] setValue:jobprefix forKey:[siteId stringValue]];
                            [[NSUserDefaults standardUserDefaults] synchronize];
                        }];
                        for (NSDictionary *contactDic in self->_siteContacts) {
                            NSMutableDictionary *siteDic = [NSMutableDictionary dictionaryWithDictionary:contactDic];
                            siteDic[@"SiteId"] = siteId;
                            [[ESServiceManager sharedInstance] createContact:siteDic withCompletionBlock:^(id object, NSError *error){
                                callindex++;
                                if (callindex == self->_siteContacts.count) {
                                    [self stopActivity];
                                    if (self->_updatedSiteDicitonary) {
                                        [self removeDrawingFile];
                                    }
                                    [self.navigationController popViewControllerAnimated:YES];
                                }
                            }];
                        }
                    }
                    
                }else{
                    [self stopActivity];
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error occurred" message:nil preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
                    [alert addAction:cancel];
                    
                    [self presentViewController:alert animated:YES completion:nil];
                }
                
            }];
        }
        
        
    } @catch (NSException *exception) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Error occurred" message:exception.reason preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:cancel];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
}