Refactor: Refer comments by @macgills in PR #1198

This commit is contained in:
Aditya-Sood 2019-06-15 23:02:56 +05:30
parent c4bd6a8ee3
commit 873eb2bce8
4 changed files with 31 additions and 41 deletions

View File

@ -41,6 +41,8 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import static org.kiwix.kiwixmobile.utils.Constants.NOTES_DIRECTORY;
/** /**
* Created by @Aditya-Sood (21/05/19) as a part of GSoC 2019 * Created by @Aditya-Sood (21/05/19) as a part of GSoC 2019
* *
@ -68,11 +70,7 @@ public class AddNoteDialog extends DialogFragment {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if(sharedPreferenceUtil != null && sharedPreferenceUtil.nightMode()) { setStyle(DialogFragment.STYLE_NORMAL, sharedPreferenceUtil.nightMode() ? R.style.AddNoteDialogStyle_Night : R.style.AddNoteDialogStyle);
setStyle(DialogFragment.STYLE_NORMAL, R.style.AddNoteDialogStyle_Night); // Night Mode support
} else {
setStyle(DialogFragment.STYLE_NORMAL, R.style.AddNoteDialogStyle);
}
zimFileTitle = ZimContentProvider.getZimFileTitle(); zimFileTitle = ZimContentProvider.getZimFileTitle();
articleTitle = ((MainActivity)getActivity()).getCurrentWebView().getTitle(); articleTitle = ((MainActivity)getActivity()).getCurrentWebView().getTitle();
@ -236,7 +234,7 @@ public class AddNoteDialog extends DialogFragment {
return; return;
} }
File notesFolder = new File(Environment.getExternalStorageDirectory() + "/Kiwix/Notes/" + zimFileTitle); File notesFolder = new File(NOTES_DIRECTORY + zimFileTitle);
boolean folderExists = true; boolean folderExists = true;
if(!notesFolder.exists()) { if(!notesFolder.exists()) {
@ -257,11 +255,11 @@ public class AddNoteDialog extends DialogFragment {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
Toast.makeText(getContext(), getActivity().getString(R.string.note_save_unsuccessful), Toast.LENGTH_LONG); Toast.makeText(this.getContext(), R.string.note_save_unsuccessful, Toast.LENGTH_LONG).show();
} }
} else { } else {
Toast.makeText(getContext(), getActivity().getString(R.string.note_save_unsuccessful), Toast.LENGTH_LONG); Toast.makeText(this.getContext(), R.string.note_save_unsuccessful, Toast.LENGTH_LONG).show();
Log.d(TAG, "Required folder doesn't exist"); Log.d(TAG, "Required folder doesn't exist");
} }
} }
@ -278,7 +276,7 @@ public class AddNoteDialog extends DialogFragment {
* is displayed in the EditText field (note content area) * is displayed in the EditText field (note content area)
* */ * */
File noteFile = new File(Environment.getExternalStorageDirectory() + "/Kiwix/Notes/" + zimFileTitle + "/" + articleTitle + ".txt"); File noteFile = new File(NOTES_DIRECTORY + zimFileTitle + "/" + articleTitle + ".txt");
if(noteFile.exists()) { if(noteFile.exists()) {
noteFileExists = true; noteFileExists = true;
@ -325,7 +323,7 @@ public class AddNoteDialog extends DialogFragment {
saveNote(addNoteEditText.getText().toString()); // Save edited note before sharing the text file saveNote(addNoteEditText.getText().toString()); // Save edited note before sharing the text file
} }
File noteFile = new File(Environment.getExternalStorageDirectory() + "/Kiwix/Notes/" + zimFileTitle + "/" + articleTitle + ".txt"); File noteFile = new File(NOTES_DIRECTORY + zimFileTitle + "/" + articleTitle + ".txt");
Uri noteFileUri = null; Uri noteFileUri = null;
if(noteFile.exists()) { if(noteFile.exists()) {
@ -357,12 +355,7 @@ public class AddNoteDialog extends DialogFragment {
} }
public static boolean isExternalStorageWritable() { public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState(); return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
if(Environment.MEDIA_MOUNTED.equals(state)) {
return true;
} else {
return false;
}
} }
@Override @Override
@ -407,19 +400,14 @@ class ConfirmationAlertDialogFragment extends DialogFragment {
} }
builder.setMessage(getString(R.string.confirmation_alert_dialog_message)) builder.setMessage(getString(R.string.confirmation_alert_dialog_message))
.setPositiveButton(getString(R.string.confirmation_alert_dialog_positive_btn), new DialogInterface.OnClickListener() { .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
// User sure of discarding unsaved changes and closing note dialog // User sure of discarding unsaved changes and closing note dialog
addNoteDialog.dismiss(); addNoteDialog.dismiss();
} }
}) })
.setNeutralButton(getString(R.string.confirmation_alert_dialog_neutral_btn), new DialogInterface.OnClickListener() { .setNegativeButton(getString(R.string.cancel), null); // Do nothing for 'Cancel' button
@Override
public void onClick(DialogInterface dialog, int which) {
// No action to be taken in case of the neutral 'Cancel' action
}
});
return builder.create(); return builder.create();
} }

View File

