fixed misleading dictionary export status messages
This commit is contained in:
parent
962792728f
commit
4c163f9038
6 changed files with 73 additions and 77 deletions
|
|
@ -25,9 +25,11 @@ public abstract class AbstractExporter {
|
||||||
final protected static String MIME_TYPE = "text/csv";
|
final protected static String MIME_TYPE = "text/csv";
|
||||||
|
|
||||||
protected Runnable failureHandler;
|
protected Runnable failureHandler;
|
||||||
|
protected Runnable startHandler;
|
||||||
protected ConsumerCompat<String> successHandler;
|
protected ConsumerCompat<String> successHandler;
|
||||||
protected Thread processThread;
|
private Thread processThread;
|
||||||
private String outputFile;
|
private String outputFile;
|
||||||
|
private String statusMessage = "";
|
||||||
|
|
||||||
|
|
||||||
public static AbstractExporter getInstance() {
|
public static AbstractExporter getInstance() {
|
||||||
|
|
@ -113,6 +115,18 @@ public abstract class AbstractExporter {
|
||||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? Environment.DIRECTORY_DOCUMENTS : Environment.DIRECTORY_DOWNLOADS;
|
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ? Environment.DIRECTORY_DOCUMENTS : Environment.DIRECTORY_DOWNLOADS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void sendFailure() {
|
||||||
|
if (failureHandler != null) {
|
||||||
|
failureHandler.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void sendStart(@NonNull String message) {
|
||||||
|
if (startHandler != null) {
|
||||||
|
statusMessage = message;
|
||||||
|
startHandler.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void sendSuccess() {
|
protected void sendSuccess() {
|
||||||
if (successHandler != null) {
|
if (successHandler != null) {
|
||||||
|
|
@ -120,30 +134,39 @@ public abstract class AbstractExporter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean export(@NonNull Activity activity) {
|
||||||
protected void sendFailure() {
|
if (isRunning()) {
|
||||||
if (failureHandler != null) {
|
return false;
|
||||||
failureHandler.run();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
processThread = new Thread(() -> exportSync(activity));
|
||||||
|
processThread.start();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isRunning() {
|
public boolean isRunning() {
|
||||||
return processThread != null && processThread.isAlive();
|
return processThread != null && processThread.isAlive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getStatusMessage() {
|
||||||
|
return statusMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFailureHandler(Runnable handler) {
|
||||||
|
failureHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStartHandler(Runnable handler) {
|
||||||
|
startHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
public void setSuccessHandler(ConsumerCompat<String> handler) {
|
public void setSuccessHandler(ConsumerCompat<String> handler) {
|
||||||
successHandler = handler;
|
successHandler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void setFailureHandler(Runnable handler) {
|
abstract protected void exportSync(Activity activity);
|
||||||
failureHandler = handler;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@NonNull abstract protected String generateFileName();
|
@NonNull abstract protected String generateFileName();
|
||||||
@NonNull abstract protected byte[] getWords(Activity activity) throws Exception;
|
@NonNull abstract protected byte[] getWords(Activity activity) throws Exception;
|
||||||
abstract public boolean export(Activity activity);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import android.database.sqlite.SQLiteDatabase;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import io.github.sspanak.tt9.R;
|
||||||
import io.github.sspanak.tt9.db.sqlite.ReadOps;
|
import io.github.sspanak.tt9.db.sqlite.ReadOps;
|
||||||
import io.github.sspanak.tt9.db.sqlite.SQLiteOpener;
|
import io.github.sspanak.tt9.db.sqlite.SQLiteOpener;
|
||||||
|
|
||||||
|
|
@ -22,22 +23,15 @@ public class CustomWordsExporter extends AbstractExporter {
|
||||||
return customWordsExporterSelf;
|
return customWordsExporterSelf;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean export(Activity activity) {
|
@Override
|
||||||
if (isRunning()) {
|
protected void exportSync(Activity activity) {
|
||||||
return false;
|
try {
|
||||||
|
sendStart(activity.getString(R.string.dictionary_export_generating_csv));
|
||||||
|
write(activity);
|
||||||
|
sendSuccess();
|
||||||
|
} catch (Exception e) {
|
||||||
|
sendFailure();
|
||||||
}
|
}
|
||||||
|
|
||||||
processThread = new Thread(() -> {
|
|
||||||
try {
|
|
||||||
write(activity);
|
|
||||||
sendSuccess();
|
|
||||||
} catch (Exception e) {
|
|
||||||
sendFailure();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
processThread.start();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import androidx.annotation.NonNull;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.Logger;
|
import io.github.sspanak.tt9.Logger;
|
||||||
|
import io.github.sspanak.tt9.R;
|
||||||
import io.github.sspanak.tt9.db.sqlite.ReadOps;
|
import io.github.sspanak.tt9.db.sqlite.ReadOps;
|
||||||
import io.github.sspanak.tt9.db.sqlite.SQLiteOpener;
|
import io.github.sspanak.tt9.db.sqlite.SQLiteOpener;
|
||||||
import io.github.sspanak.tt9.languages.Language;
|
import io.github.sspanak.tt9.languages.Language;
|
||||||
|
|
@ -28,20 +29,23 @@ public class DictionaryExporter extends AbstractExporter {
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean export(Activity activity) {
|
@Override
|
||||||
if (isRunning()) {
|
protected void exportSync(Activity activity) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (languages == null || languages.isEmpty()) {
|
if (languages == null || languages.isEmpty()) {
|
||||||
Logger.d(LOG_TAG, "Nothing to do");
|
Logger.d(LOG_TAG, "Nothing to do");
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
processThread = new Thread(() -> { for (Language l : languages) exportLanguage(activity, l); });
|
try {
|
||||||
processThread.start();
|
for (Language l : languages) {
|
||||||
|
sendStart(activity.getString(R.string.dictionary_export_generating_csv_for_language, l.getName()));
|
||||||
return true;
|
exportLanguage(activity, l);
|
||||||
|
}
|
||||||
|
sendSuccess();
|
||||||
|
} catch (Exception e) {
|
||||||
|
logExportError(e);
|
||||||
|
sendFailure();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DictionaryExporter setLanguages(ArrayList<Language> languages) {
|
public DictionaryExporter setLanguages(ArrayList<Language> languages) {
|
||||||
|
|
@ -66,21 +70,25 @@ public class DictionaryExporter extends AbstractExporter {
|
||||||
return new ReadOps().getWords(db, currentLanguage, false).getBytes();
|
return new ReadOps().getWords(db, currentLanguage, false).getBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void exportLanguage(Activity activity, Language language) {
|
private void exportLanguage(Activity activity, Language language) throws Exception {
|
||||||
currentLanguage = language;
|
currentLanguage = language;
|
||||||
if (currentLanguage == null) {
|
if (currentLanguage == null) {
|
||||||
Logger.e(LOG_TAG, "Cannot export dictionary for null language");
|
Logger.e(LOG_TAG, "Cannot export dictionary for null language");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
long start = System.currentTimeMillis();
|
||||||
long start = System.currentTimeMillis();
|
write(activity);
|
||||||
write(activity);
|
Logger.d(LOG_TAG, "All words for language '" + currentLanguage.getName() + "' exported. Time: " + (System.currentTimeMillis() - start) + "ms");
|
||||||
sendSuccess();
|
}
|
||||||
Logger.d(LOG_TAG, "All words for language '" + currentLanguage.getName() + "' exported. Time: " + (System.currentTimeMillis() - start) + "ms");
|
|
||||||
} catch (Exception e) {
|
private void logExportError(Exception e) {
|
||||||
sendFailure();
|
String message;
|
||||||
Logger.e(LOG_TAG, "Failed exporting dictionary for '" + currentLanguage.getName() + "' to '" + getOutputFile() + "'. " + e);
|
if (currentLanguage == null) {
|
||||||
|
message = "Failed exporting '" + getOutputFile() + "' . " + e;
|
||||||
|
} else {
|
||||||
|
message = "Failed exporting dictionary for '" + currentLanguage.getName() + "' to '" + getOutputFile() + "'. " + e;
|
||||||
}
|
}
|
||||||
|
Logger.e(LOG_TAG, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ abstract class ItemExportAbstract extends ItemClickable {
|
||||||
|
|
||||||
AbstractExporter exporter = getExporter();
|
AbstractExporter exporter = getExporter();
|
||||||
exporter.setFailureHandler(() -> onFinishExporting(null));
|
exporter.setFailureHandler(() -> onFinishExporting(null));
|
||||||
|
exporter.setStartHandler(() -> activity.runOnUiThread(this::setLoadingStatus));
|
||||||
exporter.setSuccessHandler(this::onFinishExporting);
|
exporter.setSuccessHandler(this::onFinishExporting);
|
||||||
refreshStatus();
|
refreshStatus();
|
||||||
}
|
}
|
||||||
|
|
@ -73,14 +74,11 @@ abstract class ItemExportAbstract extends ItemClickable {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
abstract protected String getLoadingMessage();
|
|
||||||
|
|
||||||
|
|
||||||
protected void setLoadingStatus() {
|
protected void setLoadingStatus() {
|
||||||
onStart.run();
|
onStart.run();
|
||||||
disable();
|
disable();
|
||||||
|
|
||||||
String loadingMessage = getLoadingMessage();
|
String loadingMessage = getExporter().getStatusMessage();
|
||||||
item.setSummary(loadingMessage);
|
item.setSummary(loadingMessage);
|
||||||
DictionaryNotification.getInstance(activity).showLoadingMessage(loadingMessage, "");
|
DictionaryNotification.getInstance(activity).showLoadingMessage(loadingMessage, "");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,28 +9,19 @@ import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||||
class ItemExportCustomWords extends ItemExportAbstract {
|
class ItemExportCustomWords extends ItemExportAbstract {
|
||||||
final public static String NAME = "dictionary_export_custom";
|
final public static String NAME = "dictionary_export_custom";
|
||||||
|
|
||||||
|
|
||||||
ItemExportCustomWords(Preference item, PreferencesActivity activity, Runnable onStart, Runnable onFinish) {
|
ItemExportCustomWords(Preference item, PreferencesActivity activity, Runnable onStart, Runnable onFinish) {
|
||||||
super(item, activity, onStart, onFinish);
|
super(item, activity, onStart, onFinish);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected CustomWordsExporter getExporter() {
|
protected CustomWordsExporter getExporter() {
|
||||||
return CustomWordsExporter.getInstance();
|
return CustomWordsExporter.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected boolean onStartExporting() {
|
protected boolean onStartExporting() {
|
||||||
return CustomWordsExporter.getInstance().export(activity);
|
return CustomWordsExporter.getInstance().export(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getLoadingMessage() {
|
|
||||||
return activity.getString(R.string.dictionary_export_generating_csv);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setReadyStatus() {
|
public void setReadyStatus() {
|
||||||
super.setReadyStatus();
|
super.setReadyStatus();
|
||||||
item.setSummary(activity.getString(
|
item.setSummary(activity.getString(
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,7 @@ package io.github.sspanak.tt9.preferences.screens.languages;
|
||||||
import androidx.preference.Preference;
|
import androidx.preference.Preference;
|
||||||
|
|
||||||
import io.github.sspanak.tt9.Logger;
|
import io.github.sspanak.tt9.Logger;
|
||||||
import io.github.sspanak.tt9.R;
|
|
||||||
import io.github.sspanak.tt9.db.exporter.DictionaryExporter;
|
import io.github.sspanak.tt9.db.exporter.DictionaryExporter;
|
||||||
import io.github.sspanak.tt9.languages.Language;
|
|
||||||
import io.github.sspanak.tt9.languages.LanguageCollection;
|
import io.github.sspanak.tt9.languages.LanguageCollection;
|
||||||
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
import io.github.sspanak.tt9.preferences.PreferencesActivity;
|
||||||
|
|
||||||
|
|
@ -16,7 +14,6 @@ class ItemExportDictionary extends ItemExportAbstract {
|
||||||
super(item, activity, onStart, onFinish);
|
super(item, activity, onStart, onFinish);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemExportAbstract refreshStatus() {
|
public ItemExportAbstract refreshStatus() {
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
|
|
@ -25,32 +22,17 @@ class ItemExportDictionary extends ItemExportAbstract {
|
||||||
return super.refreshStatus();
|
return super.refreshStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected DictionaryExporter getExporter() {
|
protected DictionaryExporter getExporter() {
|
||||||
return DictionaryExporter.getInstance();
|
return DictionaryExporter.getInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected boolean onStartExporting() {
|
protected boolean onStartExporting() {
|
||||||
return DictionaryExporter.getInstance()
|
return DictionaryExporter.getInstance()
|
||||||
.setLanguages(LanguageCollection.getAll(activity, activity.getSettings().getEnabledLanguageIds()))
|
.setLanguages(LanguageCollection.getAll(activity, activity.getSettings().getEnabledLanguageIds()))
|
||||||
.export(activity);
|
.export(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected String getLoadingMessage() {
|
|
||||||
String message = activity.getString(R.string.dictionary_export_generating_csv);
|
|
||||||
|
|
||||||
Language language = LanguageCollection.getLanguage(activity, activity.getSettings().getInputLanguage());
|
|
||||||
if (language != null) {
|
|
||||||
message = activity.getString(R.string.dictionary_export_generating_csv_for_language, language.getName());
|
|
||||||
}
|
|
||||||
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public void setReadyStatus() {
|
public void setReadyStatus() {
|
||||||
super.setReadyStatus();
|
super.setReadyStatus();
|
||||||
item.setSummary("");
|
item.setSummary("");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue