From 677aa538ff880fb404dcf9550220deb05639ab4b Mon Sep 17 00:00:00 2001 From: Robin Singh Date: Sat, 1 Jun 2019 20:44:50 +0530 Subject: [PATCH] #774, #775 - Font size and family enchancement option added in add text (#776) --- .../createpdf/fragment/AddTextFragment.java | 166 ++++++++++++++++-- .../util/AddTextEnhancementOptionsUtils.java | 27 +++ app/src/main/res/layout/fragment_add_text.xml | 102 +++++++---- 3 files changed, 250 insertions(+), 45 deletions(-) create mode 100644 app/src/main/java/swati4star/createpdf/util/AddTextEnhancementOptionsUtils.java diff --git a/app/src/main/java/swati4star/createpdf/fragment/AddTextFragment.java b/app/src/main/java/swati4star/createpdf/fragment/AddTextFragment.java index a2a77b233..d6e90c16d 100644 --- a/app/src/main/java/swati4star/createpdf/fragment/AddTextFragment.java +++ b/app/src/main/java/swati4star/createpdf/fragment/AddTextFragment.java @@ -12,27 +12,29 @@ import android.support.design.widget.BottomSheetBehavior; import android.support.v4.app.Fragment; import android.support.v7.preference.PreferenceManager; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; +import android.widget.RadioButton; +import android.widget.RadioGroup; import android.widget.RelativeLayout; import com.afollestad.materialdialogs.MaterialDialog; import com.airbnb.lottie.LottieAnimationView; import com.dd.morphingbutton.MorphingButton; import com.itextpdf.text.Document; -import com.itextpdf.text.Element; -import com.itextpdf.text.PageSize; +import com.itextpdf.text.Font; +import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; -import com.itextpdf.text.Rectangle; -import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; -import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; import java.io.BufferedReader; @@ -46,16 +48,19 @@ import butterknife.ButterKnife; import butterknife.OnClick; import swati4star.createpdf.R; +import swati4star.createpdf.adapter.EnhancementOptionsAdapter; import swati4star.createpdf.adapter.MergeFilesAdapter; import swati4star.createpdf.interfaces.BottomSheetPopulate; import swati4star.createpdf.interfaces.OnBackPressedInterface; +import swati4star.createpdf.interfaces.OnItemClickListner; +import swati4star.createpdf.model.EnhancementOptionsEntity; import swati4star.createpdf.util.BottomSheetCallback; import swati4star.createpdf.util.BottomSheetUtils; +import swati4star.createpdf.util.Constants; import swati4star.createpdf.util.FileUtils; import swati4star.createpdf.util.MorphButtonUtility; import swati4star.createpdf.util.PermissionsUtils; import swati4star.createpdf.util.RealPathUtil; -import swati4star.createpdf.util.SettingsOptions; import swati4star.createpdf.util.StringUtils; import static android.app.Activity.RESULT_OK; @@ -65,22 +70,24 @@ import static swati4star.createpdf.util.Constants.READ_WRITE_PERMISSIONS; import static swati4star.createpdf.util.Constants.STORAGE_LOCATION; import static swati4star.createpdf.util.Constants.pdfExtension; -import static swati4star.createpdf.util.Constants.textExtension; import static swati4star.createpdf.util.DialogUtils.createOverwriteDialog; import static swati4star.createpdf.util.StringUtils.getDefaultStorageLocation; import static swati4star.createpdf.util.StringUtils.getSnackbarwithAction; import static swati4star.createpdf.util.StringUtils.showSnackbar; +import static swati4star.createpdf.util.AddTextEnhancementOptionsUtils.getEnhancementOptions; public class AddTextFragment extends Fragment implements MergeFilesAdapter.OnClickListener, - BottomSheetPopulate, OnBackPressedInterface { + BottomSheetPopulate, OnBackPressedInterface, OnItemClickListner { private Activity mActivity; private String mPdfpath; + private String mFontTitle; private String mTextPath; private FileUtils mFileUtils; private MorphButtonUtility mMorphButtonUtility; private BottomSheetUtils mBottomSheetUtils; private SharedPreferences mSharedPreferences; private boolean mPermissionGranted; + private int mFontSize = 0; private static final int INTENT_REQUEST_PICK_PDF_FILE_CODE = 10; private static final int INTENT_REQUEST_PICK_TEXT_FILE_CODE = 0; private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT = 1; @@ -104,6 +111,12 @@ public class AddTextFragment extends Fragment implements MergeFilesAdapter.OnCli RelativeLayout mLayout; @BindView(R.id.lottie_progress) LottieAnimationView mLottieProgress; + @BindView(R.id.enhancement_options_recycle_view_text) + RecyclerView mTextEnhancementOptionsRecycleView; + + private ArrayList mTextEnhancementOptionsEntityArrayList; + private EnhancementOptionsAdapter mTextEnhancementOptionsAdapter; + private Font.FontFamily mFontFamily; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -112,14 +125,31 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, View rootView = inflater.inflate(R.layout.fragment_add_text, container, false); ButterKnife.bind(this, rootView); mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mActivity); + mFontTitle = String.format(getString(R.string.edit_font_size), + mSharedPreferences.getInt(Constants.DEFAULT_FONT_SIZE_TEXT, Constants.DEFAULT_FONT_SIZE)); + mFontFamily = Font.FontFamily.valueOf(mSharedPreferences.getString(Constants.DEFAULT_FONT_FAMILY_TEXT, + Constants.DEFAULT_FONT_FAMILY)); + mFontSize = mSharedPreferences.getInt(Constants.DEFAULT_FONT_SIZE_TEXT, Constants.DEFAULT_FONT_SIZE); sheetBehavior = BottomSheetBehavior.from(layoutBottomSheet); mBottomSheetUtils.populateBottomSheetWithPDFs(this); + showEnhancementOptions(); mLottieProgress.setVisibility(View.VISIBLE); sheetBehavior.setBottomSheetCallback(new BottomSheetCallback(mUpArrow, isAdded())); resetView(); return rootView; } + /** + * Function to show the enhancement options. + */ + private void showEnhancementOptions() { + GridLayoutManager mGridLayoutManager = new GridLayoutManager(mActivity, 2); + mTextEnhancementOptionsRecycleView.setLayoutManager(mGridLayoutManager); + mTextEnhancementOptionsEntityArrayList = getEnhancementOptions(mActivity, mFontTitle, mFontFamily); + mTextEnhancementOptionsAdapter = new EnhancementOptionsAdapter(this, mTextEnhancementOptionsEntityArrayList); + mTextEnhancementOptionsRecycleView.setAdapter(mTextEnhancementOptionsAdapter); + } + @Override public void onAttach(Context context) { super.onAttach(context); @@ -173,10 +203,10 @@ public void openPdfNameDialog() { } else { final String inputName = input.toString(); if (!mFileUtils.isFileExist(inputName + getString(R.string.pdf_ext))) { - addText(inputName); + addText(inputName, mFontSize, mFontFamily); } else { MaterialDialog.Builder builder = createOverwriteDialog(mActivity); - builder.onPositive((dialog12, which) -> addText(inputName)) + builder.onPositive((dialog12, which) -> addText(inputName, mFontSize, mFontFamily)) .onNegative((dialog1, which) -> openPdfNameDialog()) .show(); } @@ -221,6 +251,11 @@ public void resetView() { mPdfpath = mTextPath = null; mMorphButtonUtility.morphToGrey(mCreateTextPDF, mMorphButtonUtility.integer()); mCreateTextPDF.setEnabled(false); + mFontSize = mSharedPreferences.getInt(Constants.DEFAULT_FONT_SIZE_TEXT, Constants.DEFAULT_FONT_SIZE); + mFontFamily = Font.FontFamily.valueOf(mSharedPreferences.getString(Constants.DEFAULT_FONT_FAMILY_TEXT, + Constants.DEFAULT_FONT_FAMILY)); + showFontSize(); + showFontFamily(); } /** @@ -229,7 +264,7 @@ public void resetView() { * * @param fileName - the name of the new pdf that is to be created. */ - private void addText(String fileName) { + private void addText(String fileName, int fontsize, Font.FontFamily fontFamily) { String mStorePath = mSharedPreferences.getString(STORAGE_LOCATION, getDefaultStorageLocation()); String mPath = mStorePath + fileName + pdfExtension; @@ -260,7 +295,8 @@ private void addText(String fileName) { } document.setPageSize(pdfReader.getPageSize(1)); document.newPage(); - document.add(new Paragraph(text.toString())); + document.add(new Paragraph(new Paragraph(text.toString(), + FontFactory.getFont(fontFamily.name(), fontsize)))); document.close(); getSnackbarwithAction(mActivity, R.string.snackbar_pdfCreated) @@ -310,4 +346,110 @@ public void closeBottomSheet() { public boolean checkSheetBehaviour() { return checkSheetBehaviourUtil(sheetBehavior); } + + @Override + public void onItemClick(int position) { + switch (position) { + case 0: + editFontSize(); + break; + case 1: + changeFontFamily(); + break; + } + + } + + /** + * Function to take the font size of pdf as user input + */ + private void editFontSize() { + new MaterialDialog.Builder(mActivity) + .title(mFontTitle) + .customView(R.layout.dialog_font_size, true) + .positiveText(R.string.ok) + .negativeText(R.string.cancel) + .onPositive((dialog, which) -> { + final EditText fontInput = dialog.getCustomView().findViewById(R.id.fontInput); + final CheckBox cbSetDefault = dialog.getCustomView().findViewById(R.id.cbSetFontDefault); + try { + int check = Integer.parseInt(String.valueOf(fontInput.getText())); + if (check > 1000 || check < 0) { + showSnackbar(mActivity, R.string.invalid_entry); + } else { + mFontSize = check; + showFontSize(); + showSnackbar(mActivity, R.string.font_size_changed); + if (cbSetDefault.isChecked()) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putInt(Constants.DEFAULT_FONT_SIZE_TEXT, mFontSize); + editor.apply(); + mFontTitle = String.format(getString(R.string.edit_font_size), + mSharedPreferences.getInt(Constants.DEFAULT_FONT_SIZE_TEXT, + Constants.DEFAULT_FONT_SIZE)); + } + } + } catch (NumberFormatException e) { + showSnackbar(mActivity, R.string.invalid_entry); + } + }) + .show(); + } + + /** + * Displays font size in UI + */ + private void showFontSize() { + mTextEnhancementOptionsEntityArrayList.get(0) + .setName(String.format(getString(R.string.font_size), String.valueOf(mFontSize))); + mTextEnhancementOptionsAdapter.notifyDataSetChanged(); + } + + /** + * Shows dialog to change font size + */ + private void changeFontFamily() { + String fontFamily = mSharedPreferences.getString(Constants.DEFAULT_FONT_FAMILY_TEXT, + Constants.DEFAULT_FONT_FAMILY); + int ordinal = Font.FontFamily.valueOf(fontFamily).ordinal(); + MaterialDialog materialDialog = new MaterialDialog.Builder(mActivity) + .title(String.format(getString(R.string.default_font_family_text), fontFamily)) + .customView(R.layout.dialog_font_family, true) + .positiveText(R.string.ok) + .negativeText(R.string.cancel) + .onPositive((dialog, which) -> { + View view = dialog.getCustomView(); + RadioGroup radioGroup = view.findViewById(R.id.radio_group_font_family); + int selectedId = radioGroup.getCheckedRadioButtonId(); + RadioButton radioButton = view.findViewById(selectedId); + String fontFamily1 = radioButton.getText().toString(); + mFontFamily = Font.FontFamily.valueOf(fontFamily1); + final CheckBox cbSetDefault = view.findViewById(R.id.cbSetDefault); + if (cbSetDefault.isChecked()) { + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putString(Constants.DEFAULT_FONT_FAMILY_TEXT, fontFamily1); + editor.apply(); + } + showFontFamily(); + }) + .build(); + RadioGroup radioGroup = materialDialog.getCustomView().findViewById(R.id.radio_group_font_family); + RadioButton rb = (RadioButton) radioGroup.getChildAt(ordinal); + rb.setChecked(true); + materialDialog.show(); + } + + + /** + * Displays font family in UI + */ + private void showFontFamily() { + mTextEnhancementOptionsEntityArrayList.get(1) + .setName(getString(R.string.font_family_text) + mFontFamily.name()); + mTextEnhancementOptionsAdapter.notifyDataSetChanged(); + } + + + } + diff --git a/app/src/main/java/swati4star/createpdf/util/AddTextEnhancementOptionsUtils.java b/app/src/main/java/swati4star/createpdf/util/AddTextEnhancementOptionsUtils.java new file mode 100644 index 000000000..2ee1ea962 --- /dev/null +++ b/app/src/main/java/swati4star/createpdf/util/AddTextEnhancementOptionsUtils.java @@ -0,0 +1,27 @@ +package swati4star.createpdf.util; + +import android.content.Context; + +import com.itextpdf.text.Font; + +import java.util.ArrayList; + +import swati4star.createpdf.R; +import swati4star.createpdf.model.EnhancementOptionsEntity; + +public class AddTextEnhancementOptionsUtils { + + public static ArrayList getEnhancementOptions(Context context, + String fontTitle, + Font.FontFamily fontFamily) { + ArrayList options = new ArrayList<>(); + + options.add(new EnhancementOptionsEntity( + context.getResources().getDrawable(R.drawable.ic_font_black_24dp), + fontTitle)); + options.add(new EnhancementOptionsEntity( + context, R.drawable.ic_font_family_24dp, + String.format(context.getString(R.string.default_font_family_text), fontFamily.name()))); + return options; + } +} diff --git a/app/src/main/res/layout/fragment_add_text.xml b/app/src/main/res/layout/fragment_add_text.xml index 6e1d5e026..13ae50eaf 100644 --- a/app/src/main/res/layout/fragment_add_text.xml +++ b/app/src/main/res/layout/fragment_add_text.xml @@ -4,46 +4,82 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + android:orientation="vertical"> - - - - - + + + + + + + + + - + android:layout_gravity="bottom" + android:gravity="bottom|center" + android:orientation="vertical" + tools:targetApi="lollipop"> + + + + + + + +