Skip to content

Commit

Permalink
Add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattheis committed May 9, 2020
1 parent 91be6fa commit ee36b6a
Show file tree
Hide file tree
Showing 19 changed files with 205 additions and 27 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta5'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.preference:preference:1.1.0-rc01'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
android:name=".log.LogsActivity"
android:label="@string/title_activity_logs"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".settings.SettingsActivity"
android:theme="@style/AppTheme.NoActionBar"
android:label="@string/title_activity_settings" />

<service android:name=".service.WebSocketService" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;
import com.github.gotify.NotificationSupport;
import com.github.gotify.R;
import com.github.gotify.Settings;
Expand All @@ -21,6 +22,7 @@
import com.github.gotify.login.LoginActivity;
import com.github.gotify.messages.MessagesActivity;
import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.ThemeHelper;

import static com.github.gotify.api.Callback.callInUI;

Expand All @@ -31,6 +33,13 @@ public class InitializationActivity extends AppCompatActivity {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.init(this);
String theme =
PreferenceManager.getDefaultSharedPreferences(this)
.getString(
getString(R.string.setting_key_theme),
getString(R.string.theme_default));
ThemeHelper.setTheme(this, theme);

setContentView(R.layout.splash);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/github/gotify/login/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
Expand Down Expand Up @@ -135,7 +136,7 @@ public void doCheckUrl() {
}

public void showHttpWarning() {
new AlertDialog.Builder(this)
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.warning)
.setCancelable(true)
.setMessage(R.string.http_warning)
Expand Down Expand Up @@ -273,7 +274,7 @@ private void newClientDialog(ApiClient client) {
EditText clientName = new EditText(this);
clientName.setText(Build.MODEL);

new AlertDialog.Builder(this)
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.create_client_title)
.setMessage(R.string.create_client_message)
.setView(clientName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
Expand Down Expand Up @@ -61,6 +62,7 @@
import com.github.gotify.messages.provider.MessageWithImage;
import com.github.gotify.picasso.PicassoDataRequestHandler;
import com.github.gotify.service.WebSocketService;
import com.github.gotify.settings.SettingsActivity;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
Expand Down Expand Up @@ -309,14 +311,16 @@ public boolean onNavigationItemSelected(MenuItem item) {
startLoading();
toolbar.setSubtitle("");
} else if (id == R.id.logout) {
new AlertDialog.Builder(this)
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AppTheme_Dialog))
.setTitle(R.string.logout)
.setMessage(getString(R.string.logout_confirm))
.setPositiveButton(R.string.yes, this::doLogout)
.setNegativeButton(R.string.cancel, (a, b) -> {})
.show();
} else if (id == R.id.nav_logs) {
startActivity(new Intent(this, LogsActivity.class));
} else if (id == R.id.settings) {
startActivity(new Intent(this, SettingsActivity.class));
}

drawer.closeDrawer(GravityCompat.START);
Expand All @@ -326,6 +330,7 @@ public boolean onNavigationItemSelected(MenuItem item) {
public void doLogout(DialogInterface dialog, int which) {
setContentView(R.layout.splash);
new DeleteClientAndNavigateToLogin().execute();
finish();
}

private void startLoading() {
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/java/com/github/gotify/settings/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.github.gotify.settings;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import com.github.gotify.R;

public class SettingsActivity extends AppCompatActivity
implements SharedPreferences.OnSharedPreferenceChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
setSupportActionBar(findViewById(R.id.toolbar));
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
}
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (getString(R.string.setting_key_theme).equals(key)) {
ThemeHelper.setTheme(
this, sharedPreferences.getString(key, getString(R.string.theme_default)));
}
}

public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/github/gotify/settings/ThemeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.gotify.settings;

import android.content.Context;
import android.os.Build;
import androidx.appcompat.app.AppCompatDelegate;
import com.github.gotify.R;

