Skip to content

Commit

Permalink
Simplify SettingsActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlecat committed Dec 28, 2024
1 parent 68d9f57 commit 9a6bd50
Show file tree
Hide file tree
Showing 29 changed files with 150 additions and 185 deletions.
118 changes: 7 additions & 111 deletions app/src/main/java/me/tsukanov/counter/activities/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package me.tsukanov.counter.activities;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceChangeListener;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceManager;
import com.google.android.material.appbar.MaterialToolbar;
import java.io.IOException;
import me.tsukanov.counter.CounterApplication;
import me.tsukanov.counter.R;
import me.tsukanov.counter.SharedPrefKeys;
import me.tsukanov.counter.view.SettingsFragment;
Expand All @@ -26,14 +18,6 @@
public class SettingsActivity extends AppCompatActivity
implements OnPreferenceChangeListener, SharedPreferences.OnSharedPreferenceChangeListener {

private static final String TAG = SettingsActivity.class.getSimpleName();

public static final String KEY_REMOVE_COUNTERS = "removeCounters";
public static final String KEY_EXPORT_COUNTERS = "exportCounters";
public static final String KEY_HOMEPAGE = "homepage";
public static final String KEY_TIP = "tip";
public static final String KEY_VERSION = "version";

private SharedPreferences sharedPrefs;
private SettingsFragment settingsFragment;

Expand All @@ -49,68 +33,13 @@ protected void onCreate(Bundle savedInstanceState) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Themes.initCurrentTheme(sharedPrefs);

initSettingsFragment();
}

private void initSettingsFragment() {
settingsFragment = new SettingsFragment();
settingsFragment.setOnRemoveCountersClickListener(getOnRemoveCountersClickListener());
settingsFragment.setOnExportClickListener(getOnExportClickListener());
settingsFragment.setOnHomepageClickListener(getOnHomepageClickListener());
settingsFragment.setOnTipClickListener(getOnTipClickListener());
settingsFragment.setAppVersion(getAppVersion());
settingsFragment.setTheme(getCurrentThemeName());

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settingsFrame, settingsFragment)
.commit();
}

private String getCurrentThemeName() {
return getResources().getString(Themes.getCurrent(sharedPrefs).getLabelId());
}

private OnPreferenceClickListener getOnRemoveCountersClickListener() {
return preference -> {
showWipeDialog();
return true;
};
}

private OnPreferenceClickListener getOnExportClickListener() {
return preference -> {
try {
export();
} catch (IOException e) {
Log.e(TAG, "Error occurred while exporting counters", e);
Toast.makeText(
getBaseContext(),
getResources().getText(R.string.toast_unable_to_export),
Toast.LENGTH_SHORT)
.show();
}
return true;
};
}

private OnPreferenceClickListener getOnHomepageClickListener() {
return preference -> {
startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse("https://counter.roman.zone?utm_source=app")));
return true;
};
}

private OnPreferenceClickListener getOnTipClickListener() {
return preference -> {
startActivity(
new Intent(
Intent.ACTION_VIEW, Uri.parse("https://counter.roman.zone/tip?utm_source=app")));
return true;
};
}

