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

View File

@@ -17,6 +17,7 @@ import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
@@ -192,20 +193,16 @@ public class CodeMirrorEditor extends FrameLayout {
public void loadFile(final File file) {
setProgress(true);
// TODO: 2017/9/29 handle error
Observable.fromCallable(new Callable<String>() {
@Override
public String call() throws Exception {
return PFiles.read(file);
}
}).subscribeOn(Schedulers.io())
Observable.fromCallable(() -> PFiles.read(file))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(@NonNull String s) throws Exception {
setText(s);
setProgress(false);
}
.subscribe(s -> {
setText(s);
setProgress(false);
}, err -> {
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_fail">重置失败</string>
<string name="text_reset_succeed">重置成功</string>
<string name="text_cannot_read_file" formatted="true">无法读取文件: %s</string>
<string-array name="ad_showing_mode_keys">