Skip to content

Commit

Permalink
improve(settings): finish button functions
Browse files Browse the repository at this point in the history
- Implement resetting preferences to original values on clicking Cancel.
  • Loading branch information
dfyockey committed Oct 27, 2024
1 parent 85e62a6 commit 3666856
Showing 1 changed file with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,27 @@
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import java.util.Map;

public class SettingsButtonBarFragment extends Fragment implements View.OnClickListener {
Activity parentActivity;
Button btnCancel;
Button btnSave;

Map<String, ?> origprefs; // Storage for backup of original preference values
SharedPreferences prefs;

public SettingsButtonBarFragment() {
// If the layout isn't provided to the superclass, it's necessary to use a form of
// fragmentTransaction.add or .replace that takes the fragment class rather than an
Expand All @@ -49,38 +57,45 @@ public SettingsButtonBarFragment() {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

parentActivity = requireActivity();
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
origprefs = prefs.getAll();
}

public void onClick (View v) {

Activity activity = requireActivity();

// Get the appWidgetId if we're in a widget config activity
//
//noinspection ReassignedVariable
int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
String cls = activity.getComponentName().getClassName();
String cls = parentActivity.getComponentName().getClassName();
if (cls.equals("net.diffengine.romandigitalclock.TimeDisplayWidgetConfigActivity")) {
TimeDisplayWidgetConfigActivity widgetConfigActivity = (TimeDisplayWidgetConfigActivity) activity;
TimeDisplayWidgetConfigActivity widgetConfigActivity = (TimeDisplayWidgetConfigActivity) parentActivity;
appWidgetId = widgetConfigActivity.appWidgetId;
}

if (v == btnCancel) {
// Add code to reset settings here
// Set preferences back to original values
SharedPreferences.Editor spEditor = prefs.edit();
for (String key : origprefs.keySet()) {
spEditor.putBoolean(key, (boolean) origprefs.get(key));
}
spEditor.commit();
} else if (v == btnSave) {
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
// Provision of appwidget id in the extra data should prevent crash of some UIs
// (e.g. TouchWiz on old Samsung devices) at activity destruction.
// See https://stackoverflow.com/a/40709721
Intent result = new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
activity.setResult(RESULT_OK, result);
parentActivity.setResult(RESULT_OK, result);
}
} else {
// In case of some utterly stupid modification... :)
// In case of some shortsighted modification... :)
return;
}

activity.finish();
parentActivity.finish();
}

@Override
Expand Down

0 comments on commit 3666856

Please sign in to comment.