public final class ThemeHelper {
private ThemeHelper() {}

public static void setTheme(Context context, String newTheme) {
AppCompatDelegate.setDefaultNightMode(ofKey(context, newTheme));
}

private static int ofKey(Context context, String newTheme) {
if (context.getString(R.string.theme_dark).equals(newTheme)) {
return AppCompatDelegate.MODE_NIGHT_YES;
}
if (context.getString(R.string.theme_light).equals(newTheme)) {
return AppCompatDelegate.MODE_NIGHT_NO;
}
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
return AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
}
return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}
}
10 changes: 0 additions & 10 deletions app/src/main/res/drawable/gradient.xml

This file was deleted.

4 changes: 3 additions & 1 deletion app/src/main/res/drawable/ic_settings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<vector android:height="24dp" android:viewportHeight="20"
android:viewportWidth="20" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M15.95,10.78c0.03,-0.25 0.05,-0.51 0.05,-0.78s-0.02,-0.53 -0.06,-0.78l1.69,-1.32c0.15,-0.12 0.19,-0.34 0.1,-0.51l-1.6,-2.77c-0.1,-0.18 -0.31,-0.24 -0.49,-0.18l-1.99,0.8c-0.42,-0.32 -0.86,-0.58 -1.35,-0.78L12,2.34c-0.03,-0.2 -0.2,-0.34 -0.4,-0.34H8.4c-0.2,0 -0.36,0.14 -0.39,0.34l-0.3,2.12c-0.49,0.2 -0.94,0.47 -1.35,0.78l-1.99,-0.8c-0.18,-0.07 -0.39,0 -0.49,0.18l-1.6,2.77c-0.1,0.18 -0.06,0.39 0.1,0.51l1.69,1.32c-0.04,0.25 -0.07,0.52 -0.07,0.78s0.02,0.53 0.06,0.78L2.37,12.1c-0.15,0.12 -0.19,0.34 -0.1,0.51l1.6,2.77c0.1,0.18 0.31,0.24 0.49,0.18l1.99,-0.8c0.42,0.32 0.86,0.58 1.35,0.78l0.3,2.12c0.04,0.2 0.2,0.34 0.4,0.34h3.2c0.2,0 0.37,-0.14 0.39,-0.34l0.3,-2.12c0.49,-0.2 0.94,-0.47 1.35,-0.78l1.99,0.8c0.18,0.07 0.39,0 0.49,-0.18l1.6,-2.77c0.1,-0.18 0.06,-0.39 -0.1,-0.51l-1.67,-1.32zM10,13c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3 3,1.35 3,3 -1.35,3 -3,3z"/>
<path
android:fillColor="@color/icons"
android:pathData="M15.95,10.78c0.03,-0.25 0.05,-0.51 0.05,-0.78s-0.02,-0.53 -0.06,-0.78l1.69,-1.32c0.15,-0.12 0.19,-0.34 0.1,-0.51l-1.6,-2.77c-0.1,-0.18 -0.31,-0.24 -0.49,-0.18l-1.99,0.8c-0.42,-0.32 -0.86,-0.58 -1.35,-0.78L12,2.34c-0.03,-0.2 -0.2,-0.34 -0.4,-0.34H8.4c-0.2,0 -0.36,0.14 -0.39,0.34l-0.3,2.12c-0.49,0.2 -0.94,0.47 -1.35,0.78l-1.99,-0.8c-0.18,-0.07 -0.39,0 -0.49,0.18l-1.6,2.77c-0.1,0.18 -0.06,0.39 0.1,0.51l1.69,1.32c-0.04,0.25 -0.07,0.52 -0.07,0.78s0.02,0.53 0.06,0.78L2.37,12.1c-0.15,0.12 -0.19,0.34 -0.1,0.51l1.6,2.77c0.1,0.18 0.31,0.24 0.49,0.18l1.99,-0.8c0.42,0.32 0.86,0.58 1.35,0.78l0.3,2.12c0.04,0.2 0.2,0.34 0.4,0.34h3.2c0.2,0 0.37,-0.14 0.39,-0.34l0.3,-2.12c0.49,-0.2 0.94,-0.47 1.35,-0.78l1.99,0.8c0.18,0.07 0.39,0 0.49,-0.18l1.6,-2.77c0.1,-0.18 0.06,-0.39 -0.1,-0.51l-1.67,-1.32zM10,13c-1.65,0 -3,-1.35 -3,-3s1.35,-3 3,-3 3,1.35 3,3 -1.35,3 -3,3z"/>
</vector>
12 changes: 6 additions & 6 deletions app/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
android:layout_height="match_parent"
android:fillViewport="true">

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
android:orientation="vertical"
tools:context=".login.LoginActivity"
tools:layout_editor_absoluteY="25dp">

