From 5b49e83102e9375f61908962e9fad59afe0f1a02 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sun, 11 Nov 2018 22:21:52 +0200 Subject: [PATCH] Default avatars fix --- .../activities/profile/ProfileActivity.java | 83 +++++++------------ .../mthmmy/activities/topic/TopicAdapter.java | 80 ++++-------------- .../gr/thmmy/mthmmy/base/BaseActivity.java | 2 +- .../thmmy/mthmmy/utils/CircleTransform.java | 7 +- .../res/drawable/ic_default_user_avatar.xml | 5 ++ .../ic_default_user_avatar_darker.xml | 5 ++ .../main/res/layout-v21/activity_profile.xml | 5 +- .../layout-v21/activity_topic_post_row.xml | 7 +- app/src/main/res/layout/activity_profile.xml | 5 +- .../res/layout/activity_topic_edit_row.xml | 7 +- .../res/layout/activity_topic_post_row.xml | 7 +- .../layout/activity_topic_quick_reply_row.xml | 7 +- app/src/main/res/values/dimens.xml | 1 + 13 files changed, 81 insertions(+), 140 deletions(-) create mode 100644 app/src/main/res/drawable/ic_default_user_avatar.xml create mode 100644 app/src/main/res/drawable/ic_default_user_avatar_darker.xml diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index a7729cd7..162ea1f6 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java @@ -20,8 +20,6 @@ import android.widget.Toast; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.tabs.TabLayout; -import com.mikepenz.fontawesome_typeface_library.FontAwesome; -import com.mikepenz.iconics.IconicsDrawable; import com.squareup.picasso.Picasso; import org.jsoup.Jsoup; @@ -34,7 +32,6 @@ import java.util.List; import java.util.Objects; import androidx.appcompat.app.AppCompatDelegate; -import androidx.core.content.ContextCompat; import androidx.core.content.res.ResourcesCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; @@ -82,10 +79,9 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment * If username is not available put an empty string or leave it null. */ public static final String BUNDLE_PROFILE_USERNAME = "USERNAME"; - private static final int THUMBNAIL_SIZE = 200; private TextView usernameView; - private ImageView thumbnailView; + private ImageView avatarView; private TextView personalTextView; private MaterialProgressBar progressBar; private FloatingActionButton pmFAB; @@ -94,7 +90,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment private ProfileTask profileTask; private String personalText; private String profileUrl; - private String thumbnailUrl; + private String avatarUrl; private String username; private int tabSelect; @@ -111,8 +107,8 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment setContentView(R.layout.activity_profile); Bundle extras = getIntent().getExtras(); - thumbnailUrl = extras.getString(BUNDLE_PROFILE_THUMBNAIL_URL); - if (thumbnailUrl == null) thumbnailUrl = ""; + avatarUrl = extras.getString(BUNDLE_PROFILE_THUMBNAIL_URL); + if (avatarUrl == null) avatarUrl = ""; username = extras.getString(BUNDLE_PROFILE_USERNAME); profileUrl = extras.getString(BUNDLE_PROFILE_URL); @@ -129,25 +125,10 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment progressBar = findViewById(R.id.progressBar); - thumbnailView = findViewById(R.id.user_thumbnail); - if (!Objects.equals(thumbnailUrl, "")) + avatarView = findViewById(R.id.user_thumbnail); + if (!Objects.equals(avatarUrl, "")) //noinspection ConstantConditions - Picasso.with(this) - .load(thumbnailUrl) - .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) - .centerCrop() - .error(new IconicsDrawable(ProfileActivity.this) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(ProfileActivity.this, R.color.iron)) - .backgroundColor(ContextCompat.getColor(ProfileActivity.this, R.color.primary)) - .sizePx(THUMBNAIL_SIZE)) - .placeholder(new IconicsDrawable(ProfileActivity.this) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(ProfileActivity.this, R.color.iron)) - .backgroundColor(ContextCompat.getColor(ProfileActivity.this, R.color.primary)) - .sizePx(THUMBNAIL_SIZE)) - .transform(new CircleTransform()) - .into(thumbnailView); + loadAvatar(); usernameView = findViewById(R.id.profile_activity_username); usernameView.setTypeface(Typeface.createFromAsset(this.getAssets() , "fonts/fontawesome-webfont.ttf")); @@ -228,6 +209,19 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment if (pmFAB.getVisibility() != View.GONE) pmFAB.setEnabled(false); } + private void loadAvatar(){ + Picasso.with(this) + .load(avatarUrl) + .fit() + .centerCrop() + .error(Objects.requireNonNull(ResourcesCompat.getDrawable(this.getResources() + , R.drawable.ic_default_user_avatar, null))) + .placeholder(Objects.requireNonNull(ResourcesCompat.getDrawable(this.getResources() + , R.drawable.ic_default_user_avatar, null))) + .transform(new CircleTransform()) + .into(avatarView); + } + /** * An {@link AsyncTask} that handles asynchronous fetching of a profile page and parsing this * user's personal text. The {@link Document} resulting from the parse is stored for use in @@ -243,7 +237,7 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment Spannable usernameSpan; Boolean isOnline = false; - public ProfileTask() { + ProfileTask() { super(ProfileActivity.this::onProfileTaskStarted, null); } @@ -257,9 +251,9 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment if (username == null || Objects.equals(username, "")) { username = contentsTable.select("tr").first().select("td").last().text(); } - if (thumbnailUrl == null || Objects.equals(thumbnailUrl, "")) { //Maybe there is an avatar + if (avatarUrl == null || Objects.equals(avatarUrl, "")) { //Maybe there is an avatar Element profileAvatar = profilePage.select("img.avatar").first(); - if (profileAvatar != null) thumbnailUrl = profileAvatar.attr("abs:src"); + if (profileAvatar != null) avatarUrl = profileAvatar.attr("abs:src"); } { //Finds personal text Element tmpEl = profilePage.select("td.windowbg:nth-child(2)").first(); @@ -280,14 +274,10 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment usernameSpan.setSpan(new RelativeSizeSpan(0.45f) , 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - if (contentsTable.toString().contains("Online") - || contentsTable.toString().contains("Συνδεδεμένος")) { - isOnline = true; - } else { - isOnline = false; - /*usernameSpan.setSpan(new ForegroundColorSpan(Color.GRAY) - , 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/ - } + /*usernameSpan.setSpan(new ForegroundColorSpan(Color.GRAY) + , 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);*/ + isOnline = contentsTable.toString().contains("Online") + || contentsTable.toString().contains("Συνδεδεμένος"); usernameSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#26A69A")) , 2, usernameSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } @@ -310,24 +300,9 @@ public class ProfileActivity extends BaseActivity implements LatestPostsFragment } usernameView.setText(usernameSpan); } else if (usernameView.getText() != username) usernameView.setText(username); - if (thumbnailUrl != null && !Objects.equals(thumbnailUrl, "")) + if (avatarUrl != null && !Objects.equals(avatarUrl, "")) //noinspection ConstantConditions - Picasso.with(getApplicationContext()) - .load(thumbnailUrl) - .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) - .centerCrop() - .error(new IconicsDrawable(ProfileActivity.this) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(ProfileActivity.this, R.color.iron)) - .backgroundColor(ContextCompat.getColor(ProfileActivity.this, R.color.primary)) - .sizePx(THUMBNAIL_SIZE)) - .placeholder(new IconicsDrawable(ProfileActivity.this) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(ProfileActivity.this, R.color.iron)) - .backgroundColor(ContextCompat.getColor(ProfileActivity.this, R.color.primary)) - .sizePx(THUMBNAIL_SIZE)) - .transform(new CircleTransform()) - .into(thumbnailView); + loadAvatar(); if (personalText != null) { personalTextView.setText(personalText); personalTextView.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index d890a30a..577b272f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -4,7 +4,6 @@ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; -import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable; @@ -41,8 +40,6 @@ import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarEntry; -import com.mikepenz.fontawesome_typeface_library.FontAwesome; -import com.mikepenz.iconics.IconicsDrawable; import com.squareup.picasso.Picasso; import java.util.ArrayList; @@ -53,7 +50,6 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.content.res.AppCompatResources; import androidx.appcompat.widget.AppCompatButton; -import androidx.core.content.ContextCompat; import androidx.core.content.res.ResourcesCompat; import androidx.lifecycle.ViewModelProviders; import androidx.recyclerview.widget.RecyclerView; @@ -92,7 +88,6 @@ class TopicAdapter extends RecyclerView.Adapter { /** * Int that holds thumbnail's size defined in R.dimen */ - private static int THUMBNAIL_SIZE; private final Context context; private final OnPostFocusChangeListener postFocusListener; private final IEmojiKeyboard emojiKeyboard; @@ -110,8 +105,6 @@ class TopicAdapter extends RecyclerView.Adapter { this.emojiKeyboard = emojiKeyboard; viewModel = ViewModelProviders.of(context).get(TopicViewModel.class); - - THUMBNAIL_SIZE = (int) context.getResources().getDimension(R.dimen.thumbnail_size); } @Override @@ -309,30 +302,8 @@ class TopicAdapter extends RecyclerView.Adapter { holder.post.setClickable(true); holder.post.setWebViewClient(new LinkLauncher()); - //Avoids errors about layout having 0 width/height - holder.thumbnail.setMinimumWidth(1); - holder.thumbnail.setMinimumHeight(1); - //Sets thumbnail size - holder.thumbnail.setMaxWidth(THUMBNAIL_SIZE); - holder.thumbnail.setMaxHeight(THUMBNAIL_SIZE); - //noinspection ConstantConditions - Picasso.with(context) - .load(currentPost.getThumbnailURL()) - .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) - .centerCrop() - .transform(new CircleTransform()) - .error(new IconicsDrawable(context) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(context, R.color.iron)) - .backgroundColor(ContextCompat.getColor(context, R.color.primary_light)) - .sizeDp(THUMBNAIL_SIZE)) - .placeholder(new IconicsDrawable(context) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(context, R.color.iron)) - .backgroundColor(ContextCompat.getColor(context, R.color.primary_light)) - .sizeDp(THUMBNAIL_SIZE)) - .into(holder.thumbnail); + loadAvatar(currentPost.getThumbnailURL(), holder.thumbnail); //Sets username,submit date, index number, subject, post's and attached files texts holder.username.setText(currentPost.getAuthor()); @@ -613,22 +584,8 @@ class TopicAdapter extends RecyclerView.Adapter { Post reply = (Post) topicItems.get(position); //noinspection ConstantConditions - Picasso.with(context) - .load(getSessionManager().getAvatarLink()) - .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) - .centerCrop() - .error(new IconicsDrawable(context) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(context, R.color.iron)) - .backgroundColor(ContextCompat.getColor(context, R.color.primary_light)) - .sizeDp(THUMBNAIL_SIZE)) - .placeholder(new IconicsDrawable(context) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(context, R.color.iron)) - .backgroundColor(ContextCompat.getColor(context, R.color.primary_light)) - .sizeDp(THUMBNAIL_SIZE)) - .transform(new CircleTransform()) - .into(holder.thumbnail); + loadAvatar(getSessionManager().getAvatarLink(), holder.thumbnail); + holder.username.setText(getSessionManager().getUsername()); holder.itemView.setAlpha(1f); holder.itemView.setEnabled(true); @@ -675,22 +632,8 @@ class TopicAdapter extends RecyclerView.Adapter { final EditMessageViewHolder holder = (EditMessageViewHolder) currentHolder; //noinspection ConstantConditions - Picasso.with(context) - .load(getSessionManager().getAvatarLink()) - .resize(THUMBNAIL_SIZE, THUMBNAIL_SIZE) - .centerCrop() - .error(new IconicsDrawable(context) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(context, R.color.iron)) - .backgroundColor(ContextCompat.getColor(context, R.color.primary_light)) - .sizeDp(THUMBNAIL_SIZE)) - .placeholder(new IconicsDrawable(context) - .icon(FontAwesome.Icon.faw_user_circle) - .color(ContextCompat.getColor(context, R.color.iron)) - .backgroundColor(ContextCompat.getColor(context, R.color.primary_light)) - .sizeDp(THUMBNAIL_SIZE)) - .transform(new CircleTransform()) - .into(holder.thumbnail); + loadAvatar(getSessionManager().getAvatarLink(), holder.thumbnail); + holder.username.setText(getSessionManager().getUsername()); holder.editSubject.setText(currentPost.getSubject()); holder.editSubject.setRawInputType(InputType.TYPE_CLASS_TEXT); @@ -723,6 +666,19 @@ class TopicAdapter extends RecyclerView.Adapter { } } + private void loadAvatar(String imageUrl, ImageView imageView){ + Picasso.with(context) + .load(imageUrl) + .fit() + .centerCrop() + .error(Objects.requireNonNull(ResourcesCompat.getDrawable(context.getResources() + , R.drawable.ic_default_user_avatar_darker, null))) + .placeholder(Objects.requireNonNull(ResourcesCompat.getDrawable(context.getResources() + , R.drawable.ic_default_user_avatar_darker, null))) + .transform(new CircleTransform()) + .into(imageView); + } + @Override public int getItemCount() { return topicItems.size(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index ee81418c..75c7d081 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -455,7 +455,7 @@ public abstract class BaseActivity extends AppCompatActivity { private void setDefaultAvatar() { profileDrawerItem.withIcon(new IconicsDrawable(this) - .icon(FontAwesome.Icon.faw_user_circle) + .icon(FontAwesome.Icon.faw_user) .paddingDp(10) .color(ContextCompat.getColor(this, R.color.iron)) .backgroundColor(ContextCompat.getColor(this, R.color.primary_light))); diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/CircleTransform.java b/app/src/main/java/gr/thmmy/mthmmy/utils/CircleTransform.java index 2294b83e..c6da6010 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/CircleTransform.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/CircleTransform.java @@ -20,11 +20,12 @@ public class CircleTransform implements Transformation { int y = (source.getHeight() - size) / 2; Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size); - if (squaredBitmap != source) { + if (squaredBitmap != source) source.recycle(); - } - Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig()); + // For GIF images + Bitmap.Config config = source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888; + Bitmap bitmap = Bitmap.createBitmap(size, size, config); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); diff --git a/app/src/main/res/drawable/ic_default_user_avatar.xml b/app/src/main/res/drawable/ic_default_user_avatar.xml new file mode 100644 index 00000000..aa4a9116 --- /dev/null +++ b/app/src/main/res/drawable/ic_default_user_avatar.xml @@ -0,0 +1,5 @@ + + + + diff --git a/app/src/main/res/drawable/ic_default_user_avatar_darker.xml b/app/src/main/res/drawable/ic_default_user_avatar_darker.xml new file mode 100644 index 00000000..44169fa1 --- /dev/null +++ b/app/src/main/res/drawable/ic_default_user_avatar_darker.xml @@ -0,0 +1,5 @@ + + + + diff --git a/app/src/main/res/layout-v21/activity_profile.xml b/app/src/main/res/layout-v21/activity_profile.xml index 00fc8430..3db974f5 100644 --- a/app/src/main/res/layout-v21/activity_profile.xml +++ b/app/src/main/res/layout-v21/activity_profile.xml @@ -35,12 +35,13 @@ diff --git a/app/src/main/res/layout-v21/activity_topic_post_row.xml b/app/src/main/res/layout-v21/activity_topic_post_row.xml index 6218e2ff..5147852f 100644 --- a/app/src/main/res/layout-v21/activity_topic_post_row.xml +++ b/app/src/main/res/layout-v21/activity_topic_post_row.xml @@ -53,13 +53,12 @@ diff --git a/app/src/main/res/layout/activity_profile.xml b/app/src/main/res/layout/activity_profile.xml index e5a935cf..5ca3cbbb 100644 --- a/app/src/main/res/layout/activity_profile.xml +++ b/app/src/main/res/layout/activity_profile.xml @@ -35,12 +35,13 @@ + app:srcCompat="@drawable/ic_default_user_avatar_darker" /> + app:srcCompat="@drawable/ic_default_user_avatar_darker" /> + app:srcCompat="@drawable/ic_default_user_avatar_darker" /> 6dp 12dp 16sp + 90dp