Skip to content

Commit

Permalink
More options
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubkinst committed Aug 8, 2015
1 parent 744e5e2 commit 6b1808a
Show file tree
Hide file tree
Showing 36 changed files with 286 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions app/src/main/res/layout/custom_placeholder_offline.xml

This file was deleted.

9 changes: 0 additions & 9 deletions app/src/main/res/values/styles.xml

This file was deleted.

1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-annotations:22.2.1'
}
1 change: 1 addition & 0 deletions library/library.iml
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="support-annotations-22.2.1" level="project" />
</component>
</module>
187 changes: 136 additions & 51 deletions library/src/main/java/cz/kinst/jakub/view/StatefulView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -12,99 +16,161 @@
/**
* Created by jakubkinst on 05/08/15.
*/
public class StatefulView extends FrameLayout
{
private final int mTextAppearance;
public class StatefulView extends FrameLayout {
private int mTextAppearance;
private State mInitialState;
private String mCustomEmptyText;
private String mCustomOfflineText;
private View mOfflineView, mEmptyView, mProgressView;
private ViewState mViewState = null;
private State mState = null;
private View mContent;
private FrameLayout mContainerProgress, mContainerOffline, mContainerEmpty;
private TextView mDefaultEmptyText, mDefaultOfflineText;


public StatefulView(Context context)
{
public StatefulView(Context context) {
this(context, null);
}


public StatefulView(Context context, AttributeSet attrs)
{
public StatefulView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}


public StatefulView(Context context, AttributeSet attrs, int defStyleAttr)
{
public StatefulView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StatefulView);
mTextAppearance = a.getResourceId(R.styleable.StatefulView_stateTextAppearance, R.style.TextAppearanceStateDefault);
mOfflineView = LayoutInflater.from(context).inflate(a.getResourceId(R.styleable.StatefulView_offlineLayout, R.layout.default_placeholder_offline), null);
mEmptyView = LayoutInflater.from(context).inflate(a.getResourceId(R.styleable.StatefulView_emptyLayout, R.layout.default_placeholder_empty), null);
mProgressView = LayoutInflater.from(context).inflate(a.getResourceId(R.styleable.StatefulView_progressLayout, R.layout.default_placeholder_progress), null);

// get custom texts if set
if(a.hasValue(R.styleable.StatefulView_emptyText))
mCustomEmptyText = a.getString(R.styleable.StatefulView_emptyText);
if(a.hasValue(R.styleable.StatefulView_offlineText))
mCustomOfflineText = a.getString(R.styleable.StatefulView_offlineText);

// get initial state if set
if(a.hasValue(R.styleable.StatefulView_state)) { //TODO: maybe set initial state to content if not set
mInitialState = State.fromId(a.getInt(R.styleable.StatefulView_state, State.CONTENT.id));
}


}


public void setEmptyText(@StringRes int resourceId) {
setEmptyText(getResources().getString(resourceId));
}


public void setEmptyText(CharSequence emptyText) {
if(mDefaultOfflineText != null)
mDefaultOfflineText.setText(emptyText);
}


public void setEmptyImageDrawable(Drawable drawable) {
TintableImageView image = ((TintableImageView) mEmptyView.findViewById(R.id.state_image));
image.setVisibility(drawable != null ? VISIBLE : GONE);
image.setImageDrawable(drawable);
}


public void setEmptyImageResource(@DrawableRes int resourceId) {
setEmptyImageDrawable(getResources().getDrawable(resourceId));
}


public void setOfflineText(@StringRes int resourceId) {
setOfflineText(getResources().getString(resourceId));
}


public void setOfflineText(CharSequence offlineText) {
if(mDefaultOfflineText != null)
mDefaultOfflineText.setText(offlineText);
}


public void setEmptyText(CharSequence emptyText)
{
((TextView) findViewById(R.id.placeholder_empty_text)).setText(emptyText);
public void setOfflineImageDrawable(Drawable drawable) {
TintableImageView image = ((TintableImageView) mOfflineView.findViewById(R.id.state_image));
image.setVisibility(drawable != null ? VISIBLE : GONE);
image.setImageDrawable(drawable);
}


public void setOfflineText(CharSequence offlineText)
{
((TextView) findViewById(R.id.placeholder_offline_text)).setText(offlineText);
public void setOfflineImageResource(@DrawableRes int resourceId) {
setOfflineImageDrawable(getResources().getDrawable(resourceId));
}


public void showContent()
{
setViewState(ViewState.CONTENT);
public void setTextColor(@ColorInt int color) {
((TintableImageView) findViewById(R.id.state_image)).setTintColor(color);
((TintableImageView) findViewById(R.id.state_image)).setTintColor(color);
((TextView) findViewById(R.id.state_text)).setTextColor(color);
((TextView) findViewById(R.id.state_text)).setTextColor(color);
}


public void showProgress()
{
setViewState(ViewState.PROGRESS);
public void showContent() {
setState(State.CONTENT);
}


public void showOffline()
{
setViewState(ViewState.OFFLINE);
public void showProgress() {
setState(State.PROGRESS);
}


public void showEmpty()
{
setViewState(ViewState.EMPTY);
public void showOffline() {
setState(State.OFFLINE);
}


public ViewState getViewState()
{
return mViewState;
public void showEmpty() {
setState(State.EMPTY);
}


public void setViewState(ViewState viewState)
{
mViewState = viewState;
public State getState() {
return mState;
}


public void setState(State state) {
mState = state;
if(mContent != null)
mContent.setVisibility(viewState == ViewState.CONTENT ? View.VISIBLE : View.GONE);
mContent.setVisibility(state == State.CONTENT ? View.VISIBLE : View.GONE);
if(mContainerProgress != null)
mContainerProgress.setVisibility(viewState == ViewState.PROGRESS ? View.VISIBLE : View.GONE);
mContainerProgress.setVisibility(state == State.PROGRESS ? View.VISIBLE : View.GONE);
if(mContainerOffline != null)
mContainerOffline.setVisibility(viewState == ViewState.OFFLINE ? View.VISIBLE : View.GONE);
mContainerOffline.setVisibility(state == State.OFFLINE ? View.VISIBLE : View.GONE);
if(mContainerEmpty != null)
mContainerEmpty.setVisibility(viewState == ViewState.EMPTY ? View.VISIBLE : View.GONE);
mContainerEmpty.setVisibility(state == State.EMPTY ? View.VISIBLE : View.GONE);
}


@Override
protected void onFinishInflate()
{
protected void onFinishInflate() {
super.onFinishInflate();

initialize();


}


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


private void initialize() {
mContent = getChildAt(0);
addView(LayoutInflater.from(getContext()).inflate(R.layout.view_stateful, this, false));
mContainerProgress = (FrameLayout) findViewById(R.id.container_progress);
Expand All @@ -114,21 +180,40 @@ protected void onFinishInflate()
mContainerEmpty = (FrameLayout) findViewById(R.id.container_empty);
mContainerEmpty.addView(mEmptyView);

((TextView) findViewById(R.id.placeholder_empty_text)).setTextAppearance(getContext(), mTextAppearance);
((TextView) findViewById(R.id.placeholder_empty_text)).setTextAppearance(getContext(), mTextAppearance);

mDefaultEmptyText = ((TextView) mEmptyView.findViewById(R.id.state_text));
if(mDefaultEmptyText != null) {
mDefaultEmptyText.setTextAppearance(getContext(), mTextAppearance);
if(mCustomEmptyText != null)
setEmptyText(mCustomEmptyText);
}

mDefaultOfflineText = ((TextView) mOfflineView.findViewById(R.id.state_text));
if(mDefaultOfflineText != null) {
mDefaultOfflineText.setTextAppearance(getContext(), mTextAppearance);
if(mCustomOfflineText != null)
setEmptyText(mCustomOfflineText);
}

if(mInitialState != null)
setState(mInitialState);
}


@Override
protected void onAttachedToWindow()
{
super.onAttachedToWindow();
initialize();
}
public enum State {
CONTENT(0), PROGRESS(1), OFFLINE(2), EMPTY(3);
int id;


State(int id) {
this.id = id;
}


private void initialize()
{
static State fromId(int id) {
for(State f : values()) {
if(f.id == id) return f;
}
throw new IllegalArgumentException();
}
}
}
51 changes: 51 additions & 0 deletions library/src/main/java/cz/kinst/jakub/view/TintableImageView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cz.kinst.jakub.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.util.AttributeSet;
import android.widget.ImageView;


public class TintableImageView extends ImageView {


public TintableImageView(Context context) {
super(context);
}


public TintableImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}


public TintableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}


private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle, 0);
int tintColor = a.getColor(R.styleable.TintableImageView_tint, Color.BLACK);
a.recycle();
setTintColor(tintColor);
}


public void setTintColor(@ColorInt int color) {
super.setColorFilter(color, PorterDuff.Mode.SRC_IN);
}


public void setTintColorResource(@ColorRes int colorResource) {
super.setColorFilter(getContext().getResources().getColor(colorResource), PorterDuff.Mode.SRC_IN);
}


}
7 changes: 0 additions & 7 deletions library/src/main/java/cz/kinst/jakub/view/ViewState.java

This file was deleted.

Binary file added library/src/main/res/drawable-hdpi/ic_offline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added library/src/main/res/drawable-mdpi/ic_offline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6b1808a

Please sign in to comment.