FLEXDefaultEditorViewController.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // FLEXDefaultEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/23/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXDefaultEditorViewController.h"
  9. #import "FLEXFieldEditorView.h"
  10. #import "FLEXRuntimeUtility.h"
  11. #import "FLEXArgumentInputView.h"
  12. #import "FLEXArgumentInputViewFactory.h"
  13. @interface FLEXDefaultEditorViewController ()
  14. @property (nonatomic, readonly) NSUserDefaults *defaults;
  15. @property (nonatomic, strong) NSString *key;
  16. @end
  17. @implementation FLEXDefaultEditorViewController
  18. - (id)initWithDefaults:(NSUserDefaults *)defaults key:(NSString *)key
  19. {
  20. self = [super initWithTarget:defaults];
  21. if (self) {
  22. self.key = key;
  23. self.title = @"Edit Default";
  24. }
  25. return self;
  26. }
  27. - (NSUserDefaults *)defaults
  28. {
  29. return [self.target isKindOfClass:[NSUserDefaults class]] ? self.target : nil;
  30. }
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. self.fieldEditorView.fieldDescription = self.key;
  35. id currentValue = [self.defaults objectForKey:self.key];
  36. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory argumentInputViewForTypeEncoding:@encode(id) currentValue:currentValue];
  37. inputView.backgroundColor = self.view.backgroundColor;
  38. inputView.inputValue = currentValue;
  39. self.fieldEditorView.argumentInputViews = @[inputView];
  40. }
  41. - (void)actionButtonPressed:(id)sender
  42. {
  43. [super actionButtonPressed:sender];
  44. id value = self.firstInputView.inputValue;
  45. if (value) {
  46. [self.defaults setObject:value forKey:self.key];
  47. } else {
  48. [self.defaults removeObjectForKey:self.key];
  49. }
  50. [self.defaults synchronize];
  51. self.firstInputView.inputValue = [self.defaults objectForKey:self.key];
  52. }
  53. + (BOOL)canEditDefaultWithValue:(id)currentValue
  54. {
  55. return [FLEXArgumentInputViewFactory canEditFieldWithTypeEncoding:@encode(id) currentValue:currentValue];
  56. }
  57. @end