Commit 463057f8 authored by Robert Hitt's avatar Robert Hitt

Merge branch '22themes/master/lundquism' into 'master'

22themes/master/lundquism

Closes #22

See merge request !12
parents 594aea9f a380373c
Pipeline #3089 passed with stages
in 3 minutes and 14 seconds
package srct.whatsopen.views.activities;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextThemeWrapper;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import srct.whatsopen.R;
public abstract class BaseThemeActivity extends AppCompatActivity {
private static SharedPreferences prefs;
/**
* This is overriden so the theme can be changed, but it only works statically because
* <a href="https://developer.android.com/reference/android/view/ContextThemeWrapper#setTheme(int)">setTheme(int)</a>
* only applies after the contentView is set.
* @param bundle This is just your classic bundle
*/
@Override
protected void onCreate(Bundle bundle) {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean dark = prefs.getBoolean("dark_theme", false);
// look at the udacity course. In this section you might want to reload,
// but this would mean every activity would have to set the right theme
Log.e("BaseThemeActivity", ".onCreate Recreate called?");
if(dark){
setTheme(R.style.darkTheme);
Log.d("BaseThemeActivity", ".onCreate() Theme is now dark");
}else{
setTheme(R.style.AppTheme);
Log.e("BaseThemeActivity", ".onCreate() Theme is now light");
}
super.onCreate(bundle);
}
/**
* This is overriden so that the theme can be changed dynamically. onStart is called, but
* the themes are wrong, onStart will call recreate, which will destroy the app and re-create it.
* This is a dangerous practice as it could destroy volatile data,
* but we don't have any volatile data so who cares. It will set the theme correctly.
*/
@Override
protected void onStart(){
boolean dark = prefs.getBoolean("dark_theme", false);
int actTheme = getThemeID();
if(actTheme == R.style.darkTheme && !dark){
Log.d("BaseThemeActivity", ".onStart calling recreate to set to light");
recreate();
}else if (actTheme == R.style.AppTheme && dark){
Log.d("BaseThemeActivity", ".onStart calling recreate to set to dark");
recreate();
}else{
super.onStart();
}
}
/**
gets the current theme's resid:
<a href="https://stackoverflow.com/questions/7267852/android-how-to-obtain-the-resource-id-of-the-current-theme">
getThemeResId()</a>
note, the top answer on this page returns the default theme's ID, not the actual theme.
The stackoverflow warned about using getThemeResId because it was designed to be private.
@return returns the int resid of the current theme
*/
private int getThemeID(){
int themeResId = 0;
String TAG = "BaseThemeActivity";
try {
Class<?> clazz = ContextThemeWrapper.class;
Method method = clazz.getMethod("getThemeResId");
method.setAccessible(true);
themeResId = (Integer) method.invoke(this);
} catch (NoSuchMethodException e) {
Log.e(TAG, ".getThemeID Failed to get theme resource ID", e);
} catch (IllegalAccessException e) {
Log.e(TAG, ".getThemeID Failed to get theme resource ID", e);
} catch (IllegalArgumentException e) {
Log.e(TAG, ".getThemeID Failed to get theme resource ID", e);
} catch (InvocationTargetException e) {
Log.e(TAG, ".getThemeID Failed to get theme resource ID", e);
}
return themeResId;
}
}
......@@ -42,7 +42,7 @@ import srct.whatsopen.presenters.FacilityPresenter;
import srct.whatsopen.views.fragments.NotificationDialogFragment;
public class DetailActivity extends AppCompatActivity implements FacilityView,
public class DetailActivity extends BaseThemeActivity implements FacilityView,
NotificationDialogFragment.NotificationDialogListener {
@BindView(R.id.open_status)
......
......@@ -27,7 +27,7 @@ import srct.whatsopen.views.adapters.FacilityListAdapter;
import srct.whatsopen.views.adapters.FacilityListFragmentPagerAdapter;
import srct.whatsopen.views.fragments.FacilityListFragment;
public class MainActivity extends AppCompatActivity implements MainView {
public class MainActivity extends BaseThemeActivity implements MainView {
@BindView(R.id.progress_bar)
ProgressBar mProgressBar;
......
package srct.whatsopen.views.activities;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
......@@ -7,6 +8,7 @@ import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
......@@ -15,7 +17,7 @@ import srct.whatsopen.MyApplication;
import srct.whatsopen.R;
import srct.whatsopen.views.fragments.SettingsFragment;
public class SettingsActivity extends AppCompatActivity {
public class SettingsActivity extends BaseThemeActivity {
private SharedPreferences mSharedPreferences;
......@@ -72,6 +74,9 @@ public class SettingsActivity extends AppCompatActivity {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if(key.equals("turn_off_rotation_preference")) {
MyApplication.setRotation(SettingsActivity.this);
}else if (key.equals("dark_theme")){
Log.d("SettingsActivity", ".onSharedPreferenceChanged: Button was clicked to reset Themes.");
recreate();
}
}
};
......
......@@ -14,4 +14,18 @@
</style>
<!-- Dark theme when the dark button from the preferences settings screen is clicked,
the theme switches to this theme.-->
<style name="darkTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorControlHighlight">@color/tabButtonPressed</item>
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
<item name="android:windowContentTransitions">true</item>
</style>
</resources>
\ No newline at end of file
......@@ -17,6 +17,7 @@
<string name="title_turn_off_rotation">Turn off rotation</string>
<string name="title_notification">Notifications</string>
<string name="title_turn_off_vibrations">Turn off vibrations</string>
<string name="title_dark_theme">Dark theme</string>
<string-array name="entries_list_view_information_preference">
<item>Both</item>
<item>Only for open Facilities</item>
......
......@@ -11,4 +11,14 @@
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
<!-- Dark theme when the dark button from the preferences settings screen is clicked,
the theme switches to this theme.-->
<style name="darkTheme" parent="Theme.AppCompat.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:homeAsUpIndicator">@drawable/ic_arrow_back_white</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
</resources>
......@@ -17,6 +17,12 @@
android:entries="@array/entries_list_view_default_tab_preference"
android:entryValues="@array/entryvalues_list_view_default_tab_preference" />
<SwitchPreference
android:defaultValue="false"
android:key="dark_theme"
android:title="@string/title_dark_theme"
android:entries="@array/entries_list_view_default_tab_preference"
android:entryValues="@array/entryvalues_list_view_default_tab_preference" />
</PreferenceCategory>
<PreferenceCategory
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment