This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
/
TTGlobal.h
315 lines (237 loc) · 8.77 KB
/
TTGlobal.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "Three20/NSObjectAdditions.h"
#import "Three20/NSStringAdditions.h"
#import "Three20/NSDateAdditions.h"
#import "Three20/NSArrayAdditions.h"
#import "Three20/UIColorAdditions.h"
#import "Three20/UIFontAdditions.h"
#import "Three20/UIImageAdditions.h"
#import "Three20/UIViewControllerAdditions.h"
#import "Three20/UIViewAdditions.h"
#import "Three20/UIWindowAdditions.h"
#import "Three20/UITableViewAdditions.h"
#import "Three20/UIWebViewAdditions.h"
#import "Three20/UIToolbarAdditions.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
// Logging Helpers
#ifdef DEBUG
#define TTLOG NSLog
#else
#define TTLOG
#endif
#define TTLOGRECT(rect) \
TTLOG(@"%s x=%f, y=%f, w=%f, h=%f", #rect, rect.origin.x, rect.origin.y, \
rect.size.width, rect.size.height)
#define TTLOGPOINT(pt) \
TTLOG(@"%s x=%f, y=%f", #pt, pt.x, pt.y)
#define TTLOGSIZE(size) \
TTLOG(@"%s w=%f, h=%f", #size, size.width, size.height)
#define TTLOGEDGES(edges) \
TTLOG(@"%s left=%f, right=%f, top=%f, bottom=%f", #edges, edges.left, edges.right, \
edges.top, edges.bottom)
#define TTLOGHSV(_COLOR) \
TTLOG(@"%s h=%f, s=%f, v=%f", #_COLOR, _COLOR.hue, _COLOR.saturation, _COLOR.value)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Errors
#define TT_ERROR_DOMAIN @"three20.net"
#define TT_EC_INVALID_IMAGE 101
///////////////////////////////////////////////////////////////////////////////////////////////////
// Dimensions of common iPhone OS Views
#define TT_ROW_HEIGHT 44
#define TT_TOOLBAR_HEIGHT 44
#define TT_LANDSCAPE_TOOLBAR_HEIGHT 32
#define TT_KEYBOARD_HEIGHT 216
#define TT_LANDSCAPE_KEYBOARD_HEIGHT 160
#define TT_ROUNDED -1
#define TABLE_GROUPED_PADDING 10
///////////////////////////////////////////////////////////////////////////////////////////////////
// Color helpers
#define RGBCOLOR(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define HSVCOLOR(h,s,v) [UIColor colorWithHue:h saturation:s value:v alpha:1]
#define HSVACOLOR(h,s,v,a) [UIColor colorWithHue:h saturation:s value:v alpha:a]
#define RGBA(r,g,b,a) r/255.0, g/255.0, b/255.0, a
///////////////////////////////////////////////////////////////////////////////////////////////////
// Style helpers
#define TTSTYLE(_SELECTOR) [[TTStyleSheet globalStyleSheet] styleWithSelector:@#_SELECTOR]
#define TTSTYLESTATE(_SELECTOR, _STATE) [[TTStyleSheet globalStyleSheet] \
styleWithSelector:@#_SELECTOR forState:_STATE]
#define TTSTYLESHEET ((id)[TTStyleSheet globalStyleSheet])
#define TTSTYLEVAR(_VARNAME) [TTSTYLESHEET _VARNAME]
#define TTLOGVIEWS(_VIEW) \
{ for (UIView* view = _VIEW; view; view = view.superview) { TTLOG(@"%@", view); } }
#define TTIMAGE(_URL) [[TTURLCache sharedCache] imageForURL:_URL]
typedef enum {
TTPositionStatic,
TTPositionAbsolute,
TTPositionFloatLeft,
TTPositionFloatRight,
} TTPosition;
///////////////////////////////////////////////////////////////////////////////////////////////////
// Networking
typedef enum {
TTURLRequestCachePolicyNone = 0,
TTURLRequestCachePolicyMemory = 1,
TTURLRequestCachePolicyDisk = 2,
TTURLRequestCachePolicyNetwork = 4,
TTURLRequestCachePolicyAny
= (TTURLRequestCachePolicyMemory|TTURLRequestCachePolicyDisk|TTURLRequestCachePolicyNetwork),
TTURLRequestCachePolicyNoCache = 8,
TTURLRequestCachePolicyDefault = TTURLRequestCachePolicyAny,
} TTURLRequestCachePolicy;
#define TT_DEFAULT_CACHE_INVALIDATION_AGE (60*60*24) // 1 day
#define TT_DEFAULT_CACHE_EXPIRATION_AGE (60*60*24*7) // 1 week
///////////////////////////////////////////////////////////////////////////////////////////////////
// Time
#define TT_MINUTE 60
#define TT_HOUR (60*TT_MINUTE)
#define TT_DAY (24*TT_HOUR)
#define TT_WEEK (7*TT_DAY)
#define TT_MONTH (30.5*TT_DAY)
#define TT_YEAR (365*TT_DAY)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Animation
/**
* The standard duration for transition animations.
*/
#define TT_TRANSITION_DURATION 0.3
#define TT_FAST_TRANSITION_DURATION 0.2
#define TT_FLIP_TRANSITION_DURATION 0.7
///////////////////////////////////////////////////////////////////////////////////////////////////
#define TT_RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
#define TT_AUTORELEASE_SAFELY(__POINTER) { [__POINTER autorelease]; __POINTER = nil; }
#define TT_INVALIDATE_TIMER(__TIMER) { [__TIMER invalidate]; __TIMER = nil; }
/**
* Creates a mutable array which does not retain references to the objects it contains.
*/
NSMutableArray* TTCreateNonRetainingArray();
/**
* Creates a mutable dictionary which does not retain references to the values it contains.
*/
NSMutableDictionary* TTCreateNonRetainingDictionary();
/**
* Tests if an object is an array which is empty.
*/
BOOL TTIsEmptyArray(NSObject* object);
/**
* Tests if an object is an set which is empty.
*/
BOOL TTIsEmptySet(NSObject* object);
/**
* Tests if an object is a string which is empty.
*/
BOOL TTIsEmptyString(NSObject* object);
/**
* Tests if the keyboard is visible.
*/
BOOL TTIsKeyboardVisible();
/**
* Gets the current device orientation.
*/
UIDeviceOrientation TTDeviceOrientation();
/**
* Gets the current interface orientation.
*/
UIInterfaceOrientation TTInterfaceOrientation();
/**
* Checks if the orientation is portrait, landscape left, or landscape right.
*
* This helps to ignore upside down and flat orientations.
*/
BOOL TTIsSupportedOrientation(UIInterfaceOrientation orientation);
/**
* Gets the bounds of the screen with device orientation factored in.
*/
CGRect TTScreenBounds();
/**
* Gets the application frame.
*/
CGRect TTApplicationFrame();
/**
* Gets the application frame below the navigation bar.
*/
CGRect TTNavigationFrame();
/**
* Gets the application frame below the navigation bar and above a toolbar.
*/
CGRect TTToolbarNavigationFrame();
/**
* The height of the area containing the status bar and possibly the in-call status bar.
*/
CGFloat TTStatusHeight();
/**
* The height of the area containing the status bar and navigation bar.
*/
CGFloat TTBarsHeight();
/**
* Returns a rectangle that is smaller or larger than the source rectangle.
*/
CGRect TTRectContract(CGRect rect, CGFloat dx, CGFloat dy);
/**
* Returns a rectangle whose edges have been moved a distance and shortened by that distance.
*/
CGRect TTRectShift(CGRect rect, CGFloat dx, CGFloat dy);
/**
* Returns a rectangle whose edges have been added to the insets.
*/
CGRect TTRectInset(CGRect rect, UIEdgeInsets insets);
/**
* Increment the number of active network requests.
*
* The status bar activity indicator will be spinning while there are active requests.
*/
void TTNetworkRequestStarted();
/**
* Decrement the number of active network requests.
*
* The status bar activity indicator will be spinning while there are active requests.
*/
void TTNetworkRequestStopped();
/**
* A convenient way to show a UIAlertView with a message;
*/
void TTAlert(NSString* message);
void TTAlertError(NSString* message);
/**
* Gets the current runtime version of iPhone OS.
*/
float TTOSVersion();
/**
* Checks if the link-time version of the OS is at least a certain version.
*/
BOOL TTOSVersionIsAtLeast(float version);
/**
* Gets the current system locale chosen by the user.
*
* This is necessary because [NSLocale currentLocale] always returns en_US.
*/
NSLocale* TTCurrentLocale();
/**
* Returns a localized string from the Three20 bundle.
*/
NSString* TTLocalizedString(NSString* key, NSString* comment);
NSString* TTDescriptionForError(NSError* error);
NSString* TTFormatInteger(NSInteger num);
BOOL TTIsBundleURL(NSString* URL);
BOOL TTIsDocumentsURL(NSString* URL);
NSString* TTPathForBundleResource(NSString* relativePath);
NSString* TTPathForDocumentsResource(NSString* relativePath);
void TTSwapMethods(Class cls, SEL originalSel, SEL newSel);
///////////////////////////////////////////////////////////////////////////////////////////////////
@protocol TTPersistable <NSObject>
@property(nonatomic,readonly) NSString* viewURL;
+ (id<TTPersistable>)fromURL:(NSURL*)URL;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
@protocol TTLoadable <NSObject>
@property(nonatomic,readonly) NSMutableArray* delegates;
@property(nonatomic,readonly) NSDate* loadedTime;
@property(nonatomic,readonly) BOOL isLoaded;
@property(nonatomic,readonly) BOOL isLoading;
@property(nonatomic,readonly) BOOL isLoadingMore;
@property(nonatomic,readonly) BOOL isOutdated;
@property(nonatomic,readonly) BOOL isEmpty;
- (void)invalidate:(BOOL)erase;
- (void)cancel;
@end