Skip to content

Commit

Permalink
#774, #775 - Font size and family enchancement option added in add te…
Browse files Browse the repository at this point in the history
…xt (#776)
  • Loading branch information
rob729 authored and vyankatesh24 committed Jun 1, 2019
1 parent b9936a2 commit 677aa53
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 45 deletions.
166 changes: 154 additions & 12 deletions app/src/main/java/swati4star/createpdf/fragment/AddTextFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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<EnhancementOptionsEntity> mTextEnhancementOptionsEntityArrayList;
private EnhancementOptionsAdapter mTextEnhancementOptionsAdapter;
private Font.FontFamily mFontFamily;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
}



}

Original file line number Diff line number Diff line change
@@ -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<EnhancementOptionsEntity> getEnhancementOptions(Context context,
String fontTitle,
Font.FontFamily fontFamily) {
ArrayList<EnhancementOptionsEntity> 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;
}
}
Loading

0 comments on commit 677aa53

Please sign in to comment.