Skip to content

Commit

Permalink
refactor(ios): inefficient code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg committed Nov 19, 2024
1 parent 56b7dfe commit a0bb35f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 57 deletions.
45 changes: 24 additions & 21 deletions modules/ios/domutils/HippyDomUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,33 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "HippyDefines.h"

#include <memory>

namespace hippy {
inline namespace dom {
struct LayoutResult;
class DomNode;
};
};
#include "dom/dom_listener.h"
#include "dom/dom_node.h"
#include "footstone/hippy_value.h"

NS_ASSUME_NONNULL_BEGIN

HIPPY_EXTERN CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result);

HIPPY_EXTERN UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result);

HIPPY_EXTERN CGSize CGSizeMakeFromLayoutResult(hippy::LayoutResult result);

HIPPY_EXTERN CGRect CGRectMakeFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode);

HIPPY_EXTERN NSDictionary *StylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode);
/// CGRect from hippy::LayoutResult
/// - Parameter result: CGRect
static inline CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result) {
return CGRectMake(result.left, result.top, result.width, result.height);
}

/// UIEdgeInsets from hippy::LayoutResult
/// - Parameter result: UIEdgeInsets
static inline UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result) {
return UIEdgeInsetsMake(result.paddingTop, result.paddingLeft, result.paddingBottom, result.paddingRight);
}

/// CGSize from hippy::LayoutResult
/// - Parameter result: CGSize
static inline CGSize CGSizeMakeFromLayoutResult(hippy::LayoutResult result) {
return CGSizeMake(result.width, result.height);
}

/// OC Props from hippy::DomNode
/// - Parameter domNode: hippy::DomNode
HIPPY_EXTERN NSDictionary *HippyStylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode);

NS_ASSUME_NONNULL_END
39 changes: 8 additions & 31 deletions modules/ios/domutils/HippyDomUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,27 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "HippyDomUtils.h"
#import "HippyFootstoneUtils.h"

#include "dom/dom_listener.h"
#include "dom/dom_node.h"
#include "footstone/hippy_value.h"

CGRect CGRectMakeFromLayoutResult(hippy::LayoutResult result) {
return CGRectMake(result.left, result.top, result.width, result.height);
}

UIEdgeInsets UIEdgeInsetsFromLayoutResult(hippy::LayoutResult result) {
return UIEdgeInsetsMake(result.paddingTop, result.paddingLeft, result.paddingBottom, result.paddingRight);
}

CGSize CGSizeMakeFromLayoutResult(hippy::LayoutResult result) {
return CGSizeMake(result.width, result.height);
}

CGRect CGRectMakeFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode) {
return CGRectMakeFromLayoutResult(domNode->GetLayoutResult());
}

NSDictionary *StylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode) {
NSDictionary *HippyStylesFromDomNode(const std::shared_ptr<hippy::DomNode> &domNode) {
auto &styles = domNode->GetStyleMap();
auto &extStyles = domNode->GetExtStyle();
auto capacity = 0;

if (styles) {
capacity += styles->size();
capacity += styles->size();
}
if (extStyles) {
capacity += extStyles->size();
capacity += extStyles->size();
}
NSMutableDictionary *allStyles = [NSMutableDictionary dictionaryWithCapacity:capacity];
if (styles) {
NSDictionary *dicStyles = UnorderedMapDomValueToDictionary(styles);
[allStyles addEntriesFromDictionary:dicStyles];
NSDictionary *dicStyles = UnorderedMapDomValueToDictionary(styles);
[allStyles addEntriesFromDictionary:dicStyles];
}
if (extStyles) {
NSDictionary *dicExtStyles = UnorderedMapDomValueToDictionary(extStyles);
[allStyles addEntriesFromDictionary:dicExtStyles];
NSDictionary *dicExtStyles = UnorderedMapDomValueToDictionary(extStyles);
[allStyles addEntriesFromDictionary:dicExtStyles];
}
return [allStyles copy];
return allStyles;
}
5 changes: 3 additions & 2 deletions modules/ios/footstoneutils/HippyFootstoneUtils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,16 @@ id DomValueToOCType(const HippyValue *const pDomValue) {
return value;
}

NSDictionary *UnorderedMapDomValueToDictionary(const std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<HippyValue>>> &domValuesObject) {
NSDictionary *UnorderedMapDomValueToDictionary(const std::shared_ptr<std::unordered_map<std::string,
std::shared_ptr<HippyValue>>> &domValuesObject) {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:domValuesObject->size()];
for (auto it = domValuesObject->begin(); it != domValuesObject->end(); it++) {
NSString *key = [NSString stringWithUTF8String:it->first.c_str()];
std::shared_ptr<HippyValue> domValue = it->second;
id value = DomValueToOCType(domValue.get());
[dic setObject:value forKey:key];
}
return [dic copy];
return dic;
}

NSNumber *DomValueToNumber(const HippyValue *const pDomValue) {
Expand Down
6 changes: 3 additions & 3 deletions renderer/native/ios/renderer/HippyUIManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -653,12 +653,12 @@ - (HippyShadowView *)createShadowViewFromNode:(const std::shared_ptr<hippy::DomN
NSNumber *componentTag = @(domNode->GetId());
NSString *viewName = [NSString stringWithUTF8String:domNode->GetViewName().c_str()];
NSString *tagName = [NSString stringWithUTF8String:domNode->GetTagName().c_str()];
NSMutableDictionary *props = [StylesFromDomNode(domNode) mutableCopy];
NSMutableDictionary *props = [HippyStylesFromDomNode(domNode) mutableCopy];
HippyComponentData *componentData = [self componentDataForViewName:viewName];
HippyShadowView *shadowView = [componentData createShadowViewWithTag:componentTag];
shadowView.rootNode = rootNode;
NSAssert(componentData && shadowView, @"componentData and renderObject must not be nil");
[props setValue: rootTag forKey: @"rootTag"];
HippyAssert(componentData && shadowView, @"componentData and shadowView must not be nil");
[props setValue:rootTag forKey:@"rootTag"];
// Register shadow view
if (shadowView) {
shadowView.hippyTag = componentTag;
Expand Down

0 comments on commit a0bb35f

Please sign in to comment.