This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
TTNavigator.h
289 lines (239 loc) · 8.22 KB
/
TTNavigator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Copyright 2009-2010 Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol TTNavigatorDelegate;
@class TTURLAction;
@class TTURLMap;
@class TTURLPattern;
typedef enum {
TTNavigatorPersistenceModeNone, // no persistence
TTNavigatorPersistenceModeTop, // persists only the top-level controller
TTNavigatorPersistenceModeAll, // persists all navigation paths
} TTNavigatorPersistenceMode;
/**
* A URL-based navigation system with built-in persistence.
*/
@interface TTNavigator : NSObject {
id<TTNavigatorDelegate> _delegate;
TTURLMap* _URLMap;
UIWindow* _window;
UIViewController* _rootViewController;
NSMutableArray* _delayedControllers;
TTNavigatorPersistenceMode _persistenceMode;
NSTimeInterval _persistenceExpirationAge;
BOOL _delayCount;
BOOL _supportsShakeToReload;
BOOL _opensExternalURLs;
}
@property (nonatomic, assign) id<TTNavigatorDelegate> delegate;
/**
* The URL map used to translate between URLs and view controllers.
*
* @see TTURLMap
*/
@property (nonatomic, readonly) TTURLMap* URLMap;
/**
* The window that contains the view controller hierarchy.
*
* By default retrieves the keyWindow. If there is no keyWindow, creates a new
* TTNavigatorWindow.
*/
@property (nonatomic, retain) UIWindow* window;
/**
* The controller that is at the root of the view controller hierarchy.
*/
@property (nonatomic, readonly) UIViewController* rootViewController;
/**
* The currently visible view controller.
*/
@property (nonatomic, readonly) UIViewController* visibleViewController;
/**
* The view controller that is currently on top of the navigation stack.
*
* This differs from visibleViewController in that it ignores things like search
* display controllers which are visible, but not part of navigation.
*/
@property (nonatomic, readonly) UIViewController* topViewController;
/**
* The URL of the currently visible view controller;
*
* Setting this property will open a new URL.
*/
@property (nonatomic, copy) NSString* URL;
/**
* How view controllers are automatically persisted on termination and restored on launch.
*
* @default TTNavigatorPersistenceModeNone
*/
@property (nonatomic) TTNavigatorPersistenceMode persistenceMode;
/**
* The age at which persisted view controllers are too old to be restored.
*
* In some cases, it is a good practice not to restore really old navigation paths, because
* the user probably won't remember how they got there, and would prefer to start from the
* beginning.
*
* Set this to 0 to restore from any age.
*
* @default 0
*/
@property (nonatomic) NSTimeInterval persistenceExpirationAge;
/**
* Causes the current view controller to be reloaded when shaking the phone.
*
* @default NO
*/
@property (nonatomic) BOOL supportsShakeToReload;
/**
* Allows URLs to be opened externally if they don't match any patterns.
*
* @default NO
*/
@property (nonatomic) BOOL opensExternalURLs;
/**
* Indicates that we are asking controllers to delay heavy operations until a later time.
*
* @default NO
*/
@property (nonatomic, readonly) BOOL isDelayed;
+ (TTNavigator*)navigator;
/**
* Load and display the view controller with a pattern that matches the URL.
*
* This method replaces all other openURL methods by using the chainable TTURLAction object.
*
* If there is not yet a rootViewController, the view controller loaded with this URL
* will be assigned as the rootViewController and inserted into the keyWindow. If there is not
* a keyWindow, a UIWindow will be created and displayed.
*
* Example TTURLAction initialization:
* [[TTURLAction actionWithURLPath:@"tt://some/path"]
* applyAnimated:YES]
*
* Each apply* method on the TTURLAction object returns self, allowing you to chain methods
* when initializing the object. This allows for a flexible method that requires a shifting set
* of parameters that have specific defaults. The old openURL* methods are being phased out, so
* please start using openURLAction instead.
*/
- (UIViewController*)openURLAction:(TTURLAction*)URLAction;
/**
* Opens a sequence of URLs.
*
* @return The view controller of the last opened URL.
*/
- (UIViewController*)openURLs:(NSString*)URL,...;
/**
* Gets a view controller for the URL without opening it.
*
* @return The view controller mapped to URL.
*/
- (UIViewController*)viewControllerForURL:(NSString*)URL;
/**
* Gets a view controller for the URL without opening it.
*
* @return The view controller mapped to URL.
*/
- (UIViewController*)viewControllerForURL:(NSString*)URL query:(NSDictionary*)query;
/**
* Gets a view controller for the URL without opening it.
*
* @return The view controller mapped to URL.
*/
- (UIViewController*)viewControllerForURL:(NSString*)URL query:(NSDictionary*)query
pattern:(TTURLPattern**)pattern;
/**
* Tells the navigator to delay heavy operations.
*
* Initializing controllers can be very expensive, so if you are going to do some animation
* while this might be happening, this will tell controllers created through the navigator
* that they should hold off so as not to slow down the operations.
*/
- (void)beginDelay;
/**
* Tells controllers that were created during the delay to finish what they were planning to do.
*/
- (void)endDelay;
/**
* Cancels the delay without notifying delayed controllers.
*/
- (void)cancelDelay;
/**
* Persists all view controllers to user defaults.
*/
- (void)persistViewControllers;
/**
* Restores all view controllers from user defaults and returns the last one.
*/
- (UIViewController*)restoreViewControllers;
/**
* Persists a view controller's state and recursively persists the next view controller after it.
*
* Do not call this directly except from within a view controller that is being directed
* by the app map to persist itself.
*/
- (void)persistController:(UIViewController*)controller path:(NSMutableArray*)path;
/**
* Removes all view controllers from the window and releases them.
*/
- (void)removeAllViewControllers;
/**
* Gets a navigation path which can be used to locate an object.
*/
- (NSString*)pathForObject:(id)object;
/**
* Finds an object using its navigation path.
*/
- (id)objectForPath:(NSString*)path;
/**
* Erases all data stored in user defaults.
*/
- (void)resetDefaults;
/**
* Reloads the content in the visible view controller.
*/
- (void)reload;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@protocol TTNavigatorDelegate <NSObject>
@optional
/**
* Asks if the URL should be opened and allows the delegate to prevent it.
* See -navigator:URLToOpen: for a superset of functionality
*/
- (BOOL)navigator:(TTNavigator*)navigator shouldOpenURL:(NSURL*)URL;
/**
* Asks if the URL should be opened and allows the delegate to return a different URL to open
* instead. A return value of nil indicates the URL should not be opened.
*
* This is a superset of the functionality of -navigator:shouldOpenURL:. Returning YES from that
* method is equivalent to returning URL from this method.
*/
- (NSURL*)navigator:(TTNavigator*)navigator URLToOpen:(NSURL*)URL;
/**
* The URL is about to be opened in a controller.
*
* If the controller argument is nil, the URL is going to be opened externally.
*/
- (void)navigator:(TTNavigator*)navigator willOpenURL:(NSURL*)URL
inViewController:(UIViewController*)controller;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
// global
/**
* Shortcut for calling [[TTNavigator navigator] openURL:]
*/
UIViewController* TTOpenURL(NSString* URL);