Skip to content

Commit 59a0c7c

Browse files
committed
Merge pull request #285 from RickMohr/post-only-writable-fields
In tree save/edit, post only writable fields
2 parents 2f42135 + b8fc030 commit 59a0c7c

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

OpenTreeMap/src/OTM/Controllers/OTMTreeDetailViewController.m

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ - (void)toggleEditMode:(BOOL)saveChanges
647647
if (saveChanges) {
648648
OTMLoginManager* loginManager = [SharedAppDelegate loginManager];
649649
OTMUser *user = loginManager.loggedInUser;
650+
NSMutableDictionary *writableData = [self getWritableFieldData];
650651

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

657658
NSArray *pendingImageData = [self stripPendingImageData];
658-
[[[OTMEnvironment sharedEnvironment] api] addPlotWithOptionalTree:data user:user callback:^(id json, NSError *err){
659+
[[[OTMEnvironment sharedEnvironment] api] addPlotWithOptionalTree:writableData user:user
660+
callback:^(id json, NSError *err){
659661

660662
[[AZWaitingOverlayController sharedController] hideOverlay];
661663

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

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

683685
[[AZWaitingOverlayController sharedController] hideOverlay];
684686

@@ -715,6 +717,30 @@ - (void)toggleEditMode:(BOOL)saveChanges
715717
[self resetHeaderPosition];
716718
}
717719

720+
- (NSMutableDictionary *)getWritableFieldData {
721+
NSMutableDictionary *writableData = [[NSMutableDictionary alloc] init];
722+
NSDictionary *fieldData = [[OTMEnvironment sharedEnvironment] fieldData];
723+
724+
for (NSString *model in @[@"plot", @"tree"]) {
725+
NSDictionary *modelData = self.data[model];
726+
727+
if (modelData != nil) {
728+
NSMutableDictionary *writableModelData = [[NSMutableDictionary alloc] init];
729+
730+
for (NSString *key in modelData) {
731+
NSString *fieldKey = [NSString stringWithFormat:@"%@.%@", model, key];
732+
NSDictionary *field = fieldData[fieldKey];
733+
734+
if (field != nil && ([key isEqualToString:@"id"] || [field[@"can_write"] boolValue])) {
735+
writableModelData[key] = modelData[key];
736+
}
737+
}
738+
writableData[model] = writableModelData;
739+
}
740+
}
741+
return writableData;
742+
}
743+
718744
- (void)addNewUdf:(NSNotification *)notification
719745
{
720746
NSDictionary *notificationData = (NSDictionary *)[notification object];

OpenTreeMap/src/OTM/OTMEnvironment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ extern NSString * const OTMEnvironmentDateStringShort;
102102
@property (nonatomic, strong) UIColor *buttonTextColor;
103103
@property (nonatomic, assign) BOOL pendingActive;
104104
@property (nonatomic, strong) NSArray *sectionTitles;
105+
@property (nonatomic, strong) NSDictionary *fieldData;
105106
@property (nonatomic, strong) NSArray *fields;
106107
@property (nonatomic, strong) NSDictionary *sortKeys;
107108
@property (nonatomic, strong) NSArray *ecoFields;

OpenTreeMap/src/OTM/OTMEnvironment.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,20 +201,21 @@ - (void)updateEnvironmentWithDictionary:(NSDictionary *)dict {
201201
self.instance = [dict objectForKey:@"url"];
202202
self.instanceId = [dict objectForKey:@"id"];
203203
self.geoRev = [dict objectForKey:@"geoRevHash"];
204-
self.fields = [self fieldsFromDict:[dict objectForKey:@"fields"] orderedAndGroupedByDictArray:[dict objectForKey:@"field_key_groups"]];
204+
self.fieldData = [dict objectForKey:@"fields"];
205+
self.fields = [self fieldsFromDict:self.fieldData orderedAndGroupedByDictArray:[dict objectForKey:@"field_key_groups"]];
205206
self.sectionTitles = [self sectionTitlesFromDictArray:[dict objectForKey:@"field_key_groups"]];
206207
self.config = [dict objectForKey:@"config"];
207208
self.mapViewTitle = [dict objectForKey:@"name"];
208-
self.photoFieldWritable = [[[[[dict objectForKey:@"fields"] objectForKey:@"treephoto.image"] objectForKey:@"can_write"] stringValue] isEqualToString:@"0"] ? NO : YES;
209+
self.photoFieldWritable = [[[[self.fieldData objectForKey:@"treephoto.image"] objectForKey:@"can_write"] stringValue] isEqualToString:@"0"] ? NO : YES;
209210
[self setSearchRegionRadiusInMeters:[[dict objectForKey:@"extent_radius"] doubleValue]];
210211

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

213214
NSArray *regFilters = [self filtersFromDictArray:missingAndStandardFilters[@"standard"]
214-
usingFields:[dict objectForKey:@"fields"]];
215+
usingFields:self.fieldData];
215216

216217
NSArray *missingFilters = [self missingFiltersFromDictArray:missingAndStandardFilters[@"missing"]
217-
usingFields:[dict objectForKey:@"fields"]];
218+
usingFields:self.fieldData];
218219

219220
self.filters = [regFilters arrayByAddingObjectsFromArray:missingFilters];
220221

0 commit comments

Comments
 (0)