Skip to content

Commit

Permalink
Merge pull request #24 from wordpress-mobile/merge/WordPress-Android/…
Browse files Browse the repository at this point in the history
…10255

Merge latest changes from WordPress-Android
  • Loading branch information
shiki authored Jul 23, 2019
2 parents 1c6c2f3 + a5a8a54 commit efe31c7
Show file tree
Hide file tree
Showing 15 changed files with 244 additions and 63 deletions.
10 changes: 8 additions & 2 deletions WordPressLoginFlow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
minSdkVersion 17
targetSdkVersion 28
versionCode 2
versionName "1.1"

Expand Down Expand Up @@ -68,6 +68,12 @@ dependencies {
annotationProcessor 'com.google.dagger:dagger-android-processor:2.22.1'

lintChecks 'org.wordpress:lint:1.0.1'

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.27.0'
testImplementation 'androidx.arch.core:core-testing:2.0.1'
testImplementation 'org.robolectric:robolectric:3.6.1'
testImplementation 'org.assertj:assertj-core:3.11.1'
}

// Add properties named "wp.xxx" to our BuildConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Patterns;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
Expand All @@ -18,6 +16,7 @@
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
Expand Down Expand Up @@ -57,6 +56,8 @@ public class LoginSiteAddressFragment extends LoginBaseFormFragment<LoginListene

private String mRequestedSiteAddress;

private LoginSiteAddressValidator mLoginSiteAddressValidator;

@Inject AccountStore mAccountStore;
@Inject Dispatcher mDispatcher;
@Inject HTTPAuthManager mHTTPAuthManager;
Expand Down Expand Up @@ -139,6 +140,23 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
} else {
mAnalyticsListener.trackUrlFormViewed();
}

mLoginSiteAddressValidator = new LoginSiteAddressValidator();

mLoginSiteAddressValidator.getIsValid().observe(this, new Observer<Boolean>() {
@Override public void onChanged(Boolean enabled) {
getPrimaryButton().setEnabled(enabled);
}
});
mLoginSiteAddressValidator.getErrorMessageResId().observe(this, new Observer<Integer>() {
@Override public void onChanged(Integer resId) {
if (resId != null) {
showError(resId);
} else {
mSiteAddressInput.setError(null);
}
}
});
}

@Override
Expand All @@ -148,24 +166,17 @@ public void onSaveInstanceState(Bundle outState) {
outState.putString(KEY_REQUESTED_SITE_ADDRESS, mRequestedSiteAddress);
}

@Override public void onDestroyView() {
super.onDestroyView();
mLoginSiteAddressValidator.dispose();
}

