Skip to content

Commit

Permalink
refactor: DataManager & RootView fix: RxBus resubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
codeestX committed Apr 22, 2017
1 parent 2d737a3 commit 5bf0ca1
Show file tree
Hide file tree
Showing 97 changed files with 1,638 additions and 813 deletions.
24 changes: 19 additions & 5 deletions app/src/main/java/com/codeest/geeknews/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;

import com.codeest.geeknews.app.App;
import com.codeest.geeknews.di.component.ActivityComponent;
import com.codeest.geeknews.di.component.DaggerActivityComponent;
import com.codeest.geeknews.di.module.ActivityModule;
import com.codeest.geeknews.util.SnackbarUtil;

import javax.inject.Inject;

Expand Down Expand Up @@ -42,11 +44,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
initEventAndData();
}

@Override
protected void onResume() {
super.onResume();
}

protected void setToolBar(Toolbar toolbar, String title) {
toolbar.setTitle(title);
setSupportActionBar(toolbar);
Expand Down Expand Up @@ -80,6 +77,11 @@ protected void onDestroy() {
App.getInstance().removeActivity(this);
}

@Override
public void showErrorMsg(String msg) {
SnackbarUtil.show(((ViewGroup) findViewById(android.R.id.content)).getChildAt(0), msg);
}

@Override
public void useNightMode(boolean isNight) {
if (isNight) {
Expand All @@ -92,6 +94,18 @@ public void useNightMode(boolean isNight) {
recreate();
}

@Override
public void stateError() {
}

@Override
public void stateLoading() {
}

@Override
public void stateMain() {
}

protected abstract void initInject();
protected abstract int getLayout();
protected abstract void initEventAndData();
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/codeest/geeknews/base/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.codeest.geeknews.di.component.DaggerFragmentComponent;
import com.codeest.geeknews.di.component.FragmentComponent;
import com.codeest.geeknews.di.module.FragmentModule;
import com.codeest.geeknews.util.SnackbarUtil;

import javax.inject.Inject;

Expand Down Expand Up @@ -93,11 +94,31 @@ public void onDestroyView() {
mUnBinder.unbind();
}

@Override
public void showErrorMsg(String msg) {
SnackbarUtil.show(((ViewGroup) getActivity().findViewById(android.R.id.content)).getChildAt(0), msg);
}

@Override
public void useNightMode(boolean isNight) {

}

@Override
public void stateError() {

}

@Override
public void stateLoading() {

}

@Override
public void stateMain() {

}

protected abstract void initInject();
protected abstract int getLayoutId();
protected abstract void initEventAndData();
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/codeest/geeknews/base/BaseView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
*/
public interface BaseView {

void showError(String msg);
void showErrorMsg(String msg);

void useNightMode(boolean isNight);

//======= State =======
void stateError();

void stateLoading();

void stateMain();
}
93 changes: 93 additions & 0 deletions app/src/main/java/com/codeest/geeknews/base/RootActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.codeest.geeknews.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.codeest.geeknews.R;
import com.codeest.geeknews.widget.ProgressImageView;

/**
* @author: Est <[email protected]>
* @date: 2017/4/21
* @desciption:
*/

public abstract class RootActivity<T extends BasePresenter> extends BaseActivity<T>{

private static final int STATE_MAIN = 0x00;
private static final int STATE_LOADING = 0x01;
private static final int STATE_ERROR = 0x02;

private ProgressImageView ivLoading;
private LinearLayout viewError;
private FrameLayout viewLoading;
private ViewGroup viewMain;
private int currentState = STATE_MAIN;

@Override
protected void initEventAndData() {
viewMain = (ViewGroup) findViewById(R.id.view_main);
if (viewMain == null) {
throw new IllegalStateException(
"The subclass of RootActivity must contain a View named view_main.");
}
if (!(viewMain.getParent() instanceof ViewGroup)) {
throw new IllegalStateException(
"view_main's ParentView should be a ViewGroup");
}
ViewGroup parent = (ViewGroup) viewMain.getParent();
View.inflate(mContext, R.layout.view_error, parent);
View.inflate(mContext, R.layout.view_progress, parent);
viewError = (LinearLayout) parent.findViewById(R.id.view_error);
viewLoading = (FrameLayout) parent.findViewById(R.id.view_loading);
ivLoading = (ProgressImageView) viewLoading.findViewById(R.id.iv_progress);
viewError.setVisibility(View.GONE);
viewLoading.setVisibility(View.GONE);
viewMain.setVisibility(View.VISIBLE);
}

@Override
public void stateError() {
if (currentState == STATE_ERROR)
return;
hideCurrentView();
currentState = STATE_ERROR;
viewError.setVisibility(View.VISIBLE);
}

@Override
public void stateLoading() {
if (currentState == STATE_LOADING)
return;
hideCurrentView();
currentState = STATE_LOADING;
viewLoading.setVisibility(View.VISIBLE);
ivLoading.start();
}

@Override
public void stateMain() {
if (currentState == STATE_MAIN)
return;
hideCurrentView();
currentState = STATE_MAIN;
viewMain.setVisibility(View.VISIBLE);
}

private void hideCurrentView() {
switch (currentState) {
case STATE_MAIN:
viewMain.setVisibility(View.GONE);
break;
case STATE_LOADING:
ivLoading.stop();
viewLoading.setVisibility(View.GONE);
break;
case STATE_ERROR:
viewError.setVisibility(View.GONE);
break;
}
}
}
95 changes: 95 additions & 0 deletions app/src/main/java/com/codeest/geeknews/base/RootFragment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.codeest.geeknews.base;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.codeest.geeknews.R;
import com.codeest.geeknews.widget.ProgressImageView;

/**
* @author: Est <[email protected]>
* @date: 2017/4/21
* @desciption:
*/

public abstract class RootFragment<T extends BasePresenter> extends BaseFragment<T> {

private static final int STATE_MAIN = 0x00;
private static final int STATE_LOADING = 0x01;
private static final int STATE_ERROR = 0x02;

private ProgressImageView ivLoading;
private LinearLayout viewError;
private FrameLayout viewLoading;
private ViewGroup viewMain;
private int currentState = STATE_MAIN;

@Override
protected void initEventAndData() {
if (getView() == null)
return;
viewMain = (ViewGroup) getView().findViewById(R.id.view_main);
if (viewMain == null) {
throw new IllegalStateException(
"The subclass of RootActivity must contain a View named view_main.");
}
if (!(viewMain.getParent() instanceof ViewGroup)) {
throw new IllegalStateException(
"view_main's ParentView should be a ViewGroup.");
}
ViewGroup parent = (ViewGroup) viewMain.getParent();
View.inflate(mContext, R.layout.view_error, parent);
View.inflate(mContext, R.layout.view_progress, parent);
viewError = (LinearLayout) parent.findViewById(R.id.view_error);
viewLoading = (FrameLayout) parent.findViewById(R.id.view_loading);
ivLoading = (ProgressImageView) viewLoading.findViewById(R.id.iv_progress);
viewError.setVisibility(View.GONE);
viewLoading.setVisibility(View.GONE);
viewMain.setVisibility(View.VISIBLE);
}

@Override
public void stateError() {
if (currentState == STATE_ERROR)
return;
hideCurrentView();
currentState = STATE_ERROR;
viewError.setVisibility(View.VISIBLE);
}

@Override
public void stateLoading() {
if (currentState == STATE_LOADING)
return;
hideCurrentView();
currentState = STATE_LOADING;
viewLoading.setVisibility(View.VISIBLE);
ivLoading.start();
}

@Override
public void stateMain() {
if (currentState == STATE_MAIN)
return;
hideCurrentView();
currentState = STATE_MAIN;
viewMain.setVisibility(View.VISIBLE);
}

private void hideCurrentView() {
switch (currentState) {
case STATE_MAIN:
viewMain.setVisibility(View.GONE);
break;
case STATE_LOADING:
ivLoading.stop();
viewLoading.setVisibility(View.GONE);
break;
case STATE_ERROR:
viewError.setVisibility(View.GONE);
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.gank;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.gank;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.gold;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.gold;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.main;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand All @@ -23,5 +23,9 @@ interface Presenter extends BasePresenter<View> {
void deleteLikeData(String id);

void changeLikeTime(String id,long time,boolean isPlus);

boolean getLikePoint();

void setLikePoint(boolean b);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.main;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand All @@ -22,5 +22,15 @@ interface Presenter extends BasePresenter<View> {
void checkVersion(String currentVersion);

void checkPermissions(RxPermissions rxPermissions);

void setNightModeState(boolean b);

void setCurrentItem(int index);

int getCurrentItem();

void setVersionPoint(boolean b);

boolean getVersionPoint();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.main;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand All @@ -20,5 +20,16 @@ interface Presenter extends BasePresenter<SettingContract.View> {

void checkVersion(String currentVersion);

void setNightModeState(boolean b);

void setNoImageState(boolean b);

void setAutoCacheState(boolean b);

boolean getNightModeState();

boolean getNoImageState();

boolean getAutoCacheState();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.codeest.geeknews.presenter.contract;
package com.codeest.geeknews.base.contract.main;

import com.codeest.geeknews.base.BasePresenter;
import com.codeest.geeknews.base.BaseView;
Expand Down
Loading

0 comments on commit 5bf0ca1

Please sign in to comment.