Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update react-native-safe-area-context (#8459)
* Update react-native-safe-area-context * Update NCL and docs * Include all necessary files and add UIKit header import to get it to compile * Update home and expo pkg to use SafeAreaView from react-native-safe-area-context * Update dev home
- Loading branch information
1 parent
23bf7c3
commit deff557
Showing
49 changed files
with
1,229 additions
and
369 deletions.
There are no files selected for viewing
12 changes: 6 additions & 6 deletions
12
...iew/src/main/java/versioned/host/exp/exponent/modules/api/safeareacontext/EdgeInsets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../expoview/src/main/java/versioned/host/exp/exponent/modules/api/safeareacontext/Rect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package versioned.host.exp.exponent.modules.api.safeareacontext; | ||
|
||
/* package */ class Rect { | ||
float x; | ||
float y; | ||
float width; | ||
float height; | ||
|
||
Rect(float x, float y, float width, float height) { | ||
this.x = x; | ||
this.y = y; | ||
this.width = width; | ||
this.height = height; | ||
} | ||
|
||
boolean equalsToRect(Rect other) { | ||
if (this == other) { | ||
return true; | ||
} | ||
return this.x == other.x && this.y == other.y && this.width == other.width && this.height == other.height; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...c/main/java/versioned/host/exp/exponent/modules/api/safeareacontext/SafeAreaProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package versioned.host.exp.exponent.modules.api.safeareacontext; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.content.Context; | ||
import android.view.ViewGroup; | ||
import android.view.ViewTreeObserver; | ||
|
||
import com.facebook.infer.annotation.Assertions; | ||
import com.facebook.react.views.view.ReactViewGroup; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
@SuppressLint("ViewConstructor") | ||
public class SafeAreaProvider extends ReactViewGroup implements ViewTreeObserver.OnGlobalLayoutListener { | ||
public interface OnInsetsChangeListener { | ||
void onInsetsChange(SafeAreaProvider view, EdgeInsets insets, Rect frame); | ||
} | ||
|
||
private @Nullable OnInsetsChangeListener mInsetsChangeListener; | ||
private @Nullable EdgeInsets mLastInsets; | ||
private @Nullable Rect mLastFrame; | ||
|
||
public SafeAreaProvider(Context context) { | ||
super(context); | ||
} | ||
|
||
private void maybeUpdateInsets() { | ||
EdgeInsets edgeInsets = SafeAreaUtils.getSafeAreaInsets(getRootView(), this); | ||
Rect frame = SafeAreaUtils.getFrame((ViewGroup) getRootView(), this); | ||
if (edgeInsets != null && frame != null && | ||
(mLastInsets == null || | ||
mLastFrame == null || | ||
!mLastInsets.equalsToEdgeInsets(edgeInsets) || | ||
!mLastFrame.equalsToRect(frame))) { | ||
Assertions.assertNotNull(mInsetsChangeListener).onInsetsChange(this, edgeInsets, frame); | ||
mLastInsets = edgeInsets; | ||
mLastFrame = frame; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onAttachedToWindow() { | ||
super.onAttachedToWindow(); | ||
|
||
getViewTreeObserver().addOnGlobalLayoutListener(this); | ||
maybeUpdateInsets(); | ||
} | ||
|
||
@Override | ||
protected void onDetachedFromWindow() { | ||
super.onDetachedFromWindow(); | ||
|
||
getViewTreeObserver().removeOnGlobalLayoutListener(this); | ||
} | ||
|
||
@Override | ||
public void onGlobalLayout() { | ||
maybeUpdateInsets(); | ||
} | ||
|
||
public void setOnInsetsChangeListener(OnInsetsChangeListener listener) { | ||
mInsetsChangeListener = listener; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...java/versioned/host/exp/exponent/modules/api/safeareacontext/SafeAreaProviderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package versioned.host.exp.exponent.modules.api.safeareacontext; | ||
|
||
import android.app.Activity; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.common.MapBuilder; | ||
import com.facebook.react.uimanager.ThemedReactContext; | ||
import com.facebook.react.uimanager.UIManagerModule; | ||
import com.facebook.react.uimanager.ViewGroupManager; | ||
import com.facebook.react.uimanager.events.EventDispatcher; | ||
|
||
import java.util.Map; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
public class SafeAreaProviderManager extends ViewGroupManager<SafeAreaProvider> { | ||
private final ReactApplicationContext mContext; | ||
|
||
public SafeAreaProviderManager(ReactApplicationContext context) { | ||
super(); | ||
|
||
mContext = context; | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public String getName() { | ||
return "RNCSafeAreaProvider"; | ||
} | ||
|
||
@Override | ||
@NonNull | ||
public SafeAreaProvider createViewInstance(@NonNull ThemedReactContext context) { | ||
return new SafeAreaProvider(context); | ||
} | ||
|
||
@Override | ||
protected void addEventEmitters(@NonNull ThemedReactContext reactContext, @NonNull final SafeAreaProvider view) { | ||
final EventDispatcher dispatcher = | ||
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher(); | ||
view.setOnInsetsChangeListener(new SafeAreaProvider.OnInsetsChangeListener() { | ||
@Override | ||
public void onInsetsChange(SafeAreaProvider view, EdgeInsets insets, Rect frame) { | ||
dispatcher.dispatchEvent(new InsetsChangeEvent(view.getId(), insets, frame)); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Map<String, Object> getExportedCustomDirectEventTypeConstants() { | ||
return MapBuilder.<String, Object>builder() | ||
.put(InsetsChangeEvent.EVENT_NAME, MapBuilder.of("registrationName", "onInsetsChange")) | ||
.build(); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Map<String, Object> getExportedViewConstants() { | ||
Activity activity = mContext.getCurrentActivity(); | ||
if (activity == null) { | ||
return null; | ||
} | ||
|
||
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); | ||
if (decorView == null) { | ||
return null; | ||
} | ||
|
||
View contentView = decorView.findViewById(android.R.id.content); | ||
EdgeInsets insets = SafeAreaUtils.getSafeAreaInsets(decorView, contentView); | ||
Rect frame = SafeAreaUtils.getFrame(decorView, contentView); | ||
if (insets == null || frame == null) { | ||
return null; | ||
} | ||
return MapBuilder.<String, Object>of( | ||
"initialWindowMetrics", | ||
MapBuilder.<String, Object>of( | ||
"insets", | ||
SerializationUtils.edgeInsetsToJavaMap(insets), | ||
"frame", | ||
SerializationUtils.rectToJavaMap(frame))); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.