Skip to content

Commit

Permalink
Merge pull request #1047 from PhenoApps/cloud_import_fixes
Browse files Browse the repository at this point in the history
Cloud Import Fixes
  • Loading branch information
trife authored Sep 13, 2024
2 parents 2d86350 + bd7d9af commit 038b900
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -767,25 +767,33 @@ private void showFieldFileDialog(final String chosenFile, Boolean isCloud) {

DocumentFile importDoc = DocumentFile.fromSingleUri(this, docUri);

if (importDoc != null && importDoc.exists()) {
if (importDoc != null) {

ContentResolver resolver = getContentResolver();
if (resolver != null) {

String cloudName = null;
if (isCloud != null && isCloud) {
cloudName = getFileName(Uri.parse(chosenFile));
} else {
if (!importDoc.exists()) return;
}

try (InputStream is = resolver.openInputStream(docUri)) {

fieldFile = FieldFileObject.create(this, docUri, is, cloudName);

String fieldFileName = fieldFile.getStem();
if (isCloud != null && isCloud) {
int index = cloudName.lastIndexOf(".");
if (index > -1) {
cloudName = cloudName.substring(0, index);
}
fieldFile.setName(cloudName);
}

Editor e = preferences.edit();
e.putString(GeneralKeys.FIELD_FILE, fieldFileName);
e.putString(GeneralKeys.FIELD_ALIAS, fieldFileName);
e.apply();

if (database.checkFieldName(fieldFileName) >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,21 +598,30 @@ private void showFieldFileDialog(final String chosenFile, Boolean isCloud) {

DocumentFile importDoc = DocumentFile.fromSingleUri(this, docUri);

if (importDoc != null && importDoc.exists()) {
if (importDoc != null) {

ContentResolver resolver = getContentResolver();
if (resolver != null) {

String cloudName = null;
if (isCloud != null && isCloud) {
cloudName = getFileName(Uri.parse(chosenFile));
} else {
if (!importDoc.exists()) return;
}

try (InputStream is = resolver.openInputStream(docUri)) {

fieldFile = FieldFileObject.create(this, docUri, is, cloudName);

String fieldFileName = fieldFile.getStem();
if (isCloud != null && isCloud) {
int index = cloudName.lastIndexOf(".");
if (index > -1) {
cloudName = cloudName.substring(0, index);
}
fieldFile.setName(cloudName);
}

Editor e = preferences.edit();
e.putString(GeneralKeys.FIELD_FILE, fieldFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,27 @@ public static String getExtension(final String path) {
return path.substring(first).toLowerCase();
}

public static String getExtensionFromClass(FieldFileBase fieldFile) {

if (fieldFile instanceof FieldFileCSV) {
return "csv";
} else if (fieldFile instanceof FieldFileExcel) {
return "xls";
} else if (fieldFile instanceof FieldFileXlsx) {
return "xlsx";
} else {
return "";
}
}

public abstract static class FieldFileBase {
boolean openFail;
boolean specialCharactersFail;
private final Uri path_;
private final Context ctx;

private String name;

FieldFileBase(final Context ctx, final Uri path) {
this.ctx = ctx;
path_ = path;
Expand Down Expand Up @@ -146,17 +161,28 @@ public final boolean hasSpecialCharacters() {

public FieldObject createFieldObject() {
FieldObject f = new FieldObject();
f.setExp_name(this.getStem());
f.setExp_alias(this.getStem());
f.setExp_source(this.getFileStem());
f.setImport_format(ImportFormat.fromString(getExtension(this.getFileStem())));
if (name == null) {
f.setExp_name(this.getStem());
f.setExp_alias(this.getStem());
f.setExp_source(this.getFileStem());
f.setImport_format(ImportFormat.fromString(getExtension(this.getFileStem())));
} else {
f.setExp_name(name);
f.setExp_alias(name);
f.setExp_source(name + "." + getExtensionFromClass(this));
f.setImport_format(ImportFormat.fromString(getExtensionFromClass(this)));
}
return f;
}

public boolean getOpenFailed() {
return openFail;
}

public void setName(String name) { this.name = name; }

public String getName() { return name; }

abstract public boolean isCSV();

abstract public boolean isExcel();
Expand Down

0 comments on commit 038b900

Please sign in to comment.