diff --git a/README.md b/README.md index 3b51900..ffa3585 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ repositories { ``` ```Gradle dependencies { - compile 'rebus:header-view:2.0.0-BETA-3' + compile 'rebus:header-view:2.0.0-BETA-4' } ``` ### How to use diff --git a/app/src/main/java/rebus/header/view/sample/MainActivity.java b/app/src/main/java/rebus/header/view/sample/MainActivity.java index ce5b523..2269a2e 100644 --- a/app/src/main/java/rebus/header/view/sample/MainActivity.java +++ b/app/src/main/java/rebus/header/view/sample/MainActivity.java @@ -164,7 +164,8 @@ public boolean onAdd() { .build(); headerView.addProfile(newProfile); headerView.setProfileActive(100); - return true; + headerView.dismissProfileChooser(); + return false; } }); diff --git a/build.gradle b/build.gradle index 974f905..0d42bfd 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ buildscript { buildTools: "25.0.2", minSdk : 11, targetSdk : 25, - version : "2.0.0-BETA-3", + version : "2.0.0-BETA-4", supportLib: "25.1.0" ] } diff --git a/library/src/main/java/rebus/header/view/HeaderView.java b/library/src/main/java/rebus/header/view/HeaderView.java index 5b423ec..86aa1ac 100644 --- a/library/src/main/java/rebus/header/view/HeaderView.java +++ b/library/src/main/java/rebus/header/view/HeaderView.java @@ -64,14 +64,12 @@ public class HeaderView extends ViewGroup implements ProfileChooserCallback { - private static final String TAG = HeaderView.class.getName(); - private static final String PROFILE_LIST = "PROFILE_LIST"; - public static final int STYLE_NORMAL = 1; public static final int STYLE_COMPACT = 2; public static final int THEME_LIGHT = 1; public static final int THEME_DARK = 2; - + private static final String TAG = HeaderView.class.getName(); + private static final String PROFILE_LIST = "PROFILE_LIST"; private int statusBarHeight; private CircleImageView avatar; @@ -110,6 +108,7 @@ public class HeaderView extends ViewGroup implements ProfileChooserCallback { private HeaderCallback headerCallback; private FragmentManager hvFragmentManager; + private ProfileChooser profileChooser; public HeaderView(Context context) { super(context); @@ -375,6 +374,21 @@ public void setDialogTitle(String dialogTitle) { hvDialogTitle = dialogTitle; } + /** + * dismiss profile chooser dialog + */ + public void dismissProfileChooser() { + if (hvFragmentManager != null) { + ProfileChooserFragment profileChooserFragment = (ProfileChooserFragment) hvFragmentManager.findFragmentByTag(ProfileChooserFragment.FRAGMENT_TAG); + if (profileChooserFragment != null) { + profileChooserFragment.dismiss(); + } + } + if (profileChooser != null && profileChooser.isShowing()) { + profileChooser.dismiss(); + } + } + private void populateAvatar() { int size = profileSparseArray.size(); Log.d(TAG, "profileSparseArray.size() [" + profileSparseArray.size() + "]"); @@ -414,10 +428,10 @@ private void setDefaultValues() { private void setFirstProfile(Profile profile) { username.setText(profile.getUsername()); email.setText(profile.getEmail()); - if (profile.getAvatarDrawable() != null) - avatar.setImageDrawable(profile.getAvatarDrawable()); - if (profile.getBackgroundDrawable() != null) - background.setImageDrawable(profile.getBackgroundDrawable()); + if (profile.getAvatarRes() != 0) + avatar.setImageResource(profile.getAvatarRes()); + if (profile.getBackgroundRes() != 0) + background.setImageResource(profile.getBackgroundRes()); if (profile.getAvatarUri() != null) ImageLoader.loadImage(profile.getAvatarUri(), avatar, ImageLoader.AVATAR); if (profile.getBackgroundUri() != null) @@ -435,8 +449,8 @@ public void onClick(View v) { } private void setSecondProfile(Profile profile) { - if (profile.getAvatarDrawable() != null) - avatar2.setImageDrawable(profile.getAvatarDrawable()); + if (profile.getAvatarRes() != 0) + avatar2.setImageResource(profile.getAvatarRes()); if (profile.getAvatarUri() != null) ImageLoader.loadImage(profile.getAvatarUri(), avatar2, ImageLoader.AVATAR); avatar2.setVisibility(VISIBLE); @@ -451,8 +465,8 @@ public void onClick(View v) { } private void setThirdProfile(Profile profile) { - if (profile.getAvatarDrawable() != null) - avatar3.setImageDrawable(profile.getAvatarDrawable()); + if (profile.getAvatarRes() != 0) + avatar3.setImageResource(profile.getAvatarRes()); if (profile.getAvatarUri() != null) ImageLoader.loadImage(profile.getAvatarUri(), avatar3, ImageLoader.AVATAR); avatar3.setVisibility(VISIBLE); @@ -523,12 +537,13 @@ private void setupAttributeSet(AttributeSet attrs, int defStyle) { if ((hvUsername != null && !hvUsername.isEmpty()) || (hvEmail != null && !hvEmail.isEmpty()) || hvAvatar != 0 || hvBackground != 0) { Log.d(TAG, "profile created from XML"); + Profile profile = new Profile.Builder() .setId(1) .setUsername(hvUsername) .setEmail(hvEmail) - .setAvatar(Utils.getDrawable(getContext(), hvAvatar)) - .setBackground(Utils.getDrawable(getContext(), hvBackground)) + .setAvatar(hvAvatar) + .setBackground(hvBackground) .build(); profileSparseArray.put(profile.getId(), profile); @@ -613,7 +628,7 @@ public void onClick(View v) { profileChooserFragment.setCallback(HeaderView.this); profileChooserFragment.show(hvFragmentManager, ProfileChooserFragment.FRAGMENT_TAG); } else { - ProfileChooser profileChooser = new ProfileChooser(getContext(), profileSparseArray, itemArrayList, hvHighlightColor, hvShowAddButton, hvDialogTitle, hvAddIconDrawable); + profileChooser = new ProfileChooser(getContext(), profileSparseArray, itemArrayList, hvHighlightColor, hvShowAddButton, hvDialogTitle, hvAddIconDrawable); profileChooser.setCallback(HeaderView.this); profileChooser.show(); } diff --git a/library/src/main/java/rebus/header/view/ImageLoader.java b/library/src/main/java/rebus/header/view/ImageLoader.java index 76eb3a7..b6a8c0e 100644 --- a/library/src/main/java/rebus/header/view/ImageLoader.java +++ b/library/src/main/java/rebus/header/view/ImageLoader.java @@ -70,9 +70,9 @@ private void setImageLoaderInterface(ImageLoaderInterface imageLoaderInterface) public interface ImageLoaderInterface { /** - * @param url uri of image + * @param url uri of image * @param imageView reference of ImageView - * @param type type of image to load AVATAR or HEADER + * @param type type of image to load AVATAR or HEADER */ void loadImage(Uri url, ImageView imageView, @Type int type); } diff --git a/library/src/main/java/rebus/header/view/Profile.java b/library/src/main/java/rebus/header/view/Profile.java index 6e103ba..f2c5f2b 100644 --- a/library/src/main/java/rebus/header/view/Profile.java +++ b/library/src/main/java/rebus/header/view/Profile.java @@ -24,10 +24,10 @@ package rebus.header.view; -import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; +import android.support.annotation.DrawableRes; import android.support.annotation.IntRange; import android.support.annotation.NonNull; import android.support.annotation.RestrictTo; @@ -58,18 +58,18 @@ public Profile[] newArray(int size) { @IdValue private int id; private Uri avatarUri; - private Drawable avatarDrawable; + private int avatarRes; private Uri backgroundUri; - private Drawable backgroundDrawable; + private int backgroundRes; private Spanned username; private Spanned email; - private Profile(int id, Uri avatarUri, Drawable avatarDrawable, Uri backgroundUri, Drawable backgroundDrawable, Spanned username, Spanned email) { + private Profile(int id, Uri avatarUri, int avatarRes, Uri backgroundUri, int backgroundRes, Spanned username, Spanned email) { this.id = id; this.avatarUri = avatarUri; - this.avatarDrawable = avatarDrawable; + this.avatarRes = avatarRes; this.backgroundUri = backgroundUri; - this.backgroundDrawable = backgroundDrawable; + this.backgroundRes = backgroundRes; this.username = username; this.email = email; } @@ -77,9 +77,9 @@ private Profile(int id, Uri avatarUri, Drawable avatarDrawable, Uri backgroundUr private Profile(Parcel in) { id = in.readInt(); avatarUri = (Uri) in.readValue(Uri.class.getClassLoader()); - avatarDrawable = (Drawable) in.readValue(Drawable.class.getClassLoader()); + avatarRes = in.readInt(); backgroundUri = (Uri) in.readValue(Uri.class.getClassLoader()); - backgroundDrawable = (Drawable) in.readValue(Drawable.class.getClassLoader()); + backgroundRes = in.readInt(); username = (Spanned) in.readValue(Spanned.class.getClassLoader()); email = (Spanned) in.readValue(Spanned.class.getClassLoader()); } @@ -92,16 +92,16 @@ Uri getAvatarUri() { return avatarUri; } - Drawable getAvatarDrawable() { - return avatarDrawable; + int getAvatarRes() { + return avatarRes; } Uri getBackgroundUri() { return backgroundUri; } - Drawable getBackgroundDrawable() { - return backgroundDrawable; + int getBackgroundRes() { + return backgroundRes; } Spanned getUsername() { @@ -117,9 +117,9 @@ public String toString() { return "Profile{" + "id=" + id + ", avatarUri=" + avatarUri + - ", avatarDrawable=" + avatarDrawable + + ", avatarRes=" + avatarRes + ", backgroundUri=" + backgroundUri + - ", backgroundDrawable=" + backgroundDrawable + + ", backgroundRes=" + backgroundRes + ", username=" + username + ", email=" + email + '}'; @@ -134,9 +134,9 @@ public int describeContents() { public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); dest.writeValue(avatarUri); - dest.writeValue(avatarDrawable); + dest.writeValue(avatarRes); dest.writeValue(backgroundUri); - dest.writeValue(backgroundDrawable); + dest.writeValue(backgroundRes); dest.writeValue(username); dest.writeValue(email); } @@ -152,9 +152,9 @@ public static class Builder { @IdValue private int id; private Uri avatarUri; - private Drawable avatarDrawable; + private int avatarRes; private Uri backgroundUri; - private Drawable backgroundDrawable; + private int backgroundRes; private Spanned username; private Spanned email; @@ -176,7 +176,7 @@ public Builder setId(@IdValue int id) { */ public Builder setAvatar(@NonNull Uri avatar) { this.avatarUri = avatar; - this.avatarDrawable = null; + this.avatarRes = 0; return this; } @@ -186,16 +186,16 @@ public Builder setAvatar(@NonNull Uri avatar) { */ public Builder setAvatar(@NonNull String avatar) { this.avatarUri = Uri.parse(avatar); - this.avatarDrawable = null; + this.avatarRes = 0; return this; } /** - * @param avatar set avatar drawable + * @param avatar set avatar drawable res * @return current builder instance */ - public Builder setAvatar(Drawable avatar) { - this.avatarDrawable = avatar; + public Builder setAvatar(@DrawableRes int avatar) { + this.avatarRes = avatar; this.avatarUri = null; return this; } @@ -206,7 +206,7 @@ public Builder setAvatar(Drawable avatar) { */ public Builder setBackground(@NonNull Uri background) { this.backgroundUri = background; - this.backgroundDrawable = null; + this.backgroundRes = 0; return this; } @@ -216,16 +216,16 @@ public Builder setBackground(@NonNull Uri background) { */ public Builder setBackground(@NonNull String background) { this.backgroundUri = Uri.parse(background); - this.backgroundDrawable = null; + this.backgroundRes = 0; return this; } /** - * @param background set background drawable + * @param background set background drawable res * @return current builder instance */ - public Builder setBackground(Drawable background) { - this.backgroundDrawable = background; + public Builder setBackground(@DrawableRes int background) { + this.backgroundRes = background; this.backgroundUri = null; return this; } @@ -270,7 +270,7 @@ public Builder setEmail(String email) { * @return build Profile and return it */ public Profile build() { - return new Profile(id, avatarUri, avatarDrawable, backgroundUri, backgroundDrawable, username, email); + return new Profile(id, avatarUri, avatarRes, backgroundUri, backgroundRes, username, email); } } diff --git a/library/src/main/java/rebus/header/view/RowProfileView.java b/library/src/main/java/rebus/header/view/RowProfileView.java index 4a5a086..eec2a8b 100644 --- a/library/src/main/java/rebus/header/view/RowProfileView.java +++ b/library/src/main/java/rebus/header/view/RowProfileView.java @@ -112,8 +112,8 @@ public void setProfile(Profile profile, boolean active) { email.setTypeface(active ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT); username.setText(hvProfile.getUsername()); email.setText(hvProfile.getEmail()); - if (hvProfile.getAvatarDrawable() != null) - avatar.setImageDrawable(hvProfile.getAvatarDrawable()); + if (hvProfile.getAvatarRes() != 0) + avatar.setImageResource(hvProfile.getAvatarRes()); if (hvProfile.getAvatarUri() != null) ImageLoader.loadImage(hvProfile.getAvatarUri(), avatar, ImageLoader.AVATAR); } diff --git a/library/src/main/java/rebus/header/view/Utils.java b/library/src/main/java/rebus/header/view/Utils.java index 92b6bac..400c683 100644 --- a/library/src/main/java/rebus/header/view/Utils.java +++ b/library/src/main/java/rebus/header/view/Utils.java @@ -34,8 +34,6 @@ import android.text.Spanned; import android.util.TypedValue; -import java.util.Locale; - /** * Created by raphaelbussa on 13/01/17. */