Skip to content

Add dark theme #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ android {

defaultConfig {
applicationId "com.woefe.shoppinglist"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 28
versionCode 12
versionName "0.11.0"
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".ShoppingListApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand All @@ -39,8 +40,9 @@
</intent-filter>
</activity>
<activity android:name=".dialog.DirectoryChooser" android:theme="@style/AppDialogTheme" />
<activity android:name=".activity.SettingsActivity" />
<activity android:name=".activity.AboutActivity" />
<activity android:name=".activity.SettingsActivity" android:parentActivityName=".activity.MainActivity"/>
<activity android:name=".activity.AboutActivity" android:parentActivityName=".activity.MainActivity" />

</application>

</manifest>
58 changes: 58 additions & 0 deletions app/src/main/java/com/woefe/shoppinglist/SettingsRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* ShoppingList - A simple shopping list for Android
*
* Copyright (C) 2019. Wolfgang Popp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.woefe.shoppinglist;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatDelegate;

import com.woefe.shoppinglist.activity.SettingsFragment;

public class SettingsRepository {

private static final String MODE_NIGHT_FOLLOW_SYSTEM = "MODE_NIGHT_FOLLOW_SYSTEM";
private static final String MODE_NIGHT_NO = "MODE_NIGHT_NO";
private static final String MODE_NIGHT_YES = "MODE_NIGHT_YES";

private final Context context;
private final SharedPreferences prefs;

public SettingsRepository(Context context) {
this.context = context.getApplicationContext();
prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
}

public int getTheme() {
String theme = prefs.getString(SettingsFragment.KEY_THEME, MODE_NIGHT_FOLLOW_SYSTEM);
return themeStringToInt(theme);
}

private int themeStringToInt(String theme) {
switch (theme) {
case MODE_NIGHT_NO:
return AppCompatDelegate.MODE_NIGHT_NO;
case MODE_NIGHT_YES:
return AppCompatDelegate.MODE_NIGHT_YES;
default:
return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* ShoppingList - A simple shopping list for Android
*
* Copyright (C) 2019. Wolfgang Popp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.woefe.shoppinglist;

import android.app.Application;
import android.support.v7.app.AppCompatDelegate;

public class ShoppingListApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
SettingsRepository settingsRepository = new SettingsRepository(this);
AppCompatDelegate.setDefaultNightMode(settingsRepository.getTheme());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
Expand All @@ -35,30 +37,39 @@

public class AboutActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
TextView textView = findViewById(R.id.about_text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(getString(R.string.about_title)));
textView.append(Html.fromHtml(getString(R.string.about_version, BuildConfig.VERSION_NAME)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_github)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_license)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_author)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_contributors)));
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_about);

final Toolbar toolbar = findViewById(R.id.toolbar_about);
toolbar.setTitle(R.string.about);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}

TextView textView = findViewById(R.id.about_text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(getString(R.string.about_version, BuildConfig.VERSION_NAME)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_github)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_license)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_author)));
textView.append("\n");
textView.append("\n");
textView.append("\n");
textView.append(Html.fromHtml(getString(R.string.about_contributors)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.util.Log;
Expand Down Expand Up @@ -69,6 +70,7 @@ public class MainActivity extends BinderActivity implements
private Fragment currentFragment;
private String currentListName;
private ShareActionProvider actionProvider;
private int lastTheme;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -86,6 +88,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
selectList(position);
}
});
lastTheme = AppCompatDelegate.getDefaultNightMode();

final Toolbar toolbar = findViewById(R.id.toolbar_main);
toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
Expand Down Expand Up @@ -114,6 +117,14 @@ public void onPostCreate(@Nullable Bundle savedInstanceState, @Nullable Persista
drawerToggle.syncState();
}

