Added dark theme support

This commit is contained in:
luca0N! 2021-03-28 21:03:03 -03:00
parent a6ae301c39
commit 0a0b8011c4
Signed by: luca0N
GPG Key ID: 2E7B4655CF16D7D6
12 changed files with 338 additions and 172 deletions

View File

@ -11,7 +11,7 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme.Dark">
android:theme="@style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@ -24,6 +24,7 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.activities;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
@ -35,9 +36,11 @@ import com.mikepenz.aboutlibraries.LibsBuilder;
import androidx.appcompat.app.AppCompatActivity;
import com.luca0n.joguitos.pluck.R;
import com.luca0n.joguitos.pluck.settings.SettingsActivity;
import com.luca0n.joguitos.pluck.util.UiUtil;
@SuppressLint("Registered")
public class BaseActivity extends AppCompatActivity {
public static final int REQUEST_ACTIVITY_CLOSED = 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
@ -62,9 +65,24 @@ public class BaseActivity extends AppCompatActivity {
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent extras){
switch (requestCode){
case REQUEST_ACTIVITY_CLOSED:
if (resultCode == Activity.RESULT_OK){
// Check if the theme changed.
recreate();
}
break;
default:
super.onActivityResult(requestCode, resultCode, extras);
break;
}
}
private void onSettings() {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
startActivityForResult(intent, REQUEST_ACTIVITY_CLOSED);
}
private void onAbout() {
@ -72,7 +90,7 @@ public class BaseActivity extends AppCompatActivity {
String appDescription = getResources().getString(R.string.app_description);
new LibsBuilder()
.withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR)
.withActivityStyle(UiUtil.getTheme(this).equals(getString(R.string.pref_ui_theme_entryValues_dark)) ? Libs.ActivityStyle.DARK : Libs.ActivityStyle.LIGHT)
.withAboutIconShown(true)
.withAboutAppName(appName)
.withAboutVersionShownName(true)

View File

@ -43,6 +43,7 @@ import com.luca0n.joguitos.pluck.trivia.TriviaCategory;
import com.luca0n.joguitos.pluck.trivia.TriviaDifficulty;
import com.luca0n.joguitos.pluck.trivia.TriviaQuery;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class MainActivity extends BaseActivity {
@ -61,6 +62,7 @@ public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
@ -129,7 +131,7 @@ public class MainActivity extends BaseActivity {
.category(category)
.difficulty(difficulty)
.build());
startActivity(intent);
startActivityForResult(intent, BaseActivity.REQUEST_ACTIVITY_CLOSED);
});
// Create a String array that holds all options for spinnerNumber.
@ -191,6 +193,10 @@ public class MainActivity extends BaseActivity {
.show();
}
break;
case BaseActivity.REQUEST_ACTIVITY_CLOSED:
// Check if the theme changed.
recreate();
break;
default:
super.onActivityResult(requestCode, resultCode, extras);
break;

View File

@ -23,6 +23,7 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.activities;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
@ -66,6 +67,7 @@ import com.luca0n.joguitos.pluck.trivia.TriviaSource;
import com.luca0n.joguitos.pluck.trivia.TriviaFilters;
import com.luca0n.joguitos.pluck.util.ApiUtil;
import com.luca0n.joguitos.pluck.util.SoundUtil;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class TriviaGameActivity extends BaseActivity
implements IDownloadTriviaQuestionReceiver {
@ -92,10 +94,13 @@ public class TriviaGameActivity extends BaseActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game);
ButterKnife.bind(this);
setResult(Activity.RESULT_OK);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);

View File

@ -31,6 +31,7 @@ import butterknife.BindView;
import butterknife.ButterKnife;
import com.luca0n.joguitos.pluck.R;
import com.luca0n.joguitos.pluck.trivia.TriviaGame;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class TriviaGameResultsActivity extends BaseActivity {
static final String EXTRA_TRIVIA_GAME = "extra_trivia_game";
@ -48,6 +49,7 @@ public class TriviaGameResultsActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game_results);
ButterKnife.bind(this);

View File

@ -23,6 +23,7 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.settings;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
@ -30,19 +31,23 @@ import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
UiUtil.setTheme(this);
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
getFragmentManager()
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
setResult(Activity.RESULT_OK);
}
@Override

View File

