Skip to content

Commit

Permalink
chore: reformat java code
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperkapusciak committed Aug 5, 2021
1 parent 0cf46cb commit 21ea886
Show file tree
Hide file tree
Showing 13 changed files with 467 additions and 365 deletions.
26 changes: 17 additions & 9 deletions android/src/main/java/com/swmansion/rnscreens/LifecycleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@
public class LifecycleHelper {

private final Map<View, Lifecycle> mViewToLifecycleMap = new HashMap<>();
private final View.OnLayoutChangeListener mRegisterOnLayoutChange =
new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(
View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
registerViewWithLifecycleOwner(view);
view.removeOnLayoutChangeListener(this);
}
};
private final View.OnLayoutChangeListener mRegisterOnLayoutChange = new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(
View view,
int i,
int i1,
int i2,
int i3,
int i4,
int i5,
int i6,
int i7
) {
registerViewWithLifecycleOwner(view);
view.removeOnLayoutChangeListener(this);
}
};

public static @Nullable Fragment findNearestScreenFragmentAncestor(View view) {
ViewParent parent = view.getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

public class RNScreensPackage implements ReactPackage {

@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
Expand All @@ -17,10 +18,11 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(
new ScreenContainerViewManager(),
new ScreenViewManager(),
new ScreenStackViewManager(),
new ScreenStackHeaderConfigViewManager(),
new ScreenStackHeaderSubviewManager());
new ScreenContainerViewManager(),
new ScreenViewManager(),
new ScreenStackViewManager(),
new ScreenStackHeaderConfigViewManager(),
new ScreenStackHeaderSubviewManager()
);
}
}
79 changes: 47 additions & 32 deletions android/src/main/java/com/swmansion/rnscreens/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@

public class Screen extends ViewGroup {

private static final OnAttachStateChangeListener sShowSoftKeyboardOnAttach =
new OnAttachStateChangeListener() {

@Override
public void onViewAttachedToWindow(View view) {
InputMethodManager inputMethodManager =
(InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, 0);
view.removeOnAttachStateChangeListener(sShowSoftKeyboardOnAttach);
}
private static final OnAttachStateChangeListener sShowSoftKeyboardOnAttach = new OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) view
.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, 0);
view.removeOnAttachStateChangeListener(sShowSoftKeyboardOnAttach);
}

@Override
public void onViewDetachedFromWindow(View view) {}
};

@Nullable
private ScreenFragment mFragment;

@Nullable
private ScreenContainer mContainer;

@Override
public void onViewDetachedFromWindow(View view) {}
};
private @Nullable ScreenFragment mFragment;
private @Nullable ScreenContainer mContainer;
private ActivityState mActivityState;
private boolean mTransitioning;
private StackPresentation mStackPresentation = StackPresentation.PUSH;
Expand Down Expand Up @@ -98,14 +102,15 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int height = b - t;
final ReactContext reactContext = (ReactContext) getContext();
reactContext.runOnNativeModulesQueueThread(
new GuardedRunnable(reactContext) {
@Override
public void runGuarded() {
reactContext
.getNativeModule(UIManagerModule.class)
.updateNodeSize(getId(), width, height);
}
});
new GuardedRunnable(reactContext) {
@Override
public void runGuarded() {
reactContext
.getNativeModule(UIManagerModule.class)
.updateNodeSize(getId(), width, height);
}
}
);
}
}

Expand Down Expand Up @@ -155,8 +160,9 @@ public void setTransitioning(boolean transitioning) {
return;
}
super.setLayerType(
transitioning && !isWebViewInScreen ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE,
null);
transitioning && !isWebViewInScreen ? View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE,
null
);
}

private boolean hasWebView(ViewGroup viewGroup) {
Expand Down Expand Up @@ -296,7 +302,10 @@ public void setStatusBarStyle(String statusBarStyle) {
mStatusBarStyle = statusBarStyle;
if (getFragment() != null) {
ScreenWindowTraits.setStyle(
this, getFragment().tryGetActivity(), getFragment().tryGetContext());
this,
getFragment().tryGetActivity(),
getFragment().tryGetContext()
);
}
}

