Skip to content

Commit

Permalink
fix: show progress dialog first before start trimming
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 authored Sep 8, 2024
1 parent 1c5bd9c commit b15f733
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 48 deletions.
80 changes: 36 additions & 44 deletions android/src/main/java/com/videotrim/VideoTrimModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,14 +260,6 @@ public void onLoad(int duration) {
sendEvent(getReactApplicationContext(), "onLoad", map);
}

@Override
public void onStartTrim() {
sendEvent(getReactApplicationContext(), "onStartTrimming", null);
runOnUiThread(() -> {
buildDialog();
});
}

@Override
public void onTrimmingProgress(int percentage) {
// prevent onTrimmingProgress is called after onFinishTrim (some rare cases)
Expand Down Expand Up @@ -361,7 +353,7 @@ public void onCancel() {
@Override
public void onSave() {
if (!enableSaveDialog) {
trimmerView.onSaveClicked();
startTrim();
return;
}

Expand All @@ -371,7 +363,7 @@ public void onSave() {
builder.setCancelable(false);
builder.setPositiveButton(saveDialogConfirmText, (dialog, which) -> {
dialog.cancel();
trimmerView.onSaveClicked();
startTrim();
});
builder.setNegativeButton(saveDialogCancelText, (dialog, which) -> {
dialog.cancel();
Expand All @@ -390,32 +382,7 @@ public void onStatistics(WritableMap statistics) {
sendEvent(getReactApplicationContext(), "onStatistics", statistics);
}

private void hideDialog(boolean shouldCloseEditor) {
// handle the case when the cancel dialog is still showing but the trimming is finished
if (cancelTrimmingConfirmDialog != null) {
if (cancelTrimmingConfirmDialog.isShowing()) {
cancelTrimmingConfirmDialog.dismiss();
}
cancelTrimmingConfirmDialog = null;
}

if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) mProgressDialog.dismiss();
mProgressBar = null;
mProgressDialog = null;
}

if (shouldCloseEditor) {
if (alertDialog != null) {
if (alertDialog.isShowing()) {
alertDialog.dismiss();
}
alertDialog = null;
}
}
}

private void buildDialog() {
private void startTrim() {
Activity activity = getReactApplicationContext().getCurrentActivity();
// Create the parent layout for the dialog
LinearLayout layout = new LinearLayout(activity);
Expand Down Expand Up @@ -469,10 +436,7 @@ private void buildDialog() {
builder.setTitle(cancelTrimmingDialogTitle);
builder.setCancelable(false);
builder.setPositiveButton(cancelTrimmingDialogConfirmText, (dialog, which) -> {
if (trimmerView != null) {
// prevent trimmerView is null in some rare cases
trimmerView.onCancelTrimClicked();
}
trimmerView.onCancelTrimClicked();

if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
Expand All @@ -484,10 +448,7 @@ private void buildDialog() {
cancelTrimmingConfirmDialog = builder.create();
cancelTrimmingConfirmDialog.show();
} else {
if (trimmerView != null) {
// prevent trimmerView is null in some rare cases
trimmerView.onCancelTrimClicked();
}
trimmerView.onCancelTrimClicked();

if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
Expand All @@ -504,9 +465,40 @@ private void buildDialog() {

// Show the dialog
mProgressDialog = builder.create();

mProgressDialog.setOnShowListener(dialog -> {
sendEvent(getReactApplicationContext(), "onStartTrimming", null);
trimmerView.onSaveClicked();
});

mProgressDialog.show();
}

private void hideDialog(boolean shouldCloseEditor) {
// handle the case when the cancel dialog is still showing but the trimming is finished
if (cancelTrimmingConfirmDialog != null) {
if (cancelTrimmingConfirmDialog.isShowing()) {
cancelTrimmingConfirmDialog.dismiss();
}
cancelTrimmingConfirmDialog = null;
}

if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) mProgressDialog.dismiss();
mProgressBar = null;
mProgressDialog = null;
}

if (shouldCloseEditor) {
if (alertDialog != null) {
if (alertDialog.isShowing()) {
alertDialog.dismiss();
}
alertDialog = null;
}
}
}

@ReactMethod
public void addListener(String eventName) {
// This method is required by React
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public interface VideoTrimListener {
void onLoad(int duration);
void onStartTrim();
void onTrimmingProgress(int percentage);
void onFinishTrim(String url, long startMs, long endMs, int videoDuration);
void onCancelTrim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public static FFmpegSession trim(String inputFile, String outputFile, int videoD
// Format the current date and time
String formattedDateTime = dateFormat.format(currentDate);

callback.onStartTrim();

String[] cmds = {
"-ss",
startMs + "ms",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-video-trim",
"version": "2.2.2",
"version": "2.2.3",
"description": "Video trimmer for your React Native app",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit b15f733

Please sign in to comment.