From 36371dcd30578bb87751502e097366e2aeb83174 Mon Sep 17 00:00:00 2001 From: moritz haarmann Date: Thu, 13 Feb 2014 11:04:32 +0100 Subject: [PATCH] + Finalized preliminary feedback support. Now comes the polish. --- Classes/BITFeedbackComposeViewController.m | 2 +- Classes/BITFeedbackManager.m | 14 ++++++++++++-- Classes/BITFeedbackManagerPrivate.h | 2 +- Classes/BITFeedbackMessage.h | 1 + Classes/BITFeedbackMessage.m | 3 +++ Classes/BITHockeyAppClient.h | 16 ++++++++++++++-- Classes/BITHockeyAppClient.m | 8 ++++++-- 7 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Classes/BITFeedbackComposeViewController.m b/Classes/BITFeedbackComposeViewController.m index 72004106d9..a33060af90 100644 --- a/Classes/BITFeedbackComposeViewController.m +++ b/Classes/BITFeedbackComposeViewController.m @@ -351,7 +351,7 @@ NSString *text = self.textView.text; - [self.manager submitMessageWithText:text]; + [self.manager submitMessageWithText:text andPhotos:self.photos]; [self dismissWithResult:BITFeedbackComposeResultSubmitted]; } diff --git a/Classes/BITFeedbackManager.m b/Classes/BITFeedbackManager.m index 903183262f..4344fab4b2 100644 --- a/Classes/BITFeedbackManager.m +++ b/Classes/BITFeedbackManager.m @@ -837,6 +837,15 @@ [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; + NSInteger photoIndex = 0; + + for (UIImage *image in message.photos){ + NSString *contentType = @"image/png"; + NSData* imageData = UIImagePNGRepresentation(image); + [postBody appendData:[BITHockeyAppClient dataWithPostValue:imageData forKey:[NSString stringWithFormat:@"attachment%ld", (long)photoIndex] contentType:contentType boundary:boundary]]; + photoIndex++; + } + [request setHTTPBody:postBody]; } @@ -971,11 +980,12 @@ } } -- (void)submitMessageWithText:(NSString *)text { +- (void)submitMessageWithText:(NSString *)text andPhotos:(NSArray *)photos { BITFeedbackMessage *message = [[BITFeedbackMessage alloc] init]; message.text = text; [message setStatus:BITFeedbackMessageStatusSendPending]; - [message setToken:[self uuidAsLowerCaseAndShortened]]; + [message setToken:[self uuidAsLowerCaseAndShortened]]; + [message setPhotos:photos]; [message setUserMessage:YES]; [_feedbackList addObject:message]; diff --git a/Classes/BITFeedbackManagerPrivate.h b/Classes/BITFeedbackManagerPrivate.h index dd5f03a13f..9748b0dc11 100644 --- a/Classes/BITFeedbackManagerPrivate.h +++ b/Classes/BITFeedbackManagerPrivate.h @@ -65,7 +65,7 @@ - (NSUInteger)numberOfMessages; - (BITFeedbackMessage *)messageAtIndex:(NSUInteger)index; -- (void)submitMessageWithText:(NSString *)text; +- (void)submitMessageWithText:(NSString *)text andPhotos:(NSArray *)photos; - (void)submitPendingMessages; // Returns YES if manual user data can be entered, required or optional diff --git a/Classes/BITFeedbackMessage.h b/Classes/BITFeedbackMessage.h index c938e21722..78bc124fb2 100644 --- a/Classes/BITFeedbackMessage.h +++ b/Classes/BITFeedbackMessage.h @@ -69,6 +69,7 @@ typedef NS_ENUM(NSInteger, BITFeedbackMessageStatus) { @property (nonatomic, copy) NSDate *date; @property (nonatomic, copy) NSNumber *id; @property (nonatomic, copy) NSString *token; +@property (nonatomic, strong) NSArray *photos; @property (nonatomic) BITFeedbackMessageStatus status; @property (nonatomic) BOOL userMessage; diff --git a/Classes/BITFeedbackMessage.m b/Classes/BITFeedbackMessage.m index a5b0e0d8b2..b0860b6bdd 100644 --- a/Classes/BITFeedbackMessage.m +++ b/Classes/BITFeedbackMessage.m @@ -42,6 +42,7 @@ _email = nil; _date = [[NSDate alloc] init]; _token = nil; + _photos = nil; _id = [[NSNumber alloc] initWithInteger:0]; _status = BITFeedbackMessageStatusSendPending; _userMessage = NO; @@ -59,6 +60,7 @@ [encoder encodeObject:self.email forKey:@"email"]; [encoder encodeObject:self.date forKey:@"date"]; [encoder encodeObject:self.id forKey:@"id"]; + [encoder encodeObject:self.photos forKey:@"photos"]; [encoder encodeInteger:self.status forKey:@"status"]; [encoder encodeBool:self.userMessage forKey:@"userMessage"]; [encoder encodeObject:self.token forKey:@"token"]; @@ -72,6 +74,7 @@ self.email = [decoder decodeObjectForKey:@"email"]; self.date = [decoder decodeObjectForKey:@"date"]; self.id = [decoder decodeObjectForKey:@"id"]; + self.photos = [decoder decodeObjectForKey:@"photos"]; self.status = (BITFeedbackMessageStatus)[decoder decodeIntegerForKey:@"status"]; self.userMessage = [decoder decodeBoolForKey:@"userMessage"]; self.token = [decoder decodeObjectForKey:@"token"]; diff --git a/Classes/BITHockeyAppClient.h b/Classes/BITHockeyAppClient.h index a8d5fdbd15..617b9b392d 100644 --- a/Classes/BITHockeyAppClient.h +++ b/Classes/BITHockeyAppClient.h @@ -118,8 +118,8 @@ #pragma mark - Helpers /** - * create a post body from the given value, key and boundary - * c/p from HockeyBaseManager + * create a post body from the given value, key and boundary. This is a convenience call to + * dataWithPostValue:forKey:contentType:boundary and aimed at NSString-content. * * @param value - * @param key - @@ -128,4 +128,16 @@ * @return NSData instance configured to be attached on a (post) URLRequest */ + (NSData *)dataWithPostValue:(NSString *)value forKey:(NSString *)key boundary:(NSString *) boundary; + +/** + * create a post body from the given value, key and boundary and content type. + * + * @param value - + * @param key - + * @param boundary - + * + * @return NSData instance configured to be attached on a (post) URLRequest + */ ++ (NSData *)dataWithPostValue:(NSData *)value forKey:(NSString *)key contentType:(NSString *)contentType boundary:(NSString *) boundary; + @end diff --git a/Classes/BITHockeyAppClient.m b/Classes/BITHockeyAppClient.m index 168162b47c..442dbaf31b 100644 --- a/Classes/BITHockeyAppClient.m +++ b/Classes/BITHockeyAppClient.m @@ -85,12 +85,16 @@ } + (NSData *)dataWithPostValue:(NSString *)value forKey:(NSString *)key boundary:(NSString *) boundary { + return [self dataWithPostValue:[value dataUsingEncoding:NSUTF8StringEncoding] forKey:key contentType:@"text" boundary:boundary]; +} + ++ (NSData *)dataWithPostValue:(NSData *)value forKey:(NSString *)key contentType:(NSString *)contentType boundary:(NSString *) boundary { NSMutableData *postBody = [NSMutableData data]; [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\";\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; - [postBody appendData:[[NSString stringWithFormat:@"Content-Type: text\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; - [postBody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]]; + [postBody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", contentType] dataUsingEncoding:NSUTF8StringEncoding]]; + [postBody appendData:value]; [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; return postBody;