forked from AppAndFlow/react-native-safe-area-context
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Native SafeAreaView (iOS only) * Android, tests * revert unrelated change * revert unrelated change * Update examples * Fix wording * Add android implementation, change edges prop format * Update README.md Co-authored-by: Janic Duplessis <[email protected]> * Update android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaView.java Co-authored-by: Jacob Parker <[email protected]> Co-authored-by: Janic Duplessis <[email protected]>
- Loading branch information
1 parent
f126189
commit fc9a4ec
Showing
36 changed files
with
1,023 additions
and
277 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
android/src/main/java/com/th3rdwave/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 com.th3rdwave.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
android/src/main/java/com/th3rdwave/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 com.th3rdwave.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
8 changes: 8 additions & 0 deletions
8
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewEdges.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,8 @@ | ||
package com.th3rdwave.safeareacontext; | ||
|
||
public enum SafeAreaViewEdges { | ||
TOP, | ||
RIGHT, | ||
BOTTOM, | ||
LEFT | ||
} |
21 changes: 21 additions & 0 deletions
21
android/src/main/java/com/th3rdwave/safeareacontext/SafeAreaViewLocalData.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,21 @@ | ||
package com.th3rdwave.safeareacontext; | ||
|
||
import java.util.EnumSet; | ||
|
||
public class SafeAreaViewLocalData { | ||
private EdgeInsets mInsets; | ||
private EnumSet<SafeAreaViewEdges> mEdges; | ||
|
||
public SafeAreaViewLocalData(EdgeInsets insets, EnumSet<SafeAreaViewEdges> edges) { | ||
mInsets = insets; | ||
mEdges = edges; | ||
} | ||
|
||
public EdgeInsets getInsets() { | ||
return mInsets; | ||
} | ||
|
||
public EnumSet<SafeAreaViewEdges> getEdges() { | ||
return mEdges; | ||
} | ||
} |
Oops, something went wrong.