mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-03 15:24:58 +08:00
Introducing view property annotation.
Differential Revision: D2475680 committer: Service User <svcscm@fb.com>
This commit is contained in:
committed by
facebook-github-bot-9
parent
6c3fb77f30
commit
5623c831b3
@@ -18,10 +18,11 @@ import java.util.Map;
|
||||
import android.view.View;
|
||||
|
||||
import com.facebook.csslayout.CSSNode;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
import com.facebook.react.bridge.ReadableMapKeySeyIterator;
|
||||
import com.facebook.react.touch.CatalystInterceptingViewGroup;
|
||||
import com.facebook.react.touch.JSResponderHandler;
|
||||
import com.facebook.react.bridge.ReactContext;
|
||||
import com.facebook.react.bridge.ReadableArray;
|
||||
|
||||
/**
|
||||
* Class responsible for knowing how to create and update catalyst Views of a given type. It is also
|
||||
@@ -32,6 +33,28 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode> {
|
||||
|
||||
private static final Map<Class, Map<String, UIProp.Type>> CLASS_PROP_CACHE = new HashMap<>();
|
||||
|
||||
public final void updateProperties(T viewToUpdate, CatalystStylesDiffMap props) {
|
||||
Map<String, ViewManagersPropertyCache.PropSetter> propSetters =
|
||||
ViewManagersPropertyCache.getNativePropSettersForClass(getClass());
|
||||
ReadableMap propMap = props.mBackingMap;
|
||||
ReadableMapKeySeyIterator iterator = propMap.keySetIterator();
|
||||
// TODO(krzysztof): Remove missingSetters code once all views are migrated to @ReactProp
|
||||
boolean missingSetters = false;
|
||||
while (iterator.hasNextKey()) {
|
||||
String key = iterator.nextKey();
|
||||
ViewManagersPropertyCache.PropSetter setter = propSetters.get(key);
|
||||
if (setter != null) {
|
||||
setter.updateProp(this, viewToUpdate, props);
|
||||
} else {
|
||||
missingSetters = true;
|
||||
}
|
||||
}
|
||||
if (missingSetters) {
|
||||
updateView(viewToUpdate, props);
|
||||
}
|
||||
onAfterUpdateTransaction(viewToUpdate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a view and installs event emitters on it.
|
||||
*/
|
||||
@@ -84,8 +107,22 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode> {
|
||||
* Subclass should use this method to populate native view with updated style properties. In case
|
||||
* when a certain property is present in {@param props} map but the value is null, this property
|
||||
* should be reset to the default value
|
||||
*
|
||||
* TODO(krzysztof) This method should be replaced by updateProperties and removed completely after
|
||||
* all view managers adapt @ReactProp
|
||||
*/
|
||||
public abstract void updateView(T root, CatalystStylesDiffMap props);
|
||||
@Deprecated
|
||||
protected void updateView(T root, CatalystStylesDiffMap props) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback that will be triggered after all properties are updated in current update transaction
|
||||
* (all @ReactProp handlers for properties updated in current transaction have been called). If
|
||||
* you want to override this method you should call super.onAfterUpdateTransaction from it as
|
||||
* the parent class of the ViewManager may rely on callback being executed.
|
||||
*/
|
||||
protected void onAfterUpdateTransaction(T view) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Subclasses can implement this method to receive an optional extra data enqueued from the
|
||||
@@ -178,13 +215,15 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode> {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, UIProp.Type> getNativeProps() {
|
||||
Map<String, UIProp.Type> nativeProps = new HashMap<>();
|
||||
public Map<String, String> getNativeProps() {
|
||||
// TODO(krzysztof): This method will just delegate to ViewManagersPropertyRegistry once
|
||||
// refactoring is finished
|
||||
Class cls = getClass();
|
||||
Map<String, String> nativeProps = ViewManagersPropertyCache.getNativePropsForClass(cls);
|
||||
while (cls.getSuperclass() != null) {
|
||||
Map<String, UIProp.Type> props = getNativePropsForClass(cls);
|
||||
for (Map.Entry<String, UIProp.Type> entry : props.entrySet()) {
|
||||
nativeProps.put(entry.getKey(), entry.getValue());
|
||||
nativeProps.put(entry.getKey(), entry.getValue().toString());
|
||||
}
|
||||
cls = cls.getSuperclass();
|
||||
}
|
||||
@@ -192,6 +231,7 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode> {
|
||||
}
|
||||
|
||||
private Map<String, UIProp.Type> getNativePropsForClass(Class cls) {
|
||||
// TODO(krzysztof): Blow up this method once refactoring is finished
|
||||
Map<String, UIProp.Type> props = CLASS_PROP_CACHE.get(cls);
|
||||
if (props != null) {
|
||||
return props;
|
||||
|
||||
Reference in New Issue
Block a user