Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public static ConnectionChangeReceiver getInstance(){
}
return sInstance;
}

public static EventBus getEventBus() {
return EventBus.getDefault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,23 @@ private void launchHelpshift(HelpshiftHelper.Tag origin) {
private enum SiteCreationBackStackMode {
NORMAL,
MODAL,
FINISH_OK
FINISH_OK,
FINISH_DISMISS
}

private SiteCreationBackStackMode getSiteCreationBackStackMode() {
SiteCreationCreatingFragment siteCreationCreatingFragment =
(SiteCreationCreatingFragment) getSupportFragmentManager()
.findFragmentByTag(SiteCreationCreatingFragment.TAG);

if (siteCreationCreatingFragment == null) {
if (siteCreationCreatingFragment == null || siteCreationCreatingFragment.canGoBack()) {
return SiteCreationBackStackMode.NORMAL;
} else if (siteCreationCreatingFragment.isInModalMode()) {
return SiteCreationBackStackMode.MODAL;
} else if (siteCreationCreatingFragment.isCreationSucceeded()) {
return SiteCreationBackStackMode.FINISH_OK;
} else {
return SiteCreationBackStackMode.NORMAL;
return SiteCreationBackStackMode.FINISH_DISMISS;
}
}

Expand All @@ -147,6 +148,9 @@ public void onBackPressed() {
setResult(RESULT_OK);
finish();
break;
case FINISH_DISMISS:
finish();
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
}
}

protected void showHomeButton(boolean visible) {
protected void showHomeButton(boolean visible, boolean isCloseButton) {
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(visible);
if (isCloseButton) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
import org.wordpress.android.R;
import org.wordpress.android.analytics.AnalyticsTracker;
import org.wordpress.android.ui.accounts.signup.SiteCreationService.SiteCreationState;
import org.wordpress.android.ui.accounts.signup.SiteCreationService.SiteCreationStep;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.AppLog.T;
import org.wordpress.android.util.AutoForeground.ServiceEventConnection;
import org.wordpress.android.util.NetworkUtils;
import org.wordpress.android.util.URLFilteredWebViewClient;

import java.util.HashMap;
Expand All @@ -31,8 +33,6 @@ public class SiteCreationCreatingFragment extends SiteCreationBaseFormFragment<S
private static final String ARG_SITE_SLUG = "ARG_SITE_SLUG";
private static final String ARG_SITE_THEME_ID = "ARG_SITE_THEME_ID";

private static final String KEY_IN_MODAL_MODE = "KEY_IN_MODAL_MODE";
private static final String KEY_CREATION_FINISHED = "KEY_CREATION_FINISHED";
private static final String KEY_WEBVIEW_LOADED_IN_TIME = "KEY_WEBVIEW_LOADED_IN_TIME";
private static final String KEY_TRACKED_SUCCESS = "KEY_TRACKED_SUCCESS";

Expand All @@ -48,19 +48,32 @@ public class SiteCreationCreatingFragment extends SiteCreationBaseFormFragment<S
private TextView[] mLabels;

private boolean mTrackedSuccess;
private boolean mInModalMode;
private boolean mCreationSucceeded;
private boolean mWebViewLoadedInTime;
private int mNewSiteLocalId;
int mNewSiteLocalId;

private PreviewWebViewClient mPreviewWebViewClient;

public boolean isInModalMode() {
return mInModalMode;
SiteCreationState state = SiteCreationService.getState();
return state != null && state.isInProgress();
}

public boolean isCreationSucceeded() {
return mCreationSucceeded;
SiteCreationState state = SiteCreationService.getState();
return state != null && SiteCreationService.getState().getStep() == SiteCreationStep.SUCCESS;

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.

Since state is created in the line above, can't we simplify SiteCreationService.getState().getStep() to state.getStep() to minimize the SiteCreationService.getState() calls? Otherwise, creating state doesn't reduce the number of SiteCreationService.getState() calls.

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.

Oh, good find! I was going to reuse the variable but that fell between the cracks! Done with 12a8e0f.

}

public boolean canGoBack() {
SiteCreationState state = SiteCreationService.getState();
if (state == null) {
return true;
}

if (state.getStep() == SiteCreationStep.FAILURE) {
state = (SiteCreationState) state.getPayload();
}

return !state.isAfterCreation();
}

public static SiteCreationCreatingFragment newInstance(String siteTitle, String siteTagline, String siteSlug,
Expand Down Expand Up @@ -130,11 +143,8 @@ public void onCreate(Bundle savedInstanceState) {

if (savedInstanceState == null) {
// on first appearance start the Service to perform the site creation
mInModalMode = true;
createSite();
} else {
mInModalMode = savedInstanceState.getBoolean(KEY_IN_MODAL_MODE, false);
mCreationSucceeded = savedInstanceState.getBoolean(KEY_CREATION_FINISHED, false);
mWebViewLoadedInTime = savedInstanceState.getBoolean(KEY_WEBVIEW_LOADED_IN_TIME, false);
mTrackedSuccess = savedInstanceState.getBoolean(KEY_TRACKED_SUCCESS, false);
}
Expand All @@ -144,7 +154,7 @@ public void onCreate(Bundle savedInstanceState) {
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

showHomeButton(!mInModalMode);
showHomeButton(!isInModalMode(), false);

if (savedInstanceState == null) {
AnalyticsTracker.track(AnalyticsTracker.Stat.SITE_CREATION_CREATING_VIEWED);
Expand All @@ -171,21 +181,19 @@ public void onPause() {
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putBoolean(KEY_IN_MODAL_MODE, mInModalMode);
outState.putBoolean(KEY_CREATION_FINISHED, mCreationSucceeded);
outState.putBoolean(KEY_WEBVIEW_LOADED_IN_TIME, mWebViewLoadedInTime);
outState.putBoolean(KEY_TRACKED_SUCCESS, mTrackedSuccess);
}

private void createSite() {
void createSite() {
String siteTitle = getArguments().getString(ARG_SITE_TITLE);
String siteTagline = getArguments().getString(ARG_SITE_TAGLINE);
String siteSlug = getArguments().getString(ARG_SITE_SLUG);
String themeId = getArguments().getString(ARG_SITE_THEME_ID);
SiteCreationService.createSite(getContext(), siteTitle, siteTagline, siteSlug, themeId);
}

private void retryFromState(SiteCreationState retryFromState, long newSiteRemoteId) {
void retryFromState(SiteCreationState retryFromState, long newSiteRemoteId) {
String siteTagline = getArguments().getString(ARG_SITE_TAGLINE);
String themeId = getArguments().getString(ARG_SITE_THEME_ID);
SiteCreationService.retryFromState(getContext(), retryFromState, newSiteRemoteId, siteTagline, themeId);
Expand Down Expand Up @@ -217,7 +225,7 @@ private PreviewWebViewClient loadWebview() {
}

private static class PreviewWebViewClient extends URLFilteredWebViewClient {
private boolean mIsPageFinished;
boolean mIsPageFinished;

PreviewWebViewClient(String siteAddress) {
super(siteAddress);
Expand Down Expand Up @@ -245,9 +253,17 @@ private void disableUntil(@IdRes int textViewId) {
}
}

private void setModalMode(boolean inModalMode) {
mInModalMode = inModalMode;
showHomeButton(!mInModalMode);
private void configureBackButton() {
SiteCreationState currentState = SiteCreationService.getState();

SiteCreationState failedOnState = null;
if (currentState != null && currentState.getStep() == SiteCreationStep.FAILURE) {
failedOnState = (SiteCreationState) currentState.getPayload();
}

boolean isInModal = currentState != null && currentState.isInProgress();
boolean failedAfterCreation = failedOnState != null && failedOnState.isAfterCreation();
showHomeButton(!isInModal, failedAfterCreation);
}

private void configureImage(boolean hasFailure) {
Expand All @@ -269,8 +285,8 @@ public void onClick(View view) {
if (failedState.isTerminal()) {
throw new RuntimeException("Internal inconsistency: Cannot resume site creation from "
+ failedState.getStepName());
} else if (failedState.getStep() == SiteCreationService.SiteCreationStep.IDLE
|| failedState.getStep() == SiteCreationService.SiteCreationStep.NEW_SITE) {
} else if (failedState.getStep() == SiteCreationStep.IDLE
|| failedState.getStep() == SiteCreationStep.NEW_SITE) {
createSite();
} else {
retryFromState(failedState, (long) failedState.getPayload());

@theck13 theck13 Feb 18, 2018

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.

Both the createSite and retryFromState methods are declared private and they are accessed outside the private scope as well as the mIsPageFinished variable. Making them package-private or protected will avoid create wasteful synthetic accessor methods.

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.

Done with 71f4c2d.

Expand All @@ -287,6 +303,8 @@ public void onSiteCreationStateUpdated(SiteCreationState event) {
mProgressContainer.setVisibility(View.VISIBLE);
mErrorContainer.setVisibility(View.GONE);

configureBackButton();

switch (event.getStep()) {
case IDLE:
disableUntil(0);
Expand All @@ -309,21 +327,19 @@ public void onSiteCreationStateUpdated(SiteCreationState event) {
configureImage(false);
break;
case FAILURE:
setModalMode(false);
configureImage(true);
mProgressContainer.setVisibility(View.GONE);
mErrorContainer.setVisibility(View.VISIBLE);
handleFailure((SiteCreationState) event.getPayload());
NetworkUtils.checkConnection(getContext());
break;
case PRELOAD:
disableUntil(R.id.site_creation_creating_preparing_frontend);
configureImage(false);
mPreviewWebViewClient = loadWebview();
break;
case SUCCESS:
mCreationSucceeded = true;
mNewSiteLocalId = (Integer) event.getPayload();
setModalMode(false);

if (mPreviewWebViewClient == null) {
// Apparently view got rotated while at the final so, just reconfigure the WebView.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.wordpress.android.ui.accounts.signup;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -77,7 +79,13 @@ public void afterTextChanged(final Editable editable) {
mDebouncer.debounce(Void.class, new Runnable() {
@Override
public void run() {
mOnAdapterListener.onKeywordsChange(text);
// post the action on an properly initialised Handler so UI thread operations can succeed
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mOnAdapterListener.onKeywordsChange(text);
}
});
}
}, GET_SUGGESTIONS_INTERVAL_MS, TimeUnit.MILLISECONDS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.wordpress.android.fluxc.store.SiteStore.OnSuggestedDomains;
import org.wordpress.android.fluxc.store.SiteStore.SuggestDomainsPayload;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.NetworkUtils;

import javax.inject.Inject;

Expand Down Expand Up @@ -72,6 +73,9 @@ public void onDestroy() {
}

public void load(String keywords) {
// notify if no connectivity but continue anyway
NetworkUtils.checkConnection(getActivity());

postUpdate(new DomainSuggestionEvent(DomainUpdateStep.UPDATING, keywords, null));

SuggestDomainsPayload payload = new SuggestDomainsPayload(keywords, true, true, false, 20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ public enum SiteCreationStep {
}

public static class SiteCreationState implements AutoForeground.ServiceState {
private final SiteCreationStep mStep;
private @NonNull final SiteCreationStep mStep;
private final Object payload;

SiteCreationState(@NonNull SiteCreationStep step, @Nullable Object payload) {
this.mStep = step;
this.payload = payload;
}

@NonNull
SiteCreationStep getStep() {
return mStep;
}
Expand Down Expand Up @@ -109,6 +110,10 @@ public boolean isTerminal() {
public String getStepName() {
return mStep.name();
}

boolean isAfterCreation() {
return mStep.ordinal() > SiteCreationStep.NEW_SITE.ordinal();
}
}

private static class SiteCreationNotification {
Expand Down Expand Up @@ -183,6 +188,10 @@ public static void clearSiteCreationServiceState() {
clearServiceState(SiteCreationState.class);
}

public static SiteCreationState getState() {
return getState(SiteCreationState.class);
}

public SiteCreationService() {
super(new SiteCreationState(SiteCreationStep.IDLE, null));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.ui.accounts.signup;

import android.content.Context;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -21,15 +22,18 @@ public class SiteCreationThemeAdapter extends RecyclerView.Adapter<RecyclerView.

private boolean mIsLoading;
private List<ThemeModel> mThemes;
private @StringRes int mErrorMessage;
private SiteCreationListener mSiteCreationListener;

public static class HeaderViewHolder extends RecyclerView.ViewHolder {
public final View progressContainer;
public final View progress;
public final TextView label;

HeaderViewHolder(View itemView) {
super(itemView);
this.progress = itemView.findViewById(R.id.progress_container);
this.progressContainer = itemView.findViewById(R.id.progress_container);
this.progress = itemView.findViewById(R.id.progress_bar);
this.label = (TextView) itemView.findViewById(R.id.progress_label);
}
}
Expand All @@ -52,9 +56,10 @@ public SiteCreationThemeAdapter(Context context, SiteCreationListener siteCreati
mSiteCreationListener = siteCreationListener;
}

public void setData(boolean isLoading, List<ThemeModel> themes) {
public void setData(boolean isLoading, List<ThemeModel> themes, @StringRes int errorMessage) {
mIsLoading = isLoading;
mThemes = themes;
mErrorMessage = errorMessage;
notifyDataSetChanged();
}

Expand All @@ -77,10 +82,13 @@ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

if (viewType == VIEW_TYPE_HEADER) {
final HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
headerViewHolder.progressContainer.setVisibility(mIsLoading || mThemes == null ? View.VISIBLE : View.GONE);
headerViewHolder.progress.setVisibility(mIsLoading ? View.VISIBLE : View.GONE);
if (!mIsLoading && mThemes == null) {
// this is an error situation so, show an error
headerViewHolder.label.setText(R.string.error_generic);
headerViewHolder.label.setText(mErrorMessage);
} else {
headerViewHolder.label.setText(null);
}
} else {
final ThemeModel theme = getItem(position);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,16 @@ public void onThemeLoadingUpdated(OnThemeLoadingUpdated event) {

switch (event.getPhase()) {
case UPDATING:
mSiteCreationThemeAdapter.setData(true, null);
mSiteCreationThemeAdapter.setData(true, null, 0);
break;
case ERROR:
mSiteCreationThemeAdapter.setData(false, null);
mSiteCreationThemeAdapter.setData(false, null, R.string.error_generic);
break;
case ERROR_NO_CONNECTIVITY:
mSiteCreationThemeAdapter.setData(false, null, R.string.error_generic_network);
break;
case FINISHED:
mSiteCreationThemeAdapter.setData(false, getThemes());
mSiteCreationThemeAdapter.setData(false, getThemes(), 0);
break;
}
}
Expand Down
Loading