Browse Source

Default avatars fix

pull/61/merge
Ezerous 6 years ago
parent
commit
5b49e83102
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 83
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  2. 80
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 2
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  4. 7
      app/src/main/java/gr/thmmy/mthmmy/utils/CircleTransform.java
  5. 5
      app/src/main/res/drawable/ic_default_user_avatar.xml
  6. 5
      app/src/main/res/drawable/ic_default_user_avatar_darker.xml
  7. 5
      app/src/main/res/layout-v21/activity_profile.xml
  8. 7
      app/src/main/res/layout-v21/activity_topic_post_row.xml
  9. 5
      app/src/main/res/layout/activity_profile.xml
  10. 7
      app/src/main/res/layout/activity_topic_edit_row.xml
  11. 7
      app/src/main/res/layout/activity_topic_post_row.xml
  12. 7
      app/src/main/res/layout/activity_topic_quick_reply_row.xml
  13. 1
      app/src/main/res/values/dimens.xml

83
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);

80
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<RecyclerView.ViewHolder> {
/**
* 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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
}
}
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();

2
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)));

7
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();

5
app/src/main/res/drawable/ic_default_user_avatar.xml

@ -0,0 +1,5 @@
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:pathData="M256,60c-108.27,0 -196,87.73 -196,196c0,108.27 87.73,196 196,196c108.27,0 196,-87.73 196,-196c0,-108.27 -87.73,-196 -196,-196z"/>
<path android:fillColor="#3c3c3c" android:pathData="M504,256c0,137 -111,248 -248,248c-137,0 -248,-111 -248,-248c0,-137 111,-248 248,-248c137,0 248,111 248,248zM168,192c0,48.6 39.4,88 88,88c48.6,0 88,-39.4 88,-88c0,-48.6 -39.4,-88 -88,-88c-48.6,0 -88,39.4 -88,88zM402.5,379.8c-18.8,-35.4 -55.6,-59.8 -98.5,-59.8c-2.4,0 -4.8,0.4 -7.1,1.1c-12.9,4.2 -26.6,6.9 -40.9,6.9c-14.3,0 -27.9,-2.7 -40.9,-6.9c-2.3,-0.7 -4.7,-1.1 -7.1,-1.1c-42.9,0 -79.7,24.4 -98.5,59.8c35.2,41.6 87.8,68.2 146.5,68.2c58.7,0 111.3,-26.6 146.5,-68.2z"/>
</vector>

5
app/src/main/res/drawable/ic_default_user_avatar_darker.xml

@ -0,0 +1,5 @@
<vector android:height="24dp" android:viewportHeight="512"
android:viewportWidth="512" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#ffffff" android:pathData="M256,60c-108.27,0 -196,87.73 -196,196c0,108.27 87.73,196 196,196c108.27,0 196,-87.73 196,-196c0,-108.27 -87.73,-196 -196,-196z"/>
<path android:fillColor="#323232" android:pathData="M504,256c0,137 -111,248 -248,248c-137,0 -248,-111 -248,-248c0,-137 111,-248 248,-248c137,0 248,111 248,248zM168,192c0,48.6 39.4,88 88,88c48.6,0 88,-39.4 88,-88c0,-48.6 -39.4,-88 -88,-88c-48.6,0 -88,39.4 -88,88zM402.5,379.8c-18.8,-35.4 -55.6,-59.8 -98.5,-59.8c-2.4,0 -4.8,0.4 -7.1,1.1c-12.9,4.2 -26.6,6.9 -40.9,6.9c-14.3,0 -27.9,-2.7 -40.9,-6.9c-2.3,-0.7 -4.7,-1.1 -7.1,-1.1c-42.9,0 -79.7,24.4 -98.5,59.8c35.2,41.6 87.8,68.2 146.5,68.2c58.7,0 111.3,-26.6 146.5,-68.2z"/>
</vector>

5
app/src/main/res/layout-v21/activity_profile.xml

@ -35,12 +35,13 @@
<ImageView
android:id="@+id/user_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="@dimen/profile_activity_avatar_size"
android:layout_height="@dimen/profile_activity_avatar_size"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/post_thumbnail"
android:fitsSystemWindows="true"
app:srcCompat="@drawable/ic_default_user_avatar"
android:transitionName="user_thumbnail"
app:layout_collapseMode="parallax"/>

7
app/src/main/res/layout-v21/activity_topic_post_row.xml

@ -53,13 +53,12 @@
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="@dimen/thumbnail_size"
android:layout_height="@dimen/thumbnail_size"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/post_thumbnail"
android:maxHeight="@dimen/thumbnail_size"
android:maxWidth="@dimen/thumbnail_size"
app:srcCompat="@drawable/ic_default_user_avatar_darker"
android:transitionName="user_thumbnail" />
</FrameLayout>

5
app/src/main/res/layout/activity_profile.xml

@ -35,12 +35,13 @@
<ImageView
android:id="@+id/user_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="@dimen/profile_activity_avatar_size"
android:layout_height="@dimen/profile_activity_avatar_size"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/post_thumbnail"
android:fitsSystemWindows="true"
app:srcCompat="@drawable/ic_default_user_avatar"
app:layout_collapseMode="parallax"/>
<TextView

7
app/src/main/res/layout/activity_topic_edit_row.xml

@ -46,13 +46,12 @@
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="@dimen/thumbnail_size"
android:layout_height="@dimen/thumbnail_size"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/post_thumbnail"
android:maxHeight="@dimen/thumbnail_size"
android:maxWidth="@dimen/thumbnail_size" />
app:srcCompat="@drawable/ic_default_user_avatar_darker" />
</FrameLayout>
<TextView

7
app/src/main/res/layout/activity_topic_post_row.xml

@ -53,13 +53,12 @@
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="@dimen/thumbnail_size"
android:layout_height="@dimen/thumbnail_size"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/post_thumbnail"
android:maxHeight="@dimen/thumbnail_size"
android:maxWidth="@dimen/thumbnail_size" />
app:srcCompat="@drawable/ic_default_user_avatar_darker" />
</FrameLayout>
<TextView

7
app/src/main/res/layout/activity_topic_quick_reply_row.xml

@ -46,13 +46,12 @@
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="@dimen/thumbnail_size"
android:layout_height="@dimen/thumbnail_size"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="@string/post_thumbnail"
android:maxHeight="@dimen/thumbnail_size"
android:maxWidth="@dimen/thumbnail_size" />
app:srcCompat="@drawable/ic_default_user_avatar_darker" />
</FrameLayout>
<TextView

1
app/src/main/res/values/dimens.xml

@ -13,4 +13,5 @@
<dimen name="editor_format_button_margin_between">6dp</dimen>
<dimen name="refresh_label_border_radius">12dp</dimen>
<dimen name="material_drawer_account_header_title">16sp</dimen>
<dimen name="profile_activity_avatar_size">90dp</dimen>
</resources>

Loading…
Cancel
Save