Expand All @@ -323,7 +332,10 @@ public void setStatusBarTranslucent(Boolean statusBarTranslucent) {
mStatusBarTranslucent = statusBarTranslucent;
if (getFragment() != null) {
ScreenWindowTraits.setTranslucent(
this, getFragment().tryGetActivity(), getFragment().tryGetContext());
this,
getFragment().tryGetActivity(),
getFragment().tryGetContext()
);
}
}

Expand All @@ -343,7 +355,10 @@ public void setStatusBarColor(Integer statusBarColor) {
mStatusBarColor = statusBarColor;
if (getFragment() != null) {
ScreenWindowTraits.setColor(
this, getFragment().tryGetActivity(), getFragment().tryGetContext());
this,
getFragment().tryGetActivity(),
getFragment().tryGetContext()
);
}
}

Expand All @@ -358,7 +373,7 @@ public void setStatusBarAnimated(Boolean statusBarAnimated) {
public enum StackPresentation {
PUSH,
MODAL,
TRANSPARENT_MODAL
TRANSPARENT_MODAL,
}

public enum StackAnimation {
Expand All @@ -373,13 +388,13 @@ public enum StackAnimation {

public enum ReplaceAnimation {
PUSH,
POP
POP,
}

public enum ActivityState {
INACTIVE,
TRANSITIONING_OR_BELOW_TOP,
ON_TOP
ON_TOP,
}

public enum WindowTraits {
Expand All @@ -388,6 +403,6 @@ public enum WindowTraits {
STYLE,
TRANSLUCENT,
HIDDEN,
ANIMATED
ANIMATED,
}
}
111 changes: 63 additions & 48 deletions android/src/main/java/com/swmansion/rnscreens/ScreenContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,38 @@ public class ScreenContainer<T extends ScreenFragment> extends ViewGroup {

protected final ArrayList<T> mScreenFragments = new ArrayList<>();

protected @Nullable FragmentManager mFragmentManager;
private @Nullable FragmentTransaction mCurrentTransaction;
private @Nullable FragmentTransaction mProcessingTransaction;
@Nullable
protected FragmentManager mFragmentManager;

@Nullable
private FragmentTransaction mCurrentTransaction;

@Nullable
private FragmentTransaction mProcessingTransaction;

private boolean mNeedUpdate;
private boolean mIsAttached;
private final ChoreographerCompat.FrameCallback mFrameCallback =
new ChoreographerCompat.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
updateIfNeeded();
}
};
private final ChoreographerCompat.FrameCallback mFrameCallback = new ChoreographerCompat.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
updateIfNeeded();
}
};
private boolean mLayoutEnqueued = false;
private final ChoreographerCompat.FrameCallback mLayoutCallback =
new ChoreographerCompat.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
mLayoutEnqueued = false;
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout(getLeft(), getTop(), getRight(), getBottom());
}
};
private @Nullable ScreenFragment mParentScreenFragment = null;
private final ChoreographerCompat.FrameCallback mLayoutCallback = new ChoreographerCompat.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
mLayoutEnqueued = false;
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY)
);
layout(getLeft(), getTop(), getRight(), getBottom());
}
};

@Nullable
private ScreenFragment mParentScreenFragment = null;

