FLEXDefaultEditorViewController.m 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // FLEXDefaultEditorViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/23/14.
  6. // Copyright (c) 2020 FLEX Team. 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, readonly) NSString *key;
  16. @end
  17. @implementation FLEXDefaultEditorViewController
  18. + (instancetype)target:(NSUserDefaults *)defaults key:(NSString *)key commitHandler:(void(^_Nullable)())onCommit {
  19. FLEXDefaultEditorViewController *editor = [self target:defaults data:key commitHandler:onCommit];
  20. editor.title = @"Edit Default";
  21. return editor;
  22. }
  23. - (NSUserDefaults *)defaults {
  24. return [_target isKindOfClass:[NSUserDefaults class]] ? _target : nil;
  25. }
  26. - (NSString *)key {
  27. return _data;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.fieldEditorView.fieldDescription = self.key;
  32. id currentValue = [self.defaults objectForKey:self.key];
  33. FLEXArgumentInputView *inputView = [FLEXArgumentInputViewFactory
  34. argumentInputViewForTypeEncoding:FLEXEncodeObject(currentValue)
  35. currentValue:currentValue
  36. ];
  37. inputView.backgroundColor = self.view.backgroundColor;
  38. inputView.inputValue = currentValue;
  39. self.fieldEditorView.argumentInputViews = @[inputView];
  40. }
  41. - (void)actionButtonPressed:(id)sender {
  42. id value = self.firstInputView.inputValue;
  43. if (value) {
  44. [self.defaults setObject:value forKey:self.key];
  45. } else {
  46. [self.defaults removeObjectForKey:self.key];
  47. }
  48. [self.defaults synchronize];
  49. // Dismiss keyboard and handle committed changes
  50. [super actionButtonPressed:sender];
  51. // Go back after setting, but not for switches.
  52. if (sender) {
  53. [self.navigationController popViewControllerAnimated:YES];
  54. } else {
  55. self.firstInputView.inputValue = [self.defaults objectForKey:self.key];
  56. }
  57. }
  58. + (BOOL)canEditDefaultWithValue:(id)currentValue {
  59. return [FLEXArgumentInputViewFactory
  60. canEditFieldWithTypeEncoding:FLEXEncodeObject(currentValue)
  61. currentValue:currentValue
  62. ];
  63. }
  64. @end