Commit cbf41dbc authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge pull request #264 from wncc/recycle

Fix performance issues of BodyFragment
parents 538ed195 7f7508db
...@@ -54,6 +54,9 @@ public class Constants { ...@@ -54,6 +54,9 @@ public class Constants {
public static final String DATA_TYPE_NEWS = "newsentry"; public static final String DATA_TYPE_NEWS = "newsentry";
public static final String DATA_TYPE_PT = "blogentry"; public static final String DATA_TYPE_PT = "blogentry";
public static final String CARD_TYPE_TITLE = "card_type_title";
public static final String CARD_TYPE_BODY_HEAD = "card_type_body_head";
/* Map */ /* Map */
public static final double MAP_Xn = 19.134417, MAP_Yn = 72.901229, MAP_Zn = 1757, MAP_Zyn = 501; public static final double MAP_Xn = 19.134417, MAP_Yn = 72.901229, MAP_Zn = 1757, MAP_Zyn = 501;
public static final double[] MAP_WEIGHTS_X = {-11.392001766454612, -36.31634553309953, 73.91269388324432, -24.14021153064087, 3.4508817531539115, -0.1462262375477863, 5.532505074667804, -1.542391995870977, 36.14211738142935}; public static final double[] MAP_WEIGHTS_X = {-11.392001766454612, -36.31634553309953, 73.91269388324432, -24.14021153064087, 3.4508817531539115, -0.1462262375477863, 5.532505074667804, -1.542391995870977, 36.14211738142935};
......
...@@ -20,7 +20,6 @@ import com.google.gson.Gson; ...@@ -20,7 +20,6 @@ import com.google.gson.Gson;
import com.squareup.picasso.Callback; import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.lang.reflect.Field;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -39,6 +38,7 @@ import app.insti.fragment.UserFragment; ...@@ -39,6 +38,7 @@ import app.insti.fragment.UserFragment;
public final class Utils { public final class Utils {
public static UpdatableList<Event> eventCache = new UpdatableList<>(); public static UpdatableList<Event> eventCache = new UpdatableList<>();
public static UpdatableList<Notification> notificationCache = null; public static UpdatableList<Notification> notificationCache = null;
public static UpdatableList<Body> bodyCache = new UpdatableList<>();
private static String sessionId; private static String sessionId;
private static RetrofitInterface retrofitInterface; private static RetrofitInterface retrofitInterface;
......
...@@ -16,11 +16,14 @@ import com.squareup.picasso.RequestCreator; ...@@ -16,11 +16,14 @@ import com.squareup.picasso.RequestCreator;
import java.util.List; import java.util.List;
import app.insti.Constants;
import app.insti.R; import app.insti.R;
import app.insti.Utils; import app.insti.Utils;
import app.insti.interfaces.CardInterface; import app.insti.interfaces.CardInterface;
import app.insti.utils.BodyHeadCard;
import app.insti.utils.BodyHeadViewHolder;
public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.Adapter<CardAdapter<T>.ViewHolder> { public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<T> tList; private List<T> tList;
private Fragment mFragment; private Fragment mFragment;
...@@ -45,12 +48,23 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -45,12 +48,23 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.
@Override @Override
@NonNull @NonNull
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int i) { public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int viewType) {
Context context = viewGroup.getContext(); Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context); LayoutInflater inflater = LayoutInflater.from(context);
if (viewType == 2) {
View titleView = inflater.inflate(R.layout.title_card, viewGroup, false);
return new TitleViewHolder(titleView);
}
if (viewType == 4) {
View bodyView = inflater.inflate(R.layout.body_head_view, viewGroup, false);
return new BodyHeadViewHolder(bodyView);
}
View postView = inflater.inflate(R.layout.feed_card, viewGroup, false); View postView = inflater.inflate(R.layout.feed_card, viewGroup, false);
final ViewHolder postViewHolder = new ViewHolder(postView); final CardViewHolder postViewHolder = new CardViewHolder(postView);
postView.setOnClickListener(new View.OnClickListener() { postView.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
...@@ -63,7 +77,11 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -63,7 +77,11 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.
} }
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
switch (holder.getItemViewType()) {
case 0:
CardViewHolder viewHolder = (CardViewHolder) holder;
T t = tList.get(i); T t = tList.get(i);
viewHolder.title.setText(t.getTitle()); viewHolder.title.setText(t.getTitle());
viewHolder.subtitle.setText(t.getSubtitle()); viewHolder.subtitle.setText(t.getSubtitle());
...@@ -80,6 +98,10 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -80,6 +98,10 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.
// Load big image with low resolution as avatar // Load big image with low resolution as avatar
Utils.loadImageWithPlaceholder(viewHolder.bigPicture, getBigImageUrl(t)); Utils.loadImageWithPlaceholder(viewHolder.bigPicture, getBigImageUrl(t));
} else { } else {
// Make sure avatar is visible for recycled views
viewHolder.bigPicture.setVisibility(View.GONE);
viewHolder.avatar.setVisibility(View.VISIBLE);
// Build basic request // Build basic request
RequestCreator requestCreator; RequestCreator requestCreator;
if (t.getAvatarUrl() != null) if (t.getAvatarUrl() != null)
...@@ -98,15 +120,26 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -98,15 +120,26 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.
// Load the image // Load the image
requestCreator.into(viewHolder.avatar); requestCreator.into(viewHolder.avatar);
} }
return;
case 2:
TitleViewHolder titleViewHolder = (TitleViewHolder) holder;
titleViewHolder.title.setText(tList.get(i).getTitle());
return;
case 4:
((BodyHeadCard) tList.get(i)).bindView((BodyHeadViewHolder) holder, mFragment);
return;
default:
return;
}
} }
public class ViewHolder extends RecyclerView.ViewHolder { public class CardViewHolder extends RecyclerView.ViewHolder {
private ImageView avatar; private ImageView avatar;
private TextView title; private TextView title;
private TextView subtitle; private TextView subtitle;
private ImageView bigPicture; private ImageView bigPicture;
public ViewHolder(View itemView) { public CardViewHolder(View itemView) {
super(itemView); super(itemView);
avatar = itemView.findViewById(R.id.object_picture); avatar = itemView.findViewById(R.id.object_picture);
...@@ -116,10 +149,25 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -116,10 +149,25 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.
} }
} }
public class TitleViewHolder extends RecyclerView.ViewHolder {
private TextView title;
public TitleViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_text);
}
}
@Override @Override
public int getItemViewType(int position) { public int getItemViewType(int position) {
if (position == 0) return 1; if (tList.get(position) == null) return 0;
else return 2;
if (tList.get(position).getSubtitle().equals(Constants.CARD_TYPE_TITLE)) {
return 2;
} else if (tList.get(position).getSubtitle().equals(Constants.CARD_TYPE_BODY_HEAD)) {
return 4;
}
return 0;
} }
@Override @Override
......
...@@ -5,6 +5,7 @@ import android.support.annotation.NonNull; ...@@ -5,6 +5,7 @@ import android.support.annotation.NonNull;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
import java.util.Objects;
import app.insti.interfaces.CardInterface; import app.insti.interfaces.CardInterface;
...@@ -199,4 +200,12 @@ public class Body implements CardInterface { ...@@ -199,4 +200,12 @@ public class Body implements CardInterface {
public String getAvatarUrl() { public String getAvatarUrl() {
return getBodyImageURL(); return getBodyImageURL();
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Body body = (Body) o;
return Objects.equals(bodyID, body.getBodyID());
}
} }
\ No newline at end of file
...@@ -100,7 +100,7 @@ public class EventFragment extends BackHandledFragment implements TransitionTarg ...@@ -100,7 +100,7 @@ public class EventFragment extends BackHandledFragment implements TransitionTarg
* @param count integer count to show in the badge * @param count integer count to show in the badge
* @return spannable to be used as view.setText(spannable) * @return spannable to be used as view.setText(spannable)
*/ */
static Spannable getCountBadgeSpannable(String text, Integer count) { public static Spannable getCountBadgeSpannable(String text, Integer count) {
// Check for nulls // Check for nulls
if (count == null) return new SpannableString(text); if (count == null) return new SpannableString(text);
......
package app.insti.utils;
import android.support.v4.app.Fragment;
import app.insti.Constants;
import app.insti.api.model.Body;
import app.insti.interfaces.CardInterface;
public class BodyHeadCard implements CardInterface {
private Body body;
public BodyHeadCard(Body mBody) {
body = mBody;
}
@Override
public long getId() {
return 0;
}
@Override
public String getTitle() {
return null;
}
@Override
public String getSubtitle() {
return Constants.CARD_TYPE_BODY_HEAD;
}
@Override
public String getAvatarUrl() {
return null;
}
public void bindView(BodyHeadViewHolder viewHolder, Fragment fragment) {
viewHolder.bindView(body, fragment);
}
}
package app.insti.utils;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import app.insti.R;
import app.insti.ShareURLMaker;
import app.insti.Utils;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Body;
import app.insti.fragment.EventFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import ru.noties.markwon.Markwon;
public class BodyHeadViewHolder extends RecyclerView.ViewHolder {
private TextView bodyName;
private TextView bodySubtitle;
private TextView bodyDescription;
private ImageButton webBodyButton;
private ImageButton shareBodyButton;
private final Button followButton;
public BodyHeadViewHolder(View itemView) {
super(itemView);
bodyName = itemView.findViewById(R.id.body_name);
bodySubtitle = itemView.findViewById(R.id.body_subtitle);
bodyDescription = itemView.findViewById(R.id.body_description);
webBodyButton = itemView.findViewById(R.id.web_body_button);
shareBodyButton = itemView.findViewById(R.id.share_body_button);
followButton = itemView.findViewById(R.id.follow_button);
}
public void bindView(final Body body, final Fragment fragment) {
/* Set body information */
bodyName.setText(body.getBodyName());
bodySubtitle.setText(body.getBodyShortDescription());
/* Return if it's a min body */
if (body.getBodyDescription() == null) {
return;
}
Markwon.setMarkdown(bodyDescription, body.getBodyDescription());
/* Check if user is already following
* Initialize follow button */
followButton.setBackgroundColor(fragment.getResources().getColor(body.getBodyUserFollows() ? R.color.colorAccent : R.color.colorWhite));
followButton.setText(EventFragment.getCountBadgeSpannable("FOLLOW", body.getBodyFollowersCount()));
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.updateBodyFollowing(Utils.getSessionIDHeader(), body.getBodyID(), body.getBodyUserFollows() ? 0 : 1).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
body.setBodyUserFollows(!body.getBodyUserFollows());
body.setBodyFollowersCount(body.getBodyUserFollows()? body.getBodyFollowersCount()+1:body.getBodyFollowersCount()-1);
followButton.setBackgroundColor(fragment.getResources().getColor(body.getBodyUserFollows() ? R.color.colorAccent : R.color.colorWhite));
followButton.setText(EventFragment.getCountBadgeSpannable("FOLLOW", body.getBodyFollowersCount()));
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Toast.makeText(fragment.getContext(), "Network Error", Toast.LENGTH_LONG).show();
}
});
}
});
/* Initialize web button */
if (body.getBodyWebsiteURL() != null && !body.getBodyWebsiteURL().isEmpty()) {
webBodyButton.setVisibility(View.VISIBLE);
webBodyButton.setOnClickListener(new View.OnClickListener() {
String bodywebURL = body.getBodyWebsiteURL();
@Override
public void onClick(View view) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(bodywebURL));
fragment.startActivity(browserIntent);
}
});
}
/* Initialize share button */
shareBodyButton.setOnClickListener(new View.OnClickListener() {
String shareUrl = ShareURLMaker.getBodyURL(body);
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Sharing URL");
i.putExtra(Intent.EXTRA_TEXT, shareUrl);
fragment.startActivity(Intent.createChooser(i, "Share URL"));
}
});
}
}
package app.insti.utils;
import app.insti.Constants;
import app.insti.interfaces.CardInterface;
public class TitleCard implements CardInterface {
private String title;
public TitleCard(String mTitle) {
title = mTitle;
}
@Override
public long getId() {
return (getSubtitle() + getTitle()).hashCode();
}
@Override
public String getTitle() {
return title;
}
@Override
public String getSubtitle() {
return Constants.CARD_TYPE_TITLE;
}
@Override
public String getAvatarUrl() {
return null;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/colorPrimary"
app:cardCornerRadius="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="12dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_weight="3"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/body_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Org Title"
android:textColor="@color/colorWhite"
android:textSize="21sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/web_body_button"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="Org Website"
android:src="@drawable/ic_language_black_24dp"
android:tint="@color/colorWhite"
android:visibility="invisible" />
<ImageButton
android:id="@+id/share_body_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="Share Body"
android:src="@drawable/ic_menu_share"
android:tint="@color/colorWhite" />
</LinearLayout>
<TextView
android:id="@+id/body_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite" />
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/follow_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
android:text="FOLLOW"
android:textAllCaps="false"
android:textColor="@color/secondaryTextColor" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#aaa">
</View>
<TextView
android:id="@+id/body_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="12dp"
android:textColor="?attr/themeColorInverse"
android:textColorLink="?attr/urlColor"
android:textSize="16sp" />
</LinearLayout>
This diff is collapsed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:fontFamily="sans-serif-light"
android:textSize="20sp" />
</LinearLayout>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment