Skip to content

Commit 0875aac

Browse files
committed
add polling and debounce edit text
1 parent d04788e commit 0875aac

11 files changed

+540
-10
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
android:name=".Operators2Activity"
3232
android:label="Operators"
3333
android:launchMode="singleTop"/>
34+
<activity
35+
android:name=".PollingActivity"
36+
android:label="Polling"
37+
android:launchMode="singleTop"/>
38+
<activity
39+
android:name=".DebounceEditTextActivity"
40+
android:label="Polling"
41+
android:launchMode="singleTop"/>
3442
</application>
3543

3644
</manifest>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package com.demo.maat.hello_rxjava;
19+
20+
import android.os.Bundle;
21+
import android.support.v4.app.FragmentTransaction;
22+
23+
import com.demo.maat.hello_rxjava.common.activities.SampleActivityBase;
24+
import com.demo.maat.hello_rxjava.common.logger.Log;
25+
import com.demo.maat.hello_rxjava.common.logger.LogFragment;
26+
import com.demo.maat.hello_rxjava.common.logger.LogWrapper;
27+
import com.demo.maat.hello_rxjava.common.logger.MessageOnlyLogFilter;
28+
29+
30+
public class DebounceEditTextActivity extends SampleActivityBase {
31+
32+
public static final String TAG = "DebounceEditTextActivity";
33+
34+
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.debounce_activity_main);
39+
40+
if (savedInstanceState == null) {
41+
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
42+
DebounceEditTextFragment fragment = new DebounceEditTextFragment();
43+
transaction.replace(R.id.sample_content_fragment, fragment);
44+
transaction.commit();
45+
}
46+
}
47+
48+
49+
50+
51+
@Override
52+
public void initializeLogging() {
53+
LogWrapper logWrapper = new LogWrapper();
54+
Log.setLogNode(logWrapper);
55+
56+
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
57+
logWrapper.setNext(msgFilter);
58+
59+
// On screen logging via a fragment with a TextView.
60+
LogFragment logFragment = (LogFragment) getSupportFragmentManager()
61+
.findFragmentById(R.id.log_fragment);
62+
msgFilter.setNext(logFragment.getLogView());
63+
64+
Log.i(TAG, "Ready");
65+
}
66+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.demo.maat.hello_rxjava;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v4.app.Fragment;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.widget.EditText;
10+
11+
import com.demo.maat.hello_rxjava.common.logger.Log;
12+
import com.jakewharton.rxbinding.widget.RxTextView;
13+
import com.jakewharton.rxbinding.widget.TextViewTextChangeEvent;
14+
15+
import java.util.concurrent.TimeUnit;
16+
17+
import butterknife.BindView;
18+
import butterknife.ButterKnife;
19+
import rx.Observer;
20+
import rx.Subscription;
21+
import rx.android.schedulers.AndroidSchedulers;
22+
import rx.subscriptions.CompositeSubscription;
23+
24+
25+
public class DebounceEditTextFragment extends Fragment {
26+
27+
28+
static final String TAG = "DebounceEditTextFragment";
29+
30+
@BindView(R.id.edit_search)
31+
EditText mEditSearch;
32+
int N=0;
33+
private Subscription subscribe;
34+
private CompositeSubscription mCompositeSubscription;
35+
;
36+
37+
@Override
38+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
39+
Bundle savedInstanceState) {
40+
View view = inflater.inflate(R.layout.debounce_fragment, container, false);
41+
ButterKnife.bind(this, view);
42+
mCompositeSubscription = new CompositeSubscription();
43+
return view;
44+
}
45+
46+
@Override
47+
public void onResume() {
48+
super.onResume();
49+
}
50+
51+
@Override
52+
public void onViewCreated(View view, Bundle savedInstanceState) {
53+
super.onViewCreated(view, savedInstanceState);
54+
55+
}
56+
57+
58+
59+
@Override
60+
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
61+
super.onActivityCreated(savedInstanceState);
62+
subscribe= RxTextView.textChangeEvents(mEditSearch)//
63+
.debounce(400, TimeUnit.MILLISECONDS)// default Scheduler is Computation
64+
.observeOn(AndroidSchedulers.mainThread())//
65+
.subscribe(new Observer<TextViewTextChangeEvent>() {
66+
@Override
67+
public void onCompleted() {
68+
printLog("Completed");
69+
}
70+
71+
@Override
72+
public void onError(Throwable e) {
73+
printLog("Error");
74+
}
75+
76+
@Override
77+
public void onNext(TextViewTextChangeEvent onTextChangeEvent) {
78+
printLog(onTextChangeEvent.text().toString());
79+
}
80+
}
81+
);
82+
mCompositeSubscription.add(subscribe);
83+
}
84+
85+
private void printLog(String s) {
86+
Log.i(TAG, s);
87+
}
88+
89+
@Override
90+
public void onDestroy() {
91+
super.onDestroy();
92+
mCompositeSubscription.unsubscribe();
93+
}
94+
95+
96+
}
97+
98+
99+
100+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2013 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
package com.demo.maat.hello_rxjava;
19+
20+
import android.os.Bundle;
21+
import android.support.v4.app.FragmentTransaction;
22+
23+
import com.demo.maat.hello_rxjava.common.activities.SampleActivityBase;
24+
import com.demo.maat.hello_rxjava.common.logger.Log;
25+
import com.demo.maat.hello_rxjava.common.logger.LogFragment;
26+
import com.demo.maat.hello_rxjava.common.logger.LogWrapper;
27+
import com.demo.maat.hello_rxjava.common.logger.MessageOnlyLogFilter;
28+
29+
30+
public class PollingActivity extends SampleActivityBase {
31+
32+
public static final String TAG = "PollingActivity";
33+
34+
35+
@Override
36+
protected void onCreate(Bundle savedInstanceState) {
37+
super.onCreate(savedInstanceState);
38+
setContentView(R.layout.polling_activity_main);
39+
40+
if (savedInstanceState == null) {
41+
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
42+
PollingFragment fragment = new PollingFragment();
43+
transaction.replace(R.id.sample_content_fragment, fragment);
44+
transaction.commit();
45+
}
46+
}
47+
48+
49+
50+
51+
@Override
52+
public void initializeLogging() {
53+
LogWrapper logWrapper = new LogWrapper();
54+
Log.setLogNode(logWrapper);
55+
56+
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
57+
logWrapper.setNext(msgFilter);
58+
59+
// On screen logging via a fragment with a TextView.
60+
LogFragment logFragment = (LogFragment) getSupportFragmentManager()
61+
.findFragmentById(R.id.log_fragment);
62+
msgFilter.setNext(logFragment.getLogView());
63+
64+
Log.i(TAG, "Ready");
65+
}
66+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package com.demo.maat.hello_rxjava;
2+
3+
import android.os.Bundle;
4+
import android.support.v4.app.Fragment;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.Button;
9+
10+
import com.demo.maat.hello_rxjava.common.logger.Log;
11+
12+
import java.util.concurrent.TimeUnit;
13+
14+
import butterknife.BindView;
15+
import butterknife.ButterKnife;
16+
import butterknife.OnClick;
17+
import rx.Observable;
18+
import rx.Subscriber;
19+
import rx.Subscription;
20+
import rx.functions.Action0;
21+
import rx.functions.Action1;
22+
import rx.schedulers.Schedulers;
23+
import rx.subscriptions.CompositeSubscription;
24+
25+
26+
public class PollingFragment extends Fragment {
27+
28+
29+
static final String TAG = "PollingFragment";
30+
@BindView(R.id.btn_polling)
31+
Button mBtnPolling;
32+
33+
int N=0;
34+
private Subscription subscribe;
35+
private CompositeSubscription mCompositeSubscription;
36+
;
37+
38+
@Override
39+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
40+
Bundle savedInstanceState) {
41+
View view = inflater.inflate(R.layout.polling_fragment, container, false);
42+
ButterKnife.bind(this, view);
43+
mCompositeSubscription = new CompositeSubscription();
44+
return view;
45+
}
46+
47+
@Override
48+
public void onResume() {
49+
super.onResume();
50+
}
51+
52+
@Override
53+
public void onViewCreated(View view, Bundle savedInstanceState) {
54+
super.onViewCreated(view, savedInstanceState);
55+
56+
}
57+
58+
59+
@OnClick(R.id.btn_polling)
60+
public void onClick() {
61+
62+
63+
subscribe=Observable.create(new Observable.OnSubscribe<String>() {
64+
@Override
65+
public void call(final Subscriber<? super String> observer) {
66+
67+
Schedulers.newThread().createWorker()
68+
.schedulePeriodically(new Action0() {
69+
@Override
70+
public void call() {
71+
observer.onNext(" "+(N++));
72+
}
73+
}, 0, 1000, TimeUnit.MILLISECONDS);
74+
}
75+
}).subscribe(new Action1<String>() {
76+
@Override
77+
public void call(String s) {
78+
printLog("polling"+s);
79+
}
80+
});
81+
mCompositeSubscription.add(subscribe);
82+
}
83+
private void printLog(String s) {
84+
Log.i(TAG, s);
85+
}
86+
87+
@Override
88+
public void onDestroy() {
89+
super.onDestroy();
90+
mCompositeSubscription.unsubscribe();
91+
}
92+
93+
94+
}
95+
96+
97+
98+

