Merge branch 'settings_page_fixes' of git://github.com/andrewlord1990/habitrpg-android into andrewlord1990-settings_page_fixes

# Conflicts:
#	Habitica/res/values/colors.xml
This commit is contained in:
Phillip Thelen 2016-03-05 14:57:23 +01:00
commit 6c44e997e2
17 changed files with 319 additions and 187 deletions

View file

@ -37,7 +37,6 @@
</activity>
<activity
android:name=".ui.activities.PrefsActivity"
android:theme="@style/PreferencesTheme"
android:parentActivityName=".ui.activities.MainActivity"
android:label="@string/PS_settings_title"
android:screenOrientation="portrait"

View file

@ -59,6 +59,7 @@ dependencies {
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:gridlayout-v7:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:preference-v14:23.1.1'
// Image Loading/Caching
compile 'com.squareup.picasso:picasso:2.5.2'
@ -95,7 +96,7 @@ dependencies {
compile 'com.facebook.android:facebook-android-sdk:4.7.0'
//Material Dialogs
compile('com.github.afollestad.material-dialogs:core:0.8.5.0@aar')
compile('com.github.afollestad.material-dialogs:core:0.8.5.6@aar')
compile 'fr.avianey.com.viewpagerindicator:library:2.4.1@aar'

View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.PrefsActivity">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="@style/Toolbar"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>

View file

@ -92,9 +92,11 @@
<color name="checkbox_checked_fill">#c8ffffff</color>
<color name="checkbox_stroke">#32000000</color>
<!-- Preferences -->
<color name="preference_fallback_accent_color">@color/colorAccent</color>
<!-- buttons -->
<color name="btn_success">#ff5cb85c</color>
<color name="btn_warning">#fff0ad4e</color>
<color name="btn_danger">#ffd9534f</color>
</resources>
</resources>

View file

@ -36,6 +36,7 @@
<item name="popupMenuStyle">@style/PopupTheme</item>
<item name="actionOverflowMenuStyle">@style/PopupTheme</item>
<item name="android:popupMenuStyle">@style/PopupTheme</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item>
</style>
<style name="AppThemeWithActionBar" parent="AppTheme">
@ -44,12 +45,6 @@
</style>
<style name="PreferencesTheme" parent="AppThemeWithActionBar">
<item name="android:windowContentOverlay">@null</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="AppThemeWithActionBarBlackText" parent="AppThemeWithActionBar">
<item name="android:textColorPrimary">@android:color/black</item>
</style>

View file

@ -1,40 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/PS_contact_title" >
<PreferenceCategory android:title="Account Details">
<Preference
android:key="@string/SP_username"
android:title="Login Name"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="Login Name" />
<Preference
android:key="@string/SP_email"
android:title="E-mail"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="E-mail" />
<Preference
android:key="@string/SP_userID"
android:title="@string/SP_userID_title"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="@string/SP_userID_summary"/>
<Preference
android:key="@string/SP_APIToken"
android:title="@string/SP_APIToken_title"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="@string/SP_APIToken_summary"/>
</PreferenceCategory>
</PreferenceScreen>

View file

@ -1,28 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/PS_contact_title" >
<PreferenceCategory
android:title="@string/pref_account_header">
<EditTextPreference
android:key="accountDetails"
android:title="@string/account_details"
android:summary="Check your account details" />
<PreferenceScreen
android:key="accountDetails"
android:title="@string/account_details"
android:summary="Check your account details">
<PreferenceCategory android:title="Account Details">
<Preference
android:key="@string/SP_username"
android:title="Login Name"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="Login Name" />
<Preference
android:key="@string/SP_email"
android:title="E-mail"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="E-mail" />
<Preference
android:key="@string/SP_userID"
android:title="@string/SP_userID_title"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="@string/SP_userID_summary"/>
<Preference
android:key="@string/SP_APIToken"
android:title="@string/SP_APIToken_title"
android:selectable="true"
android:persistent="false"
android:shouldDisableView="false"
android:summary="@string/SP_APIToken_summary"/>
</PreferenceCategory>
</PreferenceScreen>
<Preference android:title="@string/logout"
android:key="logout"
android:summary="Log out of your account"/>
<Preference android:title="@string/logout"
android:key="logout"
android:summary="Log out of your account"/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_reminder_header">
<CheckBoxPreference
android:key="use_reminder"
android:defaultValue="false"
android:title="@string/pref_reminder_checkbox"/>
<com.habitrpg.android.habitica.prefs.TimePreference
android:key="reminder_time"
android:defaultValue="19:30"
android:title="@string/pref_reminder_picker" />
</PreferenceCategory>
</PreferenceScreen>

View file

@ -2,19 +2,12 @@ package com.habitrpg.android.habitica.prefs;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.preference.DialogPreference;
import android.support.v7.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TimePicker;
//
public class TimePreference extends DialogPreference {
private int lastHour = 0;
private int lastMinute = 0;
private String timeval;
private CharSequence mSummary;
private TimePicker picker = null;
public static int getHour(String timeval) {
String[] pieces = timeval.split(":");
return (Integer.parseInt(pieces[0]));
@ -33,48 +26,6 @@ public class TimePreference extends DialogPreference {
setNegativeButtonText("Cancel");
}
@Override
protected View onCreateDialogView() {
picker = new TimePicker(getContext());
return (picker);
}
@Override
protected void onBindDialogView(View v) {
super.onBindDialogView(v);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
picker.setHour(lastHour);
picker.setMinute(lastMinute);
} else {
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
lastHour = picker.getHour();
lastMinute = picker.getMinute();
} else {
lastHour = picker.getCurrentHour();
lastMinute = picker.getCurrentMinute();
}
String timeval = String.valueOf(lastHour) + ":" + String.format("%02d", lastMinute);
this.setSummary(timeval);
if (callChangeListener(timeval)) {
persistString(timeval);
}
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
return (a.getString(index));
@ -94,8 +45,6 @@ public class TimePreference extends DialogPreference {
timeval = defaultValue.toString();
}
setSummary(timeval);
lastHour = getHour(timeval);
lastMinute = getMinute(timeval);
}
public void setText(String text) {
@ -111,6 +60,14 @@ public class TimePreference extends DialogPreference {
}
}
public int getLastHour() {
return getHour(timeval);
}
public int getLastMinute() {
return getMinute(timeval);
}
public String getText() {
return timeval;
}

View file

@ -92,9 +92,9 @@ public class MainDrawerBuilder {
new DividerDrawerItem(),
//new SecondaryDrawerItem().withName(activity.getString(R.string.sidebar_news)),
new PrimaryDrawerItem().withName(activity.getString(R.string.sidebar_settings)).withIdentifier(SIDEBAR_SETTINGS),
new PrimaryDrawerItem().withName(activity.getString(R.string.sidebar_settings)).withIdentifier(SIDEBAR_SETTINGS).withSelectable(false),
new PrimaryDrawerItem().withName(activity.getString(R.string.sidebar_help)).withIdentifier(SIDEBAR_HELP),
new PrimaryDrawerItem().withName(activity.getString(R.string.sidebar_about)).withIdentifier(SIDEBAR_ABOUT)
new PrimaryDrawerItem().withName(activity.getString(R.string.sidebar_about)).withIdentifier(SIDEBAR_ABOUT).withSelectable(false)
)
.withStickyFooterDivider(false)

View file

@ -1,7 +1,9 @@
package com.habitrpg.android.habitica.ui.activities;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import butterknife.ButterKnife;
@ -15,4 +17,19 @@ public abstract class BaseActivity extends AppCompatActivity {
setContentView(getLayoutResId());
ButterKnife.bind(this);
}
protected void setupToolbar(Toolbar toolbar) {
if (toolbar != null) {
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setHomeButtonEnabled(false);
}
}
}
}

View file

@ -106,7 +106,6 @@ public class LoginActivity extends AppCompatActivity
setContentView(R.layout.login_screen);
//Set default values to avoid null-responses when requesting unedited settings
PreferenceManager.setDefaultValues(this, R.xml.preferences_account_details, false);
PreferenceManager.setDefaultValues(this, R.xml.preferences_fragment, false);
ButterKnife.bind(this);

View file

@ -149,18 +149,7 @@ public class MainActivity extends BaseActivity implements HabitRPGUserCallback.O
new Select().from(HabitRPGUser.class).where(Condition.column("id").eq(hostConfig.getUser())).async().querySingle(userTransactionListener);
if (toolbar != null) {
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setHomeButtonEnabled(false);
}
}
setupToolbar(toolbar);
avatarInHeader = new AvatarWithBarsViewModel(this, avatar_with_bars);
accountHeader = MainDrawerBuilder.CreateDefaultAccountHeader(this).build();

