Add Resource Loader for iOS

This commit is contained in:
pengfei.zhou
2021-10-25 16:01:44 +08:00
committed by osborn
parent 4bd4f42f52
commit 235549eea4
40 changed files with 979 additions and 242 deletions

View File

@@ -0,0 +1,25 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResource.h"
@interface DoricBase64Resource : DoricResource
@end

View File

@@ -0,0 +1,35 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricBase64Resource.h"
@implementation DoricBase64Resource
- (DoricAsyncResult <NSData *> *)fetchRaw {
DoricAsyncResult *result = [DoricAsyncResult new];
NSString *inString = nil;
if ([self.identifier hasPrefix:@"data:image"]) {
inString = [self.identifier componentsSeparatedByString:@","].lastObject;
}
NSData *data = [[NSData alloc] initWithBase64EncodedString:inString
options:NSDataBase64DecodingIgnoreUnknownCharacters];
[result setupResult:data];
return result;
}
@end

View File

@@ -0,0 +1,25 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResourceLoader.h"
@interface DoricBase64ResourceLoader : NSObject <DoricResourceLoader>
@end

View File

@@ -0,0 +1,31 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricBase64ResourceLoader.h"
#import "DoricBase64Resource.h"
@implementation DoricBase64ResourceLoader
- (NSString *)resourceType {
return @"base64";
}
- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context {
return [[DoricBase64Resource alloc] initWithContext:context identifier:identifier];
}
@end

View File

@@ -0,0 +1,26 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResource.h"
#import "DoricContext.h"
@interface DoricBundleResource : DoricResource
@property(nonatomic, strong) NSBundle *bundle;
@end

View File

@@ -0,0 +1,31 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricBundleResource.h"
@implementation DoricBundleResource
- (DoricAsyncResult <NSData *> *)fetchRaw {
DoricAsyncResult *result = [DoricAsyncResult new];
NSString *path = [self.bundle bundlePath];
NSString *fullPath = [path stringByAppendingPathComponent:self.identifier];
NSData *imgData = [[NSData alloc] initWithContentsOfFile:fullPath];
[result setupResult:imgData];
return result;
}
@end

View File

@@ -0,0 +1,26 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResourceLoader.h"
@interface DoricBundleResourceLoader : NSObject <DoricResourceLoader>
- (instancetype)initWithResourceType:(NSString *)resourceType bundle:(NSBundle *)bundle;
@end

View File

@@ -0,0 +1,45 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <DoricExtensions.h>
#import "DoricBundleResourceLoader.h"
#import "DoricBundleResource.h"
@interface DoricBundleResourceLoader ()
@property(nonatomic, strong) NSBundle *bundle;
@property(nonatomic, copy) NSString *resourceType;
@end
@implementation DoricBundleResourceLoader
- (instancetype)initWithResourceType:(NSString *)resourceType bundle:(NSBundle *)bundle {
if (self = [super init]) {
_resourceType = resourceType;
_bundle = bundle;
}
return self;
}
- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context {
return [[[DoricBundleResource alloc] initWithContext:context identifier:identifier]
also:^(DoricBundleResource *it) {
it.bundle = self.bundle;
}];
}
@end

View File

@@ -0,0 +1,24 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResource.h"
@interface DoricLocalResource : DoricResource
@end

View File

@@ -0,0 +1,30 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricLocalResource.h"
@implementation DoricLocalResource
- (DoricAsyncResult <NSData *> *)fetchRaw {
DoricAsyncResult *result = [DoricAsyncResult new];
NSData *imgData = [[NSData alloc] initWithContentsOfFile:self.identifier];
[result setupResult:imgData];
return result;
}
@end

View File

@@ -0,0 +1,24 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResourceLoader.h"
@interface DoricLocalResourceLoader : NSObject <DoricResourceLoader>
@end

View File

@@ -0,0 +1,32 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricLocalResourceLoader.h"
#import "DoricLocalResource.h"
@implementation DoricLocalResourceLoader
- (NSString *)resourceType {
return @"local";
}
- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context {
return [[DoricLocalResource alloc] initWithContext:context identifier:identifier];
}
@end

