Skip to content

Commit 3a6033d

Browse files
committed
dependencies decouple with mvp and abstract classes.
1 parent acf78fc commit 3a6033d

17 files changed

+454
-142
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.cleaner.home;
2+
3+
/**
4+
* Created by yangfeng on 16-6-28.
5+
*/
6+
public class ExitingTrigger {
7+
private static final long DEFAULT_INTERVAL = 3000; // 3 seconds
8+
9+
private long mExitTime;
10+
private final long INTERVAL;
11+
12+
public ExitingTrigger() {
13+
this(DEFAULT_INTERVAL);
14+
}
15+
16+
public ExitingTrigger(long interval) {
17+
INTERVAL = interval;
18+
}
19+
20+
public boolean testExpired(long timeStamp) {
21+
if (timeStamp - mExitTime > INTERVAL) {
22+
mExitTime = timeStamp;
23+
return true;
24+
} else {
25+
return false;
26+
}
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cleaner.home;
2+
3+
import android.view.KeyEvent;
4+
5+
/**
6+
* Created by yangfeng on 16-6-28.
7+
*/
8+
public class KeyEventHelper {
9+
private KeyEventHelper() {
10+
// EMPTY
11+
}
12+
13+
public static boolean isBackKeyDown(KeyEvent event) {
14+
return event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK;
15+
}
16+
}

app/src/main/java/com/cleaner/home/MainActivity.java

Lines changed: 135 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
import android.app.TabActivity;
44
import android.content.Intent;
55
import android.os.Bundle;
6-
import android.util.Log;
6+
import android.support.annotation.NonNull;
77
import android.view.KeyEvent;
88
import android.view.View;
99
import android.view.View.OnClickListener;
1010
import android.widget.TabHost;
1111
import android.widget.TabHost.TabSpec;
1212
import android.widget.Toast;
1313

14-
import com.cleaner.config.MainPageConfig;
14+
import com.cleaner.config.PatientUtil;
1515
import com.cleaner.view.BadgeRadioButton;
1616

1717
import butterknife.BindView;
1818
import butterknife.ButterKnife;
1919
import butterknife.Unbinder;
2020

21-
public class MainActivity extends TabActivity {
21+
public class MainActivity extends TabActivity implements MainConfigContracts.ConfigView{
2222
private static final String TAG = MainActivity.class.getSimpleName();
2323

2424
@BindView(android.R.id.tabhost)
@@ -61,13 +61,13 @@ public class MainActivity extends TabActivity {
6161
private static final int EVENT_EXPLORE = 5;
6262
private static final int EVENT_PROFILE = 3;
6363

64-
private MainPageConfig mainPageConfig;
64+
private final ExitingTrigger exitingTrigger = new ExitingTrigger();
65+
private final ToastWrapper toastWrapper = new ToastWrapper(R.string.toast_exit, Toast.LENGTH_LONG);
6566

67+
private Unbinder unbinder;
6668
private BadgeRadioButton activeBadgeRadio;
6769

68-
private long exitInterval;
69-
70-
private Unbinder unbinder;
70+
private final RadioClickListener listener = new RadioClickListener();
7171

7272
@Override
7373
protected void onCreate(Bundle savedInstanceState) {
@@ -89,9 +89,10 @@ public void onContentChanged() {
8989
// checkAndLoginXbkpIM();
9090
// PatientApplication.getInstance().addActivity(this);
9191

92-
mainPageConfig = new MainPageConfig(this);
93-
94-
initTabs();
92+
MainConfigContracts.ConfigPresenter configPresenter = new MainConfigPresenterImpl(this, parsePageConfig());
93+
initSelectTab(getIntent());
94+
initRadioButtons();
95+
configPresenter.initTabs();
9596

9697
//事件的监听
9798
// AndroidEventManager.getInstance().addEventListener(
@@ -104,110 +105,6 @@ public void onContentChanged() {
104105
// .getUnreadMessageTotalCount());
105106
}
106107

107-
private TabSpec getHomePage() {
108-
TabSpec tabNearby = null;
109-
if (mainPageConfig.hasSummary()) {
110-
tabNearby = tabHost.newTabSpec(TAB_SPEC_SUMMARY);
111-
tabNearby.setIndicator(TAB_SPEC_SUMMARY).setContent(new Intent(this, SummaryActivity.class));
112-
} else if (mainPageConfig.hasRecyclerSummary()) {
113-
tabNearby = tabHost.newTabSpec(TAB_SPEC_SUMMARY);
114-
tabNearby.setIndicator(TAB_SPEC_SUMMARY).setContent(new Intent(this, RecyclerSummaryActivity.class));
115-
} else if (mainPageConfig.hasRecyclerSummaryV2()) {
116-
tabNearby = tabHost.newTabSpec(TAB_SPEC_SUMMARY);
117-
tabNearby.setIndicator(TAB_SPEC_SUMMARY).setContent(new Intent(this, RecyclerSummaryV2Activity.class));
118-
} else {
119-
// Temporarily not processed
120-
}
121-
122-
return tabNearby;
123-
}
124-
125-
private TabSpec getMessagePage() {
126-
TabSpec tabMessage = null;
127-
if (mainPageConfig.hasMessage()) {
128-
tabConversation = tabHost.newTabSpec(TAB_SPEC_CONVERSATION);
129-
initOrUpdateConversationTab();
130-
tabMessage = tabConversation;
131-
} else {
132-
Log.e(TAG, "message is null");
133-
}
134-
return tabMessage;
135-
}
136-
137-
private TabSpec getDoctorPage() {
138-
TabSpec tabFind = null;
139-
if (mainPageConfig.hasDoctorPage()) {
140-
tabFind = tabHost.newTabSpec(TAB_SPEC_CONTACT);
141-
tabFind.setIndicator(TAB_SPEC_CONTACT).setContent(
142-
new Intent(this, TabSearchDoctorActivity.class));
143-
} else {
144-
// Temporarily not processed
145-
}
146-
147-
return tabFind;
148-
}
149-
150-
private TabSpec getMinePage() {
151-
TabSpec tabMy = null;
152-
if (mainPageConfig.hasMinePage()) {
153-
tabMy = tabHost.newTabSpec(TAB_SPEC_PROFILE);
154-
tabMy.setIndicator(TAB_SPEC_PROFILE).setContent(
155-
new Intent(this, MineActivity.class));
156-
} else {
157-
// Temporarily not processed
158-
}
159-
return tabMy;
160-
}
161-
162-
private TabSpec getDiscoveryPage() {
163-
TabSpec tabDiscovery = null;
164-
if (mainPageConfig.hasDiscoveryPage()) {
165-
tabDiscovery = tabHost.newTabSpec(TAB_SPEC_EXPLORE);
166-
tabDiscovery.setIndicator(TAB_SPEC_EXPLORE).setContent(
167-
new Intent(this, DiscoveryActivity.class));
168-
} else {
169-
// Temporarily not processed
170-
}
171-
return tabDiscovery;
172-
}
173-
private boolean initTabPanel(TabSpec tabSpec, BadgeRadioButton panel, int labelId, int iconId,
174-
RadioClickListener listener) {
175-
if (null == tabSpec) {
176-
panel.setVisibility(View.GONE);
177-
return false;
178-
} else {
179-
tabHost.addTab(tabSpec);
180-
panel.setOnClickListener(labelId, iconId, listener);
181-
panel.setVisibility(View.VISIBLE);
182-
}
183-
return true;
184-
}
185-
186-
private void initTabs() {
187-
initSelectTab(getIntent());
188-
initRadioButtons();
189-
RadioClickListener listener = new RadioClickListener();
190-
// Home page
191-
TabSpec tabNearby = getHomePage();
192-
initTabPanel(tabNearby, summaryBadgeRadio, R.string.tab_main, R.drawable.main_tab_home, listener);
193-
194-
//message
195-
TabSpec tabMessage = getMessagePage();
196-
initTabPanel(tabMessage, conversationBadgeRadio, R.string.tab_message, R.drawable.main_tab_message, listener);
197-
198-
//find doctor
199-
TabSpec tabDoctor = getDoctorPage();
200-
initTabPanel(tabDoctor, contactBadgeRadio, R.string.tab_find_doctor, R.drawable.main_tab_search, listener);
201-
202-
//discovery
203-
TabSpec tabDiscovery = getDiscoveryPage();
204-
initTabPanel(tabDiscovery, exploreBadgeRadio, R.string.tab_discovery, R.drawable.main_tab_search, listener);
205-
206-
//me
207-
TabSpec tabMy = getMinePage();
208-
initTabPanel(tabMy, profileBadgeRadio, R.string.tab_mine, R.drawable.main_tab_mine, listener);
209-
}
210-
211108
private static void setActiveIndex(int which) {
212109
activeIndex = which;
213110
}
@@ -262,9 +159,9 @@ protected void onResume() {
262159
//如果是“我的医生”页,右上角“找医生”跳转过来的,手动触发“找医生界面”
263160
Intent intent = getIntent();
264161

265-
String docter = intent.getStringExtra("callDocter");
162+
String doctor = intent.getStringExtra("callDocter");
266163

267-
if ("true".equals(docter)) {
164+
if ("true".equals(doctor)) {
268165
intent.removeExtra("callDocter");
269166
contactBadgeRadio.performClick();
270167
}
@@ -280,6 +177,122 @@ protected void onResume() {
280177
// }
281178
// }
282179

180+
private MainConfigContracts.PageConfig parsePageConfig() {
181+
String pageConfigKey = MainPageConfigImpl.getPageConfigKey();
182+
String preferredConfig = PatientUtil.loadConfigItem(this, pageConfigKey);
183+
return new MainPageConfigImpl(preferredConfig);
184+
}
185+
186+
@Override
187+
public void addSummaryTab() {
188+
initHomePageTab(SummaryActivity.class);
189+
}
190+
191+
@Override
192+
public void addRecyclerSummaryTab() {
193+
initHomePageTab(RecyclerSummaryActivity.class);
194+
}
195+
196+
@Override
197+
public void addRecyclerSummaryV2Tab() {
198+
initHomePageTab(RecyclerSummaryV2Activity.class);
199+
}
200+
201+
@Override
202+
public void addUnknownSummaryTab() {
203+
// do nothing to prevent adding any HOME tab
204+
hideTabPanel(summaryBadgeRadio);
205+
}
206+
207+
private void initHomePageTab(@NonNull Class<?> cls) {
208+
TabSpec tabNearby = tabHost.newTabSpec(TAB_SPEC_SUMMARY);
209+
tabNearby.setIndicator(TAB_SPEC_SUMMARY).setContent(new Intent(this, cls));
210+
initTabPanel(tabNearby, summaryBadgeRadio, R.string.radio_label_summary, R.drawable.radio_bg_summary, listener);
211+
}
212+
213+
// private boolean initTabPanel(TabSpec tabSpec, View panel, View tabView, RadioClickListener listener) {
214+
// if (null == tabSpec) {
215+
// panel.setVisibility(View.GONE);
216+
// return false;
217+
// } else {
218+
// tabHost.addTab(tabSpec);
219+
// tabView.setOnClickListener(listener);
220+
// panel.setVisibility(View.VISIBLE);
221+
// }
222+
// return true;
223+
// }
224+
225+
private boolean initTabPanel(TabSpec tabSpec, BadgeRadioButton panel, int labelId, int iconId,
226+
RadioClickListener listener) {
227+
// if (null == tabSpec) {
228+
// panel.setVisibility(View.GONE);
229+
// return false;
230+
// } else {
231+
tabHost.addTab(tabSpec);
232+
panel.setOnClickListener(labelId, iconId, listener);
233+
panel.setVisibility(View.VISIBLE);
234+
// }
235+
return true;
236+
}
237+
238+
private void hideTabPanel(@NonNull BadgeRadioButton panel) {
239+
panel.setVisibility(View.GONE);
240+
}
241+
242+
@Override
243+
public void addMessageTab() {
244+
tabConversation = tabHost.newTabSpec(TAB_SPEC_CONVERSATION);
245+
initOrUpdateConversationTab();
246+
initTabPanel(tabConversation, conversationBadgeRadio, R.string.radio_label_conversation, R.drawable.radio_bg_conversation, listener);
247+
}
248+
249+
@Override
250+
public void addUnknownMessageTab() {
251+
// do nothing to prevent adding any MESSAGE tab
252+
hideTabPanel(conversationBadgeRadio);
253+
}
254+
255+
@Override
256+
public void addDoctorTab() {
257+
TabSpec tabDoctor = tabHost.newTabSpec(TAB_SPEC_CONTACT);
258+
tabDoctor.setIndicator(TAB_SPEC_CONTACT).setContent(
259+
new Intent(this, TabSearchDoctorActivity.class));
260+
initTabPanel(tabDoctor, contactBadgeRadio, R.string.radio_label_contact, R.drawable.radio_bg_explore, listener);
261+
}
262+
263+
@Override
264+
public void addUnknownDoctorTab() {
265+
// do nothing to prevent adding any DOCTOR tab
266+
hideTabPanel(contactBadgeRadio);
267+
}
268+
269+
@Override
270+
public void addMineTab() {
271+
TabSpec tabMine = tabHost.newTabSpec(TAB_SPEC_PROFILE);
272+
tabMine.setIndicator(TAB_SPEC_PROFILE).setContent(new Intent(this, MineActivity.class));
273+
initTabPanel(tabMine, profileBadgeRadio, R.string.radio_label_profile, R.drawable.radio_bg_profile, listener);
274+
}
275+
276+
@Override
277+
public void addUnknownMineTab() {
278+
// do nothing to prevent adding any MINE tab
279+
hideTabPanel(profileBadgeRadio);
280+
}
281+
282+
@Override
283+
public void addDiscoveryTab() {
284+
TabSpec tabDiscovery = tabHost.newTabSpec(TAB_SPEC_EXPLORE);
285+
tabDiscovery.setIndicator(TAB_SPEC_EXPLORE).setContent(
286+
new Intent(this, DiscoveryActivity.class));
287+
initTabPanel(tabDiscovery, exploreBadgeRadio, R.string.radio_label_explore, R.drawable.radio_bg_explore, listener);
288+
}
289+
290+
@Override
291+
public void addUnknownDiscoveryTab() {
292+
// do nothing to prevent adding any DISCOVERY tab
293+
hideTabPanel(exploreBadgeRadio);
294+
}
295+
283296
private class RadioClickListener implements OnClickListener {
284297
@Override
285298
public void onClick(View v) {
@@ -297,17 +310,18 @@ protected void onDestroy() {
297310
// AndroidEventManager.getInstance().removeEventListener(
298311
// EventCode.UnreadMessageCountChanged, this);
299312
unbinder.unbind();
313+
toastWrapper.cancel();
300314
}
301315

302316
@Override
303317
public boolean dispatchKeyEvent(KeyEvent event) {
304-
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
305-
if ((System.currentTimeMillis() - exitInterval) > 3000) {
306-
Toast.makeText(this, R.string.toast_exit, Toast.LENGTH_LONG).show();
307-
exitInterval = System.currentTimeMillis();
318+
if (KeyEventHelper.isBackKeyDown(event)) {
319+
if (exitingTrigger.testExpired(System.currentTimeMillis())) {
320+
toastWrapper.show(this);
308321
} else {
309322
finish();
310323
}
324+
311325
return true;
312326
}
313327
return super.dispatchKeyEvent(event);
@@ -374,7 +388,7 @@ protected void reportTabClickEvent(int tabIndex) {
374388

375389
}
376390
protected void bindUnreadCountTextView(BadgeRadioButton view) {
377-
int viewId = view.findViewById(R.id.badge).getId();
391+
//bindUnreadCountTextView(view, R.id.badge);
378392
}
379393

380394
// refactor tab view item.

0 commit comments

Comments
 (0)