View file

@ -4,34 +4,71 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceScreen;
import android.support.v7.widget.Toolbar;
import com.habitrpg.android.habitica.HostConfig;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.ui.fragments.AccountDetailsFragment;
import com.habitrpg.android.habitica.ui.fragments.PreferencesFragment;
public class PrefsActivity extends AppCompatActivity {
import butterknife.Bind;
public class PrefsActivity extends BaseActivity implements
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
@Bind(R.id.toolbar)
Toolbar toolbar;
@Override
protected int getLayoutResId() {
return R.layout.activity_prefs;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PreferencesFragment())
setupToolbar(toolbar);
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, new PreferencesFragment())
.commit();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayUseLogoEnabled(false);
@Override
public boolean onSupportNavigateUp() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
onBackPressed();
return true;
}
return super.onSupportNavigateUp();
}
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragment,
PreferenceScreen preferenceScreen) {
PreferenceFragmentCompat fragment = createNextPage(preferenceScreen);
if (fragment != null) {
Bundle arguments = new Bundle();
arguments.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment)
.addToBackStack(null)
.commit();
return true;
}
return false;
}
private PreferenceFragmentCompat createNextPage(PreferenceScreen preferenceScreen) {
PreferenceFragmentCompat fragment = null;
if (preferenceScreen.getKey().equals("accountDetails")) {
fragment = new AccountDetailsFragment();
}
return fragment;
}
// TODO:

