From 9542cb4b76fb7cb1ab3c9742be32b485246e80e4 Mon Sep 17 00:00:00 2001 From: VivekThazhathattil Date: Mon, 3 Oct 2022 11:43:48 +0530 Subject: [PATCH] fix dark mode --- .idea/misc.xml | 8 +-- app/build.gradle | 4 +- .../example/spokennumbers/MainActivity.java | 54 +++++++++++++++++-- .../com/example/spokennumbers/PrefConfig.java | 13 +---- .../spoken_numbers_ingame_fragment.java | 6 +-- .../spoken_numbers_main_fragment.java | 28 +--------- .../main/res/drawable/ic_dark_mode_icon.xml | 5 ++ app/src/main/res/drawable/ic_info_icon.xml | 5 ++ app/src/main/res/drawable/ic_speaker.xml | 11 ---- app/src/main/res/drawable/ic_speaker_stop.xml | 12 ----- app/src/main/res/layout/activity_main.xml | 2 +- .../main/res/layout/fragment_flash_anzan.xml | 8 +-- .../main/res/layout/fragment_games_menu.xml | 20 ++++--- .../layout/spoken_numbers_main_fragment.xml | 13 +---- app/src/main/res/menu/dekadico_menu.xml | 13 +++++ app/src/main/res/values-night/colors.xml | 11 ---- app/src/main/res/values-night/strings.xml | 26 --------- app/src/main/res/values/strings.xml | 1 - app/src/main/res/values/styles.xml | 2 +- 19 files changed, 106 insertions(+), 136 deletions(-) create mode 100644 app/src/main/res/drawable/ic_dark_mode_icon.xml create mode 100644 app/src/main/res/drawable/ic_info_icon.xml delete mode 100644 app/src/main/res/drawable/ic_speaker.xml delete mode 100644 app/src/main/res/drawable/ic_speaker_stop.xml create mode 100644 app/src/main/res/menu/dekadico_menu.xml delete mode 100644 app/src/main/res/values-night/colors.xml delete mode 100644 app/src/main/res/values-night/strings.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 77ebc0b..f08e94f 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -6,15 +6,17 @@ + - - + + - + + diff --git a/app/build.gradle b/app/build.gradle index 3cc93f6..14a1efa 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.example.spokennumbers" minSdkVersion 16 targetSdkVersion 29 - versionCode 9 - versionName "1.8" + versionCode 10 + versionName "1.8.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } diff --git a/app/src/main/java/com/example/spokennumbers/MainActivity.java b/app/src/main/java/com/example/spokennumbers/MainActivity.java index 6434402..b902ac0 100644 --- a/app/src/main/java/com/example/spokennumbers/MainActivity.java +++ b/app/src/main/java/com/example/spokennumbers/MainActivity.java @@ -10,12 +10,18 @@ import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentTransaction; +import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.Bundle; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.TextView; +import android.widget.Toast; import java.time.Instant; import java.util.ArrayList; @@ -26,14 +32,13 @@ public class MainActivity extends AppCompatActivity { private static final int CONTENT_VIEW_ID = 10101010; private FragmentManager fragmentManager; - /* when I started out with this project, spoken numbers - * was the only game that I intended to add to this app. - * So mainFragment is for the Spoken numbers module */ private spoken_numbers_main_fragment mainFragment; private spoken_numbers_ingame_fragment ingameFragment; private spoken_numbers_recall_fragment recallFragment; private EvaluationFragment evaluationFragment; + private Boolean is_night_mode = false; + private FlashAnzan flashAnzanFragment; private GamesMenu gamesMenuFragment; @@ -128,12 +133,46 @@ public void switchToMainFragment(String mode){ } fragmentTransaction.commit(); } + + @Override + public boolean onCreateOptionsMenu(Menu menu){ + MenuInflater inflater = getMenuInflater(); + inflater.inflate(R.menu.dekadico_menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem menuItem){ + switch(menuItem.getItemId()){ + case R.id.toggle_dark_mode: + if(!is_night_mode){ + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); + is_night_mode = true; + } + else{ + AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); + is_night_mode = false; + } + return true; + case R.id.github_link: + Intent viewIntent = + new Intent("android.intent.action.VIEW", + Uri.parse("https://github.com/VivekThazhathattil/dekadico")); + startActivity(viewIntent); + return true; + default: + return super.onOptionsItemSelected(menuItem); + } + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FrameLayout frame = new FrameLayout(this); frame.setId(CONTENT_VIEW_ID); + is_night_mode = AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES; + // setContentView(R.layout.activity_main); setContentView(frame, new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT, @@ -172,4 +211,13 @@ public void onBackPressed() { } super.onBackPressed(); } + + @Override + public void recreate(){ + finish(); + overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); + + startActivity(getIntent()); + overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/spokennumbers/PrefConfig.java b/app/src/main/java/com/example/spokennumbers/PrefConfig.java index 0c3a514..25c3fed 100644 --- a/app/src/main/java/com/example/spokennumbers/PrefConfig.java +++ b/app/src/main/java/com/example/spokennumbers/PrefConfig.java @@ -9,7 +9,6 @@ public class PrefConfig { public static final String SHARED_PREFS = "sharedPrefs"; /* Spoken numbers module */ - private static final String NIGHT_MODE = "is_night_mode"; public static final String DELAYTIME = "delayTime"; public static final String INCTIME = "incTime"; public static final String ISFEMALE = "isFemale"; @@ -39,14 +38,13 @@ public PrefConfig(Context context){ } public void saveData(String delayTimeText, String incTimeText, boolean femaleChecked, - boolean decimalChecked, boolean isEvalMode, boolean isNightMode){ + boolean decimalChecked, boolean isEvalMode){ editor = sharedPreferences.edit(); editor.putString(DELAYTIME, delayTimeText); editor.putString(INCTIME, incTimeText); editor.putBoolean(ISFEMALE, femaleChecked); editor.putBoolean(ISDECIMAL, decimalChecked); editor.putBoolean(EVALMODE, isEvalMode); - editor.putBoolean(NIGHT_MODE, isNightMode); editor.apply(); } @@ -128,13 +126,4 @@ public void saveHighScore(int newScore){ public int loadHighScore(){ return (int)sharedPreferences.getInt(HIGHSCORE, 0); } - - public boolean loadNightModeChecked(){ - return (boolean)sharedPreferences.getBoolean(NIGHT_MODE, false); - } - public void saveNightModeChecked(boolean isNightMode){ - editor = sharedPreferences.edit(); - editor.putBoolean(NIGHT_MODE, isNightMode); - editor.apply(); - } } diff --git a/app/src/main/java/com/example/spokennumbers/spoken_numbers_ingame_fragment.java b/app/src/main/java/com/example/spokennumbers/spoken_numbers_ingame_fragment.java index 375b006..ee29097 100644 --- a/app/src/main/java/com/example/spokennumbers/spoken_numbers_ingame_fragment.java +++ b/app/src/main/java/com/example/spokennumbers/spoken_numbers_ingame_fragment.java @@ -99,7 +99,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState){ final Button incrementButton = getView().findViewById(R.id.time_increment); final TextView currentTimeTextView = getView().findViewById(R.id.current_time_control_text); - String currTimeText = timeDelay + "s"; + String currTimeText = String.format("%.3f", timeDelay) + "s"; currentTimeTextView.setText(currTimeText); String timeIncStr = "+" + timeInc; @@ -117,7 +117,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState){ incrementButton.setOnClickListener(v -> { timeDelay += timeInc; - String currTimeText12 = timeDelay + "s"; + String currTimeText12 = String.format("%.3f", timeDelay) + "s"; currentTimeTextView.setText(currTimeText12); timer.cancel(); timer = setupTimer(defaultMillisLeft, timeDelay); @@ -129,7 +129,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState){ return; } timeDelay -= timeInc; - String currTimeText1 = timeDelay + "s"; + String currTimeText1 = String.format("%.3f", timeDelay) + "s"; currentTimeTextView.setText(currTimeText1); timer.cancel(); timer = setupTimer(defaultMillisLeft, timeDelay); diff --git a/app/src/main/java/com/example/spokennumbers/spoken_numbers_main_fragment.java b/app/src/main/java/com/example/spokennumbers/spoken_numbers_main_fragment.java index 5860ce9..250a91f 100644 --- a/app/src/main/java/com/example/spokennumbers/spoken_numbers_main_fragment.java +++ b/app/src/main/java/com/example/spokennumbers/spoken_numbers_main_fragment.java @@ -83,32 +83,6 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { boolean evalState = MainActivity.prefConfig.loadEvalModeChecked(); evalSwitch.setChecked(evalState); - Switch nightSwitch = Objects.requireNonNull(getView()).findViewById(R.id.night_mode_switch); - boolean isNightModeOn = MainActivity.prefConfig.loadNightModeChecked(); - nightSwitch.setChecked(isNightModeOn); - if(isNightModeOn) - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); - else - AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); - - nightSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { -// @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { -// //AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); -// if(isChecked){ -// //AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); -// } -// else { -// MainActivity.this.recreate(); -// } - MainActivity.prefConfig.saveNightModeChecked(isChecked); - Toast toast = Toast.makeText(getContext(), "Restart app to apply theme changes.", Toast.LENGTH_SHORT); - toast.setMargin(50, 50); - toast.show(); - } - }); - - TextView highScoreText = getView().findViewById(R.id.high_score_view); int highScoreInt = MainActivity.prefConfig.loadHighScore(); String highScoreStr = "High Score: " + highScoreInt; @@ -167,7 +141,7 @@ else if(binaryButton.isChecked()){ MainActivity.evaluationMode = evalSwitch.isChecked(); MainActivity.prefConfig.saveData(Float.toString(timeDelayNum), - Float.toString(timeIncNum), isFemaleVoice, isDecimal, MainActivity.evaluationMode, false); + Float.toString(timeIncNum), isFemaleVoice, isDecimal, MainActivity.evaluationMode); ((MainActivity) Objects.requireNonNull(getActivity())).switchToInGameFragment(timeDelayNum, timeIncNum, isFemaleVoice, isDecimal); } }); diff --git a/app/src/main/res/drawable/ic_dark_mode_icon.xml b/app/src/main/res/drawable/ic_dark_mode_icon.xml new file mode 100644 index 0000000..10c4546 --- /dev/null +++ b/app/src/main/res/drawable/ic_dark_mode_icon.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_info_icon.xml b/app/src/main/res/drawable/ic_info_icon.xml new file mode 100644 index 0000000..654cfb3 --- /dev/null +++ b/app/src/main/res/drawable/ic_info_icon.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/drawable/ic_speaker.xml b/app/src/main/res/drawable/ic_speaker.xml deleted file mode 100644 index ac6f44a..0000000 --- a/app/src/main/res/drawable/ic_speaker.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - diff --git a/app/src/main/res/drawable/ic_speaker_stop.xml b/app/src/main/res/drawable/ic_speaker_stop.xml deleted file mode 100644 index 88fc3f4..0000000 --- a/app/src/main/res/drawable/ic_speaker_stop.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f7b2281..26dbbee 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -24,7 +24,7 @@ app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/time_delay_input" - app:srcCompat="@drawable/ic_speaker" /> + />