@ -129,6 +129,7 @@ import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_NOTIFICATION_ID;
import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_SEARCH; import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_SEARCH;
import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE; import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE;
import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE_2; import static org.kiwix.kiwixmobile.utils.Constants.EXTRA_ZIM_FILE_2;
import static org.kiwix.kiwixmobile.utils.Constants.NOTES_DIRECTORY;
import static org.kiwix.kiwixmobile.utils.Constants.PREF_KIWIX_MOBILE; import static org.kiwix.kiwixmobile.utils.Constants.PREF_KIWIX_MOBILE;
import static org.kiwix.kiwixmobile.utils.Constants.REQUEST_FILE_SEARCH; import static org.kiwix.kiwixmobile.utils.Constants.REQUEST_FILE_SEARCH;
import static org.kiwix.kiwixmobile.utils.Constants.REQUEST_FILE_SELECT; import static org.kiwix.kiwixmobile.utils.Constants.REQUEST_FILE_SELECT;
@ -929,13 +930,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
} }
builder.setMessage(this.getString(R.string.delete_notes_confirmation_msg)) builder.setMessage(this.getString(R.string.delete_notes_confirmation_msg))
.setNeutralButton(this.getString(R.string.dismiss), new DialogInterface.OnClickListener() { .setNegativeButton(this.getString(R.string.cancel), null) // Do nothing for 'Cancel' button
@Override .setPositiveButton(this.getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
})
.setPositiveButton(this.getString(R.string.delete), new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
clearAllNotes(); clearAllNotes();
@ -956,7 +952,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
return; return;
} }
File notesDirectory = new File(Environment.getExternalStorageDirectory() + "/Kiwix/Notes/"); // TODO: Replace below code with Kotlin's deleteRecursively() method
File notesDirectory = new File(NOTES_DIRECTORY);
File[] filesInNotesDirectory = notesDirectory.listFiles(); File[] filesInNotesDirectory = notesDirectory.listFiles();
if(filesInNotesDirectory == null) { // Notes folder doesn't exist if(filesInNotesDirectory == null) { // Notes folder doesn't exist
@ -992,14 +990,15 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
private void showAddNoteDialog() { private void showAddNoteDialog() {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("AddNoteDialog"); Fragment prev = getSupportFragmentManager().findFragmentByTag("AddNoteDialog");
if(prev != null) {
fragmentTransaction.remove(prev); // To prevent multiple instances of the DialogFragment
}
fragmentTransaction.addToBackStack(null);
AddNoteDialog dialogFragment = new AddNoteDialog(); // To prevent multiple instances of the DialogFragment
// For DialogFragments, show() handles the fragment commit and display if(prev != null) {
dialogFragment.show(fragmentTransaction, "AddNoteDialog"); fragmentTransaction.show(prev); // For DialogFragments, show() handles the fragment commit and display
} else {
fragmentTransaction.addToBackStack(null);
AddNoteDialog dialogFragment = new AddNoteDialog();
dialogFragment.show(fragmentTransaction, "AddNoteDialog");
}
} }
private boolean requestExternalStorageWritePermissionForNotes() { private boolean requestExternalStorageWritePermissionForNotes() {

View File

@ -17,6 +17,8 @@
*/ */
package org.kiwix.kiwixmobile.utils; package org.kiwix.kiwixmobile.utils;
import android.os.Environment;
import org.kiwix.kiwixmobile.BuildConfig; import org.kiwix.kiwixmobile.BuildConfig;
public final class Constants { public final class Constants {
@ -136,4 +138,7 @@ public final class Constants {
public static final String OLD_PROVIDER_DOMAIN = "org.kiwix.zim.base"; public static final String OLD_PROVIDER_DOMAIN = "org.kiwix.zim.base";
public static final String NEW_PROVIDER_DOMAIN = BuildConfig.APPLICATION_ID + ".zim.base"; public static final String NEW_PROVIDER_DOMAIN = BuildConfig.APPLICATION_ID + ".zim.base";
// Path Constants
public static final String NOTES_DIRECTORY = Environment.getExternalStorageDirectory() + "/Kiwix/Notes/";
} }

View File

@ -257,11 +257,9 @@
<string name="note_share_error_file_missing">Note file doesn\'t exist</string> <string name="note_share_error_file_missing">Note file doesn\'t exist</string>
<string name="note_share_app_chooser_title">Share note file with:</string> <string name="note_share_app_chooser_title">Share note file with:</string>
<string name="confirmation_alert_dialog_message">Discard unsaved changes?</string> <string name="confirmation_alert_dialog_message">Discard unsaved changes?</string>
<string name="confirmation_alert_dialog_positive_btn">Yes</string>
<string name="confirmation_alert_dialog_neutral_btn">Cancel</string>
<string name="clear_all_notes">Clear All Notes</string> <string name="clear_all_notes">Clear All Notes</string>
<string name="delete_notes_confirmation_msg">Delete all notes?</string> <string name="delete_notes_confirmation_msg">Delete all notes?</string>
<string name="dismiss">Dismiss</string> <string name="cancel">Cancel</string>
<string name="ext_storage_permission_not_granted">Error: Storage permissions not granted</string> <string name="ext_storage_permission_not_granted">Error: Storage permissions not granted</string>
<string name="notes_deletion_none_found">No notes found for deletion</string> <string name="notes_deletion_none_found">No notes found for deletion</string>
<string name="notes_deletion_successful">Entire notes folder deleted</string> <string name="notes_deletion_successful">Entire notes folder deleted</string>