forked from dev5tec/FBMediaViewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBMediaViewerInnerView.m
179 lines (148 loc) · 5.08 KB
/
FBMediaViewerInnerView.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
177
178
179
//
// Copyright (c) 2012 Five-technology Co.,Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "FBMediaViewerInnerView.h"
#import "FBMediaViewerView.h"
#import "FBMediaViewerItem.h"
#import "FBMediaViewerItemLoader.h"
#import "FBMediaViewerLoadingView.h"
#import "FBMediaViewerView.h"
// Inner Views
#import "FBMediaViewerGenericLoadingView.h"
#import "FBMediaViewerRendererImageView.h"
// Renderers
#import "FBMediaViewerRendererFactory.h"
//--
//#import "FBMediaViewerRendererWebView.h"
@interface FBMediaViewerInnerView()
@property (nonatomic, assign) FBMediaViewerView* mediaViewerView;
@property (nonatomic, strong) UIView <FBMediaViewerRenderer> *renderer;
@property (nonatomic, strong) UIView <FBMediaViewerLoadingView> *loadingDialogView;
@property (nonatomic, assign) BOOL displaying;
@end
@implementation FBMediaViewerInnerView
#pragma mark -
#pragma mark Privates
- (void)_setupRenderWithURL:(NSURL*)url
{
if (self.renderer) {
[self.renderer removeFromSuperview];
self.renderer = nil;
}
/*
self.renderer = [[FBMediaViewerRendererWebView alloc] initWithFrame:self.bounds];
// self.renderer = [[FBMediaViewerRendererImageView alloc] initWithFrame:self.bounds];
*/
self.renderer = [FBMediaViewerRendererFactory rendererForURL:url frame:self.bounds];
self.renderer.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self insertSubview:self.renderer belowSubview:self.loadingDialogView];
}
#pragma mark -
#pragma mark Basics
- (id)initWithMediaViewerView:(FBMediaViewerView*)view frame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.mediaViewerView = view;
self.loadingDialogView = [FBMediaViewerGenericLoadingView loadingView];
self.loadingDialogView.hidden = YES;
[self.loadingDialogView layoutInFrame:self.frame];
[self addSubview:self.loadingDialogView];
self.frame = frame;
}
return self;
}
#pragma mark -
#pragma mark Properites
- (void)setFrame:(CGRect)frame
{
[super setFrame:frame];
frame.origin = CGPointZero;
self.renderer.frame = frame;
[self.loadingDialogView layoutInFrame:frame];
}
- (void)setMediaViewerItem:(id<FBMediaViewerItem>)mediaViewerItem
{
if (_mediaViewerItem) {
[self cancel];
self.loadingDialogView.hidden = YES;
}
_mediaViewerItem = mediaViewerItem;
[self _setupRenderWithURL:mediaViewerItem.contentURL];
if (mediaViewerItem && !mediaViewerItem.loadWhenAppearing) {
if (mediaViewerItem.localFileURL) {
[self.renderer renderContentOfURL:self.mediaViewerItem.localFileURL];
self.displaying = YES;
} else {
[self.renderer clear];
self.displaying = NO;
}
} else {
[self.renderer clear];
self.displaying = NO;
}
}
#pragma mark -
#pragma mark API
- (void)loadWithMode:(FBMeditViewerItemLoaderMode)mode
{
[self.renderer clear];
if (self.mediaViewerView.itemLoader) {
self.loadingDialogView.title = self.mediaViewerItem.name;
self.loadingDialogView.progress = 0.0f;
self.loadingDialogView.hidden = NO;
[self.mediaViewerView.itemLoader
loadWithMediaViewerItem:self.mediaViewerItem
mode:mode
loading:^(NSUInteger loadedSize) {
self.loadingDialogView.progress = (CGFloat)loadedSize / (CGFloat)self.mediaViewerItem.size;
}
completion:^(BOOL canceled) {
if (!canceled) {
self.loadingDialogView.hidden = YES;
[self.renderer renderContentOfURL:self.mediaViewerItem.localFileURL];
}
}
failed:^{
}];
} else {
// nothing
}
}
- (void)cancel
{
[self.mediaViewerView.itemLoader
cancelWithMediaViewerItem:self.mediaViewerItem];
}
- (void)willAppear
{
if (!self.displaying) {
[self loadWithMode:FBMeditViewerItemLoaderModeLoadFromCache];
self.displaying = YES;
}
[self.renderer willAppear];
}
- (void)willDisAppear
{
[self.renderer willDisAppear];
}
@end