1212import android .support .design .widget .BottomSheetBehavior ;
1313import android .support .v4 .app .Fragment ;
1414import android .support .v7 .preference .PreferenceManager ;
15+ import android .support .v7 .widget .GridLayoutManager ;
1516import android .support .v7 .widget .RecyclerView ;
1617import android .view .LayoutInflater ;
1718import android .view .View ;
1819import android .view .ViewGroup ;
20+ import android .widget .CheckBox ;
21+ import android .widget .EditText ;
1922import android .widget .ImageView ;
2023import android .widget .LinearLayout ;
24+ import android .widget .RadioButton ;
25+ import android .widget .RadioGroup ;
2126import android .widget .RelativeLayout ;
2227
2328import com .afollestad .materialdialogs .MaterialDialog ;
2429import com .airbnb .lottie .LottieAnimationView ;
2530import com .dd .morphingbutton .MorphingButton ;
2631import com .itextpdf .text .Document ;
27- import com .itextpdf .text .Element ;
28- import com .itextpdf .text .PageSize ;
32+ import com .itextpdf .text .Font ;
33+ import com .itextpdf .text .FontFactory ;
2934import com .itextpdf .text .Paragraph ;
30- import com .itextpdf .text .Rectangle ;
31- import com .itextpdf .text .pdf .BaseFont ;
3235import com .itextpdf .text .pdf .PdfContentByte ;
3336import com .itextpdf .text .pdf .PdfImportedPage ;
3437import com .itextpdf .text .pdf .PdfReader ;
35- import com .itextpdf .text .pdf .PdfStamper ;
3638import com .itextpdf .text .pdf .PdfWriter ;
3739
3840import java .io .BufferedReader ;
4648import butterknife .ButterKnife ;
4749import butterknife .OnClick ;
4850import swati4star .createpdf .R ;
51+ import swati4star .createpdf .adapter .EnhancementOptionsAdapter ;
4952import swati4star .createpdf .adapter .MergeFilesAdapter ;
5053import swati4star .createpdf .interfaces .BottomSheetPopulate ;
5154import swati4star .createpdf .interfaces .OnBackPressedInterface ;
55+ import swati4star .createpdf .interfaces .OnItemClickListner ;
56+ import swati4star .createpdf .model .EnhancementOptionsEntity ;
5257import swati4star .createpdf .util .BottomSheetCallback ;
5358import swati4star .createpdf .util .BottomSheetUtils ;
59+ import swati4star .createpdf .util .Constants ;
5460import swati4star .createpdf .util .FileUtils ;
5561import swati4star .createpdf .util .MorphButtonUtility ;
5662import swati4star .createpdf .util .PermissionsUtils ;
5763import swati4star .createpdf .util .RealPathUtil ;
58- import swati4star .createpdf .util .SettingsOptions ;
5964import swati4star .createpdf .util .StringUtils ;
6065
6166import static android .app .Activity .RESULT_OK ;
6570import static swati4star .createpdf .util .Constants .READ_WRITE_PERMISSIONS ;
6671import static swati4star .createpdf .util .Constants .STORAGE_LOCATION ;
6772import static swati4star .createpdf .util .Constants .pdfExtension ;
68- import static swati4star .createpdf .util .Constants .textExtension ;
6973import static swati4star .createpdf .util .DialogUtils .createOverwriteDialog ;
7074import static swati4star .createpdf .util .StringUtils .getDefaultStorageLocation ;
7175import static swati4star .createpdf .util .StringUtils .getSnackbarwithAction ;
7276import static swati4star .createpdf .util .StringUtils .showSnackbar ;
77+ import static swati4star .createpdf .util .AddTextEnhancementOptionsUtils .getEnhancementOptions ;
7378
7479public class AddTextFragment extends Fragment implements MergeFilesAdapter .OnClickListener ,
75- BottomSheetPopulate , OnBackPressedInterface {
80+ BottomSheetPopulate , OnBackPressedInterface , OnItemClickListner {
7681 private Activity mActivity ;
7782 private String mPdfpath ;
83+ private String mFontTitle ;
7884 private String mTextPath ;
7985 private FileUtils mFileUtils ;
8086 private MorphButtonUtility mMorphButtonUtility ;
8187 private BottomSheetUtils mBottomSheetUtils ;
8288 private SharedPreferences mSharedPreferences ;
8389 private boolean mPermissionGranted ;
90+ private int mFontSize = 0 ;
8491 private static final int INTENT_REQUEST_PICK_PDF_FILE_CODE = 10 ;
8592 private static final int INTENT_REQUEST_PICK_TEXT_FILE_CODE = 0 ;
8693 private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE_RESULT = 1 ;
@@ -104,6 +111,12 @@ public class AddTextFragment extends Fragment implements MergeFilesAdapter.OnCli
104111 RelativeLayout mLayout ;
105112 @ BindView (R .id .lottie_progress )
106113 LottieAnimationView mLottieProgress ;
114+ @ BindView (R .id .enhancement_options_recycle_view_text )
115+ RecyclerView mTextEnhancementOptionsRecycleView ;
116+
117+ private ArrayList <EnhancementOptionsEntity > mTextEnhancementOptionsEntityArrayList ;
118+ private EnhancementOptionsAdapter mTextEnhancementOptionsAdapter ;
119+ private Font .FontFamily mFontFamily ;
107120
108121 @ Override
109122 public View onCreateView (LayoutInflater inflater , ViewGroup container ,
@@ -112,14 +125,31 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
112125 View rootView = inflater .inflate (R .layout .fragment_add_text , container , false );
113126 ButterKnife .bind (this , rootView );
114127 mSharedPreferences = PreferenceManager .getDefaultSharedPreferences (mActivity );
128+ mFontTitle = String .format (getString (R .string .edit_font_size ),
129+ mSharedPreferences .getInt (Constants .DEFAULT_FONT_SIZE_TEXT , Constants .DEFAULT_FONT_SIZE ));
130+ mFontFamily = Font .FontFamily .valueOf (mSharedPreferences .getString (Constants .DEFAULT_FONT_FAMILY_TEXT ,
131+ Constants .DEFAULT_FONT_FAMILY ));
132+ mFontSize = mSharedPreferences .getInt (Constants .DEFAULT_FONT_SIZE_TEXT , Constants .DEFAULT_FONT_SIZE );
115133 sheetBehavior = BottomSheetBehavior .from (layoutBottomSheet );
116134 mBottomSheetUtils .populateBottomSheetWithPDFs (this );
135+ showEnhancementOptions ();
117136 mLottieProgress .setVisibility (View .VISIBLE );
118137 sheetBehavior .setBottomSheetCallback (new BottomSheetCallback (mUpArrow , isAdded ()));
119138 resetView ();
120139 return rootView ;
121140 }
122141
142+ /**
143+ * Function to show the enhancement options.
144+ */
145+ private void showEnhancementOptions () {
146+ GridLayoutManager mGridLayoutManager = new GridLayoutManager (mActivity , 2 );
147+ mTextEnhancementOptionsRecycleView .setLayoutManager (mGridLayoutManager );
148+ mTextEnhancementOptionsEntityArrayList = getEnhancementOptions (mActivity , mFontTitle , mFontFamily );
149+ mTextEnhancementOptionsAdapter = new EnhancementOptionsAdapter (this , mTextEnhancementOptionsEntityArrayList );
150+ mTextEnhancementOptionsRecycleView .setAdapter (mTextEnhancementOptionsAdapter );
151+ }
152+
123153 @ Override
124154 public void onAttach (Context context ) {
125155 super .onAttach (context );
@@ -173,10 +203,10 @@ public void openPdfNameDialog() {
173203 } else {
174204 final String inputName = input .toString ();
175205 if (!mFileUtils .isFileExist (inputName + getString (R .string .pdf_ext ))) {
176- addText (inputName );
206+ addText (inputName , mFontSize , mFontFamily );
177207 } else {
178208 MaterialDialog .Builder builder = createOverwriteDialog (mActivity );
179- builder .onPositive ((dialog12 , which ) -> addText (inputName ))
209+ builder .onPositive ((dialog12 , which ) -> addText (inputName , mFontSize , mFontFamily ))
180210 .onNegative ((dialog1 , which ) -> openPdfNameDialog ())
181211 .show ();
182212 }
@@ -221,6 +251,11 @@ public void resetView() {
221251 mPdfpath = mTextPath = null ;
222252 mMorphButtonUtility .morphToGrey (mCreateTextPDF , mMorphButtonUtility .integer ());
223253 mCreateTextPDF .setEnabled (false );
254+ mFontSize = mSharedPreferences .getInt (Constants .DEFAULT_FONT_SIZE_TEXT , Constants .DEFAULT_FONT_SIZE );
255+ mFontFamily = Font .FontFamily .valueOf (mSharedPreferences .getString (Constants .DEFAULT_FONT_FAMILY_TEXT ,
256+ Constants .DEFAULT_FONT_FAMILY ));
257+ showFontSize ();
258+ showFontFamily ();
224259 }
225260
226261 /**
@@ -229,7 +264,7 @@ public void resetView() {
229264 *
230265 * @param fileName - the name of the new pdf that is to be created.
231266 */
232- private void addText (String fileName ) {
267+ private void addText (String fileName , int fontsize , Font . FontFamily fontFamily ) {
233268 String mStorePath = mSharedPreferences .getString (STORAGE_LOCATION ,
234269 getDefaultStorageLocation ());
235270 String mPath = mStorePath + fileName + pdfExtension ;
@@ -260,7 +295,8 @@ private void addText(String fileName) {
260295 }
261296 document .setPageSize (pdfReader .getPageSize (1 ));
262297 document .newPage ();
263- document .add (new Paragraph (text .toString ()));
298+ document .add (new Paragraph (new Paragraph (text .toString (),
299+ FontFactory .getFont (fontFamily .name (), fontsize ))));
264300 document .close ();
265301
266302 getSnackbarwithAction (mActivity , R .string .snackbar_pdfCreated )
@@ -310,4 +346,110 @@ public void closeBottomSheet() {
310346 public boolean checkSheetBehaviour () {
311347 return checkSheetBehaviourUtil (sheetBehavior );
312348 }
349+
350+ @ Override
351+ public void onItemClick (int position ) {
352+ switch (position ) {
353+ case 0 :
354+ editFontSize ();
355+ break ;
356+ case 1 :
357+ changeFontFamily ();
358+ break ;
359+ }
360+
361+ }
362+
363+ /**
364+ * Function to take the font size of pdf as user input
365+ */
366+ private void editFontSize () {
367+ new MaterialDialog .Builder (mActivity )
368+ .title (mFontTitle )
369+ .customView (R .layout .dialog_font_size , true )
370+ .positiveText (R .string .ok )
371+ .negativeText (R .string .cancel )
372+ .onPositive ((dialog , which ) -> {
373+ final EditText fontInput = dialog .getCustomView ().findViewById (R .id .fontInput );
374+ final CheckBox cbSetDefault = dialog .getCustomView ().findViewById (R .id .cbSetFontDefault );
375+ try {
376+ int check = Integer .parseInt (String .valueOf (fontInput .getText ()));
377+ if (check > 1000 || check < 0 ) {
378+ showSnackbar (mActivity , R .string .invalid_entry );
379+ } else {
380+ mFontSize = check ;
381+ showFontSize ();
382+ showSnackbar (mActivity , R .string .font_size_changed );
383+ if (cbSetDefault .isChecked ()) {
384+ SharedPreferences .Editor editor = mSharedPreferences .edit ();
385+ editor .putInt (Constants .DEFAULT_FONT_SIZE_TEXT , mFontSize );
386+ editor .apply ();
387+ mFontTitle = String .format (getString (R .string .edit_font_size ),
388+ mSharedPreferences .getInt (Constants .DEFAULT_FONT_SIZE_TEXT ,
389+ Constants .DEFAULT_FONT_SIZE ));
390+ }
391+ }
392+ } catch (NumberFormatException e ) {
393+ showSnackbar (mActivity , R .string .invalid_entry );
394+ }
395+ })
396+ .show ();
397+ }
398+
399+ /**
400+ * Displays font size in UI
401+ */
402+ private void showFontSize () {
403+ mTextEnhancementOptionsEntityArrayList .get (0 )
404+ .setName (String .format (getString (R .string .font_size ), String .valueOf (mFontSize )));
405+ mTextEnhancementOptionsAdapter .notifyDataSetChanged ();
406+ }
407+
408+ /**
409+ * Shows dialog to change font size
410+ */
411+ private void changeFontFamily () {
412+ String fontFamily = mSharedPreferences .getString (Constants .DEFAULT_FONT_FAMILY_TEXT ,
413+ Constants .DEFAULT_FONT_FAMILY );
414+ int ordinal = Font .FontFamily .valueOf (fontFamily ).ordinal ();
415+ MaterialDialog materialDialog = new MaterialDialog .Builder (mActivity )
416+ .title (String .format (getString (R .string .default_font_family_text ), fontFamily ))
417+ .customView (R .layout .dialog_font_family , true )
418+ .positiveText (R .string .ok )
419+ .negativeText (R .string .cancel )
420+ .onPositive ((dialog , which ) -> {
421+ View view = dialog .getCustomView ();
422+ RadioGroup radioGroup = view .findViewById (R .id .radio_group_font_family );
423+ int selectedId = radioGroup .getCheckedRadioButtonId ();
424+ RadioButton radioButton = view .findViewById (selectedId );
425+ String fontFamily1 = radioButton .getText ().toString ();
426+ mFontFamily = Font .FontFamily .valueOf (fontFamily1 );
427+ final CheckBox cbSetDefault = view .findViewById (R .id .cbSetDefault );
428+ if (cbSetDefault .isChecked ()) {
429+ SharedPreferences .Editor editor = mSharedPreferences .edit ();
430+ editor .putString (Constants .DEFAULT_FONT_FAMILY_TEXT , fontFamily1 );
431+ editor .apply ();
432+ }
433+ showFontFamily ();
434+ })
435+ .build ();
436+ RadioGroup radioGroup = materialDialog .getCustomView ().findViewById (R .id .radio_group_font_family );
437+ RadioButton rb = (RadioButton ) radioGroup .getChildAt (ordinal );
438+ rb .setChecked (true );
439+ materialDialog .show ();
440+ }
441+
442+
443+ /**
444+ * Displays font family in UI
445+ */
446+ private void showFontFamily () {
447+ mTextEnhancementOptionsEntityArrayList .get (1 )
448+ .setName (getString (R .string .font_family_text ) + mFontFamily .name ());
449+ mTextEnhancementOptionsAdapter .notifyDataSetChanged ();
450+ }
451+
452+
453+
313454}
455+
0 commit comments