Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/*
2
 * Copyright 2020 Google LLC
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
 
17
#import "GoogleDataTransport/GDTCCTLibrary/Public/GDTCOREvent+GDTCCTSupport.h"
18
 
19
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
20
 
21
NSString *const GDTCCTNeedsNetworkConnectionInfo = @"needs_network_connection_info";
22
 
23
NSString *const GDTCCTNetworkConnectionInfo = @"network_connection_info";
24
 
25
NSString *const GDTCCTEventCodeInfo = @"event_code_info";
26
 
27
@implementation GDTCOREvent (GDTCCTSupport)
28
 
29
- (void)setNeedsNetworkConnectionInfoPopulated:(BOOL)needsNetworkConnectionInfoPopulated {
30
  if (!needsNetworkConnectionInfoPopulated) {
31
    if (!self.customBytes) {
32
      return;
33
    }
34
 
35
    // Make sure we don't destroy the eventCode data, if any is present.
36
    @try {
37
      NSError *error;
38
      NSMutableDictionary *bytesDict =
39
          [[NSJSONSerialization JSONObjectWithData:self.customBytes options:0
40
                                             error:&error] mutableCopy];
41
      if (error) {
42
        GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
43
        return;
44
      }
45
      NSNumber *eventCode = bytesDict[GDTCCTEventCodeInfo];
46
      if (eventCode != nil) {
47
        self.customBytes =
48
            [NSJSONSerialization dataWithJSONObject:@{GDTCCTEventCodeInfo : eventCode}
49
                                            options:0
50
                                              error:&error];
51
      }
52
    } @catch (NSException *exception) {
53
      GDTCORLogDebug(@"Error when setting the event for needs_network_connection_info: %@",
54
                     exception);
55
    }
56
  } else {
57
    @try {
58
      NSError *error;
59
      NSMutableDictionary *bytesDict;
60
      if (self.customBytes) {
61
        bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes
62
                                                     options:0
63
                                                       error:&error] mutableCopy];
64
        if (error) {
65
          GDTCORLogDebug(@"Error when setting an even'ts event_code: %@", error);
66
          return;
67
        }
68
      } else {
69
        bytesDict = [[NSMutableDictionary alloc] init];
70
      }
71
      [bytesDict setObject:@YES forKey:GDTCCTNeedsNetworkConnectionInfo];
72
      self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
73
    } @catch (NSException *exception) {
74
      GDTCORLogDebug(@"Error when setting the event for needs_network_connection_info: %@",
75
                     exception);
76
    }
77
  }
78
}
79
 
80
- (BOOL)needsNetworkConnectionInfoPopulated {
81
  if (self.customBytes) {
82
    @try {
83
      NSError *error;
84
      NSDictionary *bytesDict = [NSJSONSerialization JSONObjectWithData:self.customBytes
85
                                                                options:0
86
                                                                  error:&error];
87
      return bytesDict && !error && [bytesDict[GDTCCTNeedsNetworkConnectionInfo] boolValue];
88
    } @catch (NSException *exception) {
89
      GDTCORLogDebug(@"Error when checking the event for needs_network_connection_info: %@",
90
                     exception);
91
    }
92
  }
93
  return NO;
94
}
95
 
96
- (void)setNetworkConnectionInfoData:(NSData *)networkConnectionInfoData {
97
  @try {
98
    NSError *error;
99
    NSString *dataString = [networkConnectionInfoData base64EncodedStringWithOptions:0];
100
    if (dataString != nil) {
101
      NSMutableDictionary *bytesDict;
102
      if (self.customBytes) {
103
        bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes
104
                                                     options:0
105
                                                       error:&error] mutableCopy];
106
        if (error) {
107
          GDTCORLogDebug(@"Error when setting an even'ts event_code: %@", error);
108
          return;
109
        }
110
      } else {
111
        bytesDict = [[NSMutableDictionary alloc] init];
112
      }
113
      [bytesDict setObject:dataString forKey:GDTCCTNetworkConnectionInfo];
114
      self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
115
      if (error) {
116
        self.customBytes = nil;
117
        GDTCORLogDebug(@"Error when setting an event's network_connection_info: %@", error);
118
      }
119
    }
120
  } @catch (NSException *exception) {
121
    GDTCORLogDebug(@"Error when setting an event's network_connection_info: %@", exception);
122
  }
123
}
124
 
125
- (nullable NSData *)networkConnectionInfoData {
126
  if (self.customBytes) {
127
    @try {
128
      NSError *error;
129
      NSDictionary *bytesDict = [NSJSONSerialization JSONObjectWithData:self.customBytes
130
                                                                options:0
131
                                                                  error:&error];
132
      NSString *base64Data = bytesDict[GDTCCTNetworkConnectionInfo];
133
      if (base64Data == nil) {
134
        return nil;
135
      }
136
 
137
      NSData *networkConnectionInfoData = [[NSData alloc] initWithBase64EncodedString:base64Data
138
                                                                              options:0];
139
      if (error) {
140
        GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", error);
141
        return nil;
142
      } else {
143
        return networkConnectionInfoData;
144
      }
145
    } @catch (NSException *exception) {
146
      GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", exception);
147
    }
148
  }
149
  return nil;
150
}
151
 
152
- (NSNumber *)eventCode {
153
  if (self.customBytes) {
154
    @try {
155
      NSError *error;
156
      NSDictionary *bytesDict = [NSJSONSerialization JSONObjectWithData:self.customBytes
157
                                                                options:0
158
                                                                  error:&error];
159
      NSString *eventCodeString = bytesDict[GDTCCTEventCodeInfo];
160
 
161
      if (!eventCodeString) {
162
        return nil;
163
      }
164
 
165
      NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
166
      formatter.numberStyle = NSNumberFormatterDecimalStyle;
167
      NSNumber *eventCode = [formatter numberFromString:eventCodeString];
168
 
169
      if (error) {
170
        GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", error);
171
        return nil;
172
      } else {
173
        return eventCode;
174
      }
175
    } @catch (NSException *exception) {
176
      GDTCORLogDebug(@"Error when getting an event's event_code: %@", exception);
177
    }
178
  }
179
  return nil;
180
}
181
 
182
- (void)setEventCode:(NSNumber *)eventCode {
183
  if (eventCode == nil) {
184
    if (!self.customBytes) {
185
      return;
186
    }
187
 
188
    NSError *error;
189
    NSMutableDictionary *bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes
190
                                                                      options:0
191
                                                                        error:&error] mutableCopy];
192
    if (error) {
193
      GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
194
      return;
195
    }
196
 
197
    [bytesDict removeObjectForKey:GDTCCTEventCodeInfo];
198
    self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
199
    if (error) {
200
      self.customBytes = nil;
201
      GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
202
      return;
203
    }
204
    return;
205
  }
206
 
207
  @try {
208
    NSMutableDictionary *bytesDict;
209
    NSError *error;
210
    if (self.customBytes) {
211
      bytesDict = [[NSJSONSerialization JSONObjectWithData:self.customBytes options:0
212
                                                     error:&error] mutableCopy];
213
      if (error) {
214
        GDTCORLogDebug(@"Error when setting an event's event_code: %@", error);
215
        return;
216
      }
217
    } else {
218
      bytesDict = [[NSMutableDictionary alloc] init];
219
    }
220
 
221
    NSString *eventCodeString = [eventCode stringValue];
222
    if (eventCodeString == nil) {
223
      return;
224
    }
225
 
226
    [bytesDict setObject:eventCodeString forKey:GDTCCTEventCodeInfo];
227
 
228
    self.customBytes = [NSJSONSerialization dataWithJSONObject:bytesDict options:0 error:&error];
229
    if (error) {
230
      self.customBytes = nil;
231
      GDTCORLogDebug(@"Error when setting an event's network_connection_info: %@", error);
232
      return;
233
    }
234
 
235
  } @catch (NSException *exception) {
236
    GDTCORLogDebug(@"Error when getting an event's network_connection_info: %@", exception);
237
  }
238
}
239
 
240
@end