_lifeCycleStream;
17 |
18 | private IAmYourMaster _masterFrag;
19 |
20 | /**
21 | * Since we're holding a reference to the Master a.k.a Activity/Master Frag remember to explicitly
22 | * remove the worker fragment or you'll have a mem leak in your hands.
23 | *
24 | * See {@link MainActivity#onBackPressed()}
25 | */
26 | @Override
27 | public void onAttach(Context context) {
28 | super.onAttach(context);
29 |
30 | _masterFrag =
31 | (RotationPersist2Fragment)
32 | ((MainActivity) context)
33 | .getSupportFragmentManager()
34 | .findFragmentByTag(RotationPersist2Fragment.TAG);
35 |
36 | if (_masterFrag == null) {
37 | throw new ClassCastException("We did not find a master who can understand us :(");
38 | }
39 | }
40 |
41 | /** This method will only be called once when the retained Fragment is first created. */
42 | @Override
43 | public void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 |
46 | _intStream = PublishProcessor.create();
47 | _lifeCycleStream = PublishProcessor.create();
48 |
49 | // Retain this fragment across configuration changes.
50 | setRetainInstance(true);
51 |
52 | _intStream.takeUntil(_lifeCycleStream);
53 |
54 | Flowable.interval(1, TimeUnit.SECONDS).map(Long::intValue).take(20).subscribe(_intStream);
55 | }
56 |
57 | /** The Worker fragment has started doing it's thing */
58 | @Override
59 | public void onResume() {
60 | super.onResume();
61 | _masterFrag.setStream(_intStream);
62 | }
63 |
64 | @Override
65 | public void onDestroy() {
66 | super.onDestroy();
67 | _lifeCycleStream.onComplete();
68 | }
69 |
70 | /** Set the callback to null so we don't accidentally leak the Activity instance. */
71 | @Override
72 | public void onDetach() {
73 | super.onDetach();
74 | _masterFrag = null;
75 | }
76 |
77 | public interface IAmYourMaster {
78 | void setStream(Flowable intStream);
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/app/src/main/java/com/morihacky/android/rxjava/fragments/RotationPersist1WorkerFragment.java:
--------------------------------------------------------------------------------
1 | package com.morihacky.android.rxjava.fragments;
2 |
3 | import android.content.Context;
4 | import android.os.Bundle;
5 | import android.support.v4.app.Fragment;
6 | import com.morihacky.android.rxjava.MainActivity;
7 | import io.reactivex.Flowable;
8 | import io.reactivex.disposables.Disposable;
9 | import io.reactivex.flowables.ConnectableFlowable;
10 | import java.util.concurrent.TimeUnit;
11 |
12 | public class RotationPersist1WorkerFragment extends Fragment {
13 |
14 | public static final String TAG = RotationPersist1WorkerFragment.class.toString();
15 |
16 | private IAmYourMaster _masterFrag;
17 | private ConnectableFlowable _storedIntsFlowable;
18 | private Disposable _storedIntsDisposable;
19 |
20 | /**
21 | * Hold a reference to the activity -> caller fragment this way when the worker frag kicks off we
22 | * can talk back to the master and send results
23 | */
24 | @Override
25 | public void onAttach(Context context) {
26 | super.onAttach(context);
27 |
28 | _masterFrag =
29 | (RotationPersist1Fragment)
30 | ((MainActivity) context)
31 | .getSupportFragmentManager()
32 | .findFragmentByTag(RotationPersist1Fragment.TAG);
33 |
34 | if (_masterFrag == null) {
35 | throw new ClassCastException("We did not find a master who can understand us :(");
36 | }
37 | }
38 |
39 | /** This method will only be called once when the retained Fragment is first created. */
40 | @Override
41 | public void onCreate(Bundle savedInstanceState) {
42 | super.onCreate(savedInstanceState);
43 |
44 | // Retain this fragment across configuration changes.
45 | setRetainInstance(true);
46 |
47 | if (_storedIntsFlowable != null) {
48 | return;
49 | }
50 |
51 | Flowable intsObservable =
52 | Flowable.interval(1, TimeUnit.SECONDS).map(Long::intValue).take(20);
53 |
54 | _storedIntsFlowable = intsObservable.publish();
55 | _storedIntsDisposable = _storedIntsFlowable.connect();
56 | }
57 |
58 | /** The Worker fragment has started doing it's thing */
59 | @Override
60 | public void onResume() {
61 | super.onResume();
62 | _masterFrag.observeResults(_storedIntsFlowable);
63 | }
64 |
65 | @Override
66 | public void onDestroy() {
67 | super.onDestroy();
68 | _storedIntsDisposable.dispose();
69 | }
70 |
71 | /** Set the callback to null so we don't accidentally leak the Activity instance. */
72 | @Override
73 | public void onDetach() {
74 | super.onDetach();
75 | _masterFrag = null;
76 | }
77 |
78 | public interface IAmYourMaster {
79 | void observeResults(Flowable intsObservable);
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/app/src/main/java/com/morihacky/android/rxjava/pagination/PaginationAdapter.java:
--------------------------------------------------------------------------------
1 | package com.morihacky.android.rxjava.pagination;
2 |
3 | import android.support.v7.widget.RecyclerView;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.Button;
8 | import android.widget.TextView;
9 |
10 | import com.morihacky.android.rxjava.R;
11 | import com.morihacky.android.rxjava.rxbus.RxBus;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | /** There isn't anything specific to Pagination here. Just wiring for the example */
17 | class PaginationAdapter extends RecyclerView.Adapter {
18 |
19 | private static final int ITEM_LOG = 0;
20 | private static final int ITEM_BTN = 1;
21 |
22 | private final List _items = new ArrayList<>();
23 | private final RxBus _bus;
24 |
25 | PaginationAdapter(RxBus bus) {
26 | _bus = bus;
27 | }
28 |
29 | void addItems(List items) {
30 | _items.addAll(items);
31 | }
32 |
33 | @Override
34 | public int getItemViewType(int position) {
35 | if (position == _items.size()) {
36 | return ITEM_BTN;
37 | }
38 |
39 | return ITEM_LOG;
40 | }
41 |
42 | @Override
43 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
44 | switch (viewType) {
45 | case ITEM_BTN:
46 | return ItemBtnViewHolder.create(parent);
47 | default:
48 | return ItemLogViewHolder.create(parent);
49 | }
50 | }
51 |
52 | @Override
53 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
54 | switch (getItemViewType(position)) {
55 | case ITEM_LOG:
56 | ((ItemLogViewHolder) holder).bindContent(_items.get(position));
57 | return;
58 | case ITEM_BTN:
59 | ((ItemBtnViewHolder) holder).bindContent(_bus);
60 | }
61 | }
62 |
63 | @Override
64 | public int getItemCount() {
65 | return _items.size() + 1; // add 1 for paging button
66 | }
67 |
68 | private static class ItemLogViewHolder extends RecyclerView.ViewHolder {
69 | ItemLogViewHolder(View itemView) {
70 | super(itemView);
71 | }
72 |
73 | static ItemLogViewHolder create(ViewGroup parent) {
74 | return new ItemLogViewHolder(
75 | LayoutInflater.from(parent.getContext()).inflate(R.layout.item_log, parent, false));
76 | }
77 |
78 | void bindContent(String content) {
79 | ((TextView) itemView).setText(content);
80 | }
81 | }
82 |
83 | static class ItemBtnViewHolder extends RecyclerView.ViewHolder {
84 | ItemBtnViewHolder(View itemView) {
85 | super(itemView);
86 | }
87 |
88 | static ItemBtnViewHolder create(ViewGroup parent) {
89 | return new ItemBtnViewHolder(
90 | LayoutInflater.from(parent.getContext()).inflate(R.layout.item_btn, parent, false));
91 | }
92 |
93 | void bindContent(RxBus bus) {
94 | ((Button) itemView).setText(R.string.btn_demo_pagination_more);
95 | itemView.setOnClickListener(v -> bus.send(new ItemBtnViewHolder.PageEvent()));
96 | }
97 |
98 | static class PageEvent {}
99 | }
100 | }
101 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_form_validation_comb_latest.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
18 |
23 |
24 |
31 |
32 |
39 |
40 |
41 |
46 |
47 |
54 |
55 |
61 |
62 |
63 |
64 |
69 |
70 |
77 |
78 |
85 |
86 |
87 |
97 |
98 |
99 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_retrofit.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
21 |
22 |
30 |
31 |
40 |
41 |
50 |
51 |
52 |
56 |
57 |
65 |
66 |
75 |
76 |
85 |
86 |
87 |
92 |
93 |
--------------------------------------------------------------------------------
/app/src/main/kotlin/com/morihacky/android/rxjava/fragments/UsingFragment.kt:
--------------------------------------------------------------------------------
1 | package com.morihacky.android.rxjava.fragments
2 |
3 | import android.content.Context
4 | import android.os.Bundle
5 | import android.os.Handler
6 | import android.os.Looper
7 | import android.view.LayoutInflater
8 | import android.view.View
9 | import android.view.ViewGroup
10 | import android.widget.ArrayAdapter
11 | import android.widget.ListView
12 | import android.widget.TextView
13 | import com.morihacky.android.rxjava.R
14 | import io.reactivex.Flowable
15 | import io.reactivex.functions.Consumer
16 | import io.reactivex.functions.Function
17 | import org.reactivestreams.Publisher
18 | import java.util.*
19 | import java.util.concurrent.Callable
20 |
21 | class UsingFragment : BaseFragment() {
22 |
23 | private lateinit var _logs: MutableList
24 | private lateinit var _logsList: ListView
25 | private lateinit var _adapter: UsingFragment.LogAdapter
26 |
27 | override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
28 | val view = inflater?.inflate(R.layout.fragment_buffer, container, false)
29 | _logsList = view?.findViewById(R.id.list_threading_log) as ListView
30 |
31 | (view.findViewById(R.id.text_description) as TextView).setText(R.string.msg_demo_using)
32 |
33 | _setupLogger()
34 | view.findViewById(R.id.btn_start_operation).setOnClickListener { executeUsingOperation() }
35 | return view
36 | }
37 |
38 | private fun executeUsingOperation() {
39 | val resourceSupplier = Callable { Realm() }
40 | val sourceSupplier = Function> { realm ->
41 | Flowable.just(true)
42 | .map {
43 | realm.doSomething()
44 | // i would use the copyFromRealm and change it to a POJO
45 | Random().nextInt(50)
46 | }
47 | }
48 | val disposer = Consumer { realm ->
49 | realm.clear()
50 | }
51 |
52 | Flowable.using(resourceSupplier, sourceSupplier, disposer)
53 | .subscribe({ i ->
54 | _log("got a value $i - (look at the logs)")
55 | })
56 | }
57 |
58 | inner class Realm {
59 | init {
60 | _log("initializing Realm instance")
61 | }
62 |
63 | fun doSomething() {
64 | _log("do something with Realm instance")
65 | }
66 |
67 | fun clear() {
68 | // notice how this is called even before you manually "dispose"
69 | _log("cleaning up the resources (happens before a manual 'dispose'")
70 | }
71 | }
72 |
73 | // -----------------------------------------------------------------------------------
74 | // Method that help wiring up the example (irrelevant to RxJava)
75 |
76 | private fun _log(logMsg: String) {
77 | _logs.add(0, logMsg)
78 |
79 | // You can only do below stuff on main thread.
80 | Handler(Looper.getMainLooper()).post {
81 | _adapter.clear()
82 | _adapter.addAll(_logs)
83 | }
84 | }
85 |
86 | private fun _setupLogger() {
87 | _logs = ArrayList()
88 | _adapter = LogAdapter(activity, ArrayList())
89 | _logsList.adapter = _adapter
90 | }
91 |
92 | private class LogAdapter(context: Context, logs: List) : ArrayAdapter(context, R.layout.item_log, R.id.item_log, logs)
93 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/morihacky/android/rxjava/rxbus/RxBusDemo_Bottom3Fragment.java:
--------------------------------------------------------------------------------
1 | package com.morihacky.android.rxjava.rxbus;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.view.ViewCompat;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.TextView;
10 | import butterknife.BindView;
11 | import butterknife.ButterKnife;
12 | import com.morihacky.android.rxjava.MainActivity;
13 | import com.morihacky.android.rxjava.R;
14 | import com.morihacky.android.rxjava.fragments.BaseFragment;
15 | import io.reactivex.android.schedulers.AndroidSchedulers;
16 | import io.reactivex.disposables.CompositeDisposable;
17 | import io.reactivex.flowables.ConnectableFlowable;
18 | import java.util.concurrent.TimeUnit;
19 |
20 | public class RxBusDemo_Bottom3Fragment extends BaseFragment {
21 |
22 | @BindView(R.id.demo_rxbus_tap_txt)
23 | TextView _tapEventTxtShow;
24 |
25 | @BindView(R.id.demo_rxbus_tap_count)
26 | TextView _tapEventCountShow;
27 |
28 | private RxBus _rxBus;
29 | private CompositeDisposable _disposables;
30 |
31 | @Override
32 | public View onCreateView(
33 | LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
34 | View layout = inflater.inflate(R.layout.fragment_rxbus_bottom, container, false);
35 | ButterKnife.bind(this, layout);
36 | return layout;
37 | }
38 |
39 | @Override
40 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
41 | super.onActivityCreated(savedInstanceState);
42 | _rxBus = ((MainActivity) getActivity()).getRxBusSingleton();
43 | }
44 |
45 | @Override
46 | public void onStart() {
47 | super.onStart();
48 | _disposables = new CompositeDisposable();
49 |
50 | ConnectableFlowable