<Button
android:id="@+id/open_logs"
android:layout_width="50dp"
android:layout_width="70dp"
android:layout_height="40dp"
android:background="@null"
android:insetTop="0dp"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:text="@string/logs"
app:backgroundTint="#00FFFFFF"
android:textColor="@color/colorNavPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_messages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:theme="@style/AppTheme.Nav"
app:headerLayout="@layout/nav_header_drawer"
app:menu="@menu/messages_menu" />

Expand Down
32 changes: 32 additions & 0 deletions app/src/main/res/layout/settings_activity.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

<include
layout="@layout/app_bar_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.drawerlayout.widget.DrawerLayout>
11 changes: 8 additions & 3 deletions app/src/main/res/menu/messages_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
</group>
<group android:checkableBehavior="single">
<item
android:icon="@drawable/ic_power_setting"
android:icon="@drawable/ic_settings"
android:orderInCategory="2"
android:id="@+id/logout"
android:title="@string/logout" />
android:id="@+id/settings"
android:title="@string/title_activity_settings" />
<item
android:orderInCategory="2"
android:icon="@drawable/ic_bug_report"
android:id="@+id/nav_logs"
android:title="@string/logs" />
<item
android:icon="@drawable/ic_power_setting"
android:orderInCategory="2"
android:id="@+id/logout"
android:title="@string/logout" />
</group>

</menu>
10 changes: 10 additions & 0 deletions app/src/main/res/values-notnight/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorNavPrimary">#000000</color>
<color name="colorPrimaryDark">#3867d6</color>
<color name="colorAccent">#1c49b4</color>
<color name="icons">#434343</color>
<color name="swipeBackground">#E74C3C</color>
<color name="swipeIcon">#FFFFFF</color>
</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<resources>
<string-array name="mode">
<item>@string/theme_light</item>
<item>@string/theme_dark</item>
<item>@string/theme_default</item>
</string-array>
<string name="theme_light">Light</string>
<string name="theme_dark">Dark</string>
<string name="theme_default">System Default</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorNavPrimary">#FFFFFF</color>
<color name="colorPrimaryDark">#3867d6</color>
<color name="colorAccent">#1c49b4</color>
<color name="icons">#434343</color>
<color name="icons">#797979</color>
<color name="swipeBackground">#E74C3C</color>
<color name="swipeIcon">#FFFFFF</color>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@
<string name="websocket_no_network">Waiting for network</string>
<string name="connection">%s@%s</string>
<string name="no_messages_yet">There are no messages, yet.\nSend a message to Gotify\nand it will appear here.</string>
<string name="title_activity_settings">Settings</string>
<string name="settings_appearance">Appearance</string>
<string name="setting_theme">Theme</string>
<string name="setting_key_theme">theme</string>
</resources>
13 changes: 10 additions & 3 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Nav">
<item name="colorPrimary">@color/colorNavPrimary</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="AppTheme" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="AppTheme.Dialog" parent="AppTheme" >
<item name="colorPrimary">@color/colorNavPrimary</item>
</style>

</resources>
Loading

0 comments on commit ee36b6a

Please sign in to comment.