Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 7 additions & 29 deletions Classes/Core/HSTicketSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ - (void)registerUserWithFirstName:(NSString *)firstName lastName:(NSString *)las
// Store user info so that it can be used when creating ticket.
// User info is saved only when first ticket is created successfully.
self.user = validUser;
[self clearTicketDraft];
[self clearUserDraft];
success();


Expand All @@ -118,7 +116,7 @@ - (BOOL)shouldShowUserDetailsFormWhenCreatingTicket {
}

- (void)prepareTicket:(void (^)(void))success failure:(void (^)(NSError *))failure {
// Checking if gear implements fetchAllTicket:failure:
// Checking if gear implements fetchAllTicket:failure:, not used in any of gear yet
if ([self.gear respondsToSelector:@selector(fetchAllTicketForUser:success:failure:)]) {
[self.gear fetchAllTicketForUser:self.user success:^(NSMutableArray *ticketarray) {
if (ticketarray != nil) {
Expand Down Expand Up @@ -165,13 +163,15 @@ - (void)createNewTicket:(HSNewTicket *)details success:(void (^)(void))success f

//Saves ticket array in cache and user detail in cache
[HSTicketSource saveTickets:self.ticketArray atPath:TICKET_CACHE_FILE_NAME];
[self clearTicketDraft];

if ( user != nil) {
self.user = user;
[HSTicketSource saveUser:user atPath:USER_CACHE_FILE_NAME];
[self clearUserDraft];
}

[self clearTicketDraft];

}
success();
} failure:^(NSError *e) {
Expand Down Expand Up @@ -218,13 +218,14 @@ - (void)addReply:(HSTicketReply *)details ticket:(HSTicket *)ticketDict success:
// Checking if gear implements addReply:ticket:success:failure:

__block NSMutableArray* updates = self.updateArray;
__block HSTicketSource *currentSelf = self;

if([self.gear respondsToSelector:@selector(addReply:forTicket:byUser:success:failure:)]) {
[self.gear addReply:details forTicket:ticketDict byUser:self.user success:^(HSUpdate *update) {
if(update!=nil){ // Safe check
[updates addObject:update];
}

[currentSelf clearReplyDraft];
success();
} failure:^(NSError *e) {
HALog("Add reply to a ticket failed: %@",e);
Expand Down Expand Up @@ -302,7 +303,7 @@ - (void)saveReplyDraft:(NSString *)message {

- (void)saveDraftDetails {
if (self.draft != nil) {
NSString* draftFilePath = [self documentFilePath:DRAFT_FILE_NAME];
NSString* draftFilePath = [HSTicketSource documentFilePath:DRAFT_FILE_NAME];
[NSKeyedArchiver archiveRootObject:self.draft toFile:draftFilePath];
}
}
Expand Down Expand Up @@ -380,22 +381,12 @@ + (NSString *)directoryName {
return CACHE_DIRECTORY_NAME;
}

- (NSString *)directoryName {
return CACHE_DIRECTORY_NAME;
}

+ (NSString *) documentFilePath:(NSString *)fileName {
NSString* documentDirectoryPath = [self getDocumentDirectory];
NSString* documentFilePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
return documentFilePath;
}

- (NSString *) documentFilePath:(NSString *)fileName {
NSString* documentDirectoryPath = [self getDocumentDirectory];
NSString* documentFilePath = [documentDirectoryPath stringByAppendingPathComponent:fileName];
return documentFilePath;
}

+ (NSString *) getDocumentDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
Expand All @@ -408,17 +399,4 @@ + (NSString *) getDocumentDirectory {
return documentsDirectory;
}

- (NSString *) getDocumentDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:[self directoryName]];

// Create directory if not already exist
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];

return documentsDirectory;
}


@end
40 changes: 40 additions & 0 deletions Classes/UI/HSEditImageViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// ViewController.h
// Draw
//
// Created by Anirudh S on 13/12/14.
// Copyright (c) 2014 HappyFox. All rights reserved.
//

#import <UIKit/UIKit.h>

@class HSEditImageViewController;
@protocol HSEditImageViewControllerDelegate <NSObject>
- (void)editImageViewController:(HSEditImageViewController *)controller didFinishEditingImage:(NSURL *)imageURL;
@end

@interface HSEditImageViewController : UIViewController {

CGPoint lastPoint;
CGFloat redValue;
CGFloat greenValue;
CGFloat blueValue;
CGFloat brush;
CGFloat opacity;
BOOL mouseSwiped;
}

- (IBAction)pencilPressed:(id)sender;
- (IBAction)resetImage:(id)sender;

@property (weak, nonatomic) IBOutlet UIButton *save;
@property (nonatomic, strong) UIImage *attachmentImage;
@property (nonatomic) IBOutlet UIImageView *imageOnCanvas;
@property (nonatomic, weak) id <HSEditImageViewControllerDelegate> delegate;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *redButton;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *greenButton;
@property (unsafe_unretained, nonatomic) IBOutlet UIButton *blueButton;

@end


210 changes: 210 additions & 0 deletions Classes/UI/HSEditImageViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
//
// ViewController.m
// Draw
//
// Created by Anirudh S on 13/12/14.
// Copyright (c) 2014 HappyFox. All rights reserved.
//

#import "HSEditImageViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface HSEditImageViewController ()
@end

@implementation HSEditImageViewController

UIImage* resizedImage;

- (void)viewDidLoad {
[self setInitialPaintAndBrushValues];

[super viewDidLoad];

UIBarButtonItem *btnSave = [[UIBarButtonItem alloc]
initWithTitle:@"Done"
style:UIBarButtonItemStylePlain
target:self
action:@selector(save_clicked:)];
self.navigationItem.rightBarButtonItem = btnSave;

[self drawAttachmentImage];
}

- (void)setInitialPaintAndBrushValues {
redValue = 255.0/255.0;
greenValue = 59.0/255.0;
blueValue = 48.0/255.0;

[[_redButton layer] setBorderColor:[UIColor blackColor].CGColor];
[[_redButton layer] setBorderWidth:2.0f];

brush = 10.0;
opacity = 1.0;
}

- (void)drawAttachmentImage {
self.imageOnCanvas.image = _attachmentImage;

CGFloat maxContainerWidth = self.imageOnCanvas.frame.size.width;
CGFloat maxContainerHeight = self.imageOnCanvas.frame.size.height;

float widthRatio = self.imageOnCanvas.bounds.size.width / _attachmentImage.size.width;
float heightRatio = self.imageOnCanvas.bounds.size.height / _attachmentImage.size.height;
float scale = MIN(widthRatio, heightRatio);
float resizedImageWidth = scale * _attachmentImage.size.width;
float resizedImageHeight = scale * _attachmentImage.size.height;

CGSize resizingSize = CGSizeMake(resizedImageWidth, resizedImageHeight);

UIGraphicsBeginImageContextWithOptions(resizingSize, NO, 0.0);
[_attachmentImage drawInRect:CGRectMake(0, 0, resizedImageWidth, resizedImageHeight)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIGraphicsBeginImageContextWithOptions(CGSizeMake(maxContainerWidth, maxContainerHeight), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);

CGPoint origin = CGPointMake((maxContainerWidth - resizedImageWidth) / 2.0f,
(maxContainerHeight - resizedImageHeight) / 2.0f);
[scaledImage drawAtPoint:origin];

UIGraphicsPopContext();
resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.imageOnCanvas.image = resizedImage;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
mouseSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.imageOnCanvas];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

mouseSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.imageOnCanvas];

UIGraphicsBeginImageContextWithOptions(self.imageOnCanvas.frame.size, NO, 0.0);
[self.imageOnCanvas.image drawInRect:CGRectMake(0, 0, self.imageOnCanvas.frame.size.width, self.imageOnCanvas.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redValue, greenValue, blueValue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);

CGContextStrokePath(UIGraphicsGetCurrentContext());
self.imageOnCanvas.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

lastPoint = currentPoint;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if(!mouseSwiped) {
UIGraphicsBeginImageContextWithOptions(self.imageOnCanvas.frame.size, NO, 0.0);
[self.imageOnCanvas.image drawInRect:CGRectMake(0, 0, self.imageOnCanvas.frame.size.width, self.imageOnCanvas.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), redValue, greenValue, blueValue, 1.0);
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
CGContextFlush(UIGraphicsGetCurrentContext());
self.imageOnCanvas.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}

UIGraphicsBeginImageContextWithOptions(self.imageOnCanvas.frame.size, NO, 0.0);

[self.imageOnCanvas.image drawInRect:CGRectMake(0, 0, self.imageOnCanvas.frame.size.width, self.imageOnCanvas.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];

self.imageOnCanvas.image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
}

- (void)resetAllButtonBorders {
[[_redButton layer] setBorderColor:[UIColor clearColor].CGColor];
[[_greenButton layer] setBorderColor:[UIColor clearColor].CGColor];
[[_blueButton layer] setBorderColor:[UIColor clearColor].CGColor];

[[_redButton layer] setBorderWidth:2.0f];
[[_greenButton layer] setBorderWidth:2.0f];
[[_blueButton layer] setBorderWidth:2.0f];
}

- (IBAction)pencilPressed:(id)sender {

[self resetAllButtonBorders];
UIButton * PressedButton = (UIButton*)sender;

[[sender layer] setBorderColor:[UIColor blackColor].CGColor];
[[sender layer] setBorderWidth:3.0f];

switch(PressedButton.tag)
{
case 0:
[self setValueRed:255.0 Green:49.0 Blue:48.0];
break;
case 1:
[self setValueRed:76.0 Green:217.0 Blue:100.0];
break;
case 2:
[self setValueRed:0.0 Green:170.0 Blue:255.0];
break;
}
}

- (void)setValueRed:(CGFloat)valueRed Green:(CGFloat)valueGreen Blue:(CGFloat)valueBlue {
redValue = valueRed/255.0;
greenValue = valueGreen/255.0;
blueValue = valueBlue/255.0;
}

- (IBAction)resetImage:(id)sender {
self.imageOnCanvas.image = resizedImage;
}

- (IBAction)save_clicked:(id)sender {
UIGraphicsBeginImageContextWithOptions(self.imageOnCanvas.bounds.size, NO,0.0);
[self.imageOnCanvas.image drawInRect:CGRectMake(0, 0, self.imageOnCanvas.frame.size.width, self.imageOnCanvas.frame.size.height)];
UIImage *savedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImage *viewImage = savedImage;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
NSLog(@"error");
[self.delegate editImageViewController:self didFinishEditingImage:nil];
} else {
[self.delegate editImageViewController:self didFinishEditingImage:assetURL];
}
}];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
// Was there an error?
if (error != NULL)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Image could not be saved.Please try again" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil];
[alert show];
}
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Loading