|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +package com.facebook.react.views.modal; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import android.content.DialogInterface; |
| 15 | + |
| 16 | +import com.facebook.react.common.MapBuilder; |
| 17 | +import com.facebook.react.common.SystemClock; |
| 18 | +import com.facebook.react.uimanager.LayoutShadowNode; |
| 19 | +import com.facebook.react.uimanager.ThemedReactContext; |
| 20 | +import com.facebook.react.uimanager.UIManagerModule; |
| 21 | +import com.facebook.react.uimanager.ViewGroupManager; |
| 22 | +import com.facebook.react.uimanager.annotations.ReactProp; |
| 23 | +import com.facebook.react.uimanager.events.EventDispatcher; |
| 24 | + |
| 25 | +/** |
| 26 | + * View manager for {@link ReactModalHostView} components. |
| 27 | + */ |
| 28 | +public class ReactModalHostManager extends ViewGroupManager<ReactModalHostView> { |
| 29 | + |
| 30 | + private static final String REACT_CLASS = "RCTModalHostView"; |
| 31 | + |
| 32 | + @Override |
| 33 | + public String getName() { |
| 34 | + return REACT_CLASS; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected ReactModalHostView createViewInstance(ThemedReactContext reactContext) { |
| 39 | + return new ReactModalHostView(reactContext); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public LayoutShadowNode createShadowNodeInstance() { |
| 44 | + return new ModalHostShadowNode(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Class<? extends LayoutShadowNode> getShadowNodeClass() { |
| 49 | + return ModalHostShadowNode.class; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onDropViewInstance(ReactModalHostView view) { |
| 54 | + super.onDropViewInstance(view); |
| 55 | + view.dismiss(); |
| 56 | + } |
| 57 | + |
| 58 | + @ReactProp(name = "animated") |
| 59 | + public void setAnimated(ReactModalHostView view, boolean animated) { |
| 60 | + view.setAnimated(animated); |
| 61 | + } |
| 62 | + |
| 63 | + @ReactProp(name = "transparent") |
| 64 | + public void setTransparent(ReactModalHostView view, boolean transparent) { |
| 65 | + view.setTransparent(transparent); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void addEventEmitters( |
| 70 | + ThemedReactContext reactContext, |
| 71 | + final ReactModalHostView view) { |
| 72 | + final EventDispatcher dispatcher = |
| 73 | + reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); |
| 74 | + view.setOnRequestCloseListener( |
| 75 | + new ReactModalHostView.OnRequestCloseListener() { |
| 76 | + @Override |
| 77 | + public void onRequestClose(DialogInterface dialog) { |
| 78 | + dispatcher.dispatchEvent(new RequestCloseEvent(view.getId(), SystemClock.nanoTime())); |
| 79 | + } |
| 80 | + }); |
| 81 | + view.setOnShowListener( |
| 82 | + new DialogInterface.OnShowListener() { |
| 83 | + @Override |
| 84 | + public void onShow(DialogInterface dialog) { |
| 85 | + dispatcher.dispatchEvent(new ShowEvent(view.getId(), SystemClock.nanoTime())); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Map<String, Object> getExportedCustomDirectEventTypeConstants() { |
| 92 | + return MapBuilder.<String, Object>builder() |
| 93 | + .put(RequestCloseEvent.EVENT_NAME, MapBuilder.of("registrationName", "onRequestClose")) |
| 94 | + .put(ShowEvent.EVENT_NAME, MapBuilder.of("registrationName", "onShow")) |
| 95 | + .build(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void onAfterUpdateTransaction(ReactModalHostView view) { |
| 100 | + super.onAfterUpdateTransaction(view); |
| 101 | + view.showOrUpdate(); |
| 102 | + } |
| 103 | +} |
0 commit comments