Files
rive-ios/Source/Renderer/CDNFileAssetLoader.mm
luigi-rosso 27de5abf4b Ios out of band
few bits to sort out
- [x] make our mix of simulator/emulator consistent, settling on emulator
- [x] passing the factory in works great for just in time asset decoding, but its not amazing when you want to decode ahead of time.
- [x] couple of places left to pass this function signature through. (Question) is there a neater way to get this done, feels a bit like we are going back to parameter explosion a bit?
- [x] should do a few examples, i think the complexity grows quite a bit in this one as you add caching, or callbacks
- [x] should get the cached images/fonts to draw on init as well, either warming up cache, or jitting
- [x] examples loading assets from the bundle (also there seem to be actual asset things too? should we use those?!)
- [x] add test
- [x] re-add "preview" project & rev the preview project once this has been deployed. (do this after new ios deploy)
- [x] fix up race condition (see comment)

https://github.com/rive-app/rive/assets/1216025/2c14330f-e8a4-481b-bc27-4807cabe3b82

(simple example, both swift ui and standard)

![CleanShot 2023-11-20 at 16 54 59](https://github.com/rive-app/rive/assets/1216025/a71e207c-30ad-44dd-9e4b-ad7431b22186)

Diffs=
fabb7f97f Ios out of band (#6232)

Co-authored-by: Gordon Hayes <pggordonhayes@gmail.com>
Co-authored-by: Maxwell Talbot <talbot.maxwell@gmail.com>
2023-12-05 21:23:08 +00:00

109 lines
2.7 KiB
Plaintext

//
// CDNFileAssetLoader.m
// RiveRuntime
//
// Created by Maxwell Talbot on 07/11/2023.
// Copyright © 2023 Rive. All rights reserved.
//
#import <RiveFileAsset.h>
#import <RiveFactory.h>
#import <CDNFileAssetLoader.h>
@implementation CDNFileAssetLoader
{}
- (bool)loadContentsWithAsset:(RiveFileAsset*)asset
andData:(NSData*)data
andFactory:(RiveFactory*)factory
{
// TODO: Error handling
// TODO: Track tasks, so we can cancel them if we garbage collect the asset loader
if ([[asset cdnUuid] length] > 0)
{
NSURL* URL = [NSURL
URLWithString:[NSString
stringWithFormat:@"%@/%@", [asset cdnBaseUrl], [asset cdnUuid]]];
NSURLSessionTask* task = [[NSURLSession sharedSession]
downloadTaskWithURL:URL
completionHandler:^(NSURL* location, NSURLResponse* response, NSError* error) {
if (!error)
{
// Load the data into the reader
NSData* data = [NSData dataWithContentsOfURL:location];
if ([asset isKindOfClass:[RiveFontAsset class]])
{
[(RiveFontAsset*)asset font:[factory decodeFont:data]];
}
else if ([asset isKindOfClass:[RiveImageAsset class]])
{
[(RiveImageAsset*)asset renderImage:[factory decodeImage:data]];
}
}
}];
// Kick off the http download
// QUESTION: Do we need to tie this into the RiveFile so we can wait for these loads to be
// completed?
[task resume];
return true;
}
return false;
}
@end
@implementation FallbackFileAssetLoader
{
NSMutableArray* loaders;
}
- (instancetype)init
{
self = [super init];
loaders = [NSMutableArray array];
return self;
}
- (void)addLoader:(RiveFileAssetLoader*)loader
{
[loaders addObject:loader];
}
- (bool)loadContentsWithAsset:(RiveFileAsset*)asset
andData:(NSData*)data
andFactory:(RiveFactory*)factory
{
for (RiveFileAssetLoader* loader in loaders)
{
if ([loader loadContentsWithAsset:asset andData:data andFactory:factory])
{
return true;
}
}
return false;
}
@end
@implementation CustomFileAssetLoader
- (instancetype)initWithLoader:(LoadAsset)loader
{
self = [super init];
_loadAsset = loader;
return self;
}
- (bool)loadContentsWithAsset:(RiveFileAsset*)asset
andData:(NSData*)data
andFactory:(RiveFactory*)factory
{
return _loadAsset(asset, data, factory);
}
@end