fix: crash on create directory

This commit is contained in:
hyb1996
2017-10-28 19:08:12 +08:00
parent 56c0ad3415
commit b6b204a057
3 changed files with 38 additions and 56 deletions

View File

@@ -110,51 +110,43 @@ public class ScriptOperations {
public Observable<String> importFile(final String pathFrom) { public Observable<String> importFile(final String pathFrom) {
return showFileNameInputDialog(PFiles.getNameWithoutExtension(pathFrom), PFiles.getExtension(pathFrom)) return showFileNameInputDialog(PFiles.getNameWithoutExtension(pathFrom), PFiles.getExtension(pathFrom))
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.map(new Function<String, String>() { .map(input -> {
@Override final String pathTo = getCurrentDirectoryPath() + input + "." + PFiles.getExtension(pathFrom);
public String apply(@io.reactivex.annotations.NonNull String s) throws Exception { if (PFiles.copy(pathFrom, pathTo)) {
final String pathTo = getCurrentDirectoryPath() + s + "." + PFiles.getExtension(pathFrom); showMessage(R.string.text_import_succeed);
if (PFiles.copy(pathFrom, pathTo)) { } else {
showMessage(R.string.text_import_succeed); showMessage(R.string.text_import_fail);
} else {
showMessage(R.string.text_import_fail);
}
mStorageFileProvider.notifyFileCreated(mCurrentDirectory, new ScriptFile(pathTo));
return pathTo;
} }
mStorageFileProvider.notifyFileCreated(mCurrentDirectory, new ScriptFile(pathTo));
return pathTo;
}); });
} }
public Observable<String> importFile(String prefix, final InputStream inputStream, final String ext) { public Observable<String> importFile(String prefix, final InputStream inputStream, final String ext) {
return showFileNameInputDialog(PFiles.getNameWithoutExtension(prefix), ext) return showFileNameInputDialog(PFiles.getNameWithoutExtension(prefix), ext)
.observeOn(Schedulers.io()) .observeOn(Schedulers.io())
.map(new Function<String, String>() { .map(input -> {
@Override final String pathTo = getCurrentDirectoryPath() + input + "." + ext;
public String apply(@io.reactivex.annotations.NonNull String s) throws Exception { if (PFiles.copyStream(inputStream, pathTo)) {
final String pathTo = getCurrentDirectoryPath() + s + "." + ext; showMessage(R.string.text_import_succeed);
if (PFiles.copyStream(inputStream, pathTo)) { } else {
showMessage(R.string.text_import_succeed); showMessage(R.string.text_import_fail);
} else {
showMessage(R.string.text_import_fail);
}
mStorageFileProvider.notifyFileCreated(mCurrentDirectory, new ScriptFile(pathTo));
return pathTo;
} }
mStorageFileProvider.notifyFileCreated(mCurrentDirectory, new ScriptFile(pathTo));
return pathTo;
}); });
} }
public void newDirectory() { public void newDirectory() {
showNameInputDialog("", new InputCallback()) showNameInputDialog("", new InputCallback())
.subscribe(new Consumer<String>() { .subscribe(path -> {
@Override ScriptFile newDir = new ScriptFile(getCurrentDirectory(), path);
public void accept(@io.reactivex.annotations.NonNull String path) throws Exception { if (newDir.mkdirs()) {
if (new ScriptFile(getCurrentDirectory(), path).mkdirs()) { showMessage(R.string.text_already_create);
showMessage(R.string.text_already_create); mStorageFileProvider.notifyFileCreated(mCurrentDirectory, new ScriptFile(newDir));
mStorageFileProvider.notifyFileCreated(mCurrentDirectory, new ScriptFile(path)); } else {
} else { showMessage(R.string.text_create_fail);
showMessage(R.string.text_create_fail);
}
} }
}); });
} }
@@ -164,12 +156,7 @@ public class ScriptOperations {
showMessageWithoutThreadSwitch(resId); showMessageWithoutThreadSwitch(resId);
} }
//switch to ui thread to show message //switch to ui thread to show message
App.getApp().getUiHandler().post(new Runnable() { App.getApp().getUiHandler().post(() -> showMessageWithoutThreadSwitch(resId));
@Override
public void run() {
showMessageWithoutThreadSwitch(resId);
}
});
} }
private void showMessageWithoutThreadSwitch(int resId) { private void showMessageWithoutThreadSwitch(int resId) {
@@ -191,12 +178,9 @@ public class ScriptOperations {
.inputType(InputType.TYPE_CLASS_TEXT) .inputType(InputType.TYPE_CLASS_TEXT)
.alwaysCallInputCallback() .alwaysCallInputCallback()
.input(getString(R.string.text_please_input_name), prefix, false, textWatcher) .input(getString(R.string.text_please_input_name), prefix, false, textWatcher)
.onPositive(new MaterialDialog.SingleButtonCallback() { .onPositive((dialog, which) -> {
@Override input.onNext(dialog.getInputEditText().getText().toString());
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { input.onComplete();
input.onNext(dialog.getInputEditText().getText().toString());
input.onComplete();
}
}) })
.build()); .build());
return input; return input;

View File

@@ -17,6 +17,7 @@ import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog; import com.afollestad.materialdialogs.MaterialDialog;
@@ -192,20 +193,16 @@ public class CodeMirrorEditor extends FrameLayout {
public void loadFile(final File file) { public void loadFile(final File file) {
setProgress(true); setProgress(true);
// TODO: 2017/9/29 handle error Observable.fromCallable(() -> PFiles.read(file))
Observable.fromCallable(new Callable<String>() { .subscribeOn(Schedulers.io())
@Override
public String call() throws Exception {
return PFiles.read(file);
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() { .subscribe(s -> {
@Override setText(s);
public void accept(@NonNull String s) throws Exception { setProgress(false);
setText(s); }, err -> {
setProgress(false); err.printStackTrace();
} Toast.makeText(getContext(), getContext().getString(R.string.text_cannot_read_file, file.getPath()),
Toast.LENGTH_SHORT).show();
}); });
} }

View File

@@ -280,6 +280,7 @@
<string name="text_reset_to_initial_content">重置为初始内容</string> <string name="text_reset_to_initial_content">重置为初始内容</string>
<string name="text_reset_fail">重置失败</string> <string name="text_reset_fail">重置失败</string>
<string name="text_reset_succeed">重置成功</string> <string name="text_reset_succeed">重置成功</string>
<string name="text_cannot_read_file" formatted="true">无法读取文件: %s</string>
<string-array name="ad_showing_mode_keys"> <string-array name="ad_showing_mode_keys">