@Override
protected void onRestart() {
super.onRestart();
if (AppCompatDelegate.getDefaultNightMode() != lastTheme) {
recreate();
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
if (currentFragment != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,34 @@
package com.woefe.shoppinglist.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import com.woefe.shoppinglist.R;

/**
* @author Wolfgang Popp.
*/
public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.toolbar_settings);

final Toolbar toolbar = findViewById(R.id.toolbar_settings);
toolbar.setTitle(R.string.action_settings);
setSupportActionBar(toolbar);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}

getSupportFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,22 @@
package com.woefe.shoppinglist.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
import android.view.View;

import com.woefe.shoppinglist.R;
import com.woefe.shoppinglist.SettingsRepository;
import com.woefe.shoppinglist.dialog.DirectoryChooser;

/**
Expand All @@ -41,8 +45,18 @@ public class SettingsFragment extends PreferenceFragmentCompat implements
SharedPreferences.OnSharedPreferenceChangeListener {

public static final String KEY_DIRECTORY_LOCATION = "FILE_LOCATION";
public static final String KEY_THEME = "THEME";
private static final int REQUEST_CODE_CHOOSE_DIR = 123;

private SettingsRepository settingsRepository;

@Override
public void onAttach(Context context) {
super.onAttach(context);
settingsRepository = new SettingsRepository(context);
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -53,6 +67,8 @@ public void onCreate(Bundle savedInstanceState) {
for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
initSummary(getPreferenceScreen().getPreference(i));
}
View content = getActivity().findViewById(android.R.id.content);
content.setBackgroundColor(getResources().getColor(R.color.colorBackground));
}

@Override
Expand Down Expand Up @@ -115,6 +131,13 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
if (key.equals(KEY_DIRECTORY_LOCATION)) {
Preference p = findPreference(key);
updatePreferences(p);
} else if (key.equals(KEY_THEME)) {
int theme = settingsRepository.getTheme();
AppCompatDelegate.setDefaultNightMode(theme);
Activity activity = getActivity();
if (activity != null) {
activity.recreate();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
message = savedInstanceState.getString(KEY_MESSAGE);
}

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppDialogTheme);
builder.setMessage(Html.fromHtml(message))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_directory_chooser);

directoryViewAdapter = new ArrayAdapter<>(this, R.layout.drawer_list_item);
directoryViewAdapter = new ArrayAdapter<>(this, R.layout.directory_list_item);
ListView directoryView = findViewById(R.id.directoryListView);
directoryView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
Expand Down
Binary file removed app/src/main/res/drawable-hdpi/ic_done_white_24dp.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed app/src/main/res/drawable-mdpi/ic_done_white_24dp.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions app/src/main/res/drawable-night/ic_color_lens_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M12,3c-4.97,0 -9,4.03 -9,9s4.03,9 9,9c0.83,0 1.5,-0.67 1.5,-1.5 0,-0.39 -0.15,-0.74 -0.39,-1.01 -0.23,-0.26 -0.38,-0.61 -0.38,-0.99 0,-0.83 0.67,-1.5 1.5,-1.5L16,16c2.76,0 5,-2.24 5,-5 0,-4.42 -4.03,-8 -9,-8zM6.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S5.67,9 6.5,9 8,9.67 8,10.5 7.33,12 6.5,12zM9.5,8C8.67,8 8,7.33 8,6.5S8.67,5 9.5,5s1.5,0.67 1.5,1.5S10.33,8 9.5,8zM14.5,8c-0.83,0 -1.5,-0.67 -1.5,-1.5S13.67,5 14.5,5s1.5,0.67 1.5,1.5S15.33,8 14.5,8zM17.5,12c-0.83,0 -1.5,-0.67 -1.5,-1.5S16.67,9 17.5,9s1.5,0.67 1.5,1.5 -0.67,1.5 -1.5,1.5z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-night/ic_sd_storage_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M18,2h-8L4.02,8 4,20c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,4c0,-1.1 -0.9,-2 -2,-2zM12,8h-2L10,4h2v4zM15,8h-2L13,4h2v4zM18,8h-2L16,4h2v4z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable-night/ic_swap_vert_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M16,17.01V10h-2v7.01h-3L15,21l4,-3.99h-3zM9,3L5,6.99h3V14h2V6.99h3L9,3z"/>
</vector>
Binary file removed app/src/main/res/drawable-xhdpi/ic_done_white_24dp.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed app/src/main/res/drawable-xxhdpi/ic_done_white_24dp.png
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_add_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
</vector>
Loading