Skip to content

Commit

Permalink
Merge pull request #10251 from wordpress-mobile/issue/8715-facebook-e…
Browse files Browse the repository at this point in the history
…rror-message

Add better error message for Facebook connection
  • Loading branch information
shiki authored Jul 19, 2019
2 parents 091e249 + a60ad29 commit b960cfd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.wordpress.android.util.StringUtils;

public class PublicizeService {
public static final String FACEBOOK_SERVICE_ID = "facebook";

private String mId;
private String mLabel;
private String mDescription;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.wordpress.android.R;
import org.wordpress.android.WordPress;
import org.wordpress.android.datasets.PublicizeTable;
import org.wordpress.android.models.PublicizeConnection;
import org.wordpress.android.models.PublicizeService;
import org.wordpress.android.ui.publicize.PublicizeConstants.ConnectAction;
import org.wordpress.android.ui.publicize.PublicizeEvents.ActionCompleted;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.JSONUtils;

import java.util.HashMap;
Expand All @@ -36,6 +38,14 @@ public interface OnPublicizeActionListener {
void onRequestReconnect(PublicizeService service, PublicizeConnection connection);
}

private static class PublicizeConnectionValidationException extends Exception {
private final int mReasonResId;

PublicizeConnectionValidationException(int reasonResId) {
mReasonResId = reasonResId;
}
}

/*
* disconnect a currently connected publicize service
*/
Expand Down Expand Up @@ -114,7 +124,17 @@ private static void connectStepOne(final long siteId, final String serviceId, fi
RestRequest.Listener listener = new RestRequest.Listener() {
@Override
public void onResponse(JSONObject jsonObject) {
if (shouldShowChooserDialog(siteId, serviceId, jsonObject)) {
final boolean showChooserDialog;
try {
showChooserDialog = shouldShowChooserDialog(siteId, serviceId, jsonObject);
} catch (PublicizeConnectionValidationException e) {
final ActionCompleted event =
new ActionCompleted(false, ConnectAction.CONNECT, serviceId, e.mReasonResId);
EventBus.getDefault().post(event);
return;
}

if (showChooserDialog) {
// show dialog showing multiple options
EventBus.getDefault()
.post(new PublicizeEvents.ActionRequestChooseAccount(siteId, serviceId, jsonObject));
Expand Down Expand Up @@ -169,10 +189,11 @@ public void onErrorResponse(VolleyError volleyError) {
WordPress.getRestClientUtilsV1_1().post(path, params, null, listener, errorListener);
}

private static boolean shouldShowChooserDialog(long siteId, String serviceId, JSONObject jsonObject) {
private static boolean shouldShowChooserDialog(long siteId, String serviceId, JSONObject jsonObject)
throws PublicizeConnectionValidationException {
JSONArray jsonConnectionList = jsonObject.optJSONArray("connections");

if (jsonConnectionList == null || jsonConnectionList.length() <= 1) {
if (jsonConnectionList == null || jsonConnectionList.length() <= 0) {
return false;
}

Expand All @@ -191,10 +212,18 @@ private static boolean shouldShowChooserDialog(long siteId, String serviceId, JS
}
}

final boolean hasExternalAccounts = totalExternalAccounts > 0;
if (PublicizeTable.onlyExternalConnections(serviceId)) {
return totalExternalAccounts > 0;
if (!hasExternalAccounts && serviceId.equals(PublicizeService.FACEBOOK_SERVICE_ID)) {
AppLog.i(T.SHARING,
"The Facebook account cannot be linked because either there was no Page selected or the "
+ "Page is set as not published.");
throw new PublicizeConnectionValidationException(R.string.sharing_facebook_account_must_have_pages);
} else {
return hasExternalAccounts;
}
} else {
return totalAccounts > 0 || totalExternalAccounts > 0;
return totalAccounts > 0 || hasExternalAccounts;
}
} catch (JSONException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.wordpress.android.ui.publicize;

import androidx.annotation.Nullable;

import org.json.JSONObject;
import org.wordpress.android.ui.publicize.PublicizeConstants.ConnectAction;

Expand All @@ -17,12 +19,21 @@ public static class ConnectionsChanged {
public static class ActionCompleted {
private final boolean mSucceeded;
private final ConnectAction mAction;
/**
* The reason for why {@link #mSucceeded} is false.
*/
@Nullable private final Integer mReasonResId;
private String mService;

public ActionCompleted(boolean succeeded, ConnectAction action, String service) {
this(succeeded, action, service, null);
}

ActionCompleted(boolean succeeded, ConnectAction action, String service, @Nullable Integer reasonResId) {
mSucceeded = succeeded;
mAction = action;
mService = service;
mReasonResId = reasonResId;
}

public ConnectAction getAction() {
Expand All @@ -36,6 +47,10 @@ public boolean didSucceed() {
public String getService() {
return mService;
}

@Nullable public Integer getReasonResId() {
return mReasonResId;
}
}

public static class ActionAccountChosen {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ public void onEventMainThread(PublicizeEvents.ActionCompleted event) {
AnalyticsUtils.trackWithSiteDetails(Stat.PUBLICIZE_SERVICE_DISCONNECTED, mSite, analyticsProperties);
}
} else {
ToastUtils.showToast(this, R.string.error_generic);
if (event.getReasonResId() != null) {
showAlertForFailureReason(event.getReasonResId());
} else {
ToastUtils.showToast(this, R.string.error_generic);
}
}
}

Expand Down Expand Up @@ -372,4 +376,12 @@ public void onButtonPrefsClicked() {
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.commit();
}

private void showAlertForFailureReason(int reasonResId) {
final AlertDialog.Builder builder =
new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.Calypso_Dialog));
builder.setMessage(reasonResId);
builder.setPositiveButton(R.string.ok, (dialog, which) -> dialog.dismiss());
builder.show();
}
}
2 changes: 2 additions & 0 deletions WordPress/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<string name="write_post">Write Post</string>
<string name="dismiss">dismiss</string>
<string name="exit">exit</string>
<string name="ok">OK</string>

<string name="button_not_now">Not now</string>

Expand Down Expand Up @@ -1903,6 +1904,7 @@
<string name="connecting_account">Connecting account</string>
<string name="sharing_label_message">Change the text of the sharing buttons\' label. This text won\'t appear until you add at least one sharing button.</string>
<string name="sharing_twitter_message">This will be included in tweets when people share using the Twitter button</string>
<string name="sharing_facebook_account_must_have_pages">The Facebook connection could not be made because this account does not have access to any pages. Facebook supports sharing connections to Facebook Pages, but not to Facebook Profiles.</string>

<!--Theme Browser-->
<string name="current_theme">Current Theme</string>
Expand Down

0 comments on commit b960cfd

Please sign in to comment.