@Override
public boolean onOptionsItemSelected(final @NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Expand All @@ -120,14 +49,6 @@ public boolean onOptionsItemSelected(final @NonNull MenuItem item) {
return super.onOptionsItemSelected(item);
}

private String getAppVersion() {
try {
return this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
return getResources().getString(R.string.unknown);
}
}

@Override
protected void onResume() {
super.onResume();
Expand All @@ -141,13 +62,14 @@ protected void onPause() {
}

@Override
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
public boolean onPreferenceChange(final @NonNull Preference preference, final Object newValue) {
return false;
}

@Override
public void onSharedPreferenceChanged(
final SharedPreferences sharedPreferences, final String key) {
if (key.equals(SharedPrefKeys.THEME.getName())) {
final SharedPreferences sharedPreferences, final @Nullable String key) {
if (key != null && key.equals(SharedPrefKeys.THEME.getName())) {
final Preference pref = settingsFragment.findPreference(SharedPrefKeys.THEME.getName());
if (pref != null) {
pref.setSummary(getCurrentThemeName());
Expand All @@ -156,33 +78,7 @@ public void onSharedPreferenceChanged(
}
}

private void showWipeDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.settings_wipe_confirmation);
builder.setPositiveButton(
R.string.settings_wipe_confirmation_yes,
(dialog, id) -> {
CounterApplication.getComponent().localStorage().wipe();
Toast.makeText(
getBaseContext(),
getResources().getText(R.string.toast_wipe_success),
Toast.LENGTH_SHORT)
.show();
});
builder.setNegativeButton(R.string.dialog_button_cancel, (dialog, id) -> dialog.dismiss());

builder.create().show();
}

private void export() throws IOException {
final Intent exportIntent = new Intent();
exportIntent.setAction(Intent.ACTION_SEND);
exportIntent.putExtra(
Intent.EXTRA_TEXT, CounterApplication.getComponent().localStorage().toCsv());
exportIntent.setType("text/csv");

final Intent shareIntent =
Intent.createChooser(exportIntent, getResources().getText(R.string.settings_export_title));
startActivity(shareIntent);
private String getCurrentThemeName() {
return getResources().getString(Themes.getCurrent(sharedPrefs).getLabelId());
}
}
163 changes: 116 additions & 47 deletions app/src/main/java/me/tsukanov/counter/view/SettingsFragment.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,147 @@
package me.tsukanov.counter.view;

import static me.tsukanov.counter.activities.SettingsActivity.KEY_EXPORT_COUNTERS;
import static me.tsukanov.counter.activities.SettingsActivity.KEY_HOMEPAGE;
import static me.tsukanov.counter.activities.SettingsActivity.KEY_REMOVE_COUNTERS;
import static me.tsukanov.counter.activities.SettingsActivity.KEY_TIP;
import static me.tsukanov.counter.activities.SettingsActivity.KEY_VERSION;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceClickListener;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import java.io.IOException;
import me.tsukanov.counter.CounterApplication;
import me.tsukanov.counter.R;
import me.tsukanov.counter.SharedPrefKeys;
import org.apache.commons.lang3.StringUtils;

public final class SettingsFragment extends PreferenceFragmentCompat {

private static final String TAG = SettingsFragment.class.getSimpleName();

private OnPreferenceClickListener onRemoveCountersClickListener;
private OnPreferenceClickListener onExportClickListener;
private OnPreferenceClickListener onHomepageClickListener;
private OnPreferenceClickListener onTipClickListener;
public static final String KEY_REMOVE_COUNTERS = "removeCounters";
public static final String KEY_EXPORT_COUNTERS = "exportCounters";
public static final String KEY_HOMEPAGE = "homepage";
public static final String KEY_TIP = "tip";
public static final String KEY_VERSION = "version";

private String appVersion;
private String theme;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

public void setOnRemoveCountersClickListener(
final @NonNull OnPreferenceClickListener onRemoveCountersClickListener) {
this.onRemoveCountersClickListener = onRemoveCountersClickListener;
}
try {

public void setOnExportClickListener(
final @NonNull OnPreferenceClickListener onExportClickListener) {
this.onExportClickListener = onExportClickListener;
}
SharedPreferences sharedPrefs =
PreferenceManager.getDefaultSharedPreferences(this.getActivity());
findPreference(SharedPrefKeys.THEME.getName())
.setSummary(getResources().getString(Themes.getCurrent(sharedPrefs).getLabelId()));

public void setOnHomepageClickListener(
final @NonNull OnPreferenceClickListener onHomepageClickListener) {
this.onHomepageClickListener = onHomepageClickListener;
}
findPreference(KEY_VERSION).setSummary(getAppVersion());

public void setOnTipClickListener(final @NonNull OnPreferenceClickListener onTipClickListener) {
this.onTipClickListener = onTipClickListener;
}
findPreference(KEY_REMOVE_COUNTERS)
.setOnPreferenceClickListener(getOnRemoveCountersClickListener());
findPreference(KEY_EXPORT_COUNTERS).setOnPreferenceClickListener(getOnExportClickListener());

findPreference(KEY_HOMEPAGE)
.setOnPreferenceClickListener(
(Preference p) -> {
startActivity(
new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://counter.roman.zone?utm_source=app")));
return true;
});

findPreference(KEY_TIP)
.setOnPreferenceClickListener(
(Preference p) -> {
startActivity(
new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://counter.roman.zone/tip?utm_source=app")));
return true;
});

public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
} catch (NullPointerException e) {
Log.e(TAG, "Unable to retrieve one of the preferences", e);
}
}

public void setTheme(String theme) {
this.theme = theme;
@NonNull
private String getAppVersion() {
try {
final String versionName =
this.getActivity()
.getPackageManager()
.getPackageInfo(this.getActivity().getPackageName(), 0)
.versionName;
if (StringUtils.isNotEmpty(versionName)) {
return versionName;
} else {
return getResources().getString(R.string.unknown_version);
}
} catch (NullPointerException | PackageManager.NameNotFoundException e) {
return getResources().getString(R.string.unknown_version);
}
}

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

try {
@NonNull
private OnPreferenceClickListener getOnRemoveCountersClickListener() {
return preference -> {
showWipeDialog();
return true;
};
}

// Setting summaries for necessary preferences
findPreference(SharedPrefKeys.THEME.getName()).setSummary(theme);
findPreference(KEY_VERSION).setSummary(appVersion);
@NonNull
private OnPreferenceClickListener getOnExportClickListener() {
return preference -> {
try {
export();
} catch (IOException e) {
Log.e(TAG, "Error occurred while exporting counters", e);
Toast.makeText(
this.getActivity(),
getResources().getText(R.string.toast_unable_to_export),
Toast.LENGTH_SHORT)
.show();
}
return true;
};
}

findPreference(KEY_REMOVE_COUNTERS)
.setOnPreferenceClickListener(onRemoveCountersClickListener);
findPreference(KEY_EXPORT_COUNTERS).setOnPreferenceClickListener(onExportClickListener);
private void showWipeDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
builder.setMessage(R.string.settings_wipe_confirmation);
builder.setPositiveButton(
R.string.settings_wipe_confirmation_yes,
(dialog, id) -> {
CounterApplication.getComponent().localStorage().wipe();
Toast.makeText(
this.getActivity(),
getResources().getText(R.string.toast_wipe_success),
Toast.LENGTH_SHORT)
.show();
});
builder.setNegativeButton(R.string.dialog_button_cancel, (dialog, id) -> dialog.dismiss());

builder.create().show();
}

findPreference(KEY_TIP).setOnPreferenceClickListener(onTipClickListener);
findPreference(KEY_HOMEPAGE).setOnPreferenceClickListener(onHomepageClickListener);
private void export() throws IOException {
final Intent exportIntent = new Intent();
exportIntent.setAction(Intent.ACTION_SEND);
exportIntent.putExtra(
Intent.EXTRA_TEXT, CounterApplication.getComponent().localStorage().toCsv());
exportIntent.setType("text/csv");

} catch (NullPointerException e) {
Log.e(TAG, "Unable to retrieve one of the preferences", e);
}
final Intent shareIntent =
Intent.createChooser(exportIntent, getResources().getText(R.string.settings_export_title));
startActivity(shareIntent);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ar-rSA/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
<string name="toast_unable_to_export">حدث خطأ ما أثناء تصدير العدادات. عذراً!</string>
<!-- Other stuff -->
<string name="default_counter_name">عداد جديد</string>
<string name="unknown">‮غير معروف</string>
<string name="unknown_version">‮غير معروف</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-cs-rCZ/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@
<string name="settings_wipe_confirmation">Jste si jistí, že chcete odstranit všechna počítadla?</string>
<string name="settings_wipe_confirmation_yes">Odstranit vše</string>
<!-- Other stuff -->
<string name="unknown">Neznámé</string>
<string name="unknown_version">Neznámé</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-da-rDK/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@
<string name="toast_unable_to_modify">Noget gik galt mens tælleren blev opdateret. Undskyld!</string>
<!-- Other stuff -->
<string name="default_counter_name">Ny tæller</string>
<string name="unknown">Ukendt</string>
<string name="unknown_version">Ukendt</string>
</resources>
Loading

0 comments on commit 9a6bd50

Please sign in to comment.