app/src/main/java/com/demo/maat/hello_rxjava/RxJavaMainActivity.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import android.support.v7.app.AppCompatActivity;
66
import android.view.View;
77
import android.widget.Button;
8-
import android.widget.LinearLayout;
98

109
import butterknife.BindView;
1110
import butterknife.ButterKnife;
@@ -24,8 +23,7 @@ public class RxJavaMainActivity extends AppCompatActivity {
2423
Button mBtnOperators3;
2524
@BindView(R.id.btn_operators4)
2625
Button mBtnOperators4;
27-
@BindView(R.id.activity_main)
28-
LinearLayout mActivityMain;
26+
2927

3028
@Override
3129
protected void onCreate(Bundle savedInstanceState) {
@@ -53,11 +51,14 @@ public void onClick(View view) {
5351
startActivity(operator2);
5452
break;
5553
case R.id.btn_operators3:
54+
Intent polling = new Intent(this, PollingActivity.class);
55+
startActivity(polling);
5656
break;
5757
case R.id.btn_operators4:
58+
Intent debounce = new Intent(this, DebounceEditTextActivity.class);
59+
startActivity(debounce);
5860
break;
59-
case R.id.activity_main:
60-
break;
61+
6162
}
6263
}
6364
}

0 commit comments

Comments
 (0)