From f4c896ff8b6f2e94e719ebd63cb33637bb51cd89 Mon Sep 17 00:00:00 2001 From: santalu Date: Fri, 27 Apr 2018 10:19:26 +0300 Subject: [PATCH] Add ability to include or exclude views from state changes --- .../santalu/myapplication/MainActivity.java | 6 ++-- build.gradle | 2 +- library/build.gradle | 4 +-- .../java/com/santalu/emptyview/EmptyView.java | 20 ++---------- .../santalu/emptyview/EmptyViewBuilder.java | 32 +++++++++++++++---- .../java/com/santalu/emptyview/Error.java | 5 ++- library/src/main/res/layout/empty_view.xml | 2 +- 7 files changed, 39 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/santalu/myapplication/MainActivity.java b/app/src/main/java/com/santalu/myapplication/MainActivity.java index 600a3c8..770655e 100644 --- a/app/src/main/java/com/santalu/myapplication/MainActivity.java +++ b/app/src/main/java/com/santalu/myapplication/MainActivity.java @@ -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; @@ -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();*/ } diff --git a/build.gradle b/build.gradle index 9f3a74a..8f764b0 100644 --- a/build.gradle +++ b/build.gradle @@ -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 diff --git a/library/build.gradle b/library/build.gradle index dc3509d..a3bd05d 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -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 } diff --git a/library/src/main/java/com/santalu/emptyview/EmptyView.java b/library/src/main/java/com/santalu/emptyview/EmptyView.java index 26a9df5..e02432d 100644 --- a/library/src/main/java/com/santalu/emptyview/EmptyView.java +++ b/library/src/main/java/com/santalu/emptyview/EmptyView.java @@ -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; @@ -33,7 +31,6 @@ public class EmptyView extends ConstraintLayout { private final EmptyViewBuilder builder; - private final List children; private LinearLayout container; private ProgressBar progressBar; @@ -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); } } @@ -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); } } diff --git a/library/src/main/java/com/santalu/emptyview/EmptyViewBuilder.java b/library/src/main/java/com/santalu/emptyview/EmptyViewBuilder.java index f06f64b..d32a14e 100644 --- a/library/src/main/java/com/santalu/emptyview/EmptyViewBuilder.java +++ b/library/src/main/java/com/santalu/emptyview/EmptyViewBuilder.java @@ -55,7 +55,7 @@ public class EmptyViewBuilder { private final EmptyView emptyView; private final Context context; - List excludedViews; + List children; @State int state; int gravity; @@ -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) { @@ -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; diff --git a/library/src/main/java/com/santalu/emptyview/Error.java b/library/src/main/java/com/santalu/emptyview/Error.java index 5662125..74d9103 100644 --- a/library/src/main/java/com/santalu/emptyview/Error.java +++ b/library/src/main/java/com/santalu/emptyview/Error.java @@ -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. @@ -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(); diff --git a/library/src/main/res/layout/empty_view.xml b/library/src/main/res/layout/empty_view.xml index 5f2a8fb..9772bcc 100644 --- a/library/src/main/res/layout/empty_view.xml +++ b/library/src/main/res/layout/empty_view.xml @@ -43,7 +43,7 @@ android:gravity="center" android:textAppearance="@style/EmptyTextAppearance.Body"/> -