/
TiUIiOSDocumentViewerProxy.m
176 lines (147 loc) Β· 4.12 KB
/
TiUIiOSDocumentViewerProxy.m
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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#if defined(USE_TI_UIIPADDOCUMENTVIEWER) || defined(USE_TI_UIIOSDOCUMENTVIEWER)
#import "TiUIiOSDocumentViewerProxy.h"
#import "TiUtils.h"
#import "TiBlob.h"
#import "TiApp.h"
#import "TiViewProxy.h"
@implementation TiUIiOSDocumentViewerProxy
-(void)_destroy
{
controller.delegate = nil;
RELEASE_TO_NIL(controller);
[super _destroy];
}
-(UIDocumentInteractionController*)controller
{
if (controller==nil)
{
NSURL *url = [TiUtils toURL:[self valueForUndefinedKey:@"url"] proxy:self];
controller = [[UIDocumentInteractionController interactionControllerWithURL:url] retain];
controller.delegate = self;
}
return controller;
}
-(NSString*)apiName
{
return @"Ti.UI.iOS.DocumentViewer";
}
-(void)setAnnotation:(id)args
{
[self controller].annotation = [args objectAtIndex:0];
}
-(void)show:(id)args
{
ENSURE_SINGLE_ARG_OR_NIL(args,NSDictionary);
[self rememberSelf];
ENSURE_UI_THREAD(show, args);
BOOL animated = [TiUtils boolValue:@"animated" properties:args def:YES];
TiViewProxy* view = [args objectForKey:@"view"];
if (view!=nil)
{
if ([view supportsNavBarPositioning])
{
UIBarButtonItem *item = [view barButtonItem];
[[self controller] presentOptionsMenuFromBarButtonItem:item animated:animated];
return;
}
CGRect rect = [TiUtils rectValue:args];
[[self controller] presentOptionsMenuFromRect:rect inView:[view view] animated:animated];
return;
}
[[self controller] presentPreviewAnimated:animated];
}
-(void)hide:(id)args
{
ENSURE_TYPE_OR_NIL(args,NSDictionary);
ENSURE_UI_THREAD(hide, args);
BOOL animated = [TiUtils boolValue:@"animated" properties:args def:YES];
[[self controller] dismissPreviewAnimated:animated];
}
-(id)url
{
if (controller!=nil)
{
return [[[self controller] URL] absoluteString];
}
return nil;
}
-(void)setUrl:(id)value
{
ENSURE_TYPE(value,NSString);
NSURL *url = [TiUtils toURL:value proxy:self];
//UIDocumenactionController is recommended to be a new instance for every different url
//instead of having titanium developer create a new instance every time a new document url is loaded
//we assume that setUrl is called to change doc, so we go ahead and release the controller and create
//a new one when asked to present
RELEASE_TO_NIL(controller);
[self replaceValue:url forKey:@"url" notification:NO];
}
-(id)icons
{
NSMutableArray *result = [NSMutableArray array];
for (UIImage *image in [self controller].icons)
{
TiBlob *blob = [[TiBlob alloc] initWithImage:image];
[result addObject:image];
[blob release];
}
return result;
}
-(id)name
{
if (controller!=nil)
{
return [controller name];
}
return nil;
}
#pragma mark Delegates
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
UIViewController *ac = [[TiApp app] controller];
return ac;
}
/*
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller
{
return viewController.view;
}*/
- (void)documentInteractionControllerWillBeginPreview:(UIDocumentInteractionController *)controller
{
if ([self _hasListeners:@"load"])
{
[self fireEvent:@"load" withObject:nil];
}
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
if ([self _hasListeners:@"unload"])
{
[self fireEvent:@"unload" withObject:nil];
}
[self forgetSelf];
}
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller
{
if ([self _hasListeners:@"menu"])
{
NSDictionary *event = [NSDictionary dictionaryWithObject:@"open" forKey:@"type"];
[self fireEvent:@"menu" withObject:event];
}
}
- (void)documentInteractionControllerWillPresentOptionsMenu:(UIDocumentInteractionController *)controller
{
if ([self _hasListeners:@"menu"])
{
NSDictionary *event = [NSDictionary dictionaryWithObject:@"options" forKey:@"type"];
[self fireEvent:@"menu" withObject:event];
}
}
@end
#endif