protected void discover() {
if (!NetworkUtils.checkConnection(getActivity())) {
return;
}

String cleanedSiteAddress = getCleanedSiteAddress();

if (TextUtils.isEmpty(cleanedSiteAddress)) {
showError(R.string.login_empty_site_url);
return;
}

if (!Patterns.WEB_URL.matcher(cleanedSiteAddress).matches()) {
showError(R.string.login_invalid_site_url);
return;
}

mRequestedSiteAddress = cleanedSiteAddress;
mRequestedSiteAddress = mLoginSiteAddressValidator.getCleanedSiteAddress();

String cleanedXmlrpcSuffix = UrlUtils.removeXmlrpcSuffix(mRequestedSiteAddress);

Expand All @@ -182,17 +193,16 @@ protected void discover() {
startProgress();
}

private String getCleanedSiteAddress() {
return EditTextUtils.getText(mSiteAddressInput.getEditText()).trim().replaceAll("[\r\n]", "");
}

@Override
public void onEditorCommit() {
discover();
if (getPrimaryButton().isEnabled()) {
discover();
}
}

@Override
public void afterTextChanged(Editable s) {
mLoginSiteAddressValidator.setAddress(EditTextUtils.getText(mSiteAddressInput.getEditText()));
}

@Override
Expand Down Expand Up @@ -359,7 +369,7 @@ public void onDiscoverySucceeded(OnDiscoveryResponse event) {
return;
} else {
AppLog.e(T.API, "onDiscoveryResponse has error: " + event.error.name()
+ " - " + event.error.toString());
+ " - " + event.error.toString());
handleDiscoveryError(event.error, event.failedEndpoint);
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.wordpress.android.login;

import android.util.Patterns;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import org.wordpress.android.util.helpers.Debouncer;

import java.util.concurrent.TimeUnit;

/**
* Encapsulates the site address validation, cleaning, and error reporting of {@link LoginSiteAddressFragment}.
*/
class LoginSiteAddressValidator {
private static final int SECONDS_DELAY_BEFORE_SHOWING_ERROR_MESSAGE = 2;

private MutableLiveData<Boolean> mIsValid = new MutableLiveData<>();
private MutableLiveData<Integer> mErrorMessageResId = new MutableLiveData<>();

private String mCleanedSiteAddress = "";
private final Debouncer mDebouncer;

@NonNull LiveData<Boolean> getIsValid() {
return mIsValid;
}

@NonNull LiveData<Integer> getErrorMessageResId() {
return mErrorMessageResId;
}

@NonNull String getCleanedSiteAddress() {
return mCleanedSiteAddress;
}

LoginSiteAddressValidator() {
this(new Debouncer());
}

LoginSiteAddressValidator(@NonNull Debouncer debouncer) {
mIsValid.setValue(false);
mDebouncer = debouncer;
}

void dispose() {
mDebouncer.shutdown();
}

void setAddress(@NonNull String siteAddress) {
mCleanedSiteAddress = cleanSiteAddress(siteAddress);
final boolean isValid = siteAddressIsValid(mCleanedSiteAddress);

mIsValid.setValue(isValid);
mErrorMessageResId.setValue(null);

// Call debounce regardless if there was an error so that the previous Runnable will be cancelled.
mDebouncer.debounce(Void.class, new Runnable() {
@Override public void run() {
if (!isValid && !mCleanedSiteAddress.isEmpty()) {
mErrorMessageResId.postValue(R.string.login_invalid_site_url);
}
}
}, SECONDS_DELAY_BEFORE_SHOWING_ERROR_MESSAGE, TimeUnit.SECONDS);
}

private static String cleanSiteAddress(@NonNull String siteAddress) {
return siteAddress.trim().replaceAll("[\r\n]", "");
}

private static boolean siteAddressIsValid(@NonNull String cleanedSiteAddress) {
return Patterns.WEB_URL.matcher(cleanedSiteAddress).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/login_background_color"
android:paddingLeft="@dimen/margin_small_medium"
android:paddingStart="@dimen/margin_small_medium"
android:paddingRight="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:paddingTop="@dimen/margin_medium_large"
android:paddingBottom="@dimen/margin_medium_large"
Expand All @@ -65,15 +63,12 @@
android:id="@+id/login_enter_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/login_request_magic_link"
android:layout_toStartOf="@+id/login_request_magic_link"
android:paddingLeft="@dimen/margin_medium_large"
android:paddingStart="@dimen/margin_medium_large"
android:paddingRight="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:layout_marginRight="@dimen/margin_extra_large"
android:layout_marginEnd="@dimen/margin_extra_large"
android:gravity="start|center_vertical"
android:text="@string/enter_your_password_instead"/>
Expand All @@ -83,7 +78,6 @@
android:id="@+id/login_request_magic_link"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="@string/send_link" />
</RelativeLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/login_background_color"
android:paddingLeft="@dimen/margin_small_medium"
android:paddingStart="@dimen/margin_small_medium"
android:paddingRight="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:paddingTop="@dimen/margin_medium_large"
android:paddingBottom="@dimen/margin_medium_large"
Expand All @@ -54,15 +52,12 @@
android:id="@+id/login_enter_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/login_open_email_client"
android:layout_toStartOf="@+id/login_open_email_client"
android:paddingLeft="@dimen/margin_medium_large"
android:paddingStart="@dimen/margin_medium_large"
android:paddingRight="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:layout_marginRight="@dimen/margin_extra_large"
android:layout_marginEnd="@dimen/margin_extra_large"
android:gravity="start|center_vertical"
android:text="@string/enter_your_password_instead"/>
Expand All @@ -72,7 +67,6 @@
android:id="@+id/login_open_email_client"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="@string/open_mail" />
</RelativeLayout>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
android:id="@+id/signup_email"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:layout_weight="1"
android:layout_width="wrap_content"
android:text="@string/signup_with_email_button"
Expand All @@ -45,7 +44,6 @@
android:id="@+id/signup_google"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/margin_medium"
android:layout_marginLeft="@dimen/margin_medium"
android:layout_weight="1"
android:layout_width="wrap_content"
android:text="@string/signup_with_google_button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@
android:layout_width="match_parent"
android:paddingBottom="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:paddingLeft="@dimen/margin_small_medium"
android:paddingRight="@dimen/margin_medium_large"
android:paddingStart="@dimen/margin_small_medium"
android:paddingTop="@dimen/margin_medium_large"
tools:ignore="InconsistentLayout">

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/signup_magic_link_button"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/open_mail"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="@dimen/margin_extra_large"
android:layout_marginStart="@dimen/margin_extra_large"
android:focusable="true">

Expand Down
6 changes: 0 additions & 6 deletions WordPressLoginFlow/src/main/res/layout/login_email_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingEnd="@dimen/margin_extra_large"
android:paddingLeft="@dimen/margin_extra_large"
android:paddingRight="@dimen/margin_extra_large"
android:paddingStart="@dimen/margin_extra_large" >

<TextView
Expand Down Expand Up @@ -59,7 +57,6 @@
android:orientation="horizontal"
android:paddingBottom="@dimen/margin_medium"
android:paddingEnd="@dimen/margin_medium"
android:paddingRight="@dimen/margin_medium"
android:paddingTop="@dimen/margin_medium"
android:gravity="center_vertical"
tools:ignore="RtlSymmetry" >
Expand All @@ -68,7 +65,6 @@
android:importantForAccessibility="no"
android:layout_height="@dimen/google_button_icon_sz"
android:layout_marginEnd="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:layout_width="@dimen/google_button_icon_sz"
app:srcCompat="@drawable/ic_google_60dp" >
</ImageView>
Expand All @@ -94,7 +90,6 @@
android:orientation="horizontal"
android:paddingBottom="@dimen/margin_medium"
android:paddingEnd="@dimen/margin_medium"
android:paddingRight="@dimen/margin_medium"
android:paddingTop="@dimen/margin_medium"
android:gravity="center_vertical"
tools:ignore="RtlSymmetry" >
Expand All @@ -104,7 +99,6 @@
android:importantForAccessibility="no"
android:layout_height="@dimen/google_button_icon_sz"
android:layout_marginEnd="@dimen/margin_medium"
android:layout_marginRight="@dimen/margin_medium"
android:layout_width="@dimen/google_button_icon_sz"
app:srcCompat="@drawable/ic_domains_grey_24dp" >
</ImageView>
Expand Down
6 changes: 0 additions & 6 deletions WordPressLoginFlow/src/main/res/layout/login_form_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/login_background_color"
android:paddingLeft="@dimen/margin_small_medium"
android:paddingStart="@dimen/margin_small_medium"
android:paddingRight="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:paddingTop="@dimen/margin_medium_large"
android:paddingBottom="@dimen/margin_medium_large"
Expand All @@ -42,15 +40,12 @@
android:id="@+id/secondary_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/primary_button"
android:layout_toStartOf="@+id/primary_button"
android:paddingLeft="@dimen/margin_medium_large"
android:paddingStart="@dimen/margin_medium_large"
android:paddingRight="@dimen/margin_medium_large"
android:paddingEnd="@dimen/margin_medium_large"
android:layout_marginRight="@dimen/margin_extra_large"
android:layout_marginEnd="@dimen/margin_extra_large"
android:textAlignment="viewStart"
android:gravity="start|center_vertical"
Expand All @@ -61,7 +56,6 @@
android:id="@+id/primary_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:text="@string/next" />
</RelativeLayout>
Expand Down
Loading

0 comments on commit efe31c7

Please sign in to comment.