Add dark mode

This commit is contained in:
Jannis Mattheis
2020-05-08 18:13:18 +02:00
parent 91be6fa72e
commit ee36b6aa8a
19 changed files with 205 additions and 27 deletions

View File

@@ -8,6 +8,7 @@ import android.os.Bundle;
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;
@@ -21,6 +22,7 @@ import com.github.gotify.log.UncaughtExceptionHandler;
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;
@@ -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) {

View File

@@ -14,6 +14,7 @@ import android.widget.ProgressBar;
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;
@@ -135,7 +136,7 @@ public class LoginActivity extends AppCompatActivity {
}
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)
@@ -273,7 +274,7 @@ public class LoginActivity extends AppCompatActivity {
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)

View File

@@ -22,6 +22,7 @@ import androidx.annotation.NonNull;
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;
@@ -61,6 +62,7 @@ import com.github.gotify.messages.provider.MessageState;
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;
@@ -309,7 +311,7 @@ public class MessagesActivity extends AppCompatActivity
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)
@@ -317,6 +319,8 @@ public class MessagesActivity extends AppCompatActivity
.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);
@@ -326,6 +330,7 @@ public class MessagesActivity extends AppCompatActivity
public void doLogout(DialogInterface dialog, int which) {
setContentView(R.layout.splash);
new DeleteClientAndNavigateToLogin().execute();
finish();
}
private void startLoading() {

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}