Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Android Studio and empty state for views. #3

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 2 additions & 0 deletions library/.gitignore → .gitignore
Original file line number Diff line number Diff line change
@@ -8,3 +8,5 @@ local.properties
*.BridgeSort
out/
tmp/
build/*
*.iml
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 27 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}

apply plugin: 'android-library'

android {
compileSdkVersion 19
buildToolsVersion = "19.0.3"

sourceSets {
main {
res.srcDirs = ['res']
aidl.srcDirs = ['src']
resources.srcDirs = ['src']
renderscript.srcDirs = ['src']
java.srcDirs = ['src']
manifest.srcFile 'AndroidManifest.xml'

}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
14 changes: 14 additions & 0 deletions res/layout/msv__empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/nothing_to_show"/>

</FrameLayout>
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -8,12 +8,15 @@
<enum name="content" value="0x00" />
<!-- The state is to show loading indication -->
<enum name="loading" value="0x01" />
<!-- The state is to show loading indication -->
<!-- The state is to show network errors -->
<enum name="error_network" value="0x02" />
<!-- The state is to show loading indication -->
<!-- The state is to show other errors -->
<enum name="error_unknown" value="0x03" />
<!-- The state is to show empty view state -->
<enum name="empty" value="0x04" />
</attr>

<attr name="msvEmptyLayout" format="reference" />
<attr name="msvLoadingLayout" format="reference" />
<attr name="msvErrorUnknownLayout" format="reference" />
<attr name="msvErrorNetworkLayout" format="reference" />
@@ -23,4 +26,4 @@
<attr name="msvErrorTapToRetryStringId" format="string" />
</declare-styleable>

</resources>
</resources>
Original file line number Diff line number Diff line change
@@ -4,5 +4,6 @@
<string name="error_title_network">Network Error</string>
<string name="error_title_unknown">Oops! We messed up.</string>
<string name="tap_to_retry">Tap to retry</string>
<string name="nothing_to_show">Nothing to show</string>

</resources>
</resources>
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ public class MultiStateView extends FrameLayout {
private MultiStateViewData mViewState = new MultiStateViewData(ContentState.CONTENT);

private View mContentView;
private View mEmptyView;
private View mLoadingView;
private View mNetworkErrorView;
private View mGeneralErrorView;
@@ -55,6 +56,7 @@ private void parseAttrs(Context context, AttributeSet attrs) {
setLoadingLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvLoadingLayout, R.layout.msv__loading));
setGeneralErrorLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvErrorUnknownLayout, R.layout.msv__error_unknown));
setNetworkErrorLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvErrorNetworkLayout, R.layout.msv__error_network));
setEmptyLayoutResourceId(a.getResourceId(R.styleable.MultiStateView_msvEmptyLayout, R.layout.msv__empty));

String tmpString;

@@ -82,6 +84,14 @@ private void parseAttrs(Context context, AttributeSet attrs) {

setTapToRetryString(tmpString);

tmpString = a.getString(R.styleable.MultiStateView_msvEmptyLayout);

if (tmpString == null) {
tmpString = context.getString(R.string.nothing_to_show);
}

setEmptyString(tmpString);

setState(a.getInt(R.styleable.MultiStateView_msvState, ContentState.CONTENT.nativeInt));
} finally {
a.recycle();
@@ -140,6 +150,18 @@ public void setLoadingLayoutResourceId(int loadingLayout) {
this.mViewState.loadingLayoutResId = loadingLayout;
}

private void setEmptyLayoutResourceId(int resourceId) {
mViewState.emptyLayoutResId = resourceId;
}

private void setEmptyString(String string) {
mViewState.emptyString = string;
}

public String getEmptyString() {
return mViewState.emptyString;
}

/**
* @return the {@link ContentState} the view is currently in
*/
@@ -207,6 +229,9 @@ public void setState(final ContentState state) {
*/
public View getStateView(ContentState state) {
switch (state) {
case EMPTY:
return getEmptyView();

case ERROR_NETWORK:
return getNetworkErrorView();

@@ -223,6 +248,21 @@ public View getStateView(ContentState state) {
return null;
}

/**
* Returns the view to be displayed when there is nothing to show (e.g., no search results)
*/
public View getEmptyView() {
if (mEmptyView == null) {
mEmptyView = View.inflate(getContext(), mViewState.emptyLayoutResId, null);

((TextView) mEmptyView.findViewById(R.id.empty)).setText(getEmptyString());

addView(mEmptyView);
}

return mEmptyView;
}

/**
* Returns the view to be displayed for the case of a network error
*
@@ -322,7 +362,8 @@ public void setContentView(View contentView) {
}

private boolean isViewInternal(View view) {
return view == mNetworkErrorView || view == mGeneralErrorView || view == mLoadingView;
return view == mNetworkErrorView || view == mGeneralErrorView
|| view == mLoadingView || view == mEmptyView;
}

@Override
@@ -359,6 +400,8 @@ private void setViewState(MultiStateViewData state) {
setNetworkErrorLayoutResourceId(state.networkErrorLayoutResId);
setLoadingLayoutResourceId(state.loadingLayoutResId);
setCustomErrorString(state.customErrorString);
setEmptyLayoutResourceId(state.emptyLayoutResId);
setEmptyString(state.emptyString);
}

@Override
@@ -448,7 +491,13 @@ public static enum ContentState {
*
* @see R.attr#msvState
*/
ERROR_GENERAL(0x03);
ERROR_GENERAL(0x03),
/**
* Used to indicate that the Empty indication should be displayed to the user
*
* @see R.attr#msvState
*/
EMPTY(0x04);

public final int nativeInt;
private final static SparseArray<ContentState> sStates = new SparseArray<ContentState>();
@@ -506,9 +555,11 @@ public static class MultiStateViewData implements Parcelable {
public int loadingLayoutResId;
public int generalErrorLayoutResId;
public int networkErrorLayoutResId;
public int emptyLayoutResId;
public String networkErrorTitleString;
public String generalErrorTitleString;
public String tapToRetryString;
public String emptyString;
public ContentState state;

public MultiStateViewData(ContentState contentState) {
@@ -523,6 +574,7 @@ private MultiStateViewData(Parcel in) {
networkErrorTitleString = in.readString();
generalErrorTitleString = in.readString();
tapToRetryString = in.readString();
emptyString = in.readString();
state = ContentState.valueOf(in.readString());
}

@@ -538,6 +590,7 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeString(networkErrorTitleString);
dest.writeString(generalErrorTitleString);
dest.writeString(tapToRetryString);
dest.writeString(emptyString);
dest.writeString(state.name());
}