Skip to content

Commit

Permalink
Fabric: Add missing source properties of Image (#46460)
Browse files Browse the repository at this point in the history
Summary:
Fabric: Add missing source properties of Image

## Changelog:

[IOS] [FIXED] - Fabric: Add missing source properties of Image

Pull Request resolved: #46460

Test Plan: Missing props should worked.

Reviewed By: javache

Differential Revision: D62847594

Pulled By: cipolleschi

fbshipit-source-id: 83d7c836a2e96c9053aa94e0e9b8e706103d026f
  • Loading branch information
zhongwuzw authored and facebook-github-bot committed Sep 19, 2024
1 parent a026d6d commit 052def0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,29 @@ inline void fromRawValue(
result.headers.push_back(header);
}
}

if (items.find("body") != items.end() &&
items.at("body").hasType<std::string>()) {
result.body = (std::string)items.at("body");
}

if (items.find("method") != items.end() &&
items.at("method").hasType<std::string>()) {
result.method = (std::string)items.at("method");
}

if (items.find("cache") != items.end() &&
items.at("cache").hasType<std::string>()) {
auto cache = (std::string)items.at("cache");
if (cache == "reload") {
result.cache = ImageSource::CacheStategy::Reload;
} else if (cache == "force-cache") {
result.cache = ImageSource::CacheStategy::ForceCache;
} else if (cache == "only-if-cached") {
result.cache = ImageSource::CacheStategy::OnlyIfCached;
}
}

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <UIKit/UIKit.h>

#import <React/RCTConvert.h>
#import <React/RCTImageLoader.h>
#import <react/renderer/imagemanager/primitives.h>

Expand Down Expand Up @@ -44,6 +45,25 @@ inline std::string toString(const facebook::react::ImageResizeMode &value)
}
}

inline static NSURLRequestCachePolicy NSURLRequestCachePolicyFromImageSource(
const facebook::react::ImageSource &imageSource)
{
switch (imageSource.cache) {
case facebook::react::ImageSource::CacheStategy::Reload:
return NSURLRequestReloadIgnoringLocalCacheData;
break;
case facebook::react::ImageSource::CacheStategy::ForceCache:
return NSURLRequestReturnCacheDataElseLoad;
break;
case facebook::react::ImageSource::CacheStategy::OnlyIfCached:
return NSURLRequestReturnCacheDataDontLoad;
break;
default:
return NSURLRequestUseProtocolCachePolicy;
break;
}
}

inline static NSURL *NSURLFromImageSource(const facebook::react::ImageSource &imageSource)
{
// `NSURL` has a history of crashing with bad input, so let's be safe.
Expand Down Expand Up @@ -102,13 +122,21 @@ inline static NSURLRequest *NSURLRequestFromImageSource(const facebook::react::I

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

/*
// TODO(shergin): To be implemented.
request.HTTPBody = ...;
request.HTTPMethod = ...;
request.cachePolicy = ...;
request.allHTTPHeaderFields = ...;
*/
NSString *method = @"GET";
if (!imageSource.method.empty()) {
method = [[NSString alloc] initWithUTF8String:imageSource.method.c_str()].uppercaseString;
}
NSData *body = nil;
if (!imageSource.body.empty()) {
body = [NSData dataWithBytes:imageSource.body.c_str() length:imageSource.body.size()];
}
NSURLRequestCachePolicy cachePolicy = NSURLRequestCachePolicyFromImageSource(imageSource);

if ([method isEqualToString:@"GET"] && imageSource.headers.empty() && body == nil &&
cachePolicy == NSURLRequestUseProtocolCachePolicy) {
return request;
}

for (const auto &header : imageSource.headers) {
NSString *key = [NSString stringWithUTF8String:header.first.c_str()];
NSString *value = [NSString stringWithUTF8String:header.second.c_str()];
Expand All @@ -117,5 +145,9 @@ inline static NSURLRequest *NSURLRequestFromImageSource(const facebook::react::I
}
}

return [request copy];
request.HTTPBody = body;
request.HTTPMethod = method;
request.cachePolicy = cachePolicy;

return request;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ namespace facebook::react {
class ImageSource {
public:
enum class Type { Invalid, Remote, Local };
enum class CacheStategy { Default, Reload, ForceCache, OnlyIfCached };

Type type{};
std::string uri{};
std::string bundle{};
Float scale{3};
Size size{0};
std::string body{};
std::string method{};
CacheStategy cache = CacheStategy::Default;
std::vector<std::pair<std::string, std::string>> headers{};

bool operator==(const ImageSource& rhs) const {
Expand Down

0 comments on commit 052def0

Please sign in to comment.