From c5f79dadc92ff3c748921e1591a8c14dfb675227 Mon Sep 17 00:00:00 2001 From: Marcus Radisch Date: Mon, 20 Jun 2022 09:57:18 +0200 Subject: [PATCH] =?UTF-8?q?SettingsFragment=20=C3=BCberarbeitet=20und=20me?= =?UTF-8?q?nu=20entfernt=20bis=20auf=20settings.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ExampleInstrumentedTest.java | 6 +- .../palatschinkencounter/MainActivity.java | 78 ++++++++++--------- .../palatschinkencounter/SettingActivity.java | 5 +- .../SettingsFragment.java | 67 +++++++++++----- .../drawable-night/ic_baseline_mail_24.xml | 14 ++-- .../main/res/drawable-night/ic_dn_switch.xml | 2 +- .../ic_baseline_contact_support_24.xml | 12 +-- .../ic_baseline_delete_forever_24.xml | 10 +-- .../main/res/drawable/ic_baseline_list_24.xml | 12 +-- .../main/res/drawable/ic_baseline_mail_24.xml | 13 +++- app/src/main/res/drawable/ic_dn_switch.xml | 2 +- app/src/main/res/layout/activity_about.xml | 11 +-- app/src/main/res/menu/menu.xml | 26 +------ app/src/main/res/menu/menu_about.xml | 3 +- .../res/mipmap-anydpi-v26/logo_psc_round.xml | 4 +- .../logo_psc_round_round.xml | 4 +- app/src/main/res/values-de-rDE/strings.xml | 9 ++- app/src/main/res/values/arrays.xml | 14 ++++ app/src/main/res/values/strings.xml | 19 ++++- app/src/main/res/xml/preferences.xml | 35 ++++++--- .../palatschinkencounter/ExampleUnitTest.java | 4 +- 21 files changed, 209 insertions(+), 141 deletions(-) create mode 100644 app/src/main/res/values/arrays.xml diff --git a/app/src/androidTest/java/com/git/amarradi/palatschinkencounter/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/git/amarradi/palatschinkencounter/ExampleInstrumentedTest.java index d8c6525..ca12613 100644 --- a/app/src/androidTest/java/com/git/amarradi/palatschinkencounter/ExampleInstrumentedTest.java +++ b/app/src/androidTest/java/com/git/amarradi/palatschinkencounter/ExampleInstrumentedTest.java @@ -1,15 +1,15 @@ package com.git.amarradi.palatschinkencounter; +import static org.junit.Assert.assertEquals; + import android.content.Context; -import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; import org.junit.Test; import org.junit.runner.RunWith; -import static org.junit.Assert.*; - /** * Instrumented test, which will execute on an Android device. * diff --git a/app/src/main/java/com/git/amarradi/palatschinkencounter/MainActivity.java b/app/src/main/java/com/git/amarradi/palatschinkencounter/MainActivity.java index 11355fb..06ece93 100644 --- a/app/src/main/java/com/git/amarradi/palatschinkencounter/MainActivity.java +++ b/app/src/main/java/com/git/amarradi/palatschinkencounter/MainActivity.java @@ -10,21 +10,21 @@ import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.View; import android.widget.Button; import android.widget.TextView; -import android.widget.Toast; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatDelegate; +import androidx.preference.PreferenceManager; import java.util.Objects; -public class MainActivity extends AppCompatActivity implements WipeDataDialog.WipeDialogListener { +public class MainActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public static final String SHARED_PREFS = "sharedPrefs"; public static final String COUNTER = "text"; @@ -93,30 +93,16 @@ public boolean onOptionsItemSelected(MenuItem item) { Intent intentSetting = new Intent(this, SettingActivity.class); startActivity(intentSetting); return true; - case R.id.item_clean: - openDialog(); - return true; - case R.id.item_dn_switch: - switchDayNightMode(); - return true; - case R.id.item_recipe: - Intent intentRecipe = new Intent(this, RecipeActivity.class); - startActivity(intentRecipe); - return true; - case R.id.item_about: - Intent intentAbout = new Intent(this, AboutActivity.class); - startActivity(intentAbout); - return true; default: return super.onOptionsItemSelected(item); } } - + /* public void openDialog() { WipeDataDialog dialog = new WipeDataDialog(); dialog.show(getSupportFragmentManager(), "open dialog"); - } + }*/ public void save_data() { SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE); @@ -136,23 +122,8 @@ public void updateViews() { textView.setText(format("%d", counter)); } - public void switchDayNightMode() { - SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE); - safedNightMode = sharedPreferences.getBoolean(NIGHT_MODE, false); - - nightMode = safedNightMode; - if (safedNightMode) { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); - nightMode = false; - } else { - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); - nightMode = true; - } - SharedPreferences.Editor editor = sharedPreferences.edit(); - editor.putBoolean(NIGHT_MODE, nightMode); - editor.apply(); - } + /* public void reset_counter() { counter = 0; @@ -165,12 +136,43 @@ public void reset_counter() { public void onYesClicked() { reset_counter(); } +*/ + private void setupSharedPreferences() { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); + sharedPreferences.registerOnSharedPreferenceChangeListener(this); + } - public void setupSharedPreferences() { - SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE); - safedNightMode = sharedPreferences.getBoolean(NIGHT_MODE, false); + + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals("darkmode")) { + Log.d("Value mode onShared:", key); + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } else { + Log.d("Value mode onShared:", key); + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } } + // Method to pass value from SharedPreferences + private void loadColorFromPreference(SharedPreferences sharedPreferences) { + Log.d("Parzival",sharedPreferences.getString(getString(R.string.theme_key), + getString(R.string.lightmode_preference_option_value))); + changeDarkLightMode(sharedPreferences.getString(getString(R.string.theme_key), + getString(R.string.lightmode_preference_option_value))); + } + // Method to set Color of Text. + private void changeDarkLightMode(String mode) { + Log.d("Value mode:", mode); + if (mode.equals("lightmode")) { + Log.d("Value mode:", mode); + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + } else if(mode.equals("darkmode")) { + Log.d("Value mode:", mode); + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingActivity.java b/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingActivity.java index 00ad981..2cd2bce 100644 --- a/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingActivity.java +++ b/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingActivity.java @@ -1,10 +1,10 @@ package com.git.amarradi.palatschinkencounter; +import android.os.Bundle; + import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; -import android.os.Bundle; - public class SettingActivity extends AppCompatActivity { @Override @@ -22,6 +22,7 @@ public void onBackPressed() { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + getSupportFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit(); diff --git a/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingsFragment.java b/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingsFragment.java index cb8b187..8ac2925 100644 --- a/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingsFragment.java +++ b/app/src/main/java/com/git/amarradi/palatschinkencounter/SettingsFragment.java @@ -1,36 +1,28 @@ package com.git.amarradi.palatschinkencounter; -import static android.content.Context.MODE_PRIVATE; -import static java.lang.String.format; - -import android.annotation.SuppressLint; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.Color; import android.net.Uri; import android.os.Bundle; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AppCompatDelegate; -import androidx.fragment.app.Fragment; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; - -import android.util.AttributeSet; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Toast; +import androidx.preference.PreferenceScreen; -public class SettingsFragment extends PreferenceFragmentCompat { +public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener { @Override public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) { addPreferencesFromResource(R.xml.preferences); + + SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences(); + PreferenceScreen prefScreen = getPreferenceScreen(); + Preference feedback_preference = (Preference) findPreference("feedback_preference"); feedback_preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @@ -44,13 +36,50 @@ public boolean onPreferenceClick(@NonNull Preference preference) { return true; } }); + Preference recipe_preference = (Preference) findPreference("recipe"); + recipe_preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(@NonNull Preference preference) { + Intent intentRecipe = new Intent(getActivity(), RecipeActivity.class); + startActivity(intentRecipe); + return false; + } + }); - Preference checkbox = (Preference) findPreference("check_theme_preference"); - checkbox.setOnPreferenceChangeListener((preference, newValue) -> { - Toast.makeText(getContext(), "Test", Toast.LENGTH_SHORT).show(); - return true; + Preference about_preference = (Preference) findPreference("about"); + about_preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(@NonNull Preference preference) { + Intent intentAbout = new Intent(getActivity(),AboutActivity.class); + startActivity(intentAbout); + return false; + } }); + } + + + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { } + @Override + public boolean onPreferenceClick(@NonNull Preference preference) { + return false; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getPreferenceScreen().getSharedPreferences() + .registerOnSharedPreferenceChangeListener(this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + getPreferenceScreen().getSharedPreferences() + .unregisterOnSharedPreferenceChangeListener(this); + } } \ No newline at end of file diff --git a/app/src/main/res/drawable-night/ic_baseline_mail_24.xml b/app/src/main/res/drawable-night/ic_baseline_mail_24.xml index 59801a5..279b280 100644 --- a/app/src/main/res/drawable-night/ic_baseline_mail_24.xml +++ b/app/src/main/res/drawable-night/ic_baseline_mail_24.xml @@ -1,6 +1,10 @@ - - + + diff --git a/app/src/main/res/drawable-night/ic_dn_switch.xml b/app/src/main/res/drawable-night/ic_dn_switch.xml index 9dcb5c7..6ac1fe2 100644 --- a/app/src/main/res/drawable-night/ic_dn_switch.xml +++ b/app/src/main/res/drawable-night/ic_dn_switch.xml @@ -1,8 +1,8 @@ - + android:viewportWidth="24" + android:viewportHeight="24"> + diff --git a/app/src/main/res/drawable/ic_baseline_delete_forever_24.xml b/app/src/main/res/drawable/ic_baseline_delete_forever_24.xml index b0b1d8d..789dd4f 100644 --- a/app/src/main/res/drawable/ic_baseline_delete_forever_24.xml +++ b/app/src/main/res/drawable/ic_baseline_delete_forever_24.xml @@ -1,10 +1,10 @@ - + android:viewportHeight="24"> + diff --git a/app/src/main/res/drawable/ic_baseline_list_24.xml b/app/src/main/res/drawable/ic_baseline_list_24.xml index f20eac3..1fa2e3c 100644 --- a/app/src/main/res/drawable/ic_baseline_list_24.xml +++ b/app/src/main/res/drawable/ic_baseline_list_24.xml @@ -1,11 +1,11 @@ - + android:viewportWidth="24" + android:viewportHeight="24"> + diff --git a/app/src/main/res/drawable/ic_baseline_mail_24.xml b/app/src/main/res/drawable/ic_baseline_mail_24.xml index f0abc2d..baf15be 100644 --- a/app/src/main/res/drawable/ic_baseline_mail_24.xml +++ b/app/src/main/res/drawable/ic_baseline_mail_24.xml @@ -1,5 +1,10 @@ - - + + diff --git a/app/src/main/res/drawable/ic_dn_switch.xml b/app/src/main/res/drawable/ic_dn_switch.xml index bfb9314..e52c5ba 100644 --- a/app/src/main/res/drawable/ic_dn_switch.xml +++ b/app/src/main/res/drawable/ic_dn_switch.xml @@ -1,8 +1,8 @@ + android:fillViewport="true" + android:scrollbars="vertical"> @@ -60,7 +61,7 @@ android:layout_height="wrap_content" android:layout_margin="10dp" android:text="@string/havefun" - android:textAppearance="?android:attr/textAppearanceLarge"/> + android:textAppearance="?android:attr/textAppearanceLarge" /> - + + app:showAsAction="never" /> - - - - \ No newline at end of file diff --git a/app/src/main/res/menu/menu_about.xml b/app/src/main/res/menu/menu_about.xml index 5f94120..e73b6af 100644 --- a/app/src/main/res/menu/menu_about.xml +++ b/app/src/main/res/menu/menu_about.xml @@ -1,5 +1,4 @@ - + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/logo_psc_round.xml b/app/src/main/res/mipmap-anydpi-v26/logo_psc_round.xml index 7af6ad0..f88bfa7 100644 --- a/app/src/main/res/mipmap-anydpi-v26/logo_psc_round.xml +++ b/app/src/main/res/mipmap-anydpi-v26/logo_psc_round.xml @@ -1,5 +1,5 @@ - - + + \ No newline at end of file diff --git a/app/src/main/res/mipmap-anydpi-v26/logo_psc_round_round.xml b/app/src/main/res/mipmap-anydpi-v26/logo_psc_round_round.xml index 7af6ad0..f88bfa7 100644 --- a/app/src/main/res/mipmap-anydpi-v26/logo_psc_round_round.xml +++ b/app/src/main/res/mipmap-anydpi-v26/logo_psc_round_round.xml @@ -1,5 +1,5 @@ - - + + \ No newline at end of file diff --git a/app/src/main/res/values-de-rDE/strings.xml b/app/src/main/res/values-de-rDE/strings.xml index ac88778..f394d01 100644 --- a/app/src/main/res/values-de-rDE/strings.xml +++ b/app/src/main/res/values-de-rDE/strings.xml @@ -6,8 +6,9 @@ Melden Sie ein technisches Problem oder reichen Sie eine neue Funktion ein Allgemein - Design - Umstalten zwischen dunkel und hell + + + Umschalten zwischen dunkel und hell Deaktivieren Sie den Darkmode durch Deaktivieren des Kontrollkästchens Aktivieren Sie den Darkmode, indem Sie das Kontrollkästchen aktivieren @@ -41,6 +42,10 @@ 4. Geben Sie abschließend nur so viel Teig in die Bratpfanne, dass der Teig nicht zu dick ist. Backen Sie den Teig von beiden Seiten goldbraun Dieses Projekt ist OpenSource und auf GitHub unter https://github.com/amarradi/palatschinkencounter zu finden. Nachtmodus + dunkel + hell + Das Rezept für die besten Palatschinken + Was ist das für eine app \ No newline at end of file diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml new file mode 100644 index 0000000..161c94a --- /dev/null +++ b/app/src/main/res/values/arrays.xml @@ -0,0 +1,14 @@ + + + + + + @string/darkmode_preference + @string/lightmode_preference + + + + @string/darkmode_preference + @string/lightmode_preference + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8ef2960..746a6ea 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -8,7 +8,9 @@ General Disable darkmode by unsetting the checkbox Enable darkmode by setting the checkbox - Theme + + + Toggle between dark and light theme 0 Pancake count @@ -17,6 +19,14 @@ Do you want to reset the counter? Do you want to be able to reset the counter? NO/YES + + + theme + + lightmode + + blue + cancel yes about the app @@ -26,6 +36,7 @@ With this app you can count all your eaten pancakes. have fun :-) recipe + The recipe for the best pancakes ever I take the following ingredients for the best Hungarian pancakes Ingredients 200 grams of flour @@ -41,4 +52,8 @@ 4. Finally, add only enough batter to the roasting pan so that the batter is not too thick. Bake the dough until golden brown on both sides This project is open source and can be found under https://github.com/amarradi/palatschinkencounter GitHub. Night mode - \ No newline at end of file + darkmode + lightmode + What is that app + + \ No newline at end of file diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 426ccb2..7a9c236 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -1,23 +1,36 @@ - - - + + android:entries="@array/design_mode_preference" + android:entryValues="@array/design_mode_preference_key" + android:key="@string/darkmode_preference" + android:title="@string/theme_summary" /> + + + + + + app:title="@string/send_feedback" /> \ No newline at end of file diff --git a/app/src/test/java/com/git/amarradi/palatschinkencounter/ExampleUnitTest.java b/app/src/test/java/com/git/amarradi/palatschinkencounter/ExampleUnitTest.java index d21bd18..fd38454 100644 --- a/app/src/test/java/com/git/amarradi/palatschinkencounter/ExampleUnitTest.java +++ b/app/src/test/java/com/git/amarradi/palatschinkencounter/ExampleUnitTest.java @@ -1,8 +1,8 @@ package com.git.amarradi.palatschinkencounter; -import org.junit.Test; +import static org.junit.Assert.assertEquals; -import static org.junit.Assert.*; +import org.junit.Test; /** * Example local unit test, which will execute on the development machine (host).