From 268b446b03a9432f98acbd716b06ebfcbf1d504b Mon Sep 17 00:00:00 2001 From: Ezerous Date: Mon, 15 Oct 2018 16:35:58 +0300 Subject: [PATCH] Settings fix --- .../mthmmy/activities/LoginActivity.java | 2 +- .../mthmmy/activities/main/MainActivity.java | 2 +- .../activities/settings/SettingsActivity.java | 3 +- .../activities/settings/SettingsFragment.java | 58 +++++++++++++------ .../gr/thmmy/mthmmy/base/BaseActivity.java | 3 - .../main/res/xml/app_preferences_guest.xml | 46 +++++++++++++++ ...eferences.xml => app_preferences_user.xml} | 0 7 files changed, 89 insertions(+), 25 deletions(-) create mode 100644 app/src/main/res/xml/app_preferences_guest.xml rename app/src/main/res/xml/{app_preferences.xml => app_preferences_user.xml} (100%) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java index add3abaf..a7adbf02 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java @@ -46,7 +46,7 @@ public class LoginActivity extends BaseActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); - PreferenceManager.setDefaultValues(this, R.xml.app_preferences, false); + PreferenceManager.setDefaultValues(this, R.xml.app_preferences_user, false); //Variables initialization inputUsername = findViewById(R.id.username); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 4ffc3dff..ffd82dda 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -58,7 +58,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF redirectToActivityFromIntent(intentFilter); setContentView(R.layout.activity_main); - PreferenceManager.setDefaultValues(this, R.xml.app_preferences, false); + PreferenceManager.setDefaultValues(this, R.xml.app_preferences_user, false); if (sessionManager.isLoginScreenDefault()) { //Go to login diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java index 5d864eeb..4383732b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsActivity.java @@ -42,8 +42,7 @@ public class SettingsActivity extends BaseActivity { protected void onResume() { drawer.setSelection(SETTINGS_ID); super.onResume(); - if (preferenceFragment != null) { + if (preferenceFragment != null) preferenceFragment.updateUserLoginState(sessionManager.isLoggedIn()); - } } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java index 3f65e764..922c2e21 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/settings/SettingsFragment.java @@ -22,9 +22,12 @@ import gr.thmmy.mthmmy.base.BaseApplication; import timber.log.Timber; import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.DEFAULT_HOME_TAB; -import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.POSTING_APP_SIGNATURE_ENABLE_KEY; public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { + private enum PREFS_TYPE { + NOT_SET, USER, GUEST + } + public static final String ARG_IS_LOGGED_IN = "selectedRingtoneKey"; //Preferences xml keys @@ -40,6 +43,7 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared private SharedPreferences settingsFile; + private PREFS_TYPE prefs_type = PREFS_TYPE.NOT_SET; private boolean isLoggedIn = false; private ArrayList defaultHomeTabEntries = new ArrayList<>(); private ArrayList defaultHomeTabValues = new ArrayList<>(); @@ -85,8 +89,17 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared @Override public void onCreatePreferences(Bundle bundle, String rootKey) { - // Load the Preferences from the XML file - addPreferencesFromResource(R.xml.app_preferences); + isLoggedIn = BaseApplication.getInstance().getSessionManager().isLoggedIn(); //Ensures it stays updated + // Add the Preferences from the XML file if needed + if(isLoggedIn&&(prefs_type==PREFS_TYPE.GUEST||prefs_type==PREFS_TYPE.NOT_SET)){ + prefs_type = PREFS_TYPE.USER; + addPreferencesFromResource(R.xml.app_preferences_user); + + } + else if(!isLoggedIn&&(prefs_type==PREFS_TYPE.USER||prefs_type==PREFS_TYPE.NOT_SET)){ + prefs_type = PREFS_TYPE.GUEST; + addPreferencesFromResource(R.xml.app_preferences_guest); + } } @Override @@ -152,24 +165,33 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared } private void updatePreferenceVisibility(){ - findPreference(POSTING_CATEGORY).setVisible(isLoggedIn); - findPreference(POSTING_APP_SIGNATURE_ENABLE_KEY).setVisible(isLoggedIn); - //findPreference(UPLOADING_CATEGORY).setVisible(isLoggedIn); - //findPreference(UPLOADING_APP_SIGNATURE_ENABLE_KEY).setVisible(isLoggedIn); - - if (!isLoggedIn && defaultHomeTabEntries.contains("Unread")) { - defaultHomeTabEntries.remove("Unread"); - defaultHomeTabValues.remove("2"); - } else if (isLoggedIn && !defaultHomeTabEntries.contains("Unread")) { - defaultHomeTabEntries.add("Unread"); - defaultHomeTabValues.add("2"); + boolean updateHomeTabs=false; + if(isLoggedIn&& prefs_type==PREFS_TYPE.GUEST) { + prefs_type = PREFS_TYPE.USER; + setPreferencesFromResource(R.xml.app_preferences_user, getPreferenceScreen().getKey()); + if(!defaultHomeTabEntries.contains("Unread")){ + defaultHomeTabEntries.add("Unread"); + defaultHomeTabValues.add("2"); + updateHomeTabs=true; + } + } + else if(!isLoggedIn&&prefs_type==PREFS_TYPE.USER){ + prefs_type = PREFS_TYPE.GUEST; + setPreferencesFromResource(R.xml.app_preferences_guest,getPreferenceScreen().getKey()); + if(defaultHomeTabEntries.contains("Unread")){ + defaultHomeTabEntries.remove("Unread"); + defaultHomeTabValues.remove("2"); + updateHomeTabs=true; + } } - CharSequence[] tmpCs = defaultHomeTabEntries.toArray(new CharSequence[defaultHomeTabEntries.size()]); - ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntries(tmpCs); + if(updateHomeTabs){ + CharSequence[] tmpCs = defaultHomeTabEntries.toArray(new CharSequence[defaultHomeTabEntries.size()]); + ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntries(tmpCs); - tmpCs = defaultHomeTabValues.toArray(new CharSequence[defaultHomeTabValues.size()]); - ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntryValues(tmpCs); + tmpCs = defaultHomeTabValues.toArray(new CharSequence[defaultHomeTabValues.size()]); + ((ListPreference) findPreference(DEFAULT_HOME_TAB)).setEntryValues(tmpCs); + } } @Override diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index dba06d2b..37db7c41 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -56,7 +56,6 @@ import gr.thmmy.mthmmy.model.ThmmyFile; import gr.thmmy.mthmmy.services.DownloadHelper; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.FileUtils; -import gr.thmmy.mthmmy.utils.LaunchType; import gr.thmmy.mthmmy.viewmodel.BaseViewModel; import okhttp3.OkHttpClient; import ru.noties.markwon.LinkResolverDef; @@ -74,8 +73,6 @@ import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.DEFAULT_HOME_ import static gr.thmmy.mthmmy.services.DownloadHelper.SAVE_DIR; import static gr.thmmy.mthmmy.session.SessionManager.SUCCESS; import static gr.thmmy.mthmmy.utils.FileUtils.getMimeType; -import static gr.thmmy.mthmmy.utils.LaunchType.LAUNCH_TYPE.FIRST_LAUNCH_EVER; -import static gr.thmmy.mthmmy.utils.LaunchType.LAUNCH_TYPE.INDETERMINATE; public abstract class BaseActivity extends AppCompatActivity { // Client & Cookies diff --git a/app/src/main/res/xml/app_preferences_guest.xml b/app/src/main/res/xml/app_preferences_guest.xml new file mode 100644 index 00000000..4aa2227f --- /dev/null +++ b/app/src/main/res/xml/app_preferences_guest.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/app_preferences.xml b/app/src/main/res/xml/app_preferences_user.xml similarity index 100% rename from app/src/main/res/xml/app_preferences.xml rename to app/src/main/res/xml/app_preferences_user.xml