@ -24,15 +24,32 @@ Contact us at <joguitos+pluck@luca0n.com>.
package com.luca0n.joguitos.pluck.settings;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import androidx.preference.PreferenceFragmentCompat;
import androidx.annotation.Nullable;
import com.luca0n.joguitos.pluck.R;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
public class SettingsFragment extends PreferenceFragment {
import com.luca0n.joguitos.pluck.R;
import com.luca0n.joguitos.pluck.util.UiUtil;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey){
addPreferencesFromResource(R.xml.preferences);
// Listen for theme preference changes
ListPreference preferenceTheme = (ListPreference) findPreference(getString(R.string.pref_ui_theme));
preferenceTheme.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener(){
@Override
public boolean onPreferenceChange(Preference preference, Object newValue){
getActivity().recreate();
return true;
}
});
}
}

View File

@ -0,0 +1,61 @@
/*
Pluck: an open source trivia game for Android
Copyright (C) 2021 Joguitos do luca0N!
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 <https://www.gnu.org/licenses/>.
This game is a fork of LibreTrivia, and its source code is available at
<https://github.com/tryton-vanmeer/LibreTrivia>.
Contact us at <joguitos+pluck@luca0n.com>.
*/
package com.luca0n.joguitos.pluck.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.luca0n.joguitos.pluck.R;
public class UiUtil {
/**
* Applies the theme selected by the user to the context.
* @param context The activity context.
* @since 2021-03-17
*/
public static void setTheme(Context context){
SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String dark = context.getString(R.string.pref_ui_theme_entryValues_dark),
theme = getTheme(context);
if (theme.equals(dark))
context.setTheme(R.style.AppTheme_Dark);
else
context.setTheme(R.style.AppTheme);
}
/**
* @return The app theme selected by the user.
* @param context The activity context.
* @since 2021-03-17
*/
public static String getTheme(Context context){
SharedPreferences s = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
String themeKey = context.getString(R.string.pref_ui_theme),
themeLight = context.getString(R.string.pref_ui_theme_entryValues_light);
return s.getString(themeKey, themeLight);
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Pluck: an open source trivia game for Android
Copyright (C) 2021 Joguitos do luca0N!
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 <https://www.gnu.org/licenses/>.
This game is a fork of LibreTrivia, and its source code is available at
<https://github.com/tryton-vanmeer/LibreTrivia>.
Contact us at <joguitos+pluck@luca0n.com>.
-->
<resources>
<string-array name="pref_ui_theme_entryValues">
<item>pref_ui_theme_entryValues_light</item>
<item>pref_ui_theme_entryValues_dark</item>
</string-array>
</resources>

View File

@ -28,4 +28,8 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="pref_network_tor">pref_network_tor</string>
<string name="pref_network_server">pref_network_server</string>
<string name="pref_network_server_default">https://opentdb.com/api.php</string>
<string name="pref_ui_theme">pref_ui_theme</string>
<string name="pref_ui_theme_entryValues_light">pref_ui_theme_entryValues_light</string>
<string name="pref_ui_theme_entryValues_dark">pref_ui_theme_entryValues_dark</string>
<string name="pref_ui_theme_default">pref_ui_theme_entryValues_light</string>
</resources>

View File

@ -116,4 +116,12 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="pref_network_tor_summary">Retrieves trivia data via the Tor network. Requires Orbot (recommended) or a Tor daemon with a SOCKS5 proxy listening on port 9050.</string>
<string name="pref_network_server_title">Server Address</string>
<string name="pref_network_server_summary">Specifies the server location used by the game to fetch questions when using a server as the trivia source. Leave this setting empty to use the default server.</string>
<string name="pref_category_ui_title">Interface</string>
<string name="pref_ui_theme_title">Theme</string>
<string name="pref_ui_theme_summary">Defines the game theme.</string>
<string-array name="pref_ui_theme_entries">
<item>Light</item>
<item>Dark</item>
</string-array>
</resources>

View File

@ -48,4 +48,14 @@ Contact us at <joguitos+pluck@luca0n.com>.
android:title="@string/pref_network_tor_title"
android:summary="@string/pref_network_tor_summary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_category_ui_title">
<ListPreference
android:defaultValue="@string/pref_ui_theme_default"
android:key="@string/pref_ui_theme"
android:title="@string/pref_ui_theme_title"
android:summary="@string/pref_ui_theme_summary"
android:entries="@array/pref_ui_theme_entries"
android:entryValues="@array/pref_ui_theme_entryValues" />
</PreferenceCategory>
</PreferenceScreen>