diff --git a/ageraBinding/src/main/java/xyz/zpayh/ageraView/AgeraViews.java b/ageraBinding/src/main/java/xyz/zpayh/ageraView/AgeraViews.java index 67d1e98..0a88c35 100644 --- a/ageraBinding/src/main/java/xyz/zpayh/ageraView/AgeraViews.java +++ b/ageraBinding/src/main/java/xyz/zpayh/ageraView/AgeraViews.java @@ -1,5 +1,8 @@ package xyz.zpayh.ageraView; +import android.support.annotation.NonNull; +import android.view.View; + import xyz.zpayh.ageraView.click.ClickCompiler; import xyz.zpayh.ageraView.click.ClickCompilerStates; import xyz.zpayh.ageraView.textwatcher.TextWatcherCompiler; @@ -16,8 +19,8 @@ public final class AgeraViews { - public static ClickCompilerStates.RClickView compilerClick(){ - return ClickCompiler.compiler(); + public static ClickCompilerStates.RFrequency click(@NonNull View view){ + return ClickCompiler.compiler().click(view); } public static TextWatcherCompilerStates.RTextWatcher compilerTextWatcher(){ diff --git a/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompiler.java b/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompiler.java index 3089344..4405375 100644 --- a/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompiler.java +++ b/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompiler.java @@ -5,10 +5,13 @@ import android.view.View; import com.google.android.agera.Observable; -import com.google.android.agera.Preconditions; +import com.google.android.agera.Updatable; import java.util.concurrent.Executor; +import static com.google.android.agera.Preconditions.checkNotNull; +import static com.google.android.agera.Preconditions.checkState; + /** * Created by Administrator on 2016/10/24. */ @@ -23,7 +26,7 @@ public final class ClickCompiler implements @NonNull public static ClickCompilerStates.RClickView compiler(){ - Preconditions.checkNotNull(Looper.myLooper()); + checkState(Looper.myLooper() != null, "Can only be compiler on a Looper thread"); ClickCompiler compiler = compilers.get(); if (compiler == null){ compiler = new ClickCompiler(); @@ -37,6 +40,12 @@ private static void recycle(@NonNull final ClickCompiler compiler){ compilers.set(compiler); } + private ClickCompiler(){ + this.view = null; + this.frequency = 0; + this.executor = null; + } + private int frequency; private View view; @@ -45,43 +54,43 @@ private static void recycle(@NonNull final ClickCompiler compiler){ @NonNull @Override - public ClickCompilerStates.RFrequency click(@NonNull View view) { - this.view = Preconditions.checkNotNull(view); + public ClickCompiler click(@NonNull View view) { + this.view = checkNotNull(view); return this; } @NonNull @Override - public Observable compile() { - Observable observable = compileObservableAndReset(); - recycle(this); - return observable; - } - - private Observable compileObservableAndReset() { - Observable observable = new ClickObservable(view,executor,frequency); - view = null; - frequency = 0; - return observable; + public ClickCompiler goTo(@NonNull Executor executor) { + this.executor = checkNotNull(executor); + return this; } @NonNull @Override - public ClickCompilerStates.RConfig goTo(@NonNull Executor executor) { - this.executor = Preconditions.checkNotNull(executor); + public ClickCompiler onUpdatesPer(int millis) { + frequency = Math.max(0,millis); return this; } @NonNull @Override - public ClickCompilerStates.RFlow onUpdatesPer(int millis) { - frequency = Math.max(0,millis); - return this; + public Observable compile() { + Observable observable = compileObservableAndReset(); + recycle(this); + return observable; } - @NonNull @Override - public ClickCompilerStates.RFlow onUpdatesPerLoop() { - return onUpdatesPer(0); + public void addUpdatable(@NonNull Updatable updatable) { + Observable observable = compile(); + observable.addUpdatable(updatable); + } + + private Observable compileObservableAndReset() { + Observable observable = new ClickObservable(view,executor,frequency); + view = null; + frequency = 0; + return observable; } } diff --git a/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompilerStates.java b/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompilerStates.java index 31cbd96..4a7b0f6 100644 --- a/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompilerStates.java +++ b/ageraBinding/src/main/java/xyz/zpayh/ageraView/click/ClickCompilerStates.java @@ -4,6 +4,7 @@ import android.view.View; import com.google.android.agera.Observable; +import com.google.android.agera.Updatable; import java.util.concurrent.Executor; @@ -19,13 +20,10 @@ interface RClickView{ RFrequency click(@NonNull View view); } - interface RFrequency{ + interface RFrequency extends RFlow{ @NonNull RFlow onUpdatesPer(int millis); - - @NonNull - RFlow onUpdatesPerLoop(); } interface RFlow extends RConfig{ @@ -38,5 +36,7 @@ interface RConfig{ @NonNull Observable compile(); + + void addUpdatable(@NonNull Updatable updatable); } } diff --git a/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompiler.java b/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompiler.java index aaa082d..315ab85 100644 --- a/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompiler.java +++ b/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompiler.java @@ -7,6 +7,7 @@ import com.google.android.agera.Preconditions; import com.google.android.agera.Repository; +import static com.google.android.agera.Preconditions.checkState; import static xyz.zpayh.ageraView.textwatcher.TextWatcherConfig.AFTER_TEXT_CHANGED; import static xyz.zpayh.ageraView.textwatcher.TextWatcherConfig.BEFORE_TEXT_CHANGED; import static xyz.zpayh.ageraView.textwatcher.TextWatcherConfig.ON_TEXT_CHANGED; @@ -29,7 +30,7 @@ public final class TextWatcherCompiler implements @NonNull public static TextWatcherCompilerStates.RTextWatcher compiler(){ - Preconditions.checkNotNull(Looper.myLooper()); + checkState(Looper.myLooper() != null, "Can only be compiler on a Looper thread"); TextWatcherCompiler compiler = compilers.get(); if (compiler == null){ compiler = new TextWatcherCompiler(); @@ -43,12 +44,18 @@ private static void recycle(@NonNull final TextWatcherCompiler compiler){ compilers.set(compiler); } + private TextWatcherCompiler(){ + this.frequency = 0; + this.textView = null; + this.whenWatcher = AFTER_TEXT_CHANGED; + } + private int frequency; private TextView textView; @TextWatcherConfig - private int whenWatcher = AFTER_TEXT_CHANGED; + private int whenWatcher; @NonNull @Override @@ -73,12 +80,6 @@ public TextWatcherCompiler onUpdatesPer(int millis) { return this; } - @NonNull - @Override - public TextWatcherCompiler onUpdatesPerLoop() { - return onUpdatesPer(0); - } - @NonNull @Override public TextWatcherCompiler onChanged(@NonNull TextView textView) { diff --git a/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompilerStates.java b/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompilerStates.java index 746bd7c..65bcac3 100644 --- a/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompilerStates.java +++ b/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherCompilerStates.java @@ -29,13 +29,10 @@ interface RTextWatcher{ RFrequency afterChanged(@NonNull TextView textView); } - interface RFrequency{ + interface RFrequency extends RConfig{ @NonNull RConfig onUpdatesPer(int millis); - - @NonNull - RConfig onUpdatesPerLoop(); } interface RConfig{ diff --git a/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherRepository.java b/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherRepository.java index bf695d3..5dad258 100644 --- a/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherRepository.java +++ b/ageraBinding/src/main/java/xyz/zpayh/ageraView/textwatcher/TextWatcherRepository.java @@ -8,10 +8,11 @@ import com.google.android.agera.BaseObservable; import com.google.android.agera.Observable; import com.google.android.agera.Observables; -import com.google.android.agera.Preconditions; import com.google.android.agera.Repository; import com.google.android.agera.Updatable; +import static com.google.android.agera.Preconditions.checkNotNull; + /** * 文 件 名: TextWatcherRepository * 创 建 人: 陈志鹏 @@ -88,7 +89,7 @@ private final class TextWatcherObservable extends BaseObservable{ private final TextWatcher textWatcher; TextWatcherObservable(@NonNull TextView textView, int config) { - this.textView = Preconditions.checkNotNull(textView); + this.textView = checkNotNull(textView); this.config = config; textWatcher = new TextWatcher() { diff --git a/app/build.gradle b/app/build.gradle index bcebcdc..d46986a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,11 +20,13 @@ android { } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:design:25.0.0' testCompile 'junit:junit:4.12' + compile project(':ageraBinding') + compile 'com.google.android.agera:agera:1.2.0-beta3' } diff --git a/app/src/main/java/xyz/zpayh/agerabinding/MainActivity.java b/app/src/main/java/xyz/zpayh/agerabinding/MainActivity.java index 4ff0517..1ff5e46 100644 --- a/app/src/main/java/xyz/zpayh/agerabinding/MainActivity.java +++ b/app/src/main/java/xyz/zpayh/agerabinding/MainActivity.java @@ -5,12 +5,43 @@ import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.view.View; -import android.view.Menu; -import android.view.MenuItem; +import android.text.Editable; +import android.widget.EditText; +import android.widget.TextView; + +import com.google.android.agera.Observable; +import com.google.android.agera.Repository; +import com.google.android.agera.Updatable; + +import xyz.zpayh.ageraView.AgeraViews; public class MainActivity extends AppCompatActivity { + private EditText mEditText; + + private TextView mTextView; + + private FloatingActionButton fab; + + Observable mClickObservable; + + Updatable mClickUpdatable = new Updatable() { + @Override + public void update() { + Snackbar.make(fab, "Replace with your own action", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + } + }; + + Repository mTextChangedRepository; + + Updatable mTextChangedUpdatable = new Updatable() { + @Override + public void update() { + mTextView.setText(mTextChangedRepository.get()); + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -18,35 +49,39 @@ protected void onCreate(Bundle savedInstanceState) { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); - FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - } - }); + fab = (FloatingActionButton) findViewById(R.id.fab); + + mClickObservable = AgeraViews.click(fab) + //.onUpdatesPer(2000)//可指定点击间隔,防止点击过快引起的多次响应 + //.goTo(Executors.newSingleThreadExecutor())//可指定Updatable在其他线程执行 + .compile();//生成Observable + + mEditText = (EditText) findViewById(R.id.et_input); + mTextView = (TextView) findViewById(R.id.text); + + mTextChangedRepository = AgeraViews.compilerTextWatcher() + .afterChanged(mEditText) + //.onUpdatesPer(2000)//可设置刷新频率 + .compile(); } @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; + protected void onStart() { + super.onStart(); + //一般注册完要在对应的生命周期解除注册 + mTextChangedRepository.addUpdatable(mTextChangedUpdatable); + //同一个Updatable只能注册一次,多次注册会报错 + //mTextChangedRepository.addUpdatable(mTextChangedUpdatable); + mClickObservable.addUpdatable(mClickUpdatable); } @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_settings) { - return true; - } - - return super.onOptionsItemSelected(item); + protected void onStop() { + super.onStop(); + //一般注册完要在对应的生命周期解除注册 + mTextChangedRepository.removeUpdatable(mTextChangedUpdatable); + //解除一个没有订阅事件的Updatable会报错 + //mTextChangedRepository.removeUpdatable(mTextChangedUpdatable); + mClickObservable.removeUpdatable(mClickUpdatable); } } diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 01bfce1..707683c 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -15,7 +15,14 @@ tools:showIn="@layout/activity_main"> + +