-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from wordpress-mobile/merge/WordPress-Android/…
…10255 Merge latest changes from WordPress-Android
- Loading branch information
Showing
15 changed files
with
244 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
WordPressLoginFlow/src/main/java/org/wordpress/android/login/LoginSiteAddressValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.