Skip to content

Commit

Permalink
Merge pull request #36 from santalu/development
Browse files Browse the repository at this point in the history
Add ability to include or exclude views from state changes
  • Loading branch information
santalu authored Apr 27, 2018
2 parents cf123a2 + f4c896f commit 0945b13
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 32 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/santalu/myapplication/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import com.santalu.emptyview.EmptyView;
Expand All @@ -29,16 +28,17 @@ protected void onCreate(Bundle savedInstanceState) {
.setOnClickListener(v -> emptyView.showLoading())
.show();*/

// demonstrates how to include and exclude views from state changes
/*emptyView.empty()
.setEmptyDrawable(R.mipmap.ic_launcher)
.setEmptyTitle("Empty Title")
.setEmptyText("Empty Text")
.setEmptyButtonText("Empty Button")
.exclude(R.id.text)
.setOnClickListener(v ->
emptyView.loading()
.exclude(0)
.include(R.id.text)
.show())
//.exclude(R.id.text)
.show();*/
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.android.tools.build:gradle:3.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 26
versionName "1.3.3"
versionCode 27
versionName "1.3.4"
vectorDrawables.useSupportLibrary = true
}

Expand Down
20 changes: 3 additions & 17 deletions library/src/main/java/com/santalu/emptyview/EmptyView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

import static com.santalu.emptyview.EmptyViewBuilder.CONTENT;
import static com.santalu.emptyview.EmptyViewBuilder.EMPTY;
Expand All @@ -33,7 +31,6 @@
public class EmptyView extends ConstraintLayout {

private final EmptyViewBuilder builder;
private final List<View> children;

private LinearLayout container;
private ProgressBar progressBar;
Expand All @@ -45,26 +42,23 @@ public class EmptyView extends ConstraintLayout {
public EmptyView(Context context) {
super(context);
builder = new EmptyViewBuilder(this);
children = new ArrayList<>();
}

public EmptyView(Context context, AttributeSet attrs) {
super(context, attrs);
builder = new EmptyViewBuilder(this, attrs);
children = new ArrayList<>();
}

public EmptyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
builder = new EmptyViewBuilder(this, attrs);
children = new ArrayList<>();
}

@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
if (child.getVisibility() == VISIBLE) {
children.add(child);
builder.include(child);
}
}

Expand Down Expand Up @@ -206,16 +200,8 @@ void show() {
}

private void setChildVisibility(int visibility) {
if (builder.excludedViews == null || builder.excludedViews.isEmpty()) {
for (View view : children) {
view.setVisibility(visibility);
}
return;
}
for (View view : children) {
if (!builder.excludedViews.contains(view)) {
view.setVisibility(visibility);
}
for (View view : builder.children) {
view.setVisibility(visibility);
}
}

Expand Down
32 changes: 25 additions & 7 deletions library/src/main/java/com/santalu/emptyview/EmptyViewBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class EmptyViewBuilder {

private final EmptyView emptyView;
private final Context context;
List<View> excludedViews;
List<View> children;
@State int state;
int gravity;

Expand Down Expand Up @@ -104,6 +104,7 @@ public class EmptyViewBuilder {
EmptyViewBuilder(EmptyView emptyView) {
this.emptyView = emptyView;
this.context = emptyView.getContext();
this.children = new ArrayList<>();
}

EmptyViewBuilder(EmptyView emptyView, @NonNull AttributeSet attributeSet) {
Expand Down Expand Up @@ -229,21 +230,38 @@ public EmptyViewBuilder setOnClickListener(View.OnClickListener onClickListener)
}

public EmptyViewBuilder exclude(@IdRes int... ids) {
excludedViews = new ArrayList<>();
for (int id : ids) {
View view = emptyView.findViewById(id);
if (!excludedViews.contains(view)) {
excludedViews.add(view);
if (children.contains(view)) {
children.remove(view);
}
}
return this;
}

public EmptyViewBuilder exclude(View... views) {
excludedViews = new ArrayList<>();
for (View view : views) {
if (!excludedViews.contains(view)) {
excludedViews.add(view);
if (children.contains(view)) {
children.remove(view);
}
}
return this;
}

public EmptyViewBuilder include(@IdRes int... ids) {
for (int id : ids) {
View view = emptyView.findViewById(id);
if (!children.contains(view)) {
children.add(view);
}
}
return this;
}

public EmptyViewBuilder include(View... views) {
for (View view : views) {
if (!children.contains(view)) {
children.add(view);
}
}
return this;
Expand Down
5 changes: 4 additions & 1 deletion library/src/main/java/com/santalu/emptyview/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.concurrent.TimeoutException;
import javax.net.ssl.SSLException;

/**
* Created by fatih.santalu on 24.01.2018.
Expand All @@ -30,7 +31,9 @@ public enum Error {
public static Error get(Throwable e) {
if (e instanceof ConnectException || e instanceof UnknownHostException) {
return CONNECTION;
} else if (e instanceof SocketTimeoutException || e instanceof TimeoutException) {
} else if (e instanceof SocketTimeoutException ||
e instanceof TimeoutException ||
e instanceof SSLException) {
return TIMEOUT;
} else if (!TextUtils.isEmpty(e.getMessage())) {
SERVICE.message = e.getMessage();
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/layout/empty_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
android:gravity="center"
android:textAppearance="@style/EmptyTextAppearance.Body"/>

<android.support.v7.widget.AppCompatButton
<Button
android:id="@+id/empty_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 0945b13

Please sign in to comment.