Skip to content
This repository was archived by the owner on Jun 3, 2022. It is now read-only.

Commit ca69a26

Browse files
author
Andrés
authored
Merge pull request #68 from material-foundation/firebase_v1_a
Add support for remote controllers and delete old code
2 parents 4c51879 + f1650ce commit ca69a26

25 files changed

+204
-442
lines changed

examples/objc/RemixerExample/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ - (BOOL)application:(UIApplication *)application
3535
[self.window makeKeyAndVisible];
3636

3737
// Start Remixer.
38-
[RMXRemixer startInMode:RMXStorageModeLocal];
38+
[RMXRemixer start];
3939

4040
return YES;
4141
}

examples/objc/RemixerExample/FontViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ - (void)viewDidLoad {
3737

3838
[RMXStringVariable stringVariableWithKey:@"labelText"
3939
defaultValue:@"This is a customizable label"
40+
possibleValues:nil
4041
updateBlock:^(RMXStringVariable *variable, NSString *selectedValue) {
4142
_fontLabel.text = selectedValue;
4243
[self.view setNeedsLayout];

src/core/RMXRemixer.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
@class RMXOverlayWindow;
2222

23-
typedef NS_ENUM(NSInteger, RMXStorageMode) { RMXStorageModeLocal = 0, RMXStorageModeCloud = 1 };
24-
2523
NS_ASSUME_NONNULL_BEGIN
2624

2725
/**
@@ -31,14 +29,11 @@ NS_ASSUME_NONNULL_BEGIN
3129
@interface RMXRemixer : NSObject
3230

3331
/** Starts Remixer */
34-
+ (void)startInMode:(RMXStorageMode)mode;
32+
+ (void)start;
3533

3634
/** Stops the current Remixer session. */
3735
+ (void)stop;
3836

39-
/** Sends an invitation to the web dashboard. */
40-
+ (void)sendEmailInvite;
41-
4237
/** A unique session id. */
4338
+ (NSString *)sessionId;
4439

@@ -82,11 +77,17 @@ NS_ASSUME_NONNULL_BEGIN
8277
/** Removes all Variables and empties the dictionary of Variables. */
8378
+ (void)removeAllVariables;
8479

85-
/** Saves the Variable using one of the storage options. */
80+
/**
81+
Saves the Variable using one of the storage options.
82+
@param variable The variable to save.
83+
*/
8684
+ (void)saveVariable:(RMXVariable *)variable;
8785

88-
/** Update an existing Variable using a version from one of our storage sources. */
89-
+ (void)updateVariable:(RMXVariable *)variable usingStoredVariable:(RMXVariable *)storedVariable;
86+
/**
87+
Sets and saves the updated selectedValue and triggers a notification to update the control.
88+
@param value The new value that the variable should be set to.
89+
*/
90+
+ (void)updateVariable:(RMXVariable *)variable fromRemoteControllerToValue:(id)value;
9091

9192
@end
9293

src/core/RMXRemixer.m

Lines changed: 30 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
#import "RMXRemixer.h"
2222

2323
#import "RMXLocalStorageController.h"
24+
#import "RMXRemoteController.h"
2425
#import "RMXVariable.h"
2526
#import "UI/RMXOverlayViewController.h"
2627
#import "UI/RMXOverlayWindow.h"
2728

2829
#ifdef REMIXER_CLOUD_FIREBASE
29-
#import "RMXFirebaseStorageController.h"
30+
#import "RMXFirebaseRemoteController.h"
3031
#endif
3132

3233
#if TARGET_OS_SIMULATOR
@@ -37,8 +38,8 @@
3738

3839
@interface RMXRemixer () <UIGestureRecognizerDelegate>
3940
@property(nonatomic, strong) NSMutableDictionary *variables;
40-
@property(nonatomic, assign) RMXStorageMode storageMode;
4141
@property(nonatomic, strong) id<RMXStorageController> storage;
42+
@property(nonatomic, strong) id<RMXRemoteController> remoteController;
4243
@property(nonatomic, strong) RMXOverlayViewController *overlayController;
4344
@property(nonatomic, strong) UISwipeGestureRecognizer *swipeUpGesture;
4445
@property(nonatomic, strong) RMXOverlayWindow *overlayWindow;
@@ -63,12 +64,6 @@ + (instancetype)sharedInstance {
6364
return sharedInstance;
6465
}
6566

66-
+ (void)startInMode:(RMXStorageMode)mode {
67-
RMXRemixer *instance = [self sharedInstance];
68-
instance.storageMode = mode;
69-
[self start];
70-
}
71-
7267
+ (void)start {
7368
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
7469
if (!keyWindow) {
@@ -88,24 +83,18 @@ + (void)start {
8883
instance.overlayController = [[RMXOverlayViewController alloc] init];
8984
instance.overlayWindow.rootViewController = instance.overlayController;
9085

91-
if (instance.storageMode == RMXStorageModeLocal) {
92-
instance.storage = [[RMXLocalStorageController alloc] init];
93-
} else {
86+
instance.storage = [[RMXLocalStorageController alloc] init];
87+
9488
#ifdef REMIXER_CLOUD_FIREBASE
95-
instance.storage = [[RMXFirebaseStorageController alloc] init];
96-
#else
97-
instance.storage = [[RMXLocalStorageController alloc] init];
98-
// TODO(chuga): Print out a warning.
89+
instance.remoteController = [[RMXFirebaseRemoteController alloc] init];
90+
[instance.remoteController startObservingUpdates];
9991
#endif
100-
}
101-
[instance.storage setup];
102-
[instance.storage startObservingUpdates];
10392
}
10493

10594
+ (void)stop {
106-
RMXRemixer *instance = [self sharedInstance];
107-
[instance.storage stopObservingUpdates];
108-
[instance.storage shutDown];
95+
#ifdef REMIXER_CLOUD_FIREBASE
96+
[[[self sharedInstance] remoteController] stopObservingUpdates];
97+
#endif
10998
}
11099

111100
+ (NSString *)sessionId {
@@ -141,22 +130,6 @@ - (void)didSwipe:(UISwipeGestureRecognizer *)recognizer {
141130
[_overlayController showPanelAnimated:YES];
142131
}
143132

144-
#pragma mark - Email
145-
146-
+ (void)sendEmailInvite {
147-
// Genrates a mailto: URL string.
148-
NSString *sessionId = [self sessionId];
149-
NSString *remixerURL =
150-
[NSString stringWithFormat:@"https://remix-4d1f9.firebaseapp.com/#/composer/%@", sessionId];
151-
NSString *subject = [NSString stringWithFormat:@"Invitation to Remixer session %@", sessionId];
152-
NSString *body = [NSString stringWithFormat:@"You have been invited to a Remixer session. \n\n"
153-
@"Follow this link to log in: <a href='%@'>%@</a>",
154-
remixerURL, sessionId];
155-
NSString *mailTo = [NSString stringWithFormat:@"mailto:?subject=%@&body=%@", subject, body];
156-
NSString *url = [mailTo stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
157-
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
158-
}
159-
160133
#pragma mark - Variables
161134

162135
+ (nullable RMXVariable *)variableForKey:(NSString *)key {
@@ -167,9 +140,9 @@ + (__kindof RMXVariable *)addVariable:(RMXVariable *)variable {
167140
RMXVariable *existingVariable = [self variableForKey:variable.key];
168141
if (!existingVariable) {
169142
[[[self sharedInstance] variables] setObject:variable forKey:variable.key];
170-
RMXVariable *storedVariable = [[[self sharedInstance] storage] variableForKey:variable.key];
171-
if (storedVariable) {
172-
[self updateVariable:variable usingStoredVariable:storedVariable];
143+
id storedValue = [[[self sharedInstance] storage] selectedValueForVariableKey:variable.key];
144+
if (storedValue) {
145+
[variable setSelectedValue:storedValue];
173146
} else {
174147
[variable executeUpdateBlocks];
175148
}
@@ -178,39 +151,44 @@ + (__kindof RMXVariable *)addVariable:(RMXVariable *)variable {
178151
variable = existingVariable;
179152
}
180153

181-
// TODO(chuga): Figure out when and how to do the initial |saveRemix|.
154+
[[[self sharedInstance] remoteController] addVariable:variable];
182155
[[[self sharedInstance] overlayController] reloadData];
183156
return variable;
184157
}
185158

186159
+ (void)removeVariable:(RMXVariable *)variable {
187160
[[[self sharedInstance] variables] removeObjectForKey:variable.key];
161+
[[[self sharedInstance] remoteController] removeVariable:variable];
188162
}
189163

190164
+ (void)removeVariableWithKey:(NSString *)key {
191165
RMXVariable *variable = [self variableForKey:key];
192166
[[self sharedInstance] removeVariable:variable];
193-
}
194-
195-
+ (NSArray<RMXVariable *> *)allVariables {
196-
return [[[self sharedInstance] variables] allValues];
167+
[[[self sharedInstance] remoteController] removeVariable:variable];
197168
}
198169

199170
+ (void)removeAllVariables {
200171
[[[self sharedInstance] variables] removeAllObjects];
201172
[[[self sharedInstance] overlayController] reloadData];
173+
[[[self sharedInstance] remoteController] removeAllVariables];
174+
}
175+
176+
+ (NSArray<RMXVariable *> *)allVariables {
177+
return [[[self sharedInstance] variables] allValues];
202178
}
203179

204180
+ (void)saveVariable:(RMXVariable *)variable {
205-
[[[self sharedInstance] storage] saveVariable:variable];
181+
[[[self sharedInstance] storage] saveSelectedValueOfVariable:variable];
182+
[[[self sharedInstance] remoteController] updateVariable:variable];
206183
}
207184

208-
+ (void)updateVariable:(RMXVariable *)variable usingStoredVariable:(RMXVariable *)storedVariable {
209-
RMXRemixer *instance = [self sharedInstance];
210-
if (instance.storageMode == RMXStorageModeCloud) {
211-
[variable updateToStoredVariable:storedVariable];
212-
} else {
213-
[variable setSelectedValue:storedVariable.selectedValue];
185+
+ (void)updateVariable:(RMXVariable *)variable fromRemoteControllerToValue:(id)value {
186+
// TODO(chuga): Improve this check for equality.
187+
if (![variable.selectedValue isEqual:value]) {
188+
[variable setSelectedValue:value];
189+
[[NSNotificationCenter defaultCenter] postNotificationName:RMXVariableUpdateNotification
190+
object:variable];
191+
[[[self sharedInstance] storage] saveSelectedValueOfVariable:variable];
214192
}
215193
}
216194

src/core/UI/RMXOverlayViewController.m

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ - (void)viewDidLoad {
9191
[(UIButton *)item.leftBarButtonItem.customView addTarget:self
9292
action:@selector(dismissOverlay:)
9393
forControlEvents:UIControlEventTouchUpInside];
94-
[(UIButton *)item.rightBarButtonItem.customView addTarget:self
95-
action:@selector(sendEmailInvite:)
96-
forControlEvents:UIControlEventTouchUpInside];
9794
_panGestureRecognizer =
9895
[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
9996
_panGestureRecognizer.delegate = self;
@@ -228,10 +225,6 @@ - (void)reloadData {
228225
[self.view.tableView reloadData];
229226
}
230227

231-
- (void)sendEmailInvite:(id)sender {
232-
[RMXRemixer sendEmailInvite];
233-
}
234-
235228
#pragma mark - UITableViewDataSource
236229

237230
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

src/core/UI/cells/RMXCellStepper.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ - (void)layoutSubviews {
9595
- (void)stepperUpdated:(UIStepper *)stepperControl {
9696
[self.variable setSelectedFloatValue:stepperControl.value];
9797
[self.variable save];
98+
self.textLabel.text = [NSString stringWithFormat:@"%.2f", self.variable.selectedFloatValue];
9899
}
99100

100101
@end

src/core/remixes/RMXBooleanVariable.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ + (instancetype)booleanVariableWithKey:(NSString *)key
2828
return [RMXRemixer addVariable:variable];
2929
}
3030

31-
+ (instancetype)variableFromDictionary:(NSDictionary *)dictionary {
32-
NSString *key = [dictionary objectForKey:RMXDictionaryKeyKey];
33-
BOOL selectedValue = [[dictionary objectForKey:RMXDictionaryKeySelectedValue] boolValue];
34-
return [[self alloc] initWithKey:key defaultValue:selectedValue updateBlock:nil];
35-
}
36-
3731
- (NSDictionary *)toJSON {
3832
NSMutableDictionary *json = [super toJSON];
3933
json[RMXDictionaryKeySelectedValue] = @([self selectedBooleanValue]);

src/core/remixes/RMXColorVariable.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,7 @@ typedef void (^RMXColorUpdateBlock)(RMXColorVariable *variable, UIColor *selecte
3636
/** Designated initializer. */
3737
+ (instancetype)colorVariableWithKey:(NSString *)key
3838
defaultValue:(UIColor *)defaultValue
39-
possibleValues:(NSArray<UIColor *> *)possibleValues
40-
updateBlock:(RMXColorUpdateBlock)updateBlock;
41-
42-
/**
43-
* Convenience initializer for when this Variable isn't limited to a set of possible values or when
44-
* those values are defined in the cloud.
45-
*/
46-
+ (instancetype)colorVariableWithKey:(NSString *)key
47-
defaultValue:(UIColor *)defaultValue
39+
possibleValues:(nullable NSArray<UIColor *> *)possibleValues
4840
updateBlock:(RMXColorUpdateBlock)updateBlock;
4941

5042
@end

src/core/remixes/RMXColorVariable.m

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,6 @@ + (instancetype)colorVariableWithKey:(NSString *)key
4343
return variable;
4444
}
4545

46-
+ (instancetype)colorVariableWithKey:(NSString *)key
47-
defaultValue:(UIColor *)defaultValue
48-
updateBlock:(RMXColorUpdateBlock)updateBlock {
49-
// These default values are just temporary. We change them to the right values as soon as we
50-
// get the data from the cloud service.
51-
RMXColorVariable *variable = [[self alloc] initWithKey:key
52-
defaultValue:defaultValue
53-
possibleValues:nil
54-
updateBlock:updateBlock];
55-
return [RMXRemixer addVariable:variable];
56-
}
57-
58-
+ (instancetype)variableFromDictionary:(NSDictionary *)dictionary {
59-
id selectedValue = [dictionary objectForKey:RMXDictionaryKeySelectedValue];
60-
selectedValue = [self colorFromRGBADictionary:selectedValue];
61-
NSMutableArray *possibleValues = [NSMutableArray array];
62-
for (NSDictionary *colorDict in [dictionary objectForKey:RMXDictionaryKeyPossibleValues]) {
63-
[possibleValues addObject:[self colorFromRGBADictionary:colorDict]];
64-
}
65-
return [[self alloc] initWithKey:[dictionary objectForKey:RMXDictionaryKeyKey]
66-
defaultValue:selectedValue
67-
possibleValues:possibleValues
68-
updateBlock:nil];
69-
}
70-
7146
- (NSDictionary *)toJSON {
7247
NSMutableDictionary *json = [super toJSON];
7348
json[RMXDictionaryKeySelectedValue] = [[self class] rgbaDictionaryFromColor:self.selectedValue];
@@ -77,10 +52,11 @@ - (NSDictionary *)toJSON {
7752
return json;
7853
}
7954

80-
- (void)updateToStoredVariable:(RMXVariable *)storedVariable {
81-
self.controlType =
82-
storedVariable.possibleValues.count > 0 ? RMXControlTypeColorList : RMXControlTypeColorPicker;
83-
[super updateToStoredVariable:storedVariable];
55+
- (void)setSelectedValue:(id)selectedValue {
56+
if ([selectedValue isKindOfClass:[NSDictionary class]]) {
57+
selectedValue = [[self class] colorFromRGBADictionary:selectedValue];
58+
}
59+
[super setSelectedValue:selectedValue];
8460
}
8561

8662
#pragma mark - Private

src/core/remixes/RMXNumberVariable.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,7 @@ typedef void (^RMXNumberUpdateBlock)(RMXNumberVariable *variable, CGFloat select
3737
/** Designated initializer. */
3838
+ (instancetype)numberVariableWithKey:(NSString *)key
3939
defaultValue:(CGFloat)defaultValue
40-
possibleValues:(NSArray<NSNumber *> *)possibleValues
41-
updateBlock:(RMXNumberUpdateBlock)updateBlock;
42-
43-
/**
44-
* Convenience initializer for when this Variable isn't limited to a set of possible values or when
45-
* those values are defined in the cloud.
46-
*/
47-
+ (instancetype)numberVariableWithKey:(NSString *)key
48-
defaultValue:(CGFloat)defaultValue
40+
possibleValues:(nullable NSArray<NSNumber *> *)possibleValues
4941
updateBlock:(RMXNumberUpdateBlock)updateBlock;
5042

5143
@end

0 commit comments

Comments
 (0)