Skip to content

Commit cf33104

Browse files
committed
add FirebaseUI Storage
1 parent 824a1d2 commit cf33104

File tree

8 files changed

+8071
-3191
lines changed

8 files changed

+8071
-3191
lines changed

FirebaseStorageUI/FirebaseStorageUI.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
18+
#import <UIKit/UIKit.h>
19+
20+
//! Project version number for FirebaseStorageUI.
21+
FOUNDATION_EXPORT double FirebaseStorageUIVersionNumber;
22+
23+
//! Project version string for FirebaseStorageUI.
24+
FOUNDATION_EXPORT const unsigned char FirebaseStorageUIVersionString[];
25+
26+
// In this header, you should import all the public headers of your framework using statements like #import <FirebaseStorageUI/PublicHeader.h>
27+
28+

FirebaseStorageUI/Info.plist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>FMWK</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>$(CURRENT_PROJECT_VERSION)</string>
21+
<key>NSPrincipalClass</key>
22+
<string></string>
23+
</dict>
24+
</plist>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
@import UIKit;
18+
@import SDWebImage;
19+
@import FirebaseStorage;
20+
21+
NS_ASSUME_NONNULL_BEGIN
22+
23+
@interface UIImageView (FirebaseStorage)
24+
25+
/**
26+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
27+
*
28+
* @param storageRef A Firebase Storage reference containing an image.
29+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
30+
* could not be found in cache).
31+
*/
32+
- (nullable FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef;
33+
34+
/**
35+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
36+
*
37+
* @param storageRef A Firebase Storage reference containing an image.
38+
* @param placeholder An image to display while the download is in progress.
39+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
40+
* could not be found in cache).
41+
*/
42+
- (nullable FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef
43+
placeholderImage:(UIImage *)placeholder;
44+
45+
/**
46+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
47+
*
48+
* @param storageRef A Firebase Storage reference containing an image.
49+
* @param placeholder An image to display while the download is in progress.
50+
* @param completion A closure to handle events when the image finishes downloading.
51+
* The closure is not guaranteed to be invoked on the main thread.
52+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
53+
* could not be found in cache).
54+
*/
55+
- (nullable FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef
56+
placeholderImage:(nullable UIImage *)placeholder
57+
completion:(void (^_Nullable)(UIImage *_Nullable,
58+
NSError *_Nullable,
59+
SDImageCacheType,
60+
FIRStorageReference *))completion;
61+
62+
/**
63+
* Sets the image view's image to an image downloaded from the Firebase Storage reference.
64+
*
65+
* @param storageRef A Firebase Storage reference containing an image.
66+
* @param size The maximum size of the downloaded image. If the downloaded image
67+
* exceeds this size, an error will be raised in the completion block.
68+
* @param placeholder An image to display while the download is in progress.
69+
* @param completion A closure to handle events when the image finishes downloading.
70+
* The closure is not guaranteed to be invoked on the main thread.
71+
* @return Returns a FIRStorageDownloadTask if a download was created (i.e. image
72+
* could not be found in cache).
73+
*/
74+
- (nullable FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef
75+
maxImageSize:(UInt64)size
76+
placeholderImage:(nullable UIImage *)placeholder
77+
completion:(void (^_Nullable)(UIImage *_Nullable,
78+
NSError *_Nullable,
79+
SDImageCacheType,
80+
FIRStorageReference *))completion;
81+
82+
@end
83+
84+
NS_ASSUME_NONNULL_END
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//
2+
// Copyright (c) 2016 Google Inc.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
#import <objc/runtime.h>
18+
19+
#import "UIImageView+FirebaseStorage.h"
20+
21+
@interface UIImageView (FirebaseStorage_Private)
22+
@property (nonatomic, readwrite, nullable) FIRStorageDownloadTask *currentDownload;
23+
@end
24+
25+
@implementation UIImageView (FirebaseStorage)
26+
27+
- (FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef {
28+
return [self fui_setImageWithStorageReference:storageRef placeholderImage:nil completion:nil];
29+
}
30+
31+
- (FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef
32+
placeholderImage:(UIImage *)placeholder {
33+
return [self fui_setImageWithStorageReference:storageRef placeholderImage:placeholder completion:nil];
34+
}
35+
36+
- (FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef
37+
placeholderImage:(UIImage *)placeholder
38+
completion:(void (^)(UIImage * _Nullable,
39+
NSError * _Nullable,
40+
SDImageCacheType,
41+
FIRStorageReference * _Nonnull))completion {
42+
return [self fui_setImageWithStorageReference:storageRef
43+
maxImageSize:5e6 // 5 megabytes
44+
placeholderImage:placeholder
45+
completion:completion];
46+
}
47+
48+
- (FIRStorageDownloadTask *)fui_setImageWithStorageReference:(FIRStorageReference *)storageRef
49+
maxImageSize:(UInt64)size
50+
placeholderImage:(nullable UIImage *)placeholder
51+
completion:(void (^)(UIImage *,
52+
NSError *,
53+
SDImageCacheType,
54+
FIRStorageReference *))completion {
55+
NSParameterAssert(storageRef != nil);
56+
self.image = placeholder;
57+
58+
// If there's already a download on this UIImageView, cancel it
59+
if (self.currentDownload != nil) {
60+
[self.currentDownload cancel];
61+
self.currentDownload = nil;
62+
}
63+
64+
// Query cache for image before trying to download
65+
SDImageCache *cache = [SDImageCache sharedImageCache];
66+
NSString *key = storageRef.fullPath;
67+
UIImage *cached = nil;
68+
69+
cached = [cache imageFromMemoryCacheForKey:key];
70+
if (cached != nil) {
71+
self.image = cached;
72+
if (completion != nil) {
73+
completion(cached, nil, SDImageCacheTypeMemory, storageRef);
74+
}
75+
return nil;
76+
}
77+
78+
cached = [cache imageFromDiskCacheForKey:key];
79+
if (cached != nil) {
80+
self.image = cached;
81+
if (completion != nil) {
82+
completion(cached, nil, SDImageCacheTypeDisk, storageRef);
83+
}
84+
return nil;
85+
}
86+
87+
// If nothing was found in cache, download the image from Firebase Storage
88+
FIRStorageDownloadTask *download = [storageRef dataWithMaxSize:size
89+
completion:^(NSData * _Nullable data,
90+
NSError * _Nullable error) {
91+
self.currentDownload = nil;
92+
if (data != nil) {
93+
dispatch_async(dispatch_get_main_queue(), ^{
94+
UIImage *image = [UIImage imageWithData:data];
95+
self.image = image;
96+
97+
// Cache downloaded image
98+
[cache storeImage:image forKey:storageRef.fullPath];
99+
100+
if (completion != nil) {
101+
completion(image, nil, SDImageCacheTypeNone, storageRef);
102+
}
103+
});
104+
} else {
105+
if (completion != nil) {
106+
completion(nil, error, SDImageCacheTypeNone, storageRef);
107+
}
108+
}
109+
}];
110+
self.currentDownload = download;
111+
return download;
112+
}
113+
114+
#pragma mark - Accessors
115+
116+
- (void)setCurrentDownload:(FIRStorageDownloadTask *)currentDownload {
117+
objc_setAssociatedObject(self,
118+
@selector(currentDownload),
119+
currentDownload,
120+
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
121+
}
122+
123+
- (FIRStorageDownloadTask *)currentDownload {
124+
return objc_getAssociatedObject(self, @selector(currentDownload));
125+
}
126+
127+
@end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// FirebaseStorageUITests.m
3+
// FirebaseStorageUITests
4+
//
5+
// Created by Morgan Chen on 9/21/16.
6+
// Copyright © 2016 FirebaseUI. All rights reserved.
7+
//
8+
9+
#import <XCTest/XCTest.h>
10+
11+
@interface FirebaseStorageUITests : XCTestCase
12+
13+
@end
14+
15+
@implementation FirebaseStorageUITests
16+
17+
- (void)setUp {
18+
[super setUp];
19+
// Put setup code here. This method is called before the invocation of each test method in the class.
20+
}
21+
22+
- (void)tearDown {
23+
// Put teardown code here. This method is called after the invocation of each test method in the class.
24+
[super tearDown];
25+
}
26+
27+
- (void)testExample {
28+
// This is an example of a functional test case.
29+
// Use XCTAssert and related functions to verify your tests produce the correct results.
30+
}
31+
32+
- (void)testPerformanceExample {
33+
// This is an example of a performance test case.
34+
[self measureBlock:^{
35+
// Put the code you want to measure the time of here.
36+
}];
37+
}
38+
39+
@end

FirebaseStorageUITests/Info.plist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)