WFCCLocationMessageContent.m 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // WFCCTextMessageContent.m
  3. // WFChatClient
  4. //
  5. // Created by heavyrain on 2017/8/16.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCCLocationMessageContent.h"
  9. #import "WFCCIMService.h"
  10. #import "Common.h"
  11. #import "WFCCUtilities.h"
  12. @implementation WFCCLocationMessageContent
  13. - (WFCCMessagePayload *)encode {
  14. WFCCMessagePayload *payload = [super encode];
  15. payload.contentType = [self.class getContentType];
  16. payload.searchableContent = self.title;
  17. payload.binaryContent = UIImageJPEGRepresentation(self.thumbnail, 0.67);
  18. NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
  19. [dataDict setObject:@(self.coordinate.latitude) forKey:@"lat"];
  20. [dataDict setObject:@(self.coordinate.longitude) forKey:@"long"];
  21. payload.content = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:dataDict
  22. options:kNilOptions
  23. error:nil] encoding:NSUTF8StringEncoding];
  24. return payload;
  25. }
  26. - (void)decode:(WFCCMessagePayload *)payload {
  27. [super decode:payload];
  28. self.title = payload.searchableContent;
  29. self.thumbnail = [UIImage imageWithData:payload.binaryContent];
  30. NSError *__error = nil;
  31. NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[payload.content dataUsingEncoding:NSUTF8StringEncoding]
  32. options:kNilOptions
  33. error:&__error];
  34. if (!__error) {
  35. double latitude = [dictionary[@"lat"] doubleValue];
  36. double longitude = [dictionary[@"long"] doubleValue];
  37. self.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
  38. }
  39. }
  40. + (int)getContentType {
  41. return MESSAGE_CONTENT_TYPE_LOCATION;
  42. }
  43. + (int)getContentFlags {
  44. return WFCCPersistFlag_PERSIST_AND_COUNT;
  45. }
  46. + (instancetype)contentWith:(CLLocationCoordinate2D) coordinate title:(NSString *)title thumbnail:(UIImage *)thumbnail {
  47. WFCCLocationMessageContent *content = [[WFCCLocationMessageContent alloc] init];
  48. content.coordinate = coordinate;
  49. content.title = title;
  50. content.thumbnail = [WFCCUtilities generateThumbnail:thumbnail withWidth:180 withHeight:120];;
  51. return content;
  52. }
  53. + (void)load {
  54. [[WFCCIMService sharedWFCIMService] registerMessageContent:self];
  55. }
  56. - (NSString *)digest:(WFCCMessage *)message {
  57. return @"[位置]";
  58. }
  59. @end