Skip to content

Commit

Permalink
throw IllegalStateException when some id was not set.
Browse files Browse the repository at this point in the history
  • Loading branch information
SmartDengg committed Apr 11, 2016
1 parent 69e8362 commit c98fe8f
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 29 deletions.
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
# ProgressLayout
A simple progress layout
#ProgressLayout

![](https://github.com/SmartDengg/ProgressLayout/blob/master/ScreenShot/progressLayout.gif)
![](https://github.com/SmartDengg/ProgressLayout/blob/master/screenShot/launcher.png)




##Sketch
-----------------



##Usage
-----------------



##ScreenShot
-----------------

![](https://github.com/SmartDengg/ProgressLayout/blob/master/screenShot/progressLayout.gif)


##Developed By
-----------------

- 小鄧子 - Hi4Joker@gmail.com

[小鄧子的简书](http://www.jianshu.com/users/df40282480b4/latest_articles)

[小鄧子的慕课网专题](http://www.imooc.com/myclub/article/uid/2536335)

<a href="http://weibo.com/5367097592/profile?rightmod=1&wvr=6&mod=personinfo">
<img alt="Follow me on Weibo" src="http://upload-images.jianshu.io/upload_images/268450-50e41e15ac29b776.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" />
</a>

<a href="http://chuantu.biz/t2/18/1446906570x1822611354.png">
<img alt="Follow me on Wechat" src="http://upload-images.jianshu.io/upload_images/268450-1025666a7a10ec97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" />
</a>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
public class ProgressLayout extends RelativeLayout {

private static final int defStyleAttr = R.attr.progressLayoutDefStyle;
private static final int NOT_SET = -1;

private static final String LOADING_TAG = "ProgressLayout.loading_tag";
private static final String NONE_TAG = "ProgressLayout.none_tag";
Expand Down Expand Up @@ -48,7 +49,8 @@ public class ProgressLayout extends RelativeLayout {
@IntDef(value = { LOADING, NONE, CONTENT, NETWORK_ERROR, FAILED })
public @interface LAYOUT_TYPE {}

@LAYOUT_TYPE private int currentState = LOADING;
@LAYOUT_TYPE
private int currentState = LOADING;

public ProgressLayout(Context context) {
this(context, null);
Expand All @@ -67,16 +69,25 @@ public ProgressLayout(Context context, AttributeSet attrs, int defStyleAttr) {

private void init(Context context, AttributeSet attrs, int defStyleAttr) {

this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressLayout, defStyleAttr, R.style.DefaultSmartStyle);
this.layoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TypedArray typedArray =
context.obtainStyledAttributes(attrs, R.styleable.ProgressLayout, defStyleAttr,
R.style.DefaultSmartStyle);

if (typedArray == null) return;
if (typedArray == null) {
return;
}

try {
this.loadingId = typedArray.getResourceId(R.styleable.ProgressLayout_loading_layout, -1);
this.noneId = typedArray.getResourceId(R.styleable.ProgressLayout_none_content, -1);
this.networkErrorId = typedArray.getResourceId(R.styleable.ProgressLayout_network_content, -1);
this.failedId = typedArray.getResourceId(R.styleable.ProgressLayout_failed_content, -1);
this.loadingId =
typedArray.getResourceId(R.styleable.ProgressLayout_loading_layout, NOT_SET);
this.noneId =
typedArray.getResourceId(R.styleable.ProgressLayout_none_content, NOT_SET);
this.networkErrorId =
typedArray.getResourceId(R.styleable.ProgressLayout_network_content, NOT_SET);
this.failedId =
typedArray.getResourceId(R.styleable.ProgressLayout_failed_content, NOT_SET);
} finally {
typedArray.recycle();
}
Expand All @@ -87,7 +98,8 @@ public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);

if (child.getTag() == null ||
(!child.getTag().equals(LOADING_TAG) && !child.getTag().equals(NONE_TAG) && !child.getTag().equals(ERROR_TAG))) {
(!child.getTag().equals(LOADING_TAG) && !child.getTag().equals(NONE_TAG) &&
!child.getTag().equals(ERROR_TAG))) {

this.contentViews.add(child);
this.setContentVisibility(false);
Expand All @@ -98,7 +110,7 @@ public void showLoading() {

ProgressLayout.this.showLoadingView();

ProgressLayout.this.hideNoContentView();
ProgressLayout.this.hideNoneView();
ProgressLayout.this.hideNetErrorView();
ProgressLayout.this.hideFailedView();
ProgressLayout.this.setContentVisibility(false);
Expand Down Expand Up @@ -127,7 +139,7 @@ public void showNetError(@Nullable OnClickListener retryListener) {
ProgressLayout.this.showNetErrorView(retryListener);

ProgressLayout.this.hideLoadingView();
ProgressLayout.this.hideNoContentView();
ProgressLayout.this.hideNoneView();
ProgressLayout.this.hideFailedView();
ProgressLayout.this.setContentVisibility(false);
}
Expand All @@ -141,15 +153,15 @@ public void showFailed(@Nullable OnClickListener retryListener) {
ProgressLayout.this.showFailedView(retryListener);

ProgressLayout.this.hideLoadingView();
ProgressLayout.this.hideNoContentView();
ProgressLayout.this.hideNoneView();
ProgressLayout.this.hideNetErrorView();
ProgressLayout.this.setContentVisibility(false);
}

public void showContent() {

ProgressLayout.this.hideLoadingView();
ProgressLayout.this.hideNoContentView();
ProgressLayout.this.hideNoneView();
ProgressLayout.this.hideNetErrorView();
ProgressLayout.this.hideFailedView();

Expand All @@ -166,7 +178,13 @@ private void showLoadingView() {

if (this.loadingContainer == null) {

this.loadingContainer = this.layoutInflater.inflate(loadingId, ProgressLayout.this, false);
if (loadingId == NOT_SET) {
throw new IllegalStateException(
"cannot call showLoadingView() when loadingId was equals -1");
}

this.loadingContainer =
this.layoutInflater.inflate(loadingId, ProgressLayout.this, false);
this.loadingContainer.setTag(LOADING_TAG);

LayoutParams layoutParams = (LayoutParams) loadingContainer.getLayoutParams();
Expand All @@ -188,6 +206,11 @@ private void showNoneView(OnClickListener retryListener) {

if (this.noneContainer == null) {

if (noneId == NOT_SET) {
throw new IllegalStateException(
"cannot call showNoneView() when noneId was equals -1");
}

this.noneContainer = this.layoutInflater.inflate(noneId, ProgressLayout.this, false);
this.noneContainer.setTag(NONE_TAG);

Expand Down Expand Up @@ -215,7 +238,13 @@ private void showNetErrorView(OnClickListener retryListener) {

if (this.networkErrorContainer == null) {

this.networkErrorContainer = this.layoutInflater.inflate(networkErrorId, ProgressLayout.this, false);
if (networkErrorId == NOT_SET) {
throw new IllegalStateException(
"cannot call showNetErrorView() when networkErrorId was equals -1");
}

this.networkErrorContainer =
this.layoutInflater.inflate(networkErrorId, ProgressLayout.this, false);
this.networkErrorContainer.setTag(ERROR_TAG);

LayoutParams layoutParams = (LayoutParams) networkErrorContainer.getLayoutParams();
Expand All @@ -242,7 +271,13 @@ private void showFailedView(OnClickListener retryListener) {

if (this.failedContainer == null) {

this.failedContainer = this.layoutInflater.inflate(failedId, ProgressLayout.this, false);
if (failedId == NOT_SET) {
throw new IllegalStateException(
"cannot call showFailedView() when failedId was equals -1");
}

this.failedContainer =
this.layoutInflater.inflate(failedId, ProgressLayout.this, false);
this.failedContainer.setTag(ERROR_TAG);

LayoutParams layoutParams = (LayoutParams) failedContainer.getLayoutParams();
Expand All @@ -267,7 +302,7 @@ private void hideLoadingView() {
}

/** 隐藏无内容界面 */
private void hideNoContentView() {
private void hideNoneView() {
if (noneContainer != null && noneContainer.getVisibility() != GONE) {
this.noneContainer.setVisibility(GONE);
}
Expand All @@ -280,7 +315,7 @@ private void hideNetErrorView() {
}
}

/** 隐藏网络错误界面 */
/** 隐藏数据错误界面 */
private void hideFailedView() {
if (failedContainer != null && failedContainer.getVisibility() != GONE) {
this.failedContainer.setVisibility(GONE);
Expand All @@ -291,22 +326,22 @@ public boolean isLoading() {
return this.currentState == LOADING;
}

public boolean isnOContent() {
return this.currentState == NONE;
}

public boolean isContent() {
return this.currentState == CONTENT;
}

public boolean isFailed() {
return this.currentState == FAILED;
public boolean isNone() {
return this.currentState == NONE;
}

public boolean isNetworkError() {
return this.currentState == NETWORK_ERROR;
}

public boolean isFailed() {
return this.currentState == FAILED;
}

private void setContentVisibility(boolean visible) {
for (View contentView : contentViews) {
contentView.setVisibility(visible ? View.VISIBLE : View.GONE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onClick(View v) {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(R.layout.main_layout);
ButterKnife.bind(MainActivity.this);

this.progressLayout.showLoading();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
android:textSize="@dimen/material_23sp"
android:shadowDy="10"
android:shadowRadius="4"
android:textColor="@android:color/holo_green_dark"
android:textColor="@color/homeLinkGreen"
android:shadowColor="@android:color/holo_orange_light"
android:text="@string/homelink"
/>
Expand Down
Binary file modified sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

<color name="homeLinkGreen">#00ae66</color>
</resources>
Binary file added screenShot/launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c98fe8f

Please sign in to comment.