Skip to content

Commit 3039dff

Browse files
committed
feat(android): added support for onMeasure and debug props
1 parent 789b040 commit 3039dff

6 files changed

+91
-51
lines changed

TODO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Open issues:
1212
[X] Android `resize` prop support
1313
[X] Android backgroundImage support
1414
[X] Android scale transform support
15-
[ ] Android `debug` prop support (fix measurement event)
15+
[X] Android `debug` prop support (fix measurement event)
1616
[ ] Android `align` prop support
1717
[ ] Android FirebaseUI rounded corners
1818
[ ] Android inverted scrollview clipping

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementDrawable.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424

2525
class RNSharedElementDrawable extends Drawable {
2626
enum ViewType {
27-
NONE,
28-
REACTIMAGEVIEW,
29-
IMAGEVIEW,
30-
PLAIN,
31-
GENERIC
27+
NONE("none"),
28+
REACTIMAGEVIEW("image"),
29+
IMAGEVIEW("image"),
30+
PLAIN("view"),
31+
GENERIC("generic");
32+
33+
private final String value;
34+
ViewType(final String newValue) { value = newValue;}
35+
public String getValue() { return value; }
3236
}
3337

3438
static private String LOG_TAG = "RNSharedElementDrawable";
@@ -182,7 +186,7 @@ public void getOutline(Outline outline) {
182186
outline.setConvexPath(mPathForBorderRadiusOutline);
183187
}
184188

185-
static private ViewType getViewType(View view, RNSharedElementStyle style) {
189+
static ViewType getViewType(View view, RNSharedElementStyle style) {
186190
if (view == null) return ViewType.NONE;
187191
if (view instanceof ReactImageView) {
188192
return ViewType.REACTIMAGEVIEW;

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementTransition.java

+50-40
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import android.view.View;
1212
import android.view.ViewGroup;
1313

14+
import com.facebook.react.bridge.Arguments;
1415
import com.facebook.react.bridge.Callback;
16+
import com.facebook.react.bridge.ReactContext;
17+
import com.facebook.react.bridge.WritableMap;
18+
import com.facebook.react.uimanager.PixelUtil;
1519
import com.facebook.react.uimanager.ThemedReactContext;
16-
import com.facebook.react.uimanager.UIBlock;
17-
import com.facebook.react.uimanager.UIManagerModule;
18-
import com.facebook.react.uimanager.NativeViewHierarchyManager;
20+
import com.facebook.react.uimanager.events.RCTEventEmitter;
1921

2022
public class RNSharedElementTransition extends ViewGroup {
2123
static private String LOG_TAG = "RNSharedElementTransition";
@@ -295,6 +297,16 @@ private void updateLayout() {
295297
}
296298
}
297299
}
300+
301+
// Fire events
302+
if ((startStyle != null) && !startItem.getHasCalledOnMeasure()) {
303+
startItem.setHasCalledOnMeasure(true);
304+
fireMeasureEvent("startNode", startItem, startClippedLayout);
305+
}
306+
if ((endStyle != null) && !endItem.getHasCalledOnMeasure()) {
307+
endItem.setHasCalledOnMeasure(true);
308+
fireMeasureEvent("endNode", endItem, endClippedLayout);
309+
}
298310
}
299311

300312
private void updateNodeVisibility() {
@@ -413,45 +425,43 @@ private RNSharedElementStyle getInterpolatedStyle(
413425
return result;
414426
}
415427

416-
private void fireMeasureEvent() {
417-
/*ReactContext reactContext = (ReactContext)getContext();
418-
WritableMap eventData = Arguments.createMap();
428+
private void fireMeasureEvent(String name, RNSharedElementTransitionItem item, Rect clippedLayout) {
429+
ReactContext reactContext = (ReactContext)getContext();
430+
RNSharedElementStyle style = item.getStyle();
431+
RNSharedElementContent content = item.getContent();
432+
419433
WritableMap layoutData = Arguments.createMap();
420-
layoutData.putFloat();
421-
//eventData.putString("message", "MyMessage");
422-
eventData.putString("node", item.name);
423-
eventData.putMap("layout", layoutData)
434+
layoutData.putDouble("x", PixelUtil.toDIPFromPixel(style.layout.left));
435+
layoutData.putDouble("y", PixelUtil.toDIPFromPixel(style.layout.top));
436+
layoutData.putDouble("width", PixelUtil.toDIPFromPixel(style.layout.width()));
437+
layoutData.putDouble("height", PixelUtil.toDIPFromPixel(style.layout.height()));
438+
layoutData.putDouble("visibleX", PixelUtil.toDIPFromPixel(clippedLayout.left));
439+
layoutData.putDouble("visibleY", PixelUtil.toDIPFromPixel(clippedLayout.top));
440+
layoutData.putDouble("visibleWidth", PixelUtil.toDIPFromPixel(clippedLayout.width()));
441+
layoutData.putDouble("visibleHeight", PixelUtil.toDIPFromPixel(clippedLayout.height()));
442+
layoutData.putDouble("contentX", PixelUtil.toDIPFromPixel(style.layout.left)); // TODO
443+
layoutData.putDouble("contentY", PixelUtil.toDIPFromPixel(style.layout.top)); // TODO
444+
layoutData.putDouble("contentWidth", PixelUtil.toDIPFromPixel(style.layout.width())); // TODO
445+
layoutData.putDouble("contentHeight", PixelUtil.toDIPFromPixel(style.layout.height())); // TODO
446+
447+
WritableMap styleData = Arguments.createMap();
448+
styleData.putDouble("borderTopLeftRadius", PixelUtil.toDIPFromPixel(style.borderTopLeftRadius));
449+
styleData.putDouble("borderTopRightRadius", PixelUtil.toDIPFromPixel(style.borderTopRightRadius));
450+
styleData.putDouble("borderBottomLeftRadius", PixelUtil.toDIPFromPixel(style.borderBottomLeftRadius));
451+
styleData.putDouble("borderBottomRightRadius", PixelUtil.toDIPFromPixel(style.borderBottomRightRadius));
452+
453+
WritableMap eventData = Arguments.createMap();
454+
eventData.putString("node", name);
455+
eventData.putMap("layout", layoutData);
456+
RNSharedElementDrawable.ViewType viewType = (content != null)
457+
? RNSharedElementDrawable.getViewType(content.view, style)
458+
: RNSharedElementDrawable.ViewType.NONE;
459+
eventData.putString("contentType", viewType.getValue());
460+
eventData.putMap("style", styleData);
461+
424462
reactContext.getJSModule(RCTEventEmitter.class).receiveEvent(
425463
getId(),
426-
"onMeasure",
427-
eventData);*/
428-
429-
/*
430-
- (void) fireMeasureEvent:(RNSharedElementTransitionItem*) item layout:(CGRect)layout visibleLayout:(CGRect)visibleLayout contentLayout:(CGRect)contentLayout
431-
{
432-
if (!self.onMeasureNode) return;
433-
NSDictionary* eventData = @{
434-
@"node": item.name,
435-
@"layout": @{
436-
@"x": @(layout.origin.x),
437-
@"y": @(layout.origin.y),
438-
@"width": @(layout.size.width),
439-
@"height": @(layout.height()),
440-
@"visibleX": @(visibleLayout.origin.x),
441-
@"visibleY": @(visibleLayout.origin.y),
442-
@"visibleWidth": @(visibleLayout.size.width),
443-
@"visibleHeight": @(visibleLayout.height()),
444-
@"contentX": @(contentLayout.origin.x),
445-
@"contentY": @(contentLayout.origin.y),
446-
@"contentWidth": @(contentLayout.size.width),
447-
@"contentHeight": @(contentLayout.height()),
448-
},
449-
@"contentType": item.contentTypeName,
450-
@"style": @{
451-
@"borderRadius": @(item.style.cornerRadius)
452-
}
453-
};
454-
self.onMeasureNode(eventData);
455-
}*/
464+
"onMeasureNode",
465+
eventData);
456466
}
457467
}

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementTransitionItem.java

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class RNSharedElementTransitionItem {
1919
private boolean mNeedsContent;
2020
private RNSharedElementContent mContent;
2121
private Rect mClippedLayoutCache;
22+
private boolean mHasCalledOnMeasure;
2223

2324
RNSharedElementTransitionItem(RNSharedElementNodeManager nodeManager, String name) {
2425
mNodeManager = nodeManager;
@@ -30,6 +31,7 @@ class RNSharedElementTransitionItem {
3031
mNeedsContent = false;
3132
mContent = null;
3233
mClippedLayoutCache = null;
34+
mHasCalledOnMeasure = false;
3335
}
3436

3537
String getName() {
@@ -103,6 +105,14 @@ RNSharedElementContent getContent() {
103105
return mContent;
104106
}
105107

108+
void setHasCalledOnMeasure(boolean hasCalledOnMeasure) {
109+
mHasCalledOnMeasure = hasCalledOnMeasure;
110+
}
111+
112+
boolean getHasCalledOnMeasure() {
113+
return mHasCalledOnMeasure;
114+
}
115+
106116
View getView() {
107117
return (mNode != null) ? mNode.getResolvedView() : null;
108118
}

android/src/main/java/com/ijzerenhein/sharedelement/RNSharedElementTransitionManager.java

+14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.ijzerenhein.sharedelement;
22

3+
import java.util.Map;
4+
35
import android.view.View;
46

7+
import com.facebook.react.common.MapBuilder;
58
import com.facebook.react.uimanager.annotations.ReactProp;
69
import com.facebook.react.uimanager.ThemedReactContext;
710
import com.facebook.react.uimanager.SimpleViewManager;
@@ -25,6 +28,17 @@ public String getName() {
2528
return REACT_CLASS;
2629
}
2730

31+
@Override
32+
public Map getExportedCustomBubblingEventTypeConstants() {
33+
return MapBuilder.builder()
34+
.put(
35+
"onMeasureNode",
36+
MapBuilder.of(
37+
"phasedRegistrationNames",
38+
MapBuilder.of("bubbled", "onMeasureNode")))
39+
.build();
40+
}
41+
2842
@Override
2943
public RNSharedElementTransition createViewInstance(ThemedReactContext context) {
3044
return new RNSharedElementTransition(context, mNodeManager);

src/SharedElementTransition.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ export class SharedElementTransition extends React.Component<
176176
// $FlowFixMe
177177
nodeStyle.borderColor = processColor(nodeStyle.borderColor);
178178
}
179+
if (nodeStyle.color) {
180+
// $FlowFixMe
181+
nodeStyle.color = processColor(nodeStyle.color);
182+
}
179183
}
180184
return node
181185
? {
@@ -214,9 +218,7 @@ export class SharedElementTransition extends React.Component<
214218

215219
renderDebugLayer(name: SharedElementNodeType) {
216220
const event = this.state[name];
217-
if (!event || !this.props.debug) {
218-
return undefined;
219-
}
221+
if (!event || !this.props.debug) return;
220222
const { layout, style } = event;
221223
const isContentDifferent =
222224
layout.x !== layout.contentX ||
@@ -267,7 +269,7 @@ export class SharedElementTransition extends React.Component<
267269
<Text
268270
style={[
269271
debugStyles.text,
270-
{ color, marginTop: Math.max(style.borderRadius - 7, 3) }
272+
{ color, marginTop: Math.max((style.borderRadius || 0) - 7, 3) }
271273
]}
272274
>
273275
{name}

0 commit comments

Comments
 (0)