|
1 | 1 | package com.morihacky.android.rxjava; |
2 | 2 |
|
3 | 3 | import android.os.Bundle; |
| 4 | +import android.support.annotation.NonNull; |
4 | 5 | import android.support.annotation.Nullable; |
| 6 | +import android.support.v4.app.Fragment; |
5 | 7 | import android.view.LayoutInflater; |
6 | 8 | import android.view.View; |
7 | 9 | import android.view.ViewGroup; |
8 | 10 | import butterknife.ButterKnife; |
9 | 11 | import butterknife.OnClick; |
10 | 12 | import com.morihacky.android.rxjava.rxbus.RxBusDemoFragment; |
11 | 13 |
|
12 | | -public class MainFragment |
13 | | - extends BaseFragment { |
| 14 | +public class MainFragment extends BaseFragment { |
14 | 15 |
|
15 | 16 | @Override |
16 | | - public View onCreateView(LayoutInflater inflater, |
17 | | - @Nullable ViewGroup container, |
18 | | - @Nullable Bundle savedInstanceState) { |
| 17 | + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
19 | 18 | View layout = inflater.inflate(R.layout.fragment_main, container, false); |
20 | 19 | ButterKnife.inject(this, layout); |
21 | 20 | return layout; |
22 | 21 | } |
23 | 22 |
|
24 | 23 | @OnClick(R.id.btn_demo_schedulers) |
25 | | - public void demoConcurrencyWithSchedulers() { |
26 | | - getActivity().getSupportFragmentManager() |
27 | | - .beginTransaction() |
28 | | - .addToBackStack(ConcurrencyWithSchedulersDemoFragment.class.getName()) |
29 | | - .replace(android.R.id.content, |
30 | | - new ConcurrencyWithSchedulersDemoFragment(), |
31 | | - ConcurrencyWithSchedulersDemoFragment.class.getName()) |
32 | | - .commit(); |
| 24 | + void demoConcurrencyWithSchedulers() { |
| 25 | + clickedOn(new ConcurrencyWithSchedulersDemoFragment()); |
33 | 26 | } |
34 | 27 |
|
35 | 28 | @OnClick(R.id.btn_demo_buffer) |
36 | | - public void demoBuffer() { |
37 | | - getActivity().getSupportFragmentManager() |
38 | | - .beginTransaction() |
39 | | - .addToBackStack(BufferDemoFragment.class.toString()) |
40 | | - .replace(android.R.id.content, |
41 | | - new BufferDemoFragment(), |
42 | | - BufferDemoFragment.class.toString()) |
43 | | - .commit(); |
| 29 | + void demoBuffer() { |
| 30 | + clickedOn(new BufferDemoFragment()); |
44 | 31 | } |
45 | 32 |
|
46 | 33 | @OnClick(R.id.btn_demo_debounce) |
47 | | - public void demoThrottling() { |
48 | | - getActivity().getSupportFragmentManager() |
49 | | - .beginTransaction() |
50 | | - .addToBackStack(DebounceSearchEmitterFragment.class.toString()) |
51 | | - .replace(android.R.id.content, |
52 | | - new DebounceSearchEmitterFragment(), |
53 | | - DebounceSearchEmitterFragment.class.toString()) |
54 | | - .commit(); |
| 34 | + void demoThrottling() { |
| 35 | + clickedOn(new DebounceSearchEmitterFragment()); |
55 | 36 | } |
56 | 37 |
|
57 | 38 | @OnClick(R.id.btn_demo_retrofit) |
58 | | - public void demoRetrofitCalls() { |
59 | | - getActivity().getSupportFragmentManager() |
60 | | - .beginTransaction().addToBackStack(RetrofitFragment.class.toString()) |
61 | | - //.replace(android.R.id.content, new RetrofitAsyncTaskDeathFragment(), RetrofitAsyncTaskDeathFragment.class.toString()) |
62 | | - .replace(android.R.id.content, |
63 | | - new RetrofitFragment(), |
64 | | - RetrofitFragment.class.toString()).commit(); |
| 39 | + void demoRetrofitCalls() { |
| 40 | + clickedOn(new RetrofitFragment()); |
65 | 41 | } |
66 | 42 |
|
67 | 43 | @OnClick(R.id.btn_demo_double_binding_textview) |
68 | | - public void demoDoubleBindingWithPublishSubject() { |
69 | | - getActivity().getSupportFragmentManager() |
70 | | - .beginTransaction() |
71 | | - .addToBackStack(DoubleBindingTextViewFragment.class.toString()) |
72 | | - .replace(android.R.id.content, |
73 | | - new DoubleBindingTextViewFragment(), |
74 | | - DoubleBindingTextViewFragment.class.toString()) |
75 | | - .commit(); |
| 44 | + void demoDoubleBindingWithPublishSubject() { |
| 45 | + clickedOn(new DoubleBindingTextViewFragment()); |
76 | 46 | } |
77 | 47 |
|
78 | | - /*@OnClick(R.id.btn_demo_polling) |
79 | | - public void demoPolling() { |
80 | | - getActivity().getSupportFragmentManager() |
81 | | - .beginTransaction() |
82 | | - .addToBackStack(PollingFragment.class.toString()) |
83 | | - .replace(android.R.id.content, new PollingFragment(), PollingFragment.class.toString()) |
84 | | - .commit(); |
85 | | - }*/ |
86 | | - |
87 | 48 | @OnClick(R.id.btn_demo_rxbus) |
88 | | - public void demoRxBus() { |
89 | | - getActivity().getSupportFragmentManager() |
90 | | - .beginTransaction() |
91 | | - .addToBackStack(RxBusDemoFragment.class.toString()) |
92 | | - .replace(android.R.id.content, |
93 | | - new RxBusDemoFragment(), |
94 | | - RxBusDemoFragment.class.toString()) |
95 | | - .commit(); |
96 | | - } |
97 | | - |
98 | | - //@OnClick(R.id.btn_demo_subject_timeout) |
99 | | - public void demoTimeout() { |
100 | | - getActivity().getSupportFragmentManager() |
101 | | - .beginTransaction() |
102 | | - .addToBackStack(TimeoutDemoFragment.class.toString()) |
103 | | - .replace(android.R.id.content, |
104 | | - new TimeoutDemoFragment(), |
105 | | - TimeoutDemoFragment.class.toString()) |
106 | | - .commit(); |
| 49 | + void demoRxBus() { |
| 50 | + clickedOn(new RxBusDemoFragment()); |
107 | 51 | } |
108 | 52 |
|
109 | 53 | @OnClick(R.id.btn_demo_form_validation_combinel) |
110 | | - public void formValidation() { |
111 | | - getActivity().getSupportFragmentManager() |
112 | | - .beginTransaction() |
113 | | - .addToBackStack(FormValidationCombineLatestFragment.class.toString()) |
114 | | - .replace(android.R.id.content, |
115 | | - new FormValidationCombineLatestFragment(), |
116 | | - FormValidationCombineLatestFragment.class.toString()) |
117 | | - .commit(); |
| 54 | + void formValidation() { |
| 55 | + clickedOn(new FormValidationCombineLatestFragment()); |
118 | 56 | } |
119 | 57 |
|
120 | 58 | @OnClick(R.id.btn_demo_pseudo_cache) |
121 | | - public void pseudoCacheDemo() { |
122 | | - getActivity().getSupportFragmentManager().beginTransaction().addToBackStack( |
123 | | - PseudoCacheMergeFragment.class.toString()) |
124 | | - //.replace(android.R.id.content, new PseudoCacheConcatFragment(), PseudoCacheConcatFragment.class.toString()) |
125 | | - .replace(android.R.id.content, |
126 | | - new PseudoCacheMergeFragment(), |
127 | | - PseudoCacheMergeFragment.class.toString()).commit(); |
| 59 | + void pseudoCacheDemo() { |
| 60 | + clickedOn(new PseudoCacheMergeFragment()); |
128 | 61 | } |
129 | 62 |
|
130 | 63 | @OnClick(R.id.btn_demo_timing) |
131 | | - public void demoTimerIntervalDelays() { |
132 | | - getActivity().getSupportFragmentManager() |
133 | | - .beginTransaction() |
134 | | - .addToBackStack(TimingDemoFragment.class.toString()) |
135 | | - .replace(android.R.id.content, |
136 | | - new TimingDemoFragment(), |
137 | | - TimingDemoFragment.class.toString()) |
138 | | - .commit(); |
| 64 | + void demoTimerIntervalDelays() { |
| 65 | + clickedOn(new TimingDemoFragment()); |
139 | 66 | } |
140 | 67 |
|
141 | 68 | @OnClick(R.id.btn_demo_exponential_backoff) |
142 | | - public void demoExponentialBackoff() { |
143 | | - getActivity().getSupportFragmentManager() |
144 | | - .beginTransaction() |
145 | | - .addToBackStack(ExponentialBackoffFragment.class.toString()) |
146 | | - .replace(android.R.id.content, |
147 | | - new ExponentialBackoffFragment(), |
148 | | - ExponentialBackoffFragment.class.toString()) |
149 | | - .commit(); |
| 69 | + void demoExponentialBackoff() { |
| 70 | + clickedOn(new ExponentialBackoffFragment()); |
150 | 71 | } |
151 | 72 |
|
152 | 73 | @OnClick(R.id.btn_demo_rotation_persist) |
153 | | - public void demoRotationPersist() { |
| 74 | + void demoRotationPersist() { |
| 75 | + clickedOn(new RotationPersistFragment()); |
| 76 | + } |
| 77 | + |
| 78 | + private void clickedOn(@NonNull Fragment fragment) { |
| 79 | + final String tag = fragment.getClass().toString(); |
154 | 80 | getActivity().getSupportFragmentManager() |
155 | | - .beginTransaction() |
156 | | - .addToBackStack(RotationPersistFragment.class.toString()) |
157 | | - .replace(android.R.id.content, |
158 | | - new RotationPersistFragment(), |
159 | | - RotationPersistFragment.class.toString()) |
160 | | - .commit(); |
| 81 | + .beginTransaction() |
| 82 | + .addToBackStack(tag) |
| 83 | + .replace(android.R.id.content, fragment, tag) |
| 84 | + .commit(); |
161 | 85 | } |
162 | 86 | } |