mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 06:42:21 -04:00
Refactor: Moved ConfirmationAlertDialogFragment to a separate file from AddNoteDialog
ConfirmationAlertDialogFragment is now a generic helper class for displaying a 2-button (positive & negative) confirmation dialog fragment on top of an existing dialog fragment
This commit is contained in:
parent
ffba82e0c7
commit
7b8bd2b406
@ -29,6 +29,7 @@ import org.kiwix.kiwixmobile.di.modules.NetworkModule;
|
|||||||
import org.kiwix.kiwixmobile.downloader.DownloadService;
|
import org.kiwix.kiwixmobile.downloader.DownloadService;
|
||||||
import org.kiwix.kiwixmobile.library.LibraryAdapter;
|
import org.kiwix.kiwixmobile.library.LibraryAdapter;
|
||||||
import org.kiwix.kiwixmobile.main.AddNoteDialog;
|
import org.kiwix.kiwixmobile.main.AddNoteDialog;
|
||||||
|
import org.kiwix.kiwixmobile.main.ConfirmationAlertDialogFragment;
|
||||||
import org.kiwix.kiwixmobile.main.KiwixWebView;
|
import org.kiwix.kiwixmobile.main.KiwixWebView;
|
||||||
import org.kiwix.kiwixmobile.search.AutoCompleteAdapter;
|
import org.kiwix.kiwixmobile.search.AutoCompleteAdapter;
|
||||||
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
|
import org.kiwix.kiwixmobile.settings.KiwixSettingsActivity;
|
||||||
@ -64,4 +65,6 @@ public interface ApplicationComponent {
|
|||||||
void inject(AutoCompleteAdapter autoCompleteAdapter);
|
void inject(AutoCompleteAdapter autoCompleteAdapter);
|
||||||
|
|
||||||
void inject(AddNoteDialog addNoteDialog);
|
void inject(AddNoteDialog addNoteDialog);
|
||||||
|
|
||||||
|
void inject(ConfirmationAlertDialogFragment confirmationAlertDialogFragment);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package org.kiwix.kiwixmobile.main;
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -24,7 +23,6 @@ import android.widget.Toast;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.appcompat.app.AlertDialog;
|
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.core.content.FileProvider;
|
import androidx.core.content.FileProvider;
|
||||||
@ -58,7 +56,9 @@ import static org.kiwix.kiwixmobile.utils.Constants.NOTES_DIRECTORY;
|
|||||||
* Notes are saved as text files at location: "{External Storage}/Kiwix/Notes/ZimFileTitle/ArticleTitle.txt"
|
* Notes are saved as text files at location: "{External Storage}/Kiwix/Notes/ZimFileTitle/ArticleTitle.txt"
|
||||||
* */
|
* */
|
||||||
|
|
||||||
public class AddNoteDialog extends DialogFragment {
|
public class AddNoteDialog extends DialogFragment implements ConfirmationAlertDialogFragment.UserClickListener {
|
||||||
|
|
||||||
|
public static String TAG = "AddNoteDialog";
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
SharedPreferenceUtil sharedPreferenceUtil;
|
SharedPreferenceUtil sharedPreferenceUtil;
|
||||||
@ -77,8 +77,6 @@ public class AddNoteDialog extends DialogFragment {
|
|||||||
private boolean noteFileExists = false;
|
private boolean noteFileExists = false;
|
||||||
private boolean noteEdited = false; // Keeps track of state of the note (whether edited since last save)
|
private boolean noteEdited = false; // Keeps track of state of the note (whether edited since last save)
|
||||||
|
|
||||||
private final String TAG = "AddNoteDialog";
|
|
||||||
|
|
||||||
public AddNoteDialog() {
|
public AddNoteDialog() {
|
||||||
super();
|
super();
|
||||||
KiwixApplication.getApplicationComponent().inject(this);
|
KiwixApplication.getApplicationComponent().inject(this);
|
||||||
@ -169,13 +167,12 @@ public class AddNoteDialog extends DialogFragment {
|
|||||||
private void exitAddNoteDialog() {
|
private void exitAddNoteDialog() {
|
||||||
if(noteEdited) {
|
if(noteEdited) {
|
||||||
// Custom AlertDialog for taking user confirmation before closing note dialog in case of unsaved changes
|
// Custom AlertDialog for taking user confirmation before closing note dialog in case of unsaved changes
|
||||||
DialogFragment newFragment = new ConfirmationAlertDialogFragment(getDialog());
|
DialogFragment newFragment = new ConfirmationAlertDialogFragment(TAG, R.string.confirmation_alert_dialog_message);
|
||||||
newFragment.show(getActivity().getSupportFragmentManager(), "ConfirmationAlertDialog");
|
newFragment.show(getActivity().getSupportFragmentManager(), ConfirmationAlertDialogFragment.TAG);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Closing unedited note dialog straightaway
|
// Closing unedited note dialog straightaway
|
||||||
Dialog dialog = getDialog();
|
dismissAddNoteDialog();
|
||||||
dialog.dismiss();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,6 +375,22 @@ public class AddNoteDialog extends DialogFragment {
|
|||||||
Toast.makeText(getActivity(), stringResource, duration).show();
|
Toast.makeText(getActivity(), stringResource, duration).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Methods from ConfirmationAlertDialogFragment.UserClickListener interface
|
||||||
|
@Override
|
||||||
|
public void onPositiveClick() {
|
||||||
|
dismissAddNoteDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onNegativeClick() {
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
|
||||||
|
private void dismissAddNoteDialog() {
|
||||||
|
Dialog dialog = getDialog();
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
@ -399,45 +412,3 @@ public class AddNoteDialog extends DialogFragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConfirmationAlertDialogFragment extends DialogFragment {
|
|
||||||
/**
|
|
||||||
* Helper class to show the alert dialog in case the user tries to exit the
|
|
||||||
* AddNoteDialog with unsaved file changes
|
|
||||||
**/
|
|
||||||
|
|
||||||
private SharedPreferenceUtil sharedPreferenceUtil = new SharedPreferenceUtil(KiwixApplication.getInstance());
|
|
||||||
private Dialog addNoteDialog;
|
|
||||||
|
|
||||||
public ConfirmationAlertDialogFragment(Dialog dialog) {
|
|
||||||
super();
|
|
||||||
addNoteDialog = dialog;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
|
||||||
|
|
||||||
AlertDialog.Builder builder;
|
|
||||||
|
|
||||||
if (sharedPreferenceUtil != null && sharedPreferenceUtil.nightMode()) {
|
|
||||||
// Night Mode support
|
|
||||||
builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_Dialog_Night);
|
|
||||||
|
|
||||||
} else {
|
|
||||||
builder = new AlertDialog.Builder(getActivity());
|
|
||||||
|
|
||||||
}
|
|
||||||
builder.setMessage(getString(R.string.confirmation_alert_dialog_message))
|
|
||||||
.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
|
||||||
// User sure of discarding unsaved changes and closing note dialog
|
|
||||||
addNoteDialog.dismiss();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.setNegativeButton(getString(R.string.cancel), null); // Do nothing for 'Cancel' button
|
|
||||||
|
|
||||||
return builder.create();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
package org.kiwix.kiwixmobile.main;
|
||||||
|
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AlertDialog;
|
||||||
|
import androidx.fragment.app.DialogFragment;
|
||||||
|
import androidx.fragment.app.Fragment;
|
||||||
|
|
||||||
|
import org.kiwix.kiwixmobile.KiwixApplication;
|
||||||
|
import org.kiwix.kiwixmobile.R;
|
||||||
|
import org.kiwix.kiwixmobile.utils.SharedPreferenceUtil;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by @Aditya-Sood as a part of GSoC 2019
|
||||||
|
*
|
||||||
|
* This is a generic helper class for displaying a 2-button (positive & negative) confirmation dialog fragment on top of an existing dialog fragment
|
||||||
|
* - Only for confirmation dialogs with a Positive & Negative button
|
||||||
|
* - If you also need a Neutral button, add it selectively (if-else) for the required use case (Take care of the callback interface as well)
|
||||||
|
*
|
||||||
|
* Currently used as:
|
||||||
|
* - Helper class to show the alert dialog in case the user tries to exit the {@link AddNoteDialog} with unsaved file changes
|
||||||
|
*
|
||||||
|
**/
|
||||||
|
|
||||||
|
public class ConfirmationAlertDialogFragment extends DialogFragment {
|
||||||
|
|
||||||
|
public static String TAG = "ConfirmationAlertDialog";
|
||||||
|
@Inject SharedPreferenceUtil sharedPreferenceUtil;
|
||||||
|
private int stringResource;
|
||||||
|
private String parentDialogFragmentTAG;
|
||||||
|
|
||||||
|
public ConfirmationAlertDialogFragment(String dialogFragmentTAG, int resourceId) {
|
||||||
|
super();
|
||||||
|
parentDialogFragmentTAG = dialogFragmentTAG;
|
||||||
|
stringResource = resourceId;
|
||||||
|
KiwixApplication.getApplicationComponent().inject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
|
Fragment parentDialogFragment = getFragmentManager().findFragmentByTag(parentDialogFragmentTAG);
|
||||||
|
|
||||||
|
AlertDialog.Builder builder;
|
||||||
|
|
||||||
|
if (sharedPreferenceUtil != null && sharedPreferenceUtil.nightMode()) { // Night Mode support
|
||||||
|
builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_Dialog_Night);
|
||||||
|
} else {
|
||||||
|
builder = new AlertDialog.Builder(getActivity());
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.setMessage(stringResource)
|
||||||
|
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
|
||||||
|
if(parentDialogFragment != null) {
|
||||||
|
((UserClickListener) parentDialogFragment).onPositiveClick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
|
||||||
|
if(parentDialogFragment != null) {
|
||||||
|
((UserClickListener) parentDialogFragment).onNegativeClick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return builder.create();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Callback interface for responding to user clicks to a {@link ConfirmationAlertDialogFragment} dialog */
|
||||||
|
public interface UserClickListener {
|
||||||
|
void onPositiveClick();
|
||||||
|
|
||||||
|
void onNegativeClick();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -989,7 +989,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
|||||||
/** Creates the full screen AddNoteDialog, which is a DialogFragment */
|
/** Creates the full screen AddNoteDialog, which is a DialogFragment */
|
||||||
private void showAddNoteDialog() {
|
private void showAddNoteDialog() {
|
||||||
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||||
Fragment prev = getSupportFragmentManager().findFragmentByTag("AddNoteDialog");
|
Fragment prev = getSupportFragmentManager().findFragmentByTag(AddNoteDialog.TAG);
|
||||||
|
|
||||||
// To prevent multiple instances of the DialogFragment
|
// To prevent multiple instances of the DialogFragment
|
||||||
if(prev != null) {
|
if(prev != null) {
|
||||||
@ -997,7 +997,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
|||||||
} else {
|
} else {
|
||||||
fragmentTransaction.addToBackStack(null);
|
fragmentTransaction.addToBackStack(null);
|
||||||
AddNoteDialog dialogFragment = new AddNoteDialog();
|
AddNoteDialog dialogFragment = new AddNoteDialog();
|
||||||
dialogFragment.show(fragmentTransaction, "AddNoteDialog");
|
dialogFragment.show(fragmentTransaction, AddNoteDialog.TAG);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user