Skip to content

Commit

Permalink
Merge pull request #285 from RickMohr/post-only-writable-fields
Browse files Browse the repository at this point in the history
In tree save/edit, post only writable fields
  • Loading branch information
RickMohr committed Mar 7, 2016
2 parents 2f42135 + b8fc030 commit 59a0c7c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
30 changes: 28 additions & 2 deletions OpenTreeMap/src/OTM/Controllers/OTMTreeDetailViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ - (void)toggleEditMode:(BOOL)saveChanges
if (saveChanges) {
OTMLoginManager* loginManager = [SharedAppDelegate loginManager];
OTMUser *user = loginManager.loggedInUser;
NSMutableDictionary *writableData = [self getWritableFieldData];

if (self.data[@"plot"][@"id"] == nil) {
// No 'id' parameter indicates that this is a new plot/tree
Expand All @@ -655,7 +656,8 @@ - (void)toggleEditMode:(BOOL)saveChanges
[[AZWaitingOverlayController sharedController] showOverlayWithTitle:@"Saving"];

NSArray *pendingImageData = [self stripPendingImageData];
[[[OTMEnvironment sharedEnvironment] api] addPlotWithOptionalTree:data user:user callback:^(id json, NSError *err){
[[[OTMEnvironment sharedEnvironment] api] addPlotWithOptionalTree:writableData user:user
callback:^(id json, NSError *err){

[[AZWaitingOverlayController sharedController] hideOverlay];

Expand All @@ -678,7 +680,7 @@ - (void)toggleEditMode:(BOOL)saveChanges
[[AZWaitingOverlayController sharedController] showOverlayWithTitle:@"Saving"];

NSArray *pendingImageData = [self stripPendingImageData];
[[[OTMEnvironment sharedEnvironment] api] updatePlotAndTree:data user:user callback:^(id json, NSError *err){
[[[OTMEnvironment sharedEnvironment] api] updatePlotAndTree:writableData user:user callback:^(id json, NSError *err){

[[AZWaitingOverlayController sharedController] hideOverlay];

Expand Down Expand Up @@ -715,6 +717,30 @@ - (void)toggleEditMode:(BOOL)saveChanges
[self resetHeaderPosition];
}

- (NSMutableDictionary *)getWritableFieldData {
NSMutableDictionary *writableData = [[NSMutableDictionary alloc] init];
NSDictionary *fieldData = [[OTMEnvironment sharedEnvironment] fieldData];

for (NSString *model in @[@"plot", @"tree"]) {
NSDictionary *modelData = self.data[model];

if (modelData != nil) {
NSMutableDictionary *writableModelData = [[NSMutableDictionary alloc] init];

for (NSString *key in modelData) {
NSString *fieldKey = [NSString stringWithFormat:@"%@.%@", model, key];
NSDictionary *field = fieldData[fieldKey];

if (field != nil && ([key isEqualToString:@"id"] || [field[@"can_write"] boolValue])) {
writableModelData[key] = modelData[key];
}
}
writableData[model] = writableModelData;
}
}
return writableData;
}

- (void)addNewUdf:(NSNotification *)notification
{
NSDictionary *notificationData = (NSDictionary *)[notification object];
Expand Down
1 change: 1 addition & 0 deletions OpenTreeMap/src/OTM/OTMEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ extern NSString * const OTMEnvironmentDateStringShort;
@property (nonatomic, strong) UIColor *buttonTextColor;
@property (nonatomic, assign) BOOL pendingActive;
@property (nonatomic, strong) NSArray *sectionTitles;
@property (nonatomic, strong) NSDictionary *fieldData;
@property (nonatomic, strong) NSArray *fields;
@property (nonatomic, strong) NSDictionary *sortKeys;
@property (nonatomic, strong) NSArray *ecoFields;
Expand Down
9 changes: 5 additions & 4 deletions OpenTreeMap/src/OTM/OTMEnvironment.m
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,21 @@ - (void)updateEnvironmentWithDictionary:(NSDictionary *)dict {
self.instance = [dict objectForKey:@"url"];
self.instanceId = [dict objectForKey:@"id"];
self.geoRev = [dict objectForKey:@"geoRevHash"];
self.fields = [self fieldsFromDict:[dict objectForKey:@"fields"] orderedAndGroupedByDictArray:[dict objectForKey:@"field_key_groups"]];
self.fieldData = [dict objectForKey:@"fields"];
self.fields = [self fieldsFromDict:self.fieldData orderedAndGroupedByDictArray:[dict objectForKey:@"field_key_groups"]];
self.sectionTitles = [self sectionTitlesFromDictArray:[dict objectForKey:@"field_key_groups"]];
self.config = [dict objectForKey:@"config"];
self.mapViewTitle = [dict objectForKey:@"name"];
self.photoFieldWritable = [[[[[dict objectForKey:@"fields"] objectForKey:@"treephoto.image"] objectForKey:@"can_write"] stringValue] isEqualToString:@"0"] ? NO : YES;
self.photoFieldWritable = [[[[self.fieldData objectForKey:@"treephoto.image"] objectForKey:@"can_write"] stringValue] isEqualToString:@"0"] ? NO : YES;
[self setSearchRegionRadiusInMeters:[[dict objectForKey:@"extent_radius"] doubleValue]];

NSDictionary *missingAndStandardFilters = [dict objectForKey:@"search"];

NSArray *regFilters = [self filtersFromDictArray:missingAndStandardFilters[@"standard"]
usingFields:[dict objectForKey:@"fields"]];
usingFields:self.fieldData];

NSArray *missingFilters = [self missingFiltersFromDictArray:missingAndStandardFilters[@"missing"]
usingFields:[dict objectForKey:@"fields"]];
usingFields:self.fieldData];

self.filters = [regFilters arrayByAddingObjectsFromArray:missingFilters];

Expand Down

0 comments on commit 59a0c7c

Please sign in to comment.