Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit 6c2b252

Browse files
jainkuniyakunall17
authored andcommitted
Added functionality for auto night theme.
- Added a checkbox option in menu. - If auto theme is enabled then irrespective of preffered theme, it will be overridden by auto theme (depending on time). - User can switch theme even if auto theme is enabled, but it is for that instance only.
1 parent a47801a commit 6c2b252

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

app/src/main/java/com/zulip/android/ZulipApp.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.content.pm.PackageInfo;
88
import android.content.pm.PackageManager.NameNotFoundException;
99
import android.os.Handler;
10+
import android.support.v7.app.AppCompatDelegate;
1011
import android.util.Log;
1112

1213
import com.crashlytics.android.Crashlytics;
@@ -135,11 +136,13 @@ public void onCreate() {
135136
if (!BuildConfig.DEBUG)
136137
Fabric.with(this, new Crashlytics());
137138
ZulipApp.setInstance(this);
138-
139139
// This used to be from HumbugActivity.getPreferences, so we keep that
140140
// file name.
141141
this.settings = getSharedPreferences("HumbugActivity", Context.MODE_PRIVATE);
142-
142+
//check for auto theme is enabled or not
143+
if (getSettings().getBoolean(Constants.AUTO_NIGHT_THEME, false)) {
144+
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
145+
}
143146
max_message_id = settings.getInt("max_message_id", -1);
144147
eventQueueId = settings.getString("eventQueueId", null);
145148
lastEventId = settings.getInt("lastEventId", -1);
@@ -609,4 +612,15 @@ public void makeNightThemeDefault(boolean nightTheme) {
609612
editor.putBoolean(Constants.NIGHT_THEME, nightTheme);
610613
editor.apply();
611614
}
615+
616+
/**
617+
* update auto night theme preference
618+
* @param value preference value
619+
*/
620+
public void setAutoNightTheme(boolean value) {
621+
SharedPreferences.Editor editor = getSettings().edit();
622+
623+
editor.putBoolean(Constants.AUTO_NIGHT_THEME, value);
624+
editor.apply();
625+
}
612626
}

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ protected void onCreate(Bundle savedInstanceState) {
357357
boolean isCurrentThemeNight = (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES);
358358

359359
//apply preferred theme
360-
if (app.getSettings().getBoolean(Constants.NIGHT_THEME, false) && !isCurrentThemeNight) {
360+
if (app.getSettings().getBoolean(Constants.NIGHT_THEME, false) && !isCurrentThemeNight
361+
&& !app.getSettings().getBoolean(Constants.AUTO_NIGHT_THEME,false)) {
361362
setNightMode(AppCompatDelegate.MODE_NIGHT_YES);
362363
}
363364

@@ -2258,6 +2259,8 @@ public boolean onCreateOptionsMenu(Menu menu) {
22582259
getMenuInflater().inflate(R.menu.options, menu);
22592260
prepareSearchView(menu);
22602261
this.menu = menu;
2262+
menu.findItem(R.id.autoTheme).setChecked(app.getSettings()
2263+
.getBoolean(Constants.AUTO_NIGHT_THEME,false));
22612264
return true;
22622265
}
22632266

@@ -2373,6 +2376,13 @@ public void onClick(
23732376
break;
23742377
}
23752378
break;
2379+
case R.id.autoTheme:
2380+
item.setChecked(!item.isChecked());
2381+
app.setAutoNightTheme(item.isChecked());
2382+
if (item.isChecked()) {
2383+
setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
2384+
}
2385+
break;
23762386
case R.id.refresh:
23772387
Log.w("menu", "Refreshed manually by user. We shouldn't need this.");
23782388
commonProgressDialog.showWithMessage(getString(R.string.refreshing));

app/src/main/java/com/zulip/android/util/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ public class Constants {
2828
public static long HIDE_TIMESTAMP_THRESHOLD = 60 * 1000;// 1 minute
2929
public static String ACTION_MESSAGE_PUSH_NOTIFICATION = "ACTION_MESSAGE_PUSH_NOTIFICATION";
3030
public static String NIGHT_THEME = "NIGHT_THEME";
31+
public static String AUTO_NIGHT_THEME = "AUTO_NIGHT_THEME";
3132
}

app/src/main/res/menu/options.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,20 @@
3535
</item>
3636

3737
<item
38-
android:id="@+id/daynight"
39-
android:title="@string/switch_theme" />
38+
android:id="@+id/theme"
39+
android:title="@string/theme">
40+
<menu>
41+
<item
42+
android:id="@+id/daynight"
43+
android:title="@string/switch_theme" />
44+
<item
45+
android:id="@+id/autoTheme"
46+
android:checkable="true"
47+
android:title="@string/autoTheme"
48+
app:actionViewClass="android.widget.CheckBox" />
49+
</menu>
50+
</item>
51+
4052
<item
4153
android:id="@+id/isStarred"
4254
android:title="@string/starred_messages" />

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,15 @@
173173
<string name="no_message_to_display">Sorry, no messages to display!</string>
174174
<string name="crop">Crop</string>
175175
<string name="logout_title">Are you sure you want to logout?</string>
176+
<string name="autoTheme">Auto Night Theme</string>
177+
<string name="theme">Theme</string>
178+
<string name="terms">Terms of Service</string>
176179
<!--Placeholder strings-->
177180
<string name="placeholder_my_stream">MyStream</string>
178181
<string name="placeholder_short_stream">Short stream</string>
179182
<string name="placeholder_topic">Topic</string>
180183
<string name="placeholder_message_here">Message here</string>
181184
<string name="placeholder_sender_name">Sender name</string>
182185
<string name="placeholder_social">Social</string>
183-
<!--Placeholder strings-->
184-
<string name="terms">Terms of Service</string>
186+
185187
</resources>

0 commit comments

Comments
 (0)