Skip to content

Commit

Permalink
fix(android): MarkerView anchor was missing, also fix existing warnin…
Browse files Browse the repository at this point in the history
…g for vectorsource if tileUrlTemplates is defined (rnmapbox#3235)
  • Loading branch information
mfazekas authored Dec 5, 2023
1 parent 1db7847 commit bf890cb
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 7 deletions.
2 changes: 2 additions & 0 deletions android/src/main/java/com/rnmapbox/rnmbx/RNMBXPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.facebook.react.module.model.ReactModuleInfo
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.uimanager.ViewManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXCalloutManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXMarkerViewContentManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXMarkerViewManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotationManager
import com.rnmapbox.rnmbx.components.annotation.RNMBXPointAnnotationModule
Expand Down Expand Up @@ -117,6 +118,7 @@ class RNMBXPackage : TurboReactPackage() {

// annotations
managers.add(RNMBXMarkerViewManager(reactApplicationContext))
managers.add(RNMBXMarkerViewContentManager(reactApplicationContext))
managers.add(RNMBXPointAnnotationManager(reactApplicationContext, getViewTagResolver(reactApplicationContext, "RNMBXPointAnnotationManager")))
managers.add(RNMBXCalloutManager())
managers.add(RNMBXNativeUserLocationManager())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@ class RNMBXMarkerView(context: Context?, private val mManager: RNMBXMarkerViewMa

val options = getOptions()


val annotation = mMapView?.viewAnnotationManager?.addViewAnnotation(
view,
options
)
didAddToMap = true

}

fun update() {
Expand Down Expand Up @@ -176,6 +178,7 @@ class RNMBXMarkerView(context: Context?, private val mManager: RNMBXMarkerViewMa
allowOverlap(mAllowOverlap)
offsets(offset.dx, offset.dy)
selected(mIsSelected)

}
return options
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.rnmapbox.rnmbx.components.annotation

import android.content.Context
import com.facebook.react.uimanager.MeasureSpecAssertions
import com.facebook.react.views.view.ReactViewGroup

class RNMBXMarkerViewContent(context: Context): ReactViewGroup(context) {
// see https://github.com/rnmapbox/maps/pull/3235
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
try {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
} catch(e: IllegalStateException) {
val w = MeasureSpec.getSize(widthMeasureSpec)
val h = MeasureSpec.getSize(heightMeasureSpec)
setMeasuredDimension(
if (w == 0) measuredWidth else w,
if (h == 0) measuredHeight else h
)
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.rnmapbox.rnmbx.components.annotation

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.ViewGroupManager
import com.facebook.react.viewmanagers.RNMBXMarkerViewContentManagerInterface


class RNMBXMarkerViewContentManager(reactApplicationContext: ReactApplicationContext) :
ViewGroupManager<RNMBXMarkerViewContent>(),
RNMBXMarkerViewContentManagerInterface<RNMBXMarkerView> {
override fun getName(): String {
return REACT_CLASS
}

override fun createViewInstance(context: ThemedReactContext): RNMBXMarkerViewContent {
return RNMBXMarkerViewContent(context)
}

companion object {
const val REACT_CLASS = "RNMBXMarkerViewContent"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RNMBXVectorSource(context: Context?, private val mManager: RNMBXVectorSour
}

override fun hasNoDataSoRefersToExisting(): Boolean {
return uRL == null;
return uRL == null && tileUrlTemplates == null;
}

override fun makeSource(): VectorSource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.view.View
import com.mapbox.geojson.Geometry
import com.mapbox.geojson.Point
import com.mapbox.maps.ScreenCoordinate
import com.mapbox.maps.ViewAnnotationAnchor
import com.mapbox.maps.ViewAnnotationAnchorConfig
import com.mapbox.maps.ViewAnnotationOptions
import com.mapbox.maps.viewannotation.geometry as _geometry
Expand All @@ -22,7 +23,7 @@ fun ViewAnnotationOptions.Builder.geometry(point: Geometry): ViewAnnotationOptio

fun ViewAnnotationOptions.Builder.offsets(x: Double, y: Double) {
this.variableAnchors(listOf(
ViewAnnotationAnchorConfig.Builder().offsetY(x).offsetY(y).build()
ViewAnnotationAnchorConfig.Builder().anchor(ViewAnnotationAnchor.CENTER).offsetY(x).offsetY(y).build()
))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaDelegate.js
*/

package com.facebook.react.viewmanagers;

import android.view.View;
import androidx.annotation.Nullable;
import com.facebook.react.uimanager.BaseViewManagerDelegate;
import com.facebook.react.uimanager.BaseViewManagerInterface;

public class RNMBXMarkerViewContentManagerDelegate<T extends View, U extends BaseViewManagerInterface<T> & RNMBXMarkerViewContentManagerInterface<T>> extends BaseViewManagerDelegate<T, U> {
public RNMBXMarkerViewContentManagerDelegate(U viewManager) {
super(viewManager);
}
@Override
public void setProperty(T view, String propName, @Nullable Object value) {
super.setProperty(view, propName, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GeneratePropsJavaInterface.js
*/

package com.facebook.react.viewmanagers;

import android.view.View;

public interface RNMBXMarkerViewContentManagerInterface<T extends View> {
// No props
}
15 changes: 15 additions & 0 deletions ios/RNMBX/RNMBXMarkerViewContentComponentView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifdef RCT_NEW_ARCH_ENABLED

#import <UIKit/UIKit.h>

#import <React/RCTUIManager.h>
#import <React/RCTViewComponentView.h>

NS_ASSUME_NONNULL_BEGIN

@interface RNMBXMarkerViewContentComponentView : RCTViewComponentView
@end

NS_ASSUME_NONNULL_END

#endif // RCT_NEW_ARCH_ENABLED
41 changes: 41 additions & 0 deletions ios/RNMBX/RNMBXMarkerViewContentComponentView.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifdef RCT_NEW_ARCH_ENABLED

#import "RNMBXMarkerViewContentComponentView.h"
#import "RNMBXFabricHelpers.h"

#import <React/RCTConversions.h>
#import <React/RCTFabricComponentsPlugins.h>

#import <react/renderer/components/rnmapbox_maps_specs/ComponentDescriptors.h>
#import <react/renderer/components/rnmapbox_maps_specs/EventEmitters.h>
#import <react/renderer/components/rnmapbox_maps_specs/Props.h>
#import <react/renderer/components/rnmapbox_maps_specs/RCTComponentViewHelpers.h>

using namespace facebook::react;

@interface RNMBXMarkerViewContentComponentView () <RCTRNMBXMarkerViewContentViewProtocol>
@end

@implementation RNMBXMarkerViewContentComponentView {
}

- (instancetype)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
}

#pragma mark - RCTComponentViewProtocol

+ (ComponentDescriptorProvider)componentDescriptorProvider
{
return concreteComponentDescriptorProvider<RNMBXMarkerViewContentComponentDescriptor>();
}

@end

Class<RCTComponentViewProtocol> RNMBXMarkerViewContentCls(void)
{
return RNMBXMarkerViewContentComponentView.class;
}

#endif // RCT_NEW_ARCH_ENABLED
6 changes: 6 additions & 0 deletions ios/RNMBX/RNMBXMarkerViewContentManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#import "React/RCTBridgeModule.h"
#import <React/RCTViewManager.h>
#import <Foundation/Foundation.h>

@interface RCT_EXTERN_REMAP_MODULE(RNMBXMarkerViewContent, RNMBXMarkerViewContentManager, RCTViewManager)
@end
14 changes: 14 additions & 0 deletions ios/RNMBX/RNMBXMarkerViewContentManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation
import MapboxMaps

@objc(RNMBXMarkerViewContentManager)
class RNMBXMarkerViewContentManager : RCTViewManager {
@objc
override static func requiresMainQueueSetup() -> Bool {
return true
}

override func view() -> UIView! {
return UIView()
}
}
9 changes: 4 additions & 5 deletions src/components/MarkerView.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import React from 'react';
import { Platform, NativeModules, type ViewProps, View } from 'react-native';
import { Platform, NativeModules, type ViewProps } from 'react-native';

import { toJSONString } from '../utils';
import { makePoint } from '../utils/geoUtils';
import { type Position } from '../types/Position';
import NativeMarkerViewComponent from '../specs/RNMBXMarkerViewNativeComponent';
import RNMBXMakerViewContentCoponent from '../specs/RNMBXMarkerViewContentNativeComponent';

import PointAnnotation from './PointAnnotation';

const Mapbox = NativeModules.RNMBXModule;

export const NATIVE_MODULE_NAME = 'RNMBXMarkerView';

type Props = ViewProps & {
/**
* The center point (specified as a map coordinate) of the marker.
Expand Down Expand Up @@ -118,7 +117,7 @@ class MarkerView extends React.PureComponent<Props> {
e.stopPropagation();
}}
>
<View
<RNMBXMakerViewContentCoponent
style={{ flex: 0, alignSelf: 'flex-start' }}
onStartShouldSetResponder={(_event) => {
return true;
Expand All @@ -128,7 +127,7 @@ class MarkerView extends React.PureComponent<Props> {
}}
>
{this.props.children}
</View>
</RNMBXMakerViewContentCoponent>
</RNMBXMarkerView>
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/specs/RNMBXMarkerViewContentNativeComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { HostComponent, ViewProps } from 'react-native';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface NativeProps extends ViewProps {}

export default codegenNativeComponent<NativeProps>(
'RNMBXMarkerViewContent',
) as HostComponent<ViewProps>;

0 comments on commit bf890cb

Please sign in to comment.