diff --git a/app/build.gradle b/app/build.gradle index 14a8b03..1816858 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java b/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java index 59e45cb..8fb7511 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/EditFragment.java @@ -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) { - updatePreview(mContext); - } - - @Override - public void afterTextChanged(Editable editable) { - } + Observable 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); }); + return view; } diff --git a/build.gradle b/build.gradle index ac667d0..5dde61a 100644 --- a/build.gradle +++ b/build.gradle @@ -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 }