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

Commit

Permalink
Merge branch 'feature/form_of_forms'
Browse files Browse the repository at this point in the history
  • Loading branch information
mlykotom committed Nov 14, 2019
2 parents 34c7cf2 + ce5ffe7 commit 92c4fa5
Show file tree
Hide file tree
Showing 51 changed files with 423 additions and 550 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

#### 1. Add gradle dependency
```groovy
implementation 'com.mlykotom:valifi:1.4.2'
implementation 'com.mlykotom:valifi:1.5.0'
```
#### 2. Setup project with data binding
``` groovy
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:3.5.2'
// Required plugins added to classpath to facilitate pushing to Jcenter/Bintray
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
Expand Down
4 changes: 2 additions & 2 deletions example-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'

if (isLocalBuild) {
implementation project(':valifi')
} else {
implementation 'com.mlykotom:valifi:1.4.2'
implementation 'com.mlykotom:valifi:1.5.0'
}
}
3 changes: 1 addition & 2 deletions example-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mlykotom.exampleandroid">

<application
Expand Down
3 changes: 1 addition & 2 deletions example-android/src/main/res/layout/activity_example.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
Expand Down
8 changes: 4 additions & 4 deletions example-arch-viewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'

implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"

debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'
testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.2'

if(isLocalBuild) {
if (isLocalBuild) {
implementation project(':valifi')
} else {
implementation 'com.mlykotom:valifi:1.4.2'
implementation 'com.mlykotom:valifi:1.5.0'
}
}
2 changes: 1 addition & 1 deletion example-arch-viewmodel/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<application
android:name=".ExampleViewModelApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
package com.mlykotom.example_arch_viewmodel;

import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.tabs.TabLayout;
import com.mlykotom.example_arch_viewmodel.form.FormValidationFragment;
import com.mlykotom.example_arch_viewmodel.manual.ManualValidationFragment;
import com.mlykotom.example_arch_viewmodel.single.SingleValidationFragment;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class ExampleViewModelActivity extends AppCompatActivity {
private PagesAdapter mAdapter;
private ViewPager mViewPager;
private TabLayout mTabLayout;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -29,14 +28,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
setupAdapter();
}


private void setupActionbar() {
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("ValiFi example");
setSupportActionBar(toolbar);
}


private void setupAdapter() {
mViewPager = findViewById(R.id.pager);
mTabLayout = findViewById(R.id.tabs);
Expand All @@ -47,19 +44,16 @@ private void setupAdapter() {
mTabLayout.setupWithViewPager(mViewPager);
}


private static class PagesAdapter extends FragmentStatePagerAdapter {
private static String[] sTitles = new String[]{"Single", "Form", "Manual"};


public PagesAdapter(FragmentManager fm) {
super(fm);
}


@Override
public Fragment getItem(int position) {
switch(position) {
switch (position) {
case 0:
default:
return SingleValidationFragment.newInstance();
Expand All @@ -72,13 +66,11 @@ public Fragment getItem(int position) {
}
}


@Override
public int getCount() {
return sTitles.length;
}


@Override
public CharSequence getPageTitle(int position) {
return sTitles[position];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mlykotom.valifi.ValiFi;
import com.squareup.leakcanary.LeakCanary;


public class ExampleViewModelApplication extends Application {
@Override
public void onCreate() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.mlykotom.example_arch_viewmodel;

import androidx.annotation.Nullable;

import com.mlykotom.valifi.fields.ValiFieldText;

import androidx.annotation.Nullable;

/**
* Custom validator that extends text validator
Expand All @@ -14,13 +13,11 @@ public MyValiFieldCaptcha() {
addMyValidator();
}


public MyValiFieldCaptcha(String defaultValue) {
super(defaultValue);
addMyValidator();
}


private void addMyValidator() {
addCustomValidator("Captcha must be correct", new PropertyValidator<String>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
package com.mlykotom.example_arch_viewmodel.form;

import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.mlykotom.example_arch_viewmodel.databinding.FragmentExampleFormBinding;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

public class FormValidationFragment extends Fragment implements FormValidationView {
private FragmentExampleFormBinding mBinding;
private FormValidationViewModel mViewModel;


public static FormValidationFragment newInstance() {
Bundle args = new Bundle();
FormValidationFragment fragment = new FormValidationFragment();
fragment.setArguments(args);
return fragment;
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(FormValidationViewModel.class);
}


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -42,13 +39,11 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return mBinding.getRoot();
}


@Override
public void onSubmitClicked() {
Toast.makeText(getContext(), "Submit clicked and form is valid!", Toast.LENGTH_LONG).show();
}


@Override
public void onResetClicked() {
mViewModel.email.reset();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mlykotom.example_arch_viewmodel.form;


public interface FormValidationView {
void onSubmitClicked();
void onResetClicked();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package com.mlykotom.example_arch_viewmodel.form;

import androidx.lifecycle.ViewModel;

import com.mlykotom.valifi.ValiFiForm;
import com.mlykotom.valifi.fields.ValiFieldEmail;
import com.mlykotom.valifi.fields.ValiFieldPassword;
import com.mlykotom.valifi.fields.ValiFieldPhone;

import androidx.lifecycle.ViewModel;

public class FormValidationViewModel extends ViewModel {
public final ValiFieldEmail email = new ValiFieldEmail();
public final ValiFieldPassword password = new ValiFieldPassword();
public final ValiFieldPassword password2 = new ValiFieldPassword();
public final ValiFieldPhone phone = new ValiFieldPhone();
public final ValiFiForm form = new ValiFiForm(email, password, password2, phone);

public final ValiFiForm passwordForm = new ValiFiForm(password, password2);
public final ValiFiForm form = new ValiFiForm(email, passwordForm, phone);

public FormValidationViewModel() {
phone.setEmptyAllowed(true);
password2.addVerifyFieldValidator("Passwords must be the same", password);
}


@Override
protected void onCleared() {
form.destroy();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.mlykotom.example_arch_viewmodel.manual;

import androidx.lifecycle.ViewModelProviders;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.mlykotom.example_arch_viewmodel.databinding.FragmentExampleManualBinding;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProviders;

public class ManualValidationFragment extends Fragment implements ManualValidationView {
private FragmentExampleManualBinding mBinding;
private ManualValidationViewModel mViewModel;


public static ManualValidationFragment newInstance() {

Bundle args = new Bundle();
Expand All @@ -27,14 +26,12 @@ public static ManualValidationFragment newInstance() {
return fragment;
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewModel = ViewModelProviders.of(this).get(ManualValidationViewModel.class);
}


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Expand All @@ -44,10 +41,9 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
return mBinding.getRoot();
}


@Override
public void onManualSubmitClicked() {
if(mViewModel.onManualSubmit()) {
if (mViewModel.onManualSubmit()) {
Toast.makeText(getContext(), "Form is valid!", Toast.LENGTH_LONG).show();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mlykotom.example_arch_viewmodel.manual;


public interface ManualValidationView {
void onManualSubmitClicked();
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
package com.mlykotom.example_arch_viewmodel.manual;

import androidx.lifecycle.ViewModel;

import com.mlykotom.example_arch_viewmodel.MyValiFieldCaptcha;
import com.mlykotom.valifi.ValiFiErrorDelay;
import com.mlykotom.valifi.ValiFiForm;
import com.mlykotom.valifi.fields.ValiFieldEmail;

import androidx.lifecycle.ViewModel;

public class ManualValidationViewModel extends ViewModel {
public final ValiFieldEmail email = new ValiFieldEmail();
public final MyValiFieldCaptcha captcha = new MyValiFieldCaptcha();
public final ValiFiForm form = new ValiFiForm(email, captcha);


public ManualValidationViewModel() {
email.setErrorDelay(ValiFiErrorDelay.NEVER);
}


@Override
protected void onCleared() {
form.destroy();
super.onCleared();
}


public boolean onManualSubmit() {
form.refreshError();

if(form.isValid()) {
if (form.isValid()) {
// do what you want after validation
return true;
}
Expand Down
Loading

0 comments on commit 92c4fa5

Please sign in to comment.