View file

@ -3,55 +3,40 @@ package com.habitrpg.android.habitica.ui.fragments;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.support.v7.preference.Preference;
import android.widget.Toast;
import com.habitrpg.android.habitica.R;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Created by franzejr on 28/11/15.
*/
public class AccountDetailsFragment extends PreferenceFragment {
private Context context;
public class AccountDetailsFragment extends BasePreferencesFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this.getActivity();
addPreferencesFromResource(R.xml.preferences_account_details);
setupPreferences();
}
private void setupPreferences() {
String[] accountDetailsPreferences = {
context.getResources().getString(R.string.SP_username),
context.getResources().getString(R.string.SP_email),
context.getResources().getString(R.string.SP_APIToken),
context.getResources().getString(R.string.SP_userID)
};
for(Map.Entry<String, ?> preference : getPreferenceScreen().getSharedPreferences().getAll().entrySet() ){
protected void setupPreferences() {
for (Map.Entry<String, ?> preference : getPreferenceScreen().getSharedPreferences().getAll().entrySet()) {
String key = preference.getKey();
if (Arrays.asList(accountDetailsPreferences).contains(key)) {
findPreference(key).setSummary(preference.getValue().toString());
if (getAccountDetailsPreferences().contains(key)) {
findPreference(key).setSummary(preference.getValue().toString());
}
}
}
protected List<String> getAccountDetailsPreferences() {
return Arrays.asList(getString(R.string.SP_username), getString(R.string.SP_email),
getString(R.string.SP_APIToken), getString(R.string.SP_userID));
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
public boolean onPreferenceTreeClick(Preference preference) {
ClipboardManager clipMan = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
clipMan.setPrimaryClip(ClipData.newPlainText(preference.getKey(), preference.getSummary()));
Toast.makeText(getActivity(), "Copied " + preference.getKey() + " to clipboard.", Toast.LENGTH_SHORT).show();
return super.onPreferenceTreeClick(preferenceScreen, preference);
return super.onPreferenceTreeClick(preference);
}
}

View file

@ -0,0 +1,18 @@
package com.habitrpg.android.habitica.ui.fragments;
import android.os.Bundle;
import android.support.v7.preference.PreferenceFragmentCompat;
import com.habitrpg.android.habitica.R;
public abstract class BasePreferencesFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
setPreferencesFromResource(R.xml.preferences_fragment, rootKey);
setupPreferences();
}
protected abstract void setupPreferences();
}

View file

@ -6,13 +6,10 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.support.v7.preference.Preference;
import com.habitrpg.android.habitica.HabiticaApplication;
import com.habitrpg.android.habitica.NotificationPublisher;
import com.habitrpg.android.habitica.R;
import com.habitrpg.android.habitica.prefs.TimePreference;
import java.util.Calendar;
@ -20,18 +17,24 @@ import java.util.Calendar;
/**
* Created by franzejr on 28/11/15.
*/
public class PreferencesFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public class PreferencesFragment extends BasePreferencesFragment implements
SharedPreferences.OnSharedPreferenceChangeListener {
private Context context;
private TimePreference timePreference;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this.getActivity();
addPreferencesFromResource(R.xml.preferences_fragment);
context = getActivity();
}
@Override
protected void setupPreferences() {
timePreference = (TimePreference) findPreference("reminder_time");
boolean use_reminder = getPreferenceManager().getSharedPreferences().getBoolean("use_reminder", false);
timePreference.setEnabled(use_reminder);
boolean useReminder = getPreferenceManager().getSharedPreferences().getBoolean("use_reminder", false);
timePreference.setEnabled(useReminder);
}
@Override
@ -48,24 +51,14 @@ public class PreferencesFragment extends PreferenceFragment implements SharedPre
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
public boolean onPreferenceTreeClick(Preference preference) {
if (preference.getKey().equals("logout")) {
HabiticaApplication.logout(context);
getActivity().finish();
}else if(preference.getKey().equals("accountDetails")) {
openAccountDetailsFragment();
}
return false;
return super.onPreferenceTreeClick(preference);
}
private void openAccountDetailsFragment() {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new AccountDetailsFragment()).addToBackStack(null)
.commit();
}
private void scheduleNotifications() {
String timeval = getPreferenceManager().getSharedPreferences().getString("reminder_time", "19:00");
@ -114,4 +107,17 @@ public class PreferencesFragment extends PreferenceFragment implements SharedPre
scheduleNotifications();
}
}
@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (preference instanceof TimePreference) {
if (getFragmentManager().findFragmentByTag(TimePreferenceDialogFragment.TAG) == null) {
TimePreferenceDialogFragment.newInstance(this, preference.getKey())
.show(getFragmentManager(), TimePreferenceDialogFragment.TAG);
}
} else {
super.onDisplayPreferenceDialog(preference);
}
}
}

