Skip to content

Commit

Permalink
添加用法示例
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBT committed Nov 12, 2016
1 parent 97a3072 commit 0635aa7
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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{
Expand All @@ -38,5 +36,7 @@ interface RConfig{

@NonNull
Observable compile();

void addUpdatable(@NonNull Updatable updatable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@ interface RTextWatcher{
RFrequency<Editable> afterChanged(@NonNull TextView textView);
}

interface RFrequency<TVal>{
interface RFrequency<TVal> extends RConfig<TVal>{

@NonNull
RConfig<TVal> onUpdatesPer(int millis);

@NonNull
RConfig<TVal> onUpdatesPerLoop();
}

interface RConfig<TVal>{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
* 创 建 人: 陈志鹏
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
89 changes: 62 additions & 27 deletions app/src/main/java/xyz/zpayh/agerabinding/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,83 @@
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<Editable> mTextChangedRepository;

Updatable mTextChangedUpdatable = new Updatable() {
@Override
public void update() {
mTextView.setText(mTextChangedRepository.get());
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
}
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
tools:showIn="@layout/activity_main">

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>

<EditText
android:layout_below="@+id/text"
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>

0 comments on commit 0635aa7

Please sign in to comment.