Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.ui.prefs;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

Expand Down Expand Up @@ -91,7 +92,7 @@ class DotComSiteSettings extends SiteSettingsInterface {
private Exception mSaveError = null;

/** Only instantiated by {@link SiteSettingsInterface}. */
DotComSiteSettings(Activity host, SiteModel site, SiteSettingsListener listener) {
DotComSiteSettings(Context host, SiteModel site, SiteSettingsListener listener) {
super(host, site, listener);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.ui.prefs;

import android.app.Activity;
import android.content.Context;

import org.wordpress.android.datasets.SiteSettingsTable;
import org.wordpress.android.fluxc.model.SiteModel;
Expand All @@ -9,7 +10,7 @@ class DotOrgSiteSettings extends SiteSettingsInterface {
/**
* Only instantiated by {@link SiteSettingsInterface}.
*/
DotOrgSiteSettings(Activity host, SiteModel site, SiteSettingsListener listener) {
DotOrgSiteSettings(Context host, SiteModel site, SiteSettingsListener listener) {
super(host, site, listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html;
Expand Down Expand Up @@ -66,7 +67,7 @@
* This class is marked abstract. This is due to the fact that .org (self-hosted) and .com sites
* expose different API's to query and edit their respective settings (even though the options
* offered by each is roughly the same). To get an instance of this interface class use the
* {@link SiteSettingsInterface#getInterface(Activity, SiteModel, SiteSettingsListener)} method.
* {@link SiteSettingsInterface#getInterface(Context, SiteModel, SiteSettingsListener)} method.
*/

public abstract class SiteSettingsInterface {
Expand Down Expand Up @@ -125,7 +126,7 @@ public abstract class SiteSettingsInterface {
* Instantiates the appropriate (self-hosted or .com) SiteSettingsInterface.
*/
@Nullable
public static SiteSettingsInterface getInterface(Activity host, SiteModel site, SiteSettingsListener listener) {
public static SiteSettingsInterface getInterface(Context host, SiteModel site, SiteSettingsListener listener) {
if (host == null || site == null) return null;

if (SiteUtils.isAccessedViaWPComRest(site)) {
Expand Down Expand Up @@ -193,7 +194,7 @@ public interface SiteSettingsListener {
*/
protected abstract void fetchRemoteData();

protected final Activity mActivity;
protected final Context mContext;
protected final SiteModel mSite;
protected final SiteSettingsListener mListener;
protected final SiteSettingsModel mSettings;
Expand All @@ -205,10 +206,10 @@ public interface SiteSettingsListener {
@Inject SiteStore mSiteStore;
@Inject Dispatcher mDispatcher;

protected SiteSettingsInterface(Activity host, SiteModel site, SiteSettingsListener listener) {
protected SiteSettingsInterface(Context host, SiteModel site, SiteSettingsListener listener) {
((WordPress) host.getApplicationContext()).component().inject(this);
mDispatcher.register(this);
mActivity = host;
mContext = host;
mSite = site;
mListener = listener;
mSettings = new SiteSettingsModel();
Expand All @@ -226,9 +227,9 @@ protected void finalize() throws Throwable {

public void saveSettings() {
SiteSettingsTable.saveSettings(mSettings);
siteSettingsPreferences(mActivity).edit().putString(LANGUAGE_PREF_KEY, mSettings.language).apply();
siteSettingsPreferences(mActivity).edit().putInt(DEF_CATEGORY_PREF_KEY, mSettings.defaultCategory).apply();
siteSettingsPreferences(mActivity).edit().putString(DEF_FORMAT_PREF_KEY, mSettings.defaultPostFormat).apply();
siteSettingsPreferences(mContext).edit().putString(LANGUAGE_PREF_KEY, mSettings.language).apply();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is pre-existing code, but how about using the builder pattern here?

siteSettingsPreferences(mContext).edit()
        .putString(LANGUAGE_PREF_KEY, mSettings.language)
        .putInt(DEF_CATEGORY_PREF_KEY, mSettings.defaultCategory)
        .putString(DEF_FORMAT_PREF_KEY, mSettings.defaultPostFormat)
        .apply();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, done in c97620d.

siteSettingsPreferences(mContext).edit().putInt(DEF_CATEGORY_PREF_KEY, mSettings.defaultCategory).apply();
siteSettingsPreferences(mContext).edit().putString(DEF_FORMAT_PREF_KEY, mSettings.defaultPostFormat).apply();
}

public @NonNull String getTitle() {
Expand All @@ -248,14 +249,14 @@ public int getPrivacy() {
}

public @NonNull String getPrivacyDescription() {
if (mActivity != null) {
if (mContext != null) {
switch (getPrivacy()) {
case -1:
return mActivity.getString(R.string.site_settings_privacy_private_summary);
return mContext.getString(R.string.site_settings_privacy_private_summary);
case 0:
return mActivity.getString(R.string.site_settings_privacy_hidden_summary);
return mContext.getString(R.string.site_settings_privacy_hidden_summary);
case 1:
return mActivity.getString(R.string.site_settings_privacy_public_summary);
return mContext.getString(R.string.site_settings_privacy_public_summary);
}
}
return "";
Expand All @@ -275,8 +276,8 @@ public int getPrivacy() {

public @NonNull Map<String, String> getFormats() {
mSettings.postFormats = new HashMap<>();
String[] postFormatDisplayNames = mActivity.getResources().getStringArray(R.array.post_format_display_names);
String[] postFormatKeys = mActivity.getResources().getStringArray(R.array.post_format_keys);
String[] postFormatDisplayNames = mContext.getResources().getStringArray(R.array.post_format_display_names);
String[] postFormatKeys = mContext.getResources().getStringArray(R.array.post_format_keys);
// Add standard post format (only for .com)
mSettings.postFormats.put(STANDARD_POST_FORMAT_KEY, STANDARD_POST_FORMAT);
// Add default post formats
Expand Down Expand Up @@ -350,8 +351,8 @@ public boolean getShowRelatedPostImages() {
}

public @NonNull String getRelatedPostsDescription() {
if (mActivity == null) return "";
String desc = mActivity.getString(getShowRelatedPosts() ? R.string.on : R.string.off);
if (mContext == null) return "";
String desc = mContext.getString(getShowRelatedPosts() ? R.string.on : R.string.off);
return StringUtils.capitalize(desc);
}

Expand Down Expand Up @@ -388,11 +389,11 @@ public int getCloseAfterPeriodForDescription() {
}

public @NonNull String getCloseAfterDescriptionForPeriod(int period) {
if (mActivity == null) return "";
if (mContext == null) return "";

if (!getShouldCloseAfter()) return mActivity.getString(R.string.never);
if (!getShouldCloseAfter()) return mContext.getString(R.string.never);

return StringUtils.getQuantityString(mActivity, R.string.never, R.string.days_quantity_one,
return StringUtils.getQuantityString(mContext, R.string.never, R.string.days_quantity_one,
R.string.days_quantity_other, period);
}

Expand All @@ -401,16 +402,16 @@ public int getCommentSorting() {
}

public @NonNull String getSortingDescription() {
if (mActivity == null) return "";
if (mContext == null) return "";

int order = getCommentSorting();
switch (order) {
case SiteSettingsInterface.ASCENDING_SORT:
return mActivity.getString(R.string.oldest_first);
return mContext.getString(R.string.oldest_first);
case SiteSettingsInterface.DESCENDING_SORT:
return mActivity.getString(R.string.newest_first);
return mContext.getString(R.string.newest_first);
default:
return mActivity.getString(R.string.unknown);
return mContext.getString(R.string.unknown);
}
}

Expand All @@ -431,10 +432,10 @@ public int getThreadingLevelsForDescription() {
}

public @NonNull String getThreadingDescriptionForLevel(int level) {
if (mActivity == null) return "";
if (mContext == null) return "";

if (level <= 1) return mActivity.getString(R.string.none);
return String.format(mActivity.getString(R.string.site_settings_threading_summary), level);
if (level <= 1) return mContext.getString(R.string.none);
return String.format(mContext.getString(R.string.site_settings_threading_summary), level);
}

public boolean getShouldPageComments() {
Expand All @@ -450,14 +451,14 @@ public int getPagingCountForDescription() {
}

public @NonNull String getPagingDescription() {
if (mActivity == null) return "";
if (mContext == null) return "";

if (!getShouldPageComments()) {
return mActivity.getString(R.string.disabled);
return mContext.getString(R.string.disabled);
}

int count = getPagingCountForDescription();
return StringUtils.getQuantityString(mActivity, R.string.none, R.string.site_settings_paging_summary_one,
return StringUtils.getQuantityString(mContext, R.string.none, R.string.site_settings_paging_summary_one,
R.string.site_settings_paging_summary_other, count);
}

Expand Down Expand Up @@ -548,9 +549,9 @@ public boolean getAllowCommentLikes() {
}

public @NonNull String getKeysDescription(int count) {
if (mActivity == null) return "";
if (mContext == null) return "";

return StringUtils.getQuantityString(mActivity, R.string.site_settings_list_editor_no_items_text,
return StringUtils.getQuantityString(mContext, R.string.site_settings_list_editor_no_items_text,
R.string.site_settings_list_editor_summary_one,
R.string.site_settings_list_editor_summary_other, count);
}
Expand Down Expand Up @@ -928,9 +929,9 @@ private void loadCachedSettings() {
* Notifies listener that credentials have been validated or are incorrect.
*/
private void notifyCredentialsVerifiedOnUiThread(final Exception error) {
if (mActivity == null || mListener == null) return;
if (mContext == null || mListener == null) return;

mActivity.runOnUiThread(new Runnable() {
new Handler().post(new Runnable() {
@Override
public void run() {
mListener.onCredentialsValidated(error);
Expand All @@ -939,11 +940,13 @@ public void run() {
}

protected void notifyFetchErrorOnUiThread(final Exception error) {
if (mActivity == null || mActivity.isFinishing() || mListener == null) {
if (mContext == null
|| (mContext instanceof Activity && ((Activity) mContext).isFinishing())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to check isFinishing() here? That seems like something the caller should deal with.

@hypest hypest Nov 6, 2017

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was only adapting the existing code without changing the logic and yeah, that check is awkward. I removed it with 0247fd6, letting the listener decide whether the Activity (if any) needs to be checked for finishing. It seems that almost all listeners are already checking for isAdded so, no new checks are needed.

Is that what you had in mind @nbradbury ?

|| mListener == null) {
return;
}

mActivity.runOnUiThread(new Runnable() {
new Handler().post(new Runnable() {
@Override
public void run() {
mListener.onFetchError(error);
Expand All @@ -952,11 +955,13 @@ public void run() {
}

protected void notifySaveErrorOnUiThread(final Exception error) {
if (mActivity == null || mActivity.isFinishing() || mListener == null) {
if (mContext == null
|| (mContext instanceof Activity && ((Activity) mContext).isFinishing())
|| mListener == null) {
return;
}

mActivity.runOnUiThread(new Runnable() {
new Handler().post(new Runnable() {
@Override
public void run() {
mListener.onSaveError(error);
Expand All @@ -968,9 +973,13 @@ public void run() {
* Notifies listener that settings have been updated with the latest remote data.
*/
protected void notifyUpdatedOnUiThread() {
if (mActivity == null || mActivity.isFinishing() || mListener == null) return;
if (mContext == null
|| (mContext instanceof Activity && ((Activity) mContext).isFinishing())
|| mListener == null) {
return;
}

mActivity.runOnUiThread(new Runnable() {
new Handler().post(new Runnable() {
@Override
public void run() {
mListener.onSettingsUpdated();
Expand All @@ -982,9 +991,9 @@ public void run() {
* Notifies listener that settings have been saved or an error occurred while saving.
*/
protected void notifySavedOnUiThread() {
if (mActivity == null || mListener == null) return;
if (mContext == null || mListener == null) return;

mActivity.runOnUiThread(new Runnable() {
new Handler().post(new Runnable() {
@Override
public void run() {
mListener.onSettingsSaved();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.util;

import android.app.Activity;
import android.content.Context;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
Expand Down Expand Up @@ -202,9 +203,9 @@ public static Locale languageLocale(String languageCode) {
/**
* Creates a map from language codes to WordPress language IDs.
*/
public static Map<String, String> generateLanguageMap(Activity activity) {
String[] languageIds = activity.getResources().getStringArray(R.array.lang_ids);
String[] languageCodes = activity.getResources().getStringArray(R.array.language_codes);
public static Map<String, String> generateLanguageMap(Context context) {
String[] languageIds = context.getResources().getStringArray(R.array.lang_ids);
String[] languageCodes = context.getResources().getStringArray(R.array.language_codes);

Map<String, String> languageMap = new HashMap<>();
for (int i = 0; i < languageIds.length && i < languageCodes.length; ++i) {
Expand Down