Replaced EditText onTextChangedListener with RxJava implementation

This commit is contained in:
William Brawner 2017-08-07 17:54:18 -05:00
parent fcba8979f6
commit 68b994feb4
3 changed files with 25 additions and 14 deletions

View file

@ -1,4 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
packagingOptions {
@ -10,6 +11,10 @@ android {
}
compileSdkVersion 26
buildToolsVersion "26.0.0"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.wbrawner.simplemarkdown"
minSdkVersion 15
@ -40,6 +45,10 @@ dependencies {
compile 'com.jakewharton:butterknife:8.7.0'
compile 'com.android.support:support-v4:26.+'
compile 'com.commonsware.cwac:anddown:0.3.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.0'
compile 'com.jakewharton.rxbinding2:rxbinding:2.0.0'
compile 'com.jakewharton.rxbinding2:rxbinding-design:2.0.0'
testCompile 'junit:junit:4.12'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.7.0'
}

View file

@ -23,13 +23,20 @@ import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;
import com.jakewharton.rxbinding2.widget.RxTextView;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import static android.content.ContentValues.TAG;
@ -77,20 +84,15 @@ public class EditFragment extends Fragment {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
mMarkdownEditor.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Observable<String> obs = RxTextView.textChanges(mMarkdownEditor)
.debounce(50, TimeUnit.MILLISECONDS).map(editable -> editable.toString());
obs.subscribeOn(Schedulers.io());
obs.observeOn(AndroidSchedulers.mainThread());
obs.subscribe(string -> {
Log.d(TAG, "debounced " + string);
updatePreview(mContext);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
return view;
}

View file

@ -6,7 +6,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}