Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from mlykotom/develop
Browse files Browse the repository at this point in the history
Add delayed error + stable 1.0.0!
  • Loading branch information
mlykotom authored Jan 25, 2017
2 parents a58e55c + c901af3 commit d95bf8b
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 125 deletions.
9 changes: 7 additions & 2 deletions example-viewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,10 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'eu.inloop:androidviewmodel:1.2.3'
compile 'com.mlykotom:valifi:0.3.1'
}

if(project.file('../local.properties').exists()) {
compile project(':valifi')
} else {
compile 'com.mlykotom:valifi:0.3.1'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


public class ExampleViewModel extends AbstractViewModel<ExampleView> {
public final ValiFieldEmail email = new ValiFieldEmail("this is not email :(");
public final ValiFieldEmail email = new ValiFieldEmail();
public final ValiFieldUsername username = new ValiFieldUsername();
public final ValiFieldPassword password = new ValiFieldPassword();
public final ValiFieldPassword password2 = new ValiFieldPassword();
Expand Down
6 changes: 4 additions & 2 deletions example-viewmodel/src/main/res/layout/fragment_example.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
app:passwordToggleEnabled="true"
app:error="@{viewModel.password.error}">

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:inputType="textPassword"
android:text="@={viewModel.password.value}"
android:hint="Password" />
</android.support.design.widget.TextInputLayout>
Expand All @@ -70,12 +71,13 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true"
app:passwordToggleEnabled="true"
app:error="@{viewModel.password2.error}">

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:inputType="textPassword"
android:text="@={viewModel.password2.value}"
android:hint="Verify password" />
</android.support.design.widget.TextInputLayout>
Expand Down
6 changes: 3 additions & 3 deletions valifi/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
final VERSION_MAJOR = 0 // max two digits
final VERSION_MINOR = 3 // max two digits
final VERSION_PATCH = 1 // max two digits
final VERSION_MAJOR = 1 // max two digits
final VERSION_MINOR = 0 // max two digits
final VERSION_PATCH = 0 // max two digits
final VERSION_BUILD = 0 // max three digits

apply plugin: 'com.android.library'
Expand Down
27 changes: 21 additions & 6 deletions valifi/src/main/java/com/mlykotom/valifi/ValiFi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.util.regex.Pattern;


@SuppressWarnings("unused")
public class ValiFi {
static String TAG = ValiFi.class.getSimpleName();
@SuppressLint("StaticFieldLeak")
private static ValiFi ourInstance;
private final Context mAppContext;
Expand Down Expand Up @@ -49,9 +51,6 @@ public static void install(Application appContext) {
}


// ------ Might be used for ValiFieldText


static int getErrorRes(@Builder.ValiFiErrorResource int field) {
return getInstance().mParameters.mErrorResources[field];
}
Expand All @@ -62,6 +61,11 @@ static Pattern getPattern(@Builder.ValiFiPattern int field) {
}


static long getErrorDelay() {
return getInstance().mParameters.mErrorDelay;
}


static Context getContext() {
if(getInstance().mAppContext == null) {
throw new ValiFiException("ValiFi was installed without Context!");
Expand All @@ -86,11 +90,13 @@ private static ValiFi getInstance() {
public static class ValiFiConfig {
@StringRes final int[] mErrorResources;
final Pattern mPatterns[];
final long mErrorDelay;


ValiFiConfig(Pattern[] patterns, @StringRes int[] errorResources) {
ValiFiConfig(Pattern[] patterns, @StringRes int[] errorResources, long errorDelay) {
mPatterns = patterns;
mErrorResources = errorResources;
mErrorDelay = errorDelay;
}
}

Expand Down Expand Up @@ -121,8 +127,12 @@ public static class Builder {
public static final int PATTERN_PASSWORD = 2;
public static final int PATTERN_USERNAME = 3;
public static final int PATTERN_COUNT = PATTERN_USERNAME + 1;
// ----- other
private static final long DEFAULT_ERROR_DELAY_MILLIS = 500;

private Pattern[] mPatterns;
private int[] mErrorResources;
private long mErrorDelay = DEFAULT_ERROR_DELAY_MILLIS;


@IntDef({
Expand Down Expand Up @@ -187,13 +197,18 @@ public Builder setPattern(@ValiFiPattern int field, Pattern value) {
}


public Builder setErrorDelay(long millis) {
mErrorDelay = millis;
return this;
}


public ValiFiConfig build() {
return new ValiFiConfig(mPatterns, mErrorResources);
return new ValiFiConfig(mPatterns, mErrorResources, mErrorDelay);
}


private void setupPatterns() {
// TODO maybe use Patterns#EMAIL_ADDRESS ?
mPatterns[PATTERN_EMAIL] = Pattern.compile("^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
mPatterns[PATTERN_PHONE] = Pattern.compile("^\\+420 ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$" + "|" + "^(\\+?1)?[2-9]\\d{2}[2-9](?!11)\\d{6}$"); // phone czech | phone en-US
mPatterns[PATTERN_USERNAME] = Pattern.compile(".{4,}");
Expand Down
4 changes: 2 additions & 2 deletions valifi/src/main/java/com/mlykotom/valifi/ValiFiForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public void destroy() {
/**
* Field validation was changed and informs this form about it
*
* @param field which was changed
* @param field which was changed (ignored and handled by observable callback)
*/
void fieldValidationChanged(ValiFieldBase field) {
void notifyValidationChanged(ValiFieldBase field) {
notifyPropertyChanged(com.mlykotom.valifi.BR.isValid);
}
}
Loading

0 comments on commit d95bf8b

Please sign in to comment.