Skip to content

Commit

Permalink
Fix/ios marker render (#9)
Browse files Browse the repository at this point in the history
* fix: ios marker rerender

* fix: marker rerender compare

* build: version 4.8.6
  • Loading branch information
Kt00s authored Jan 17, 2025
1 parent 1806f21 commit b5913ed
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 93 deletions.
29 changes: 29 additions & 0 deletions android/src/main/java/ru/vvdev/mappable/view/MappableMarker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,35 @@ class MappableMarker(context: Context?) : ReactViewGroup(context), MapObjectTapL
updateMarker()
}

// private fun createCustomMarker(): Bitmap {
// val markerView: View = LayoutInflater.from(context).inflate(R.layout.custom_marker_layout, null)
// val markerTextLayout = markerView.findViewById<FrameLayout>(R.id.marker_text_layout);
// markerTextLayout.visibility = View.GONE
//// val markerText = markerView.findViewById<TextView>(R.id.marker_text);
//// markerText.setText("МаркерТекст1")
//// val markerSubText = markerView.findViewById<TextView>(R.id.marker_sub_text);
//// markerSubText.setText("МаркерСубТекст1")
// val markerIconLayout = markerView.findViewById<FrameLayout>(R.id.marker_icon_layout);
// val markerIcon = markerView.findViewById<FrameLayout>(R.id.marker_icon);
// markerIconLayout.backgroundTintList = ColorStateList(arrayOf(intArrayOf(Color.RED)), intArrayOf(Color.CYAN));
// markerIcon.setBackgroundResource(R.drawable.restaurants_24)
// markerIcon.backgroundTintList = ColorStateList(arrayOf(intArrayOf(Color.WHITE)), intArrayOf(Color.WHITE));
//// markerIconLayout.background = ColorStateList(arrayOf(intArrayOf(Color.RED)), intArrayOf(Color.CYAN));
//
// // Convert the view to a bitmap
// markerView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED)
// markerView.layout(0, 0, markerView.measuredWidth, markerView.measuredHeight)
// val bitmap = Bitmap.createBitmap(
// markerView.measuredWidth,
// markerView.measuredHeight,
// Bitmap.Config.ARGB_8888
// )
// val canvas = Canvas(bitmap)
// markerView.draw(canvas)
//
// return bitmap
// }

private fun updateMarker() {
if (rnMapObject != null && rnMapObject!!.isValid) {
val iconStyle = IconStyle()
Expand Down
56 changes: 46 additions & 10 deletions build/components/Marker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/components/Marker.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions build/components/MarkerImage.d.ts

This file was deleted.

30 changes: 0 additions & 30 deletions build/components/MarkerImage.js

This file was deleted.

1 change: 0 additions & 1 deletion build/components/MarkerImage.js.map

This file was deleted.

1 change: 0 additions & 1 deletion build/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './components/Marker';
export * from './components/Polygon';
export * from './components/Polyline';
export * from './components/Circle';
export * from './components/MarkerImage';
export * from './components/ClusteredMappable';
export * from './interfaces';
export * from './Suggest';
Expand Down
1 change: 0 additions & 1 deletion build/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions ios/View/MappableMarkerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ - (void)updateClusterMarker {
}
}];
}
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[self setChildView];
});
}
[mapObject setIconStyleWithStyle:iconStyle];
}
}

Expand Down Expand Up @@ -174,7 +177,7 @@ - (void)setMapObject:(MMKPlacemarkMapObject *)_mapObject {
- (void)setClusterMapObject:(MMKPlacemarkMapObject *)_mapObject {
mapObject = _mapObject;
[mapObject addTapListenerWithTapListener:self];
[self updateClusterMarker];
[self updateMarker];
}

- (void)setHandled:(BOOL)_handled {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-mappable",
"version": "4.8.4",
"version": "4.8.6",
"description": "Mappable Kit for react-native",
"main": "build/index.js",
"types": "build/index.d.ts",
Expand Down
61 changes: 44 additions & 17 deletions src/components/Marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@ interface State {
children: any;
}

const deepCompareChildren = (nextChildren?: React.ReactElement, prevChildren?: React.ReactElement): boolean => {
try {
if (!nextChildren || !prevChildren) {
return nextChildren === prevChildren;
}
if (Array.isArray(nextChildren) && Array.isArray(prevChildren)) {
if (nextChildren.length !== prevChildren.length) {
return false;
}
return nextChildren.every((child, index) =>
deepCompareChildren(child, prevChildren[index])
);
}

if (React.isValidElement(nextChildren) && React.isValidElement(prevChildren)) {
if (nextChildren.type !== prevChildren.type) {
return false;
}

if (nextChildren.key !== prevChildren.key) {
return false;
}
const nextChildrenProps = Object.fromEntries(Object.entries(nextChildren).filter(([key]) => key !== 'children'));
const prevChildrenProps = Object.fromEntries(Object.entries(prevChildren).filter(([key]) => key !== 'children'));
// @ts-ignore
return (deepCompareChildren(nextChildren.props.children, prevChildren.props.children) &&
!(Object.keys(nextChildrenProps).find(key => nextChildrenProps[key] != prevChildrenProps[key]) && Object.keys(prevChildrenProps).find(key => nextChildrenProps[key] != prevChildrenProps[key])));
}

return false;
} catch (e) {
return false;
}
};


export class Marker extends React.Component<MarkerProps, State> {
static defaultProps = {
rotated: false,
Expand All @@ -43,13 +79,16 @@ export class Marker extends React.Component<MarkerProps, State> {
}

static getDerivedStateFromProps(nextProps: MarkerProps, prevState: State): Partial<State> {
if (!deepCompareChildren(nextProps.children, prevState.children)) {
return {
children: nextProps.children,
recreateKey:
nextProps.children === prevState.children
? prevState.recreateKey
: !prevState.recreateKey,
recreateKey: !prevState.recreateKey,
};
}
return {
...prevState,
children: nextProps.children,
}
}

private resolveImageUri(img?: ImageSourcePropType) {
Expand Down Expand Up @@ -81,24 +120,12 @@ export class Marker extends React.Component<MarkerProps, State> {
}

render() {
if (Platform.OS === 'ios') {
return (
<NativeMarkerComponent
{...this.getProps()}
key={String(this.state.recreateKey)}
pointerEvents="none"
>
{this.state.children}
</NativeMarkerComponent>
);
}

return (
<NativeMarkerComponent
{...this.getProps()}
pointerEvents="none"
>
<View key={String(this.state.recreateKey)}>
<View style={{position: 'absolute', zIndex: 10}} key={String(this.state.recreateKey)}>
{this.props.children}
</View>
</NativeMarkerComponent>
Expand Down
21 changes: 0 additions & 21 deletions src/components/MarkerImage.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from './components/Marker';
export * from './components/Polygon';
export * from './components/Polyline';
export * from './components/Circle';
export * from './components/MarkerImage';
export * from './components/ClusteredMappable';
export * from './interfaces';
export * from './Suggest';
Expand Down

0 comments on commit b5913ed

Please sign in to comment.