mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-05-18 19:59:00 +00:00
refactor settings
This commit is contained in:
parent
e50976f3c5
commit
00ffd7847e
13 changed files with 473 additions and 359 deletions
|
|
@ -1,25 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<LinearLayout 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">
|
||||
|
||||
<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:layout_gravity="top|center" >
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.activities.MainActivity"
|
||||
android:layout_gravity="center_horizontal|bottom">
|
||||
tools:context=".ui.activities.PrefsActivity"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.design.widget.AppBarLayout
|
||||
android:layout_width="match_parent"
|
||||
|
|
@ -35,7 +21,13 @@
|
|||
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>
|
||||
|
||||
</android.support.design.widget.AppBarLayout>
|
||||
<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:layout_gravity="top|center" >
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
</FrameLayout>
|
||||
|
||||
</FrameLayout>
|
||||
</LinearLayout>
|
||||
|
|
@ -62,7 +62,6 @@
|
|||
|
||||
<string name="logout">Logout</string>
|
||||
<string name="logout_description">Log out of your account</string>
|
||||
<string name="account_details_sum">Check your account details</string>
|
||||
<string name="account_details">Account Details</string>
|
||||
<string name="LoginActivityName">Welcome</string>
|
||||
|
||||
|
|
@ -676,4 +675,14 @@
|
|||
<string name="forgot_password_description">Enter the email address you used to register your Habitica account.</string>
|
||||
<string name="forgot_password_confirmation">If we have your email on file, instructions for setting a new password have been sent to your email.</string>
|
||||
<string name="ok">OK</string>
|
||||
<string name="profile">Profile</string>
|
||||
<string name="profile_summary">Edit your public profile.</string>
|
||||
<string name="display_name">Display Name</string>
|
||||
<string name="photo_url">Photo URL</string>
|
||||
<string name="login_name">Login Name</string>
|
||||
<string name="about">About</string>
|
||||
<string name="app_settings">App Settings</string>
|
||||
<string name="authentication">Authentication</string>
|
||||
<string name="authentication_summary">Change your authentication options.</string>
|
||||
<string name="change_password">Change Password</string>
|
||||
</resources>
|
||||
|
|
|
|||
|
|
@ -1,36 +1,65 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen 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:title="@string/PS_contact_title">
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/pref_account_header"
|
||||
android:key="account_prefs">
|
||||
|
||||
<PreferenceScreen android:title="@string/profile"
|
||||
android:summary="@string/profile_summary"
|
||||
android:key="profile">
|
||||
<EditTextPreference android:key="display_name"
|
||||
android:title="@string/display_name" />
|
||||
<EditTextPreference android:key="photo_url"
|
||||
android:title="@string/photo_url" />
|
||||
<EditTextPreference android:key="about"
|
||||
android:title="@string/about" />
|
||||
</PreferenceScreen>
|
||||
|
||||
<PreferenceScreen
|
||||
android:key="accountDetails"
|
||||
android:title="@string/account_details"
|
||||
android:summary="@string/account_details_sum"
|
||||
android:order="1">
|
||||
|
||||
<PreferenceCategory android:title="Account Details">
|
||||
|
||||
android:key="authentication"
|
||||
android:title="@string/authentication"
|
||||
android:summary="@string/authentication_summary"
|
||||
app:key="authentication">
|
||||
<Preference
|
||||
android:key="@string/SP_username"
|
||||
android:title="Login Name"
|
||||
android:key="login_name"
|
||||
android:title="@string/login_name"
|
||||
android:selectable="true"
|
||||
android:persistent="false"
|
||||
android:shouldDisableView="false"
|
||||
android:summary="Login Name" />
|
||||
android:shouldDisableView="false" />
|
||||
|
||||
<Preference
|
||||
android:key="@string/SP_email"
|
||||
android:title="E-mail"
|
||||
android:key="email"
|
||||
android:title="@string/email"
|
||||
android:selectable="true"
|
||||
android:persistent="false"
|
||||
android:shouldDisableView="false"
|
||||
android:summary="E-mail" />
|
||||
android:shouldDisableView="false"/>
|
||||
|
||||
<Preference
|
||||
android:key="password"
|
||||
android:title="@string/change_password"
|
||||
android:selectable="true"
|
||||
android:persistent="false"
|
||||
android:shouldDisableView="false" />
|
||||
<Preference
|
||||
android:key="subscription_status"
|
||||
android:title="@string/subscription_status"
|
||||
android:selectable="true"
|
||||
android:persistent="false"
|
||||
android:shouldDisableView="false"/>
|
||||
<PreferenceCategory android:title="@string/danger_zone">
|
||||
<Preference android:title="@string/reset_account"
|
||||
android:key="reset_account" />
|
||||
<Preference android:title="@string/delete_account"
|
||||
android:key="delete_account" />
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
||||
|
||||
<PreferenceScreen android:title="API"
|
||||
android:key="api">
|
||||
<Preference
|
||||
android:key="@string/SP_userID"
|
||||
android:title="@string/SP_userID_title"
|
||||
|
|
@ -54,56 +83,40 @@
|
|||
android:persistent="false"
|
||||
android:shouldDisableView="false"
|
||||
android:summary="@string/SP_user_qr_code"/>
|
||||
<Preference
|
||||
android:key="subscription_status"
|
||||
android:title="@string/subscription_status"
|
||||
android:selectable="true"
|
||||
android:persistent="false"
|
||||
android:shouldDisableView="false"/>
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory android:title="@string/danger_zone">
|
||||
<Preference android:title="@string/reset_account"
|
||||
android:key="reset_account" />
|
||||
<Preference android:title="@string/delete_account"
|
||||
android:key="delete_account" />
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
||||
|
||||
<Preference
|
||||
android:key="choose_class"
|
||||
android:order="2"/>
|
||||
tools:title="Change Class"/>
|
||||
<Preference android:title="@string/fix_character_values"
|
||||
app:key="fixCharacterValues"/>
|
||||
<Preference android:title="@string/logout"
|
||||
android:key="logout"
|
||||
android:summary="@string/logout_description"/>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:title="@string/app_settings">
|
||||
<ListPreference android:title="@string/pref_first_day_of_the_week_title"
|
||||
android:key="@string/pref_first_day_of_the_week_key"
|
||||
android:entries="@array/weekdays"
|
||||
android:entryValues="@array/weekdayValues"
|
||||
android:summary="@string/pref_first_day_of_the_week_summary"
|
||||
android:order="5"/>
|
||||
android:summary="@string/pref_first_day_of_the_week_summary"/>
|
||||
<ListPreference android:title="@string/Language_title"
|
||||
android:key="language"
|
||||
android:entries="@array/Language"
|
||||
android:entryValues="@array/LanguageValues"
|
||||
android:summary="@string/Language_summary"
|
||||
android:order="4"/>
|
||||
android:summary="@string/Language_summary"/>
|
||||
<ListPreference android:title="@string/AudioTheme_title"
|
||||
android:key="audioTheme"
|
||||
android:entries="@array/AudioThemes"
|
||||
android:entryValues="@array/AudioValues"
|
||||
android:summary="@string/AudioTheme_summary"
|
||||
android:order="3"/>
|
||||
android:summary="@string/AudioTheme_summary"/>
|
||||
<CheckBoxPreference android:title="@string/dailyDueDefaultView"
|
||||
android:key="dailyDueDefaultView"
|
||||
android:order="6"
|
||||
android:summary="@string/dailyDueDefaultViewDescription"
|
||||
/>
|
||||
<Preference android:title="@string/fix_character_values"
|
||||
app:key="fixCharacterValues"
|
||||
android:order="7"/>
|
||||
<Preference android:title="@string/logout"
|
||||
android:key="logout"
|
||||
android:summary="@string/logout_description"
|
||||
android:order="99"/>
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
|
|
@ -142,8 +155,7 @@
|
|||
<PreferenceScreen
|
||||
android:key="pushNotifications"
|
||||
android:title="@string/push_notifications"
|
||||
android:summary="@string/push_notifications_sum"
|
||||
android:order="1">
|
||||
android:summary="@string/push_notifications_sum">
|
||||
|
||||
<PreferenceCategory android:title="Push Notifications">
|
||||
|
||||
|
|
@ -197,8 +209,7 @@
|
|||
<PreferenceCategory
|
||||
android:title="@string/maintenance">
|
||||
<Preference android:title="@string/reload_content"
|
||||
android:key="reload_content"
|
||||
android:order="99"/>
|
||||
android:key="reload_content"/>
|
||||
|
||||
</PreferenceCategory>
|
||||
</PreferenceScreen>
|
||||
|
|
@ -57,8 +57,10 @@ import com.habitrpg.android.habitica.ui.fragments.inventory.stable.MountDetailRe
|
|||
import com.habitrpg.android.habitica.ui.fragments.inventory.stable.PetDetailRecyclerFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.inventory.stable.StableFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.inventory.stable.StableRecyclerFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.AccountDetailsFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.APIPreferenceFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.AuthenticationPreferenceFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PreferencesFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.ProfilePreferencesFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.setup.AvatarSetupFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.setup.IntroFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.setup.TaskSetupFragment;
|
||||
|
|
@ -262,11 +264,8 @@ public interface AppComponent {
|
|||
|
||||
void inject(HabiticaFirebaseMessagingService habiticaFirebaseMessagingService);
|
||||
|
||||
void inject(AccountDetailsFragment accountDetailsFragment);
|
||||
|
||||
void inject(BaseWidgetProvider baseWidgetProvider);
|
||||
|
||||
|
||||
void inject(NotificationPublisher notificationPublisher);
|
||||
|
||||
void inject(CreateChallengeActivity createChallengeActivity);
|
||||
|
|
@ -282,4 +281,10 @@ public interface AppComponent {
|
|||
void inject(PurchaseDialog purchaseDialog);
|
||||
|
||||
void inject(@NotNull FixCharacterValuesActivity fixCharacterValuesActivity);
|
||||
|
||||
void inject(@NotNull AuthenticationPreferenceFragment authenticationPreferenceFragment);
|
||||
|
||||
void inject(@NotNull ProfilePreferencesFragment profilePreferencesFragment);
|
||||
|
||||
void inject(@NotNull APIPreferenceFragment apiPreferenceFragment);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,99 +0,0 @@
|
|||
package com.habitrpg.android.habitica.ui.activities;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v7.preference.PreferenceFragmentCompat;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
|
||||
import com.habitrpg.android.habitica.BuildConfig;
|
||||
import com.habitrpg.android.habitica.api.HostConfig;
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.components.AppComponent;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.AccountDetailsFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PreferencesFragment;
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PushNotificationsPreferencesFragment;
|
||||
|
||||
import butterknife.BindView;
|
||||
|
||||
public class PrefsActivity extends BaseActivity implements
|
||||
PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
|
||||
|
||||
@BindView(R.id.toolbar)
|
||||
Toolbar toolbar;
|
||||
|
||||
// TODO:
|
||||
// This method should be moved to HabiticaApplication
|
||||
public static HostConfig fromContext(Context ctx) {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
|
||||
HostConfig config;
|
||||
String httpPort = BuildConfig.PORT;
|
||||
String address = BuildConfig.DEBUG ? BuildConfig.BASE_URL : ctx.getString(R.string.base_url);
|
||||
String api = prefs.getString(ctx.getString(R.string.SP_APIToken), null);
|
||||
String userID = prefs.getString(ctx.getString(R.string.SP_userID), null);
|
||||
config = new HostConfig(address, httpPort, api, userID);
|
||||
return config;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getLayoutResId() {
|
||||
return R.layout.activity_prefs;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setupToolbar(toolbar);
|
||||
|
||||
getSupportFragmentManager().beginTransaction()
|
||||
.add(R.id.fragment_container, new PreferencesFragment())
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void injectActivity(AppComponent component) {
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
if (preferenceScreen.getKey().equals("pushNotifications")) {
|
||||
fragment = new PushNotificationsPreferencesFragment();
|
||||
}
|
||||
return fragment;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
package com.habitrpg.android.habitica.ui.activities
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v7.preference.PreferenceFragmentCompat
|
||||
import android.support.v7.preference.PreferenceScreen
|
||||
import android.support.v7.widget.Toolbar
|
||||
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.components.AppComponent
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PreferencesFragment
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.PushNotificationsPreferencesFragment
|
||||
|
||||
import butterknife.BindView
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.APIPreferenceFragment
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.AuthenticationPreferenceFragment
|
||||
import com.habitrpg.android.habitica.ui.fragments.preferences.ProfilePreferencesFragment
|
||||
|
||||
class PrefsActivity : BaseActivity(), PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
|
||||
|
||||
@BindView(R.id.toolbar)
|
||||
lateinit var toolbar: Toolbar
|
||||
|
||||
override fun getLayoutResId(): Int = R.layout.activity_prefs
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
setupToolbar(toolbar)
|
||||
|
||||
supportFragmentManager.beginTransaction()
|
||||
.add(R.id.fragment_container, PreferencesFragment())
|
||||
.commit()
|
||||
}
|
||||
|
||||
override fun injectActivity(component: AppComponent) {
|
||||
component.inject(this)
|
||||
}
|
||||
|
||||
override fun onSupportNavigateUp(): Boolean {
|
||||
if (supportFragmentManager.backStackEntryCount > 0) {
|
||||
onBackPressed()
|
||||
return true
|
||||
}
|
||||
return super.onSupportNavigateUp()
|
||||
}
|
||||
|
||||
override fun onPreferenceStartScreen(preferenceFragment: PreferenceFragmentCompat,
|
||||
preferenceScreen: PreferenceScreen): Boolean {
|
||||
val fragment = createNextPage(preferenceScreen)
|
||||
if (fragment != null) {
|
||||
val arguments = Bundle()
|
||||
arguments.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.key)
|
||||
fragment.arguments = arguments
|
||||
supportFragmentManager.beginTransaction()
|
||||
.replace(R.id.fragment_container, fragment)
|
||||
.addToBackStack(null)
|
||||
.commit()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun createNextPage(preferenceScreen: PreferenceScreen): PreferenceFragmentCompat? =
|
||||
when (preferenceScreen.key) {
|
||||
"profile" -> ProfilePreferencesFragment()
|
||||
"authentication" -> AuthenticationPreferenceFragment()
|
||||
"api" -> APIPreferenceFragment()
|
||||
"pushNotifications" -> PushNotificationsPreferencesFragment()
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences
|
||||
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.support.v7.preference.Preference
|
||||
import android.widget.Toast
|
||||
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.events.commands.OpenGemPurchaseFragmentCommand
|
||||
import com.habitrpg.android.habitica.helpers.QrCodeManager
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import java.util.*
|
||||
|
||||
class APIPreferenceFragment: BasePreferencesFragment() {
|
||||
private val apiPreferences: List<String>
|
||||
get() = Arrays.asList(getString(R.string.SP_APIToken), getString(R.string.SP_userID))
|
||||
|
||||
lateinit private var qrCodeManager: QrCodeManager
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
HabiticaBaseApplication.getComponent().inject(this)
|
||||
|
||||
qrCodeManager = QrCodeManager(userRepository, this.context)
|
||||
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
|
||||
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
||||
when (preference.key) {
|
||||
"SP_user_qr_code" -> qrCodeManager.showDialogue()
|
||||
else -> {
|
||||
val clipMan = activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipMan.primaryClip = ClipData.newPlainText(preference.key, preference.summary)
|
||||
Toast.makeText(activity, "Copied " + preference.key + " to clipboard.", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
|
||||
override fun setupPreferences() {
|
||||
for ((key, value) in preferenceScreen.sharedPreferences.all) {
|
||||
if (apiPreferences.contains(key) && value != null) {
|
||||
findPreference(key).summary = value.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,169 +0,0 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.ClipData;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.habitrpg.android.habitica.HabiticaApplication;
|
||||
import com.habitrpg.android.habitica.HabiticaBaseApplication;
|
||||
import com.habitrpg.android.habitica.R;
|
||||
import com.habitrpg.android.habitica.data.UserRepository;
|
||||
import com.habitrpg.android.habitica.events.commands.OpenGemPurchaseFragmentCommand;
|
||||
import com.habitrpg.android.habitica.helpers.QrCodeManager;
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler;
|
||||
import com.habitrpg.android.habitica.models.user.User;
|
||||
import com.habitrpg.android.habitica.models.user.SubscriptionPlan;
|
||||
import com.habitrpg.android.habitica.ui.views.subscriptions.SubscriptionDetailsView;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class AccountDetailsFragment extends BasePreferencesFragment {
|
||||
|
||||
@Inject
|
||||
UserRepository userRepository;
|
||||
|
||||
private QrCodeManager qrCodeManager;
|
||||
private User user;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
HabiticaBaseApplication.getComponent().inject(this);
|
||||
|
||||
String userID = getPreferenceManager().getSharedPreferences().getString(getContext().getString(R.string.SP_userID), null);
|
||||
if (userID != null) {
|
||||
userRepository.getUser(userID).subscribe(this::setUser, RxErrorHandler.handleEmptyError());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
userRepository.close();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setupPreferences() {
|
||||
for (Map.Entry<String, ?> preference : getPreferenceScreen().getSharedPreferences().getAll().entrySet()) {
|
||||
String key = preference.getKey();
|
||||
if (getAccountDetailsPreferences().contains(key) && preference.getValue() != null) {
|
||||
findPreference(key).setSummary(preference.getValue().toString());
|
||||
}
|
||||
}
|
||||
|
||||
qrCodeManager = new QrCodeManager(userRepository, this.getContext());
|
||||
}
|
||||
|
||||
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), getString(R.string.SP_user_qr_code));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceTreeClick(Preference preference) {
|
||||
if (preference.getKey().equals("SP_user_qr_code")) {
|
||||
qrCodeManager.showDialogue();
|
||||
} else if (preference.getKey().equals("subscription_status")) {
|
||||
if (user != null && user.getPurchased() != null && user.getPurchased().getPlan() != null) {
|
||||
SubscriptionPlan plan = user.getPurchased().getPlan();
|
||||
if (plan.isActive()) {
|
||||
showSubscriptionStatusDialog();
|
||||
return super.onPreferenceTreeClick(preference);
|
||||
}
|
||||
}
|
||||
EventBus.getDefault().post(new OpenGemPurchaseFragmentCommand());
|
||||
} else if ("reset_account".equals(preference.getKey())) {
|
||||
showAccountResetConfirmation();
|
||||
} else if ("delete_account".equals(preference.getKey())) {
|
||||
showAccountDeleteConfirmation();
|
||||
} else {
|
||||
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(preference);
|
||||
}
|
||||
|
||||
private void showAccountDeleteConfirmation() {
|
||||
final EditText input = new EditText(getContext());
|
||||
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT);
|
||||
input.setLayoutParams(lp);
|
||||
AlertDialog dialog = new AlertDialog.Builder(getContext())
|
||||
.setTitle(R.string.delete_account)
|
||||
.setMessage(R.string.delete_account_description)
|
||||
.setPositiveButton(R.string.delete_account_confirmation, ((thisDialog, which) -> {
|
||||
thisDialog.dismiss();
|
||||
deleteAccount(input.getText().toString());
|
||||
}))
|
||||
.setNegativeButton(R.string.nevermind, ((thisDialog, which) -> thisDialog.dismiss()))
|
||||
.create();
|
||||
dialog.setOnShowListener(arg0 -> dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getContext(), R.color.red_10)));
|
||||
dialog.setView(input);
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void deleteAccount(String password) {
|
||||
ProgressDialog dialog = ProgressDialog.show(getContext(), getContext().getString(R.string.deleting_account), null, true);
|
||||
userRepository.deleteAccount(password).subscribe(user -> {
|
||||
HabiticaApplication.logout(getContext());
|
||||
getActivity().finish();
|
||||
}, throwable -> {
|
||||
dialog.dismiss();
|
||||
RxErrorHandler.reportError(throwable);
|
||||
});
|
||||
}
|
||||
|
||||
private void showAccountResetConfirmation() {
|
||||
AlertDialog dialog = new AlertDialog.Builder(getContext())
|
||||
.setTitle(R.string.reset_account)
|
||||
.setMessage(R.string.reset_account_description)
|
||||
.setPositiveButton(R.string.reset_account_confirmation, ((thisDialog, which) -> {
|
||||
thisDialog.dismiss();
|
||||
resetAccount();
|
||||
}))
|
||||
.setNegativeButton(R.string.nevermind, ((thisDialog, which) -> thisDialog.dismiss()))
|
||||
.create();
|
||||
dialog.setOnShowListener(arg0 -> dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getContext(), R.color.red_10)));
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void resetAccount() {
|
||||
ProgressDialog dialog = ProgressDialog.show(getContext(), getContext().getString(R.string.resetting_account), null, true);
|
||||
userRepository.resetAccount().subscribe(user -> dialog.dismiss(), throwable -> {
|
||||
dialog.dismiss();
|
||||
RxErrorHandler.reportError(throwable);
|
||||
});
|
||||
}
|
||||
|
||||
private void showSubscriptionStatusDialog() {
|
||||
SubscriptionDetailsView view = new SubscriptionDetailsView(getContext());
|
||||
view.setPlan(user.getPurchased().getPlan());
|
||||
final AlertDialog dialog = new AlertDialog.Builder(getContext())
|
||||
.setView(view)
|
||||
.setTitle(R.string.subscription_status)
|
||||
.setPositiveButton(R.string.close, (dialogInterface, i) -> dialogInterface.dismiss()).create();
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences
|
||||
|
||||
import android.app.ProgressDialog
|
||||
import android.content.ClipData
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.support.v4.content.ContextCompat
|
||||
import android.support.v7.app.AlertDialog
|
||||
import android.support.v7.preference.EditTextPreference
|
||||
import android.support.v7.preference.Preference
|
||||
import android.support.v7.preference.PreferenceCategory
|
||||
import android.widget.EditText
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.Toast
|
||||
import com.habitrpg.android.habitica.HabiticaApplication
|
||||
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.events.commands.OpenGemPurchaseFragmentCommand
|
||||
import com.habitrpg.android.habitica.helpers.QrCodeManager
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.user.User
|
||||
import com.habitrpg.android.habitica.ui.views.subscriptions.SubscriptionDetailsView
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import rx.Observable
|
||||
import rx.functions.Action1
|
||||
|
||||
class AuthenticationPreferenceFragment: BasePreferencesFragment() {
|
||||
|
||||
override var user: User? = null
|
||||
set(value) {
|
||||
field = value
|
||||
updateUserFields()
|
||||
}
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
HabiticaBaseApplication.getComponent().inject(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
|
||||
private fun updateUserFields() {
|
||||
configurePreference(findPreference("display_name"), user?.profile?.name)
|
||||
configurePreference(findPreference("photo_url"), user?.profile?.imageUrl)
|
||||
configurePreference(findPreference("about"), user?.profile?.blurb)
|
||||
}
|
||||
|
||||
private fun configurePreference(preference: Preference?, value: String?) {
|
||||
val editPreference = preference as? EditTextPreference
|
||||
editPreference?.text = value
|
||||
preference?.summary = value
|
||||
}
|
||||
|
||||
override fun setupPreferences() {
|
||||
updateUserFields()
|
||||
}
|
||||
|
||||
override fun onPreferenceTreeClick(preference: Preference): Boolean {
|
||||
when (preference.key) {
|
||||
"subscription_status" -> {
|
||||
if (user != null && user!!.purchased != null && user!!.purchased.plan != null) {
|
||||
val plan = user!!.purchased.plan
|
||||
if (plan.isActive) {
|
||||
showSubscriptionStatusDialog()
|
||||
return super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
}
|
||||
EventBus.getDefault().post(OpenGemPurchaseFragmentCommand())
|
||||
}
|
||||
"reset_account" -> showAccountResetConfirmation()
|
||||
"delete_account" -> showAccountDeleteConfirmation()
|
||||
else -> {
|
||||
val clipMan = activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipMan.primaryClip = ClipData.newPlainText(preference.key, preference.summary)
|
||||
Toast.makeText(activity, "Copied " + preference.key + " to clipboard.", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
return super.onPreferenceTreeClick(preference)
|
||||
}
|
||||
|
||||
private fun showAccountDeleteConfirmation() {
|
||||
val input = EditText(context)
|
||||
val lp = LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT)
|
||||
input.layoutParams = lp
|
||||
val dialog = AlertDialog.Builder(context)
|
||||
.setTitle(R.string.delete_account)
|
||||
.setMessage(R.string.delete_account_description)
|
||||
.setPositiveButton(R.string.delete_account_confirmation) { thisDialog, _ ->
|
||||
thisDialog.dismiss()
|
||||
deleteAccount(input.text.toString())
|
||||
}
|
||||
.setNegativeButton(R.string.nevermind) { thisDialog, _ -> thisDialog.dismiss() }
|
||||
.create()
|
||||
dialog.setOnShowListener { _ -> dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.red_10)) }
|
||||
dialog.setView(input)
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun deleteAccount(password: String) {
|
||||
val dialog = ProgressDialog.show(context, context.getString(R.string.deleting_account), null, true)
|
||||
userRepository.deleteAccount(password).subscribe({ _ ->
|
||||
HabiticaApplication.logout(context)
|
||||
activity.finish()
|
||||
}) { throwable ->
|
||||
dialog.dismiss()
|
||||
RxErrorHandler.reportError(throwable)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showAccountResetConfirmation() {
|
||||
val dialog = AlertDialog.Builder(context)
|
||||
.setTitle(R.string.reset_account)
|
||||
.setMessage(R.string.reset_account_description)
|
||||
.setPositiveButton(R.string.reset_account_confirmation) { thisDialog, _ ->
|
||||
thisDialog.dismiss()
|
||||
resetAccount()
|
||||
}
|
||||
.setNegativeButton(R.string.nevermind) { thisDialog, _ -> thisDialog.dismiss() }
|
||||
.create()
|
||||
dialog.setOnShowListener { _ -> dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(context, R.color.red_10)) }
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
private fun resetAccount() {
|
||||
val dialog = ProgressDialog.show(context, context.getString(R.string.resetting_account), null, true)
|
||||
userRepository.resetAccount().subscribe({ _ -> dialog.dismiss() }) { throwable ->
|
||||
dialog.dismiss()
|
||||
RxErrorHandler.reportError(throwable)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showSubscriptionStatusDialog() {
|
||||
val view = SubscriptionDetailsView(context)
|
||||
view.setPlan(user?.purchased?.plan)
|
||||
val dialog = AlertDialog.Builder(context)
|
||||
.setView(view)
|
||||
.setTitle(R.string.subscription_status)
|
||||
.setPositiveButton(R.string.close) { dialogInterface, _ -> dialogInterface.dismiss() }.create()
|
||||
dialog.show()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences;
|
||||
|
||||
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();
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.support.v7.preference.PreferenceFragmentCompat
|
||||
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
||||
|
||||
import com.habitrpg.android.habitica.R
|
||||
import com.habitrpg.android.habitica.data.UserRepository
|
||||
import com.habitrpg.android.habitica.helpers.QrCodeManager
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.user.User
|
||||
import com.habitrpg.android.habitica.modules.AppModule
|
||||
import rx.functions.Action1
|
||||
import java.util.*
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Named
|
||||
|
||||
abstract class BasePreferencesFragment : PreferenceFragmentCompat() {
|
||||
|
||||
@Inject
|
||||
lateinit var userRepository: UserRepository
|
||||
@field:[Inject Named(AppModule.NAMED_USER_ID)]
|
||||
lateinit var userId: String
|
||||
|
||||
internal open var user: User? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
userRepository.getUser(userId).first().subscribe(Action1<User> {
|
||||
this.user = it
|
||||
}, RxErrorHandler.handleEmptyError())
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
userRepository.close()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.preferences_fragment, rootKey)
|
||||
setupPreferences()
|
||||
}
|
||||
|
||||
protected abstract fun setupPreferences()
|
||||
|
||||
}
|
||||
|
|
@ -57,9 +57,9 @@ public class PreferencesFragment extends BasePreferencesFragment implements
|
|||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
HabiticaBaseApplication.getComponent().inject(this);
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
HabiticaBaseApplication.getComponent().inject(this);
|
||||
context = getActivity();
|
||||
|
||||
String userID = getPreferenceManager().getSharedPreferences().getString(context.getString(R.string.SP_userID), null);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,66 @@
|
|||
package com.habitrpg.android.habitica.ui.fragments.preferences
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.support.v7.preference.EditTextPreference
|
||||
import android.support.v7.preference.Preference
|
||||
import android.support.v7.preference.PreferenceCategory
|
||||
import com.habitrpg.android.habitica.HabiticaBaseApplication
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import com.habitrpg.android.habitica.models.user.User
|
||||
import rx.Observable
|
||||
import rx.functions.Action1
|
||||
|
||||
class ProfilePreferencesFragment: BasePreferencesFragment(), SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
override var user: User? = null
|
||||
set(value) {
|
||||
field = value
|
||||
updateUserFields()
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
HabiticaBaseApplication.getComponent().inject(this)
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
preferenceManager.sharedPreferences.registerOnSharedPreferenceChangeListener(this)
|
||||
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
preferenceManager.sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
|
||||
super.onPause()
|
||||
}
|
||||
|
||||
private fun updateUserFields() {
|
||||
configurePreference(findPreference("display_name"), user?.profile?.name)
|
||||
configurePreference(findPreference("photo_url"), user?.profile?.imageUrl)
|
||||
configurePreference(findPreference("about"), user?.profile?.blurb)
|
||||
}
|
||||
|
||||
private fun configurePreference(preference: Preference?, value: String?) {
|
||||
val editPreference = preference as? EditTextPreference
|
||||
editPreference?.text = value
|
||||
preference?.summary = value
|
||||
}
|
||||
|
||||
override fun setupPreferences() {
|
||||
updateUserFields()
|
||||
}
|
||||
|
||||
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
||||
val profileCategory = findPreference("profile") as? PreferenceCategory
|
||||
configurePreference(profileCategory?.findPreference(key), sharedPreferences?.getString(key, ""))
|
||||
val observable: Observable<User>? = when (key) {
|
||||
"display_name" -> userRepository.updateUser(user, "profile.name", sharedPreferences?.getString(key, ""))
|
||||
"photo_url" -> userRepository.updateUser(user, "profile.photo", sharedPreferences?.getString(key, ""))
|
||||
"about" -> userRepository.updateUser(user, "profile.blurb", sharedPreferences?.getString(key, ""))
|
||||
else -> null
|
||||
}
|
||||
observable?.subscribe(Action1 {}, RxErrorHandler.handleEmptyError())
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue