Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image-to-pdf: Filename InputField is autofilled with the filename #796

Merged
merged 2 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import static swati4star.createpdf.util.DialogUtils.createCustomDialog;
import static swati4star.createpdf.util.DialogUtils.createCustomDialogWithoutContent;
import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog;
import static swati4star.createpdf.util.FileUtils.getFileNameWithoutExtension;
import static swati4star.createpdf.util.ImageEnhancementOptionsUtils.getEnhancementOptions;
import static swati4star.createpdf.util.ImageUtils.mImageScaleType;
import static swati4star.createpdf.util.ImageUtils.showImageScaleTypeDialog;
Expand Down Expand Up @@ -262,9 +263,11 @@ void createPdf(boolean isgrayScale) {
mPdfOptions.setMasterPwd(mSharedPreferences.getString(MASTER_PWD_STRING, appName));
mPdfOptions.setPageColor(mPageColor);

String preFillName = mFileUtils.getLastFileName(mImagesUri);

MaterialDialog.Builder builder = createCustomDialog(mActivity,
R.string.creating_pdf, R.string.enter_file_name);
builder.input(getString(R.string.example), null, (dialog, input) -> {
builder.input(getString(R.string.example), preFillName, (dialog, input) -> {
if (StringUtils.isEmpty(input)) {
showSnackbar(mActivity, R.string.snackbar_name_not_blank);
} else {
Expand Down
33 changes: 33 additions & 0 deletions app/src/main/java/swati4star/createpdf/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,39 @@ public static String getFileDirectoryPath(String path) {
return path.substring(0, path.lastIndexOf(PATH_SEPERATOR) + 1);
}

/**
* Returns name of the last file with "_pdf" suffix.
* @param filesPath - ArrayList of image paths
* @return fileName with _pdf suffix
*/
public String getLastFileName(ArrayList<String> filesPath) {

if (filesPath.size() == 0)
return "";
String lastSelectedFilePath = filesPath.get(filesPath.size() - 1);
String nameWithoutExt = stripExtension(getFileNameWithoutExtension(lastSelectedFilePath));
return nameWithoutExt + mContext.getString(swati4star.createpdf.R.string.pdf_suffix);
}

/**
* Returns the filename without its extension
* @param fileNameWithExt fileName with extension. Ex: androidDev.jpg
* @return fileName without extension. Ex: androidDev
*/
private String stripExtension(String fileNameWithExt) {
// Handle null case specially.
if (fileNameWithExt == null) return null;

// Get position of last '.'.
int pos = fileNameWithExt.lastIndexOf(".");

// If there wasn't any '.' just return the string as is.
if (pos == -1) return fileNameWithExt;

// Otherwise return the string, up to the dot.
return fileNameWithExt.substring(0, pos);
}

/**
* Saves bitmap to external storage
* @param filename - name of the file
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,4 +522,7 @@
<string name="no_excel_file">No excel file selected</string>
<string name="split_range_alert">Entered split range will create Selected PDF again!</string>

<!--file name suffix-->
<string name="pdf_suffix" translatable="false">_pdf</string>

</resources>