From 5544ae42484a8e13022aac9faed45d9c3e68e025 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 2 Aug 2021 18:59:30 +0200 Subject: [PATCH] Add option to change times that are used to switch to light/dark theme (#932) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Describe what your pull request does and which issue you’re targeting Closes #928 This allows to select the time to switch between themes. The mentioned color/contrast problem with the time selection isn't fixed. I also simplified the settings code (https://codeberg.org/qwerty287/GitNex/commit/0fe854fd49b0ba1ee0ac73f1ac1a50b7a4ca955a). And with [this](https://codeberg.org/qwerty287/GitNex/commit/a590f5bc4cc0325fa57e0d65a1e6b00582d90884) GitNex hides the two options to change the time if your theme isn't `Auto (Light/Dark)` or `Auto (Retro/Dark)`. I could apply this also to the notifications settings, that means that the advanced settings for notifications (polling delay, light...) are hidden if the notifications aren't enabled. this is ready to review/merge

- [X] I carefully read the [contribution guidelines](https://codeberg.org/GitNex/GitNex/src/branch/main/CONTRIBUTING.md). - [X] I'm following the code standards as defined [here](https://codeberg.org/gitnex/GitNex/wiki/Code-Standards). - [X] By submitting this pull request, I permit GitNex to license my work under the [GNU General Public License v3](https://codeberg.org/GitNex/GitNex/src/branch/main/LICENSE). Co-authored-by: qwerty287 Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/932 Reviewed-by: M M Arif Co-authored-by: qwerty287 Co-committed-by: qwerty287 --- .../mian/gitnex/activities/BaseActivity.java | 4 +- .../SettingsAppearanceActivity.java | 133 +++++++++++++----- .../org/mian/gitnex/core/MainApplication.java | 10 ++ .../org/mian/gitnex/helpers/TimeHelper.java | 6 +- .../main/res/drawable/shape_list_divider.xml | 5 + .../layout/activity_settings_appearance.xml | 66 +++++++++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/themes.xml | 3 +- 8 files changed, 190 insertions(+), 40 deletions(-) create mode 100644 app/src/main/res/drawable/shape_list_divider.xml diff --git a/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java b/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java index e054ba6e..ba0c36dc 100644 --- a/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/BaseActivity.java @@ -38,7 +38,7 @@ public abstract class BaseActivity extends AppCompatActivity { break; case 2: - if(TimeHelper.timeBetweenHours(18, 6)) { // 6pm to 6am + if(TimeHelper.timeBetweenHours(tinyDB.getInt("darkThemeTimeHour"), tinyDB.getInt("lightThemeTimeHour"), tinyDB.getInt("darkThemeTimeMinute"), tinyDB.getInt("lightThemeTimeMinute"))) { tinyDB.putString("currentTheme", "dark"); setTheme(R.style.AppTheme); @@ -55,7 +55,7 @@ public abstract class BaseActivity extends AppCompatActivity { setTheme(R.style.AppThemeRetro); break; case 4: - if(TimeHelper.timeBetweenHours(18, 6)) { // 6pm to 6am + if(TimeHelper.timeBetweenHours(tinyDB.getInt("darkThemeTimeHour"), tinyDB.getInt("lightThemeTimeHour"), tinyDB.getInt("darkThemeTimeMinute"), tinyDB.getInt("lightThemeTimeMinute"))) { tinyDB.putString("currentTheme", "dark"); setTheme(R.style.AppTheme); diff --git a/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java b/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java index 56c78aca..04aa0315 100644 --- a/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/SettingsAppearanceActivity.java @@ -1,14 +1,20 @@ package org.mian.gitnex.activities; +import android.app.Dialog; +import android.app.TimePickerDialog; import android.os.Bundle; +import android.text.format.DateFormat; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; -import android.widget.TextView; +import android.widget.TimePicker; import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.DialogFragment; import com.google.android.material.switchmaterial.SwitchMaterial; +import org.jetbrains.annotations.NotNull; import org.mian.gitnex.R; import org.mian.gitnex.databinding.ActivitySettingsAppearanceBinding; +import org.mian.gitnex.helpers.TinyDB; import org.mian.gitnex.helpers.Toasty; /** @@ -38,13 +44,11 @@ public class SettingsAppearanceActivity extends BaseActivity { ImageView closeActivity = activitySettingsAppearanceBinding.close; - final TextView tvDateTimeSelected = activitySettingsAppearanceBinding.tvDateTimeSelected; // setter for time - final TextView customFontSelected = activitySettingsAppearanceBinding.customFontSelected; // setter for custom font - final TextView themeSelected = activitySettingsAppearanceBinding.themeSelected; // setter for theme - LinearLayout timeFrame = activitySettingsAppearanceBinding.timeFrame; LinearLayout customFontFrame = activitySettingsAppearanceBinding.customFontFrame; LinearLayout themeFrame = activitySettingsAppearanceBinding.themeSelectionFrame; + LinearLayout lightTimeFrame = activitySettingsAppearanceBinding.lightThemeTimeSelectionFrame; + LinearLayout darkTimeFrame = activitySettingsAppearanceBinding.darkThemeTimeSelectionFrame; SwitchMaterial counterBadgesSwitch = activitySettingsAppearanceBinding.switchCounterBadge; @@ -55,35 +59,36 @@ public class SettingsAppearanceActivity extends BaseActivity { initCloseListener(); closeActivity.setOnClickListener(onClickListener); - if(!tinyDB.getString("timeStr").isEmpty()) { + String lightMinute = String.valueOf(tinyDB.getInt("lightThemeTimeMinute")); + String lightHour = String.valueOf(tinyDB.getInt("lightThemeTimeHour")); + if(lightMinute.length() == 1) lightMinute = "0" + lightMinute; + if(lightHour.length() == 1) lightHour = "0" + lightHour; - tvDateTimeSelected.setText(tinyDB.getString("timeStr")); + String darkMinute = String.valueOf(tinyDB.getInt("darkThemeTimeMinute")); + String darkHour = String.valueOf(tinyDB.getInt("darkThemeTimeHour")); + if(darkMinute.length() == 1) darkMinute = "0" + darkMinute; + if(darkHour.length() == 1) darkHour = "0" + darkHour; + + activitySettingsAppearanceBinding.lightThemeSelectedTime.setText(ctx.getResources().getString(R.string.settingsThemeTimeSelectedHint, lightHour, + lightMinute)); + activitySettingsAppearanceBinding.darkThemeSelectedTime.setText(ctx.getResources().getString(R.string.settingsThemeTimeSelectedHint, darkHour, + darkMinute)); + activitySettingsAppearanceBinding.tvDateTimeSelected.setText(tinyDB.getString("timeStr")); + activitySettingsAppearanceBinding.customFontSelected.setText(tinyDB.getString("customFontStr", "Manrope")); + activitySettingsAppearanceBinding.themeSelected.setText(tinyDB.getString("themeStr", "Dark")); + + if(tinyDB.getString("themeStr").startsWith("Auto")) { + darkTimeFrame.setVisibility(View.VISIBLE); + lightTimeFrame.setVisibility(View.VISIBLE); + } + else { + darkTimeFrame.setVisibility(View.GONE); + lightTimeFrame.setVisibility(View.GONE); } - if(!tinyDB.getString("customFontStr").isEmpty()) { - - customFontSelected.setText(tinyDB.getString("customFontStr")); - } - - if(!tinyDB.getString("themeStr").isEmpty()) { - - themeSelected.setText(tinyDB.getString("themeStr")); - } - - if(timeSelectedChoice == 0) { - - timeSelectedChoice = tinyDB.getInt("timeId"); - } - - if(customFontSelectedChoice == 0) { - - customFontSelectedChoice = tinyDB.getInt("customFontId", 1); - } - - if(themeSelectedChoice == 0) { - - themeSelectedChoice = tinyDB.getInt("themeId"); - } + timeSelectedChoice = tinyDB.getInt("timeId"); + customFontSelectedChoice = tinyDB.getInt("customFontId", 1); + themeSelectedChoice = tinyDB.getInt("themeId"); counterBadgesSwitch.setChecked(tinyDB.getBoolean("enableCounterBadges")); @@ -105,7 +110,7 @@ public class SettingsAppearanceActivity extends BaseActivity { tsBuilder.setSingleChoiceItems(themeList, themeSelectedChoice, (dialogInterfaceTheme, i) -> { themeSelectedChoice = i; - themeSelected.setText(themeList[i]); + activitySettingsAppearanceBinding.themeSelected.setText(themeList[i]); tinyDB.putString("themeStr", themeList[i]); tinyDB.putInt("themeId", i); @@ -120,6 +125,16 @@ public class SettingsAppearanceActivity extends BaseActivity { cfDialog.show(); }); + lightTimeFrame.setOnClickListener(view -> { + LightTimePicker timePicker = new LightTimePicker(); + timePicker.show(getSupportFragmentManager(), "timePicker"); + }); + + darkTimeFrame.setOnClickListener(view -> { + DarkTimePicker timePicker = new DarkTimePicker(); + timePicker.show(getSupportFragmentManager(), "timePicker"); + }); + // custom font dialog customFontFrame.setOnClickListener(view -> { @@ -131,7 +146,7 @@ public class SettingsAppearanceActivity extends BaseActivity { cfBuilder.setSingleChoiceItems(customFontList, customFontSelectedChoice, (dialogInterfaceCustomFont, i) -> { customFontSelectedChoice = i; - customFontSelected.setText(customFontList[i]); + activitySettingsAppearanceBinding.customFontSelected.setText(customFontList[i]); tinyDB.putString("customFontStr", customFontList[i]); tinyDB.putInt("customFontId", i); @@ -157,7 +172,7 @@ public class SettingsAppearanceActivity extends BaseActivity { tBuilder.setSingleChoiceItems(timeList, timeSelectedChoice, (dialogInterfaceTime, i) -> { timeSelectedChoice = i; - tvDateTimeSelected.setText(timeList[i]); + activitySettingsAppearanceBinding.tvDateTimeSelected.setText(timeList[i]); tinyDB.putString("timeStr", timeList[i]); tinyDB.putInt("timeId", i); @@ -184,4 +199,54 @@ public class SettingsAppearanceActivity extends BaseActivity { onClickListener = view -> finish(); } + public static class LightTimePicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener { + + TinyDB db = TinyDB.getInstance(getContext()); + + @NotNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + int hour = db.getInt("lightThemeTimeHour"); + int minute = db.getInt("lightThemeTimeMinute"); + + return new TimePickerDialog(getActivity(), this, hour, minute, true); + } + + @Override + public void onTimeSet(TimePicker view, int hourOfDay, int minute) { + db.putInt("lightThemeTimeHour", hourOfDay); + db.putInt("lightThemeTimeMinute", minute); + db.putBoolean("refreshParent", true); + requireActivity().overridePendingTransition(0, 0); + this.dismiss(); + Toasty.success(requireActivity().getApplicationContext(), requireContext().getResources().getString(R.string.settingsSave)); + requireActivity().recreate(); + } + } + + public static class DarkTimePicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener { + + TinyDB db = TinyDB.getInstance(getContext()); + + @NotNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + int hour = db.getInt("darkThemeTimeHour"); + int minute = db.getInt("darkThemeTimeMinute"); + + return new TimePickerDialog(getActivity(), this, hour, minute, true); + } + + @Override + public void onTimeSet(TimePicker view, int hourOfDay, int minute) { + db.putInt("darkThemeTimeHour", hourOfDay); + db.putInt("darkThemeTimeMinute", minute); + db.putBoolean("refreshParent", true); + requireActivity().overridePendingTransition(0, 0); + this.dismiss(); + Toasty.success(requireActivity().getApplicationContext(), requireContext().getResources().getString(R.string.settingsSave)); + requireActivity().recreate(); + } + } + } diff --git a/app/src/main/java/org/mian/gitnex/core/MainApplication.java b/app/src/main/java/org/mian/gitnex/core/MainApplication.java index 23250edc..7142a2e6 100644 --- a/app/src/main/java/org/mian/gitnex/core/MainApplication.java +++ b/app/src/main/java/org/mian/gitnex/core/MainApplication.java @@ -157,5 +157,15 @@ public class MainApplication extends Application { tinyDB.putInt("langId", 0); } + if(tinyDB.getInt("darkThemeTimeHour", 100) == 100) { + tinyDB.putInt("lightThemeTimeHour", 6); + tinyDB.putInt("lightThemeTimeMinute", 0); + tinyDB.putInt("darkThemeTimeHour", 18); + tinyDB.putInt("darkThemeTimeMinute", 0); + } + + if(tinyDB.getString("timeStr").isEmpty()) { + tinyDB.putString("timeStr", getString(R.string.settingsDateTimeHeaderDefault)); + } } } diff --git a/app/src/main/java/org/mian/gitnex/helpers/TimeHelper.java b/app/src/main/java/org/mian/gitnex/helpers/TimeHelper.java index 022cd9c9..37d08332 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/TimeHelper.java +++ b/app/src/main/java/org/mian/gitnex/helpers/TimeHelper.java @@ -69,17 +69,17 @@ public class TimeHelper { } - public static boolean timeBetweenHours(int fromHour, int toHour) { + public static boolean timeBetweenHours(int fromHour, int toHour, int fromMinute, int toMinute) { Calendar cal = Calendar.getInstance(); Calendar from = Calendar.getInstance(); from.set(Calendar.HOUR_OF_DAY, fromHour); - from.set(Calendar.MINUTE, 0); + from.set(Calendar.MINUTE, fromMinute); Calendar to = Calendar.getInstance(); to.set(Calendar.HOUR_OF_DAY, toHour); - to.set(Calendar.MINUTE, 0); + to.set(Calendar.MINUTE, toMinute); if(to.before(from)) { if(cal.after(to)) { diff --git a/app/src/main/res/drawable/shape_list_divider.xml b/app/src/main/res/drawable/shape_list_divider.xml new file mode 100644 index 00000000..ff307019 --- /dev/null +++ b/app/src/main/res/drawable/shape_list_divider.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/activity_settings_appearance.xml b/app/src/main/res/layout/activity_settings_appearance.xml index 16853f75..90ac744a 100644 --- a/app/src/main/res/layout/activity_settings_appearance.xml +++ b/app/src/main/res/layout/activity_settings_appearance.xml @@ -81,6 +81,72 @@ + + + + + + + + + + + + + + + + Pretty Normal Choose Language + Light Theme Switch Time + Dark Theme Switch Time + %s:%s Choose Time Format Translate GitNex via Crowdin Green - Black diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index d782d482..96d33d6a 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -2,7 +2,7 @@ -