Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VO-428] feat: Call resolve on all Android's path for hide and show, and improve debug #4

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
Expand Down Expand Up @@ -31,6 +32,7 @@ public class RNBootSplashModule extends ReactContextBaseJavaModule implements Li

public static final String MODULE_NAME = "RNBootSplash";
private static final int ANIMATION_DURATION = 220;
private static final String TAG = "🌊 RNBootSplash";

private enum Status {
VISIBLE,
Expand Down Expand Up @@ -147,17 +149,19 @@ private void show(final RNBootSplashTask task) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run show " + task.getBootsplashName());
addBootsplashName(task.getBootsplashName());
final Activity activity = getReactApplicationContext().getCurrentActivity();
final Promise promise = task.getPromise();

if (activity == null || activity.isFinishing()) {
promise.resolve("activity_finishing");
waitAndShiftNextTask();
return;
}

if (activity.findViewById(R.id.bootsplash_layout_id) != null) {
promise.resolve(true); // splash screen is already visible
promise.resolve("already_visible"); // splash screen is already visible
shiftNextTask();
return;
}
Expand Down Expand Up @@ -201,25 +205,29 @@ private void hide(final RNBootSplashTask task) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d(TAG, "run hide " + task.getBootsplashName());
removeBootsplashName(task.getBootsplashName());

final Promise promise = task.getPromise();

if (hasBootsplashToDisplay()) {
promise.resolve("shift_next");
waitAndShiftNextTask(); // splash screen should still be displayed for some BootsplashNames
return;
}

final Activity activity = getReactApplicationContext().getCurrentActivity();
final Promise promise = task.getPromise();

if (activity == null || activity.isFinishing()) {
promise.resolve("activity_finishing");
waitAndShiftNextTask();
return;
}

final LinearLayout layout = activity.findViewById(R.id.bootsplash_layout_id);

if (layout == null) {
promise.resolve(true); // splash screen is already hidden
promise.resolve("already_hidden"); // splash screen is already hidden
shiftNextTask();
return;
}
Expand Down Expand Up @@ -262,6 +270,7 @@ public void show(final boolean fade, final String bootsplashName, final Promise
if (mDrawableResId == -1) {
promise.reject("uninitialized_module", "react-native-bootsplash has not been initialized");
} else {
Log.d(TAG, "show " + bootsplashName);
mTaskQueue.add(new RNBootSplashTask(RNBootSplashTask.Type.SHOW, fade, bootsplashName, promise));
shiftNextTask();
}
Expand All @@ -272,6 +281,7 @@ public void hide(final boolean fade, final String bootsplashName, final Promise
if (mDrawableResId == -1) {
promise.reject("uninitialized_module", "react-native-bootsplash has not been initialized");
} else {
Log.d(TAG, "hide " + bootsplashName);
mTaskQueue.add(new RNBootSplashTask(RNBootSplashTask.Type.HIDE, fade, bootsplashName, promise));
shiftNextTask();
}
Expand All @@ -297,12 +307,14 @@ protected void addBootsplashName(String name) {
if (!mBootsplashNames.contains(name)) {
mBootsplashNames.add(name);
}
Log.d(TAG, "mBootsplashNames = " + mBootsplashNames.toString());
}

protected void removeBootsplashName(String name) {
if (mBootsplashNames.contains(name)) {
mBootsplashNames.remove(name);
}
Log.d(TAG, "mBootsplashNames = " + mBootsplashNames.toString());
}

protected boolean hasBootsplashToDisplay() {
Expand Down
4 changes: 2 additions & 2 deletions dist/commonjs/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/commonjs/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/module/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/module/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions dist/typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ export declare type Config = {
fade?: boolean;
bootsplashName?: string;
};
export declare function show(config?: Config): Promise<void>;
export declare function hide(config?: Config): Promise<void>;
export declare type ResultStatus = boolean | "activity_finishing" | "already_visible" | "already_hidden" | "shift_next";
export declare function show(config?: Config): Promise<ResultStatus>;
export declare function hide(config?: Config): Promise<ResultStatus>;
export declare function getVisibilityStatus(): Promise<VisibilityStatus>;
declare const _default: {
show: typeof show;
Expand Down
8 changes: 5 additions & 3 deletions ios/RNBootSplash.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ + (void)addBootsplashName:(NSString * _Nonnull)name {
if ([_bootsplashNames indexOfObject:name] == NSNotFound) {
[_bootsplashNames addObject:name];
}
NSLog(@"🌊 RNBootSplash _bootsplashNames = %@", _bootsplashNames);
}

+ (void)removeBootsplashName:(NSString * _Nonnull)name {
if ([_bootsplashNames indexOfObject:name] != NSNotFound) {
[_bootsplashNames removeObject:name];
}
NSLog(@"🌊 RNBootSplash _bootsplashNames = %@", _bootsplashNames);
}

+ (BOOL)hasBootsplashToDisplay {
Expand Down Expand Up @@ -155,7 +157,7 @@ + (void)shiftNextTask {
+ (void)showWithTask:(RNBootSplashTask *)task {
[RNBootSplash addBootsplashName:task.bootsplashName];
if (_splashVC != nil) {
task.resolve(@(true)); // splash screen is already visible
task.resolve(@("already_visible")); // splash screen is already visible
[self shiftNextTask];
} else {
_status = RNBootSplashStatusTransitioningToVisible;
Expand All @@ -178,10 +180,10 @@ + (void)showWithTask:(RNBootSplashTask *)task {
+ (void)hideWithTask:(RNBootSplashTask *)task {
[RNBootSplash removeBootsplashName:task.bootsplashName];
if ([self hasBootsplashToDisplay]) {
task.resolve(@(true)); // splash screen should still be displayed for some BootsplashNames
task.resolve(@("shift_next")); // splash screen should still be displayed for some BootsplashNames
[self shiftNextTask];
} else if (_splashVC == nil) {
task.resolve(@(true)); // splash screen is already hidden
task.resolve(@("already_hidden")); // splash screen is already hidden
[self shiftNextTask];
} else {
_status = RNBootSplashStatusTransitioningToHidden;
Expand Down
18 changes: 12 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ import { NativeModules } from "react-native";

export type VisibilityStatus = "visible" | "hidden" | "transitioning";
export type Config = { fade?: boolean; bootsplashName?: string };
export type ResultStatus =
| boolean
| "activity_finishing"
| "already_visible"
| "already_hidden"
| "shift_next";

const NativeModule: {
show: (fade: boolean, bootsplashName: string) => Promise<true>;
hide: (fade: boolean, bootsplashName: string) => Promise<true>;
show: (fade: boolean, bootsplashName: string) => Promise<ResultStatus>;
hide: (fade: boolean, bootsplashName: string) => Promise<ResultStatus>;
getVisibilityStatus: () => Promise<VisibilityStatus>;
} = NativeModules.RNBootSplash;

export function show(config: Config = {}): Promise<void> {
export function show(config: Config = {}): Promise<ResultStatus> {
return NativeModule.show(
{ fade: false, ...config }.fade,
config.bootsplashName ?? "global",
).then(() => {});
).then((result) => result);
}

export function hide(config: Config = {}): Promise<void> {
export function hide(config: Config = {}): Promise<ResultStatus> {
return NativeModule.hide(
{ fade: false, ...config }.fade,
config.bootsplashName ?? "global",
).then(() => {});
).then((result) => result);
}

export function getVisibilityStatus(): Promise<VisibilityStatus> {
Expand Down