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 86d93cd
Show file tree
Hide file tree
Showing 29 changed files with 147 additions and 176 deletions.
118 changes: 10 additions & 108 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,8 +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";
Expand All @@ -49,66 +39,11 @@ 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;
};
.beginTransaction()
.replace(R.id.settingsFrame, settingsFragment)
.commit();
}

@Override
Expand All @@ -120,14 +55,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 +68,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 +84,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());
}
}
151 changes: 110 additions & 41 deletions app/src/main/java/me/tsukanov/counter/view/SettingsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,73 +6,142 @@
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;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

private String appVersion;
private String theme;
try {

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

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

public void setOnHomepageClickListener(
final @NonNull OnPreferenceClickListener onHomepageClickListener) {
this.onHomepageClickListener = onHomepageClickListener;
}
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 setOnTipClickListener(final @NonNull OnPreferenceClickListener onTipClickListener) {
this.onTipClickListener = onTipClickListener;
} catch (NullPointerException e) {
Log.e(TAG, "Unable to retrieve one of the preferences", e);
}
}

public void setAppVersion(String appVersion) {
this.appVersion = appVersion;
@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);
}
}

public void setTheme(String theme) {
this.theme = theme;
@NonNull
private OnPreferenceClickListener getOnRemoveCountersClickListener() {
return preference -> {
showWipeDialog();
return true;
};
}

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

try {

// 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>
2 changes: 1 addition & 1 deletion app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@
<string name="toast_unable_to_export">Beim Exportieren der Zähler ist etwas schief gegangen. Entschuldige!</string>
<!-- Other stuff -->
<string name="default_counter_name">Neuer Zähler</string>
<string name="unknown">Unbekannt</string>
<string name="unknown_version">Unbekannt</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-el-rGR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
<string name="settings_wipe_confirmation">Θέλετε σίγουρα να διαγράψετε όλους τους μετρητές;</string>
<string name="settings_wipe_confirmation_yes">Διαγραφή όλων</string>
<!-- Other stuff -->
<string name="unknown">Άγνωστο</string>
<string name="unknown_version">Άγνωστο</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-eo-rUY/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@
<string name="toast_unable_to_export">Io misokazis dum elportado de la nombriloj. Pardonu!</string>
<!-- Other stuff -->
<string name="default_counter_name">Nova nombrilo</string>
<string name="unknown">Nekonata</string>
<string name="unknown_version">Nekonata</string>
</resources>
Loading

0 comments on commit 86d93cd

Please sign in to comment.