public ScreenContainer(Context context) {
super(context);
Expand Down Expand Up @@ -75,8 +82,9 @@ public void removeView(View view) {
// detect that in removeView as super.removeView causes the input view to un focus while keeping
// the keyboard open).
if (view == getFocusedChild()) {
((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
(
(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE)
).hideSoftInputFromWindow(getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
super.removeView(view);
}
Expand All @@ -89,9 +97,9 @@ public void requestLayout() {
mLayoutEnqueued = true;
// we use NATIVE_ANIMATED_MODULE choreographer queue because it allows us to catch the current
// looper loop instead of enqueueing the update in the next loop causing a one frame delay.
ReactChoreographer.getInstance()
.postFrameCallback(
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, mLayoutCallback);
ReactChoreographer
.getInstance()
.postFrameCallback(ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, mLayoutCallback);
}
}

Expand All @@ -104,9 +112,9 @@ protected void markUpdated() {
mNeedUpdate = true;
// enqueue callback of NATIVE_ANIMATED_MODULE type as all view operations are executed in
// DISPATCH_UI type and we want the callback to be called right after in the same frame.
ReactChoreographer.getInstance()
.postFrameCallback(
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, mFrameCallback);
ReactChoreographer
.getInstance()
.postFrameCallback(ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, mFrameCallback);
}
}

Expand Down Expand Up @@ -165,8 +173,9 @@ private void setFragmentManager(FragmentManager fm) {
private void setupFragmentManager() {
ViewParent parent = this;
// We traverse view hierarchy up until we find screen parent or a root view
while (!(parent instanceof ReactRootView || parent instanceof Screen)
&& parent.getParent() != null) {
while (
!(parent instanceof ReactRootView || parent instanceof Screen) && parent.getParent() != null
) {
parent = parent.getParent();
}
// If parent is of type Screen it means we are inside a nested fragment structure.
Expand Down Expand Up @@ -195,7 +204,8 @@ private void setupFragmentManager() {
}
if (!(context instanceof FragmentActivity)) {
throw new IllegalStateException(
"In order to use RNScreens components your app's activity need to extend ReactFragmentActivity or ReactCompatActivity");
"In order to use RNScreens components your app's activity need to extend ReactFragmentActivity or ReactCompatActivity"
);
}
setFragmentManager(((FragmentActivity) context).getSupportFragmentManager());
}
Expand All @@ -213,18 +223,19 @@ protected void tryCommitTransaction() {
final FragmentTransaction transaction = mCurrentTransaction;
mProcessingTransaction = transaction;
mProcessingTransaction.runOnCommit(
new Runnable() {
@Override
public void run() {
if (mProcessingTransaction == transaction) {
// we need to take into account that commit is initiated with some other transaction
// while the previous one is still processing. In this case mProcessingTransaction
// gets overwritten and we don't want to set it to null until the second transaction
// is finished.
mProcessingTransaction = null;
}
new Runnable() {
@Override
public void run() {
if (mProcessingTransaction == transaction) {
// we need to take into account that commit is initiated with some other transaction
// while the previous one is still processing. In this case mProcessingTransaction
// gets overwritten and we don't want to set it to null until the second transaction
// is finished.
mProcessingTransaction = null;
}
});
}
}
);
mCurrentTransaction.commitAllowingStateLoss();
mCurrentTransaction = null;
}
Expand Down Expand Up @@ -266,8 +277,10 @@ private void removeMyFragments() {
boolean hasFragments = false;

for (Fragment fragment : mFragmentManager.getFragments()) {
if (fragment instanceof ScreenFragment
&& ((ScreenFragment) fragment).mScreenView.getContainer() == this) {
if (
fragment instanceof ScreenFragment &&
((ScreenFragment) fragment).mScreenView.getContainer() == this
) {
transaction.remove(fragment);
hasFragments = true;
}
Expand Down Expand Up @@ -349,8 +362,10 @@ protected void performUpdate() {
// detach screens that are no longer active
Set<Fragment> orphaned = new HashSet<>(mFragmentManager.getFragments());
for (ScreenFragment screenFragment : mScreenFragments) {
if (getActivityState(screenFragment) == Screen.ActivityState.INACTIVE
&& screenFragment.isAdded()) {
if (
getActivityState(screenFragment) == Screen.ActivityState.INACTIVE &&
screenFragment.isAdded()
) {
detachScreen(screenFragment);
}
orphaned.remove(screenFragment);
Expand Down
Loading

0 comments on commit 21ea886

Please sign in to comment.