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
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
0 commit comments