View File

@@ -0,0 +1,24 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResource.h"
@interface DoricRemoteResource : DoricResource
@end

View File

@@ -0,0 +1,33 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricRemoteResource.h"
@implementation DoricRemoteResource
- (DoricAsyncResult <NSData *> *)fetchRaw {
DoricAsyncResult *result = [DoricAsyncResult new];
NSURL *url = [NSURL URLWithString:self.identifier];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *urlData = [NSData dataWithContentsOfURL:url];
[result setupResult:urlData];
});
return result;
}
@end

View File

@@ -0,0 +1,25 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import <Foundation/Foundation.h>
#import "DoricResourceLoader.h"
@interface DoricRemoteResourceLoader : NSObject <DoricResourceLoader>
@end

View File

@@ -0,0 +1,31 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/25.
//
#import "DoricRemoteResourceLoader.h"
#import "DoricRemoteResource.h"
@implementation DoricRemoteResourceLoader
- (NSString *)resourceType {
return @"remote";
}
- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context {
return [[DoricRemoteResource alloc] initWithContext:context identifier:identifier];
}
@end

View File

@@ -0,0 +1,32 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/22.
//
#import <Foundation/Foundation.h>
#import "DoricAsyncResult.h"
@class DoricContext;
@interface DoricResource : NSObject
@property(nonatomic, weak) DoricContext *context;
@property(nonatomic, copy) NSString *identifier;
- (instancetype)initWithContext:(DoricContext *)context identifier:(NSString *)identifier;
- (DoricAsyncResult <NSData *> *)fetchRaw;
@end

View File

@@ -0,0 +1,33 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/22.
//
#import "DoricResource.h"
@implementation DoricResource
- (instancetype)initWithContext:(DoricContext *)context identifier:(NSString *)identifier {
if (self = [super init]) {
_context = context;
_identifier = identifier;
}
return self;
}
- (DoricAsyncResult<NSData *> *)fetchRaw {
return nil;
}
@end

View File

@@ -0,0 +1,27 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/22.
//
#import <Foundation/Foundation.h>
#import "DoricResource.h"
@protocol DoricResourceLoader
@property(nonatomic, readonly) NSString *resourceType;
- (__kindof DoricResource *)load:(NSString *)identifier withContext:(DoricContext *)context;
@end

View File

@@ -0,0 +1,31 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/22.
//
#import <Foundation/Foundation.h>
#import "DoricResourceLoader.h"
@interface DoricResourceLoaderManager : NSObject
- (void)registerLoader:(id <DoricResourceLoader>)loader;
- (void)unRegisterLoader:(id <DoricResourceLoader>)loader;
- (__kindof DoricResource *)load:(NSString *)identifier
withResourceType:(NSString *)resourceType
withContext:(DoricContext *)context;
@end

View File

@@ -0,0 +1,49 @@
/*
* Copyright [2021] [Doric.Pub]
*
* 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.
*/
//
// Created by pengfei.zhou on 2021/10/22.
//
#import "DoricResourceLoaderManager.h"
@interface DoricResourceLoaderManager ()
@property(nonatomic, strong) NSMutableDictionary <NSString *, id <DoricResourceLoader>> *loaders;
@end
@implementation DoricResourceLoaderManager
- (instancetype)init {
if (self = [super init]) {
_loaders = [NSMutableDictionary new];
}
return self;
}
- (void)registerLoader:(id <DoricResourceLoader>)loader {
self.loaders[loader.resourceType] = loader;
}
- (void)unRegisterLoader:(id <DoricResourceLoader>)loader {
[self.loaders removeObjectForKey:loader.resourceType];
}
- (__kindof DoricResource *)load:(NSString *)identifier
withResourceType:(NSString *)resourceType
withContext:(DoricContext *)context {
id <DoricResourceLoader> loader = self.loaders[resourceType];
return [loader load:identifier withContext:context];
}
@end