View file

@ -0,0 +1,87 @@
package com.habitrpg.android.habitica.ui.fragments;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.preference.PreferenceDialogFragmentCompat;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;
import android.widget.TimePicker;
import com.habitrpg.android.habitica.prefs.TimePreference;
import java.util.Locale;
public class TimePreferenceDialogFragment extends PreferenceDialogFragmentCompat {
public static final String TAG = TimePreferenceDialogFragment.class.getSimpleName();
private TimePicker picker = null;
public TimePreferenceDialogFragment() {
}
public static TimePreferenceDialogFragment newInstance(
PreferenceFragmentCompat preferenceFragment, String key) {
TimePreferenceDialogFragment fragment = new TimePreferenceDialogFragment();
Bundle arguments = new Bundle(1);
arguments.putString(ARG_KEY, key);
fragment.setArguments(arguments);
fragment.setTargetFragment(preferenceFragment, 0);
return fragment;
}
private TimePreference getTimePreference() {
return (TimePreference) getPreference();
}
@Override
protected View onCreateDialogView(Context context) {
picker = new TimePicker(getContext());
return picker;
}
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
TimePreference preference = getTimePreference();
int lastHour = preference.getLastHour();
int lastMinute = preference.getLastMinute();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
picker.setHour(lastHour);
picker.setMinute(lastMinute);
} else {
picker.setCurrentHour(lastHour);
picker.setCurrentMinute(lastMinute);
}
}
@Override
public void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
TimePreference preference = getTimePreference();
String time = getNewTimeValue();
preference.setSummary(time);
if (preference.callChangeListener(time)) {
preference.setText(time);
}
}
}
private String getNewTimeValue() {
int lastHour;
int lastMinute;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
lastHour = picker.getHour();
lastMinute = picker.getMinute();
} else {
lastHour = picker.getCurrentHour();
lastMinute = picker.getCurrentMinute();
}
return String.valueOf(lastHour) + ":" + String.format(Locale.UK, "%02d", lastMinute);
}
}