Commit 11aff4b6 authored by Varun Patil's avatar Varun Patil

Rewrite BodyFragment to be fully RecylerView

parent 538ed195
...@@ -57,5 +57,6 @@ dependencies { ...@@ -57,5 +57,6 @@ dependencies {
implementation "me.relex:circleindicator:${circleIndicatorVersion}" implementation "me.relex:circleindicator:${circleIndicatorVersion}"
implementation "com.google.android:flexbox:${flexboxVersion}" implementation "com.google.android:flexbox:${flexboxVersion}"
implementation "io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:${sectionedRecyclerViewVersion}" implementation "io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:${sectionedRecyclerViewVersion}"
implementation 'com.android.support:support-v4:28.0.0'
} }
apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.gms.google-services'
...@@ -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};
......
...@@ -31,6 +31,7 @@ import app.insti.api.model.Event; ...@@ -31,6 +31,7 @@ import app.insti.api.model.Event;
import app.insti.api.model.Notification; import app.insti.api.model.Notification;
import app.insti.api.model.User; import app.insti.api.model.User;
import app.insti.fragment.BodyFragment; import app.insti.fragment.BodyFragment;
import app.insti.fragment.BodyFragmentNew;
import app.insti.fragment.EventFragment; import app.insti.fragment.EventFragment;
import app.insti.fragment.TransitionTargetChild; import app.insti.fragment.TransitionTargetChild;
import app.insti.fragment.TransitionTargetFragment; import app.insti.fragment.TransitionTargetFragment;
...@@ -139,11 +140,11 @@ public final class Utils { ...@@ -139,11 +140,11 @@ public final class Utils {
.commit(); .commit();
} }
public static BodyFragment getBodyFragment(Body body, boolean sharedElements) { public static BodyFragmentNew getBodyFragment(Body body, boolean sharedElements) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(Constants.BODY_JSON, new Gson().toJson(body)); bundle.putString(Constants.BODY_JSON, new Gson().toJson(body));
bundle.putBoolean(Constants.NO_SHARED_ELEM, !sharedElements); bundle.putBoolean(Constants.NO_SHARED_ELEM, !sharedElements);
BodyFragment bodyFragment = new BodyFragment(); BodyFragmentNew bodyFragment = new BodyFragmentNew();
bodyFragment.setArguments(bundle); bodyFragment.setArguments(bundle);
return bodyFragment; return bodyFragment;
} }
......
...@@ -4,6 +4,7 @@ import android.content.Context; ...@@ -4,6 +4,7 @@ import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
...@@ -16,11 +17,14 @@ import com.squareup.picasso.RequestCreator; ...@@ -16,11 +17,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 +49,23 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -45,12 +49,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.fragment_body, 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,50 +78,63 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -63,50 +78,63 @@ 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) {
T t = tList.get(i);
viewHolder.title.setText(t.getTitle()); switch (holder.getItemViewType()) {
viewHolder.subtitle.setText(t.getSubtitle()); case 0:
CardViewHolder viewHolder = (CardViewHolder) holder;
// Set transition names T t = tList.get(i);
viewHolder.avatar.setTransitionName(uid + Integer.toString((int) t.getId()) + "_sharedAvatar"); viewHolder.title.setText(t.getTitle());
viewHolder.subtitle.setText(t.getSubtitle());
if (getBigImageUrl(t) != null) {
// Show big image, hide avatar // Set transition names
viewHolder.bigPicture.setVisibility(View.VISIBLE); viewHolder.avatar.setTransitionName(uid + Integer.toString((int) t.getId()) + "_sharedAvatar");
viewHolder.bigPicture.setTransitionName(uid + Integer.toString((int) t.getId()) + "_sharedBigPicture");
viewHolder.avatar.setVisibility(View.GONE); if (getBigImageUrl(t) != null) {
// Show big image, hide avatar
// Load big image with low resolution as avatar viewHolder.bigPicture.setVisibility(View.VISIBLE);
Utils.loadImageWithPlaceholder(viewHolder.bigPicture, getBigImageUrl(t)); viewHolder.bigPicture.setTransitionName(uid + Integer.toString((int) t.getId()) + "_sharedBigPicture");
} else { viewHolder.avatar.setVisibility(View.GONE);
// Build basic request
RequestCreator requestCreator; // Load big image with low resolution as avatar
if (t.getAvatarUrl() != null) Utils.loadImageWithPlaceholder(viewHolder.bigPicture, getBigImageUrl(t));
requestCreator = Picasso.get().load(Utils.resizeImageUrl(t.getAvatarUrl())); } else {
else if (getAvatarPlaceholder(t) != 0) { // Build basic request
requestCreator = Picasso.get().load(getAvatarPlaceholder(t)); RequestCreator requestCreator;
} else { if (t.getAvatarUrl() != null)
requestCreator = Picasso.get().load(Utils.resizeImageUrl(t.getAvatarUrl()));
else if (getAvatarPlaceholder(t) != 0) {
requestCreator = Picasso.get().load(getAvatarPlaceholder(t));
} else {
return;
}
// Check if we have a placeholder
if (getAvatarPlaceholder(t) != 0) {
requestCreator.placeholder(getAvatarPlaceholder(t));
}
// Load the image
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; return;
}
// Check if we have a placeholder
if (getAvatarPlaceholder(t) != 0) {
requestCreator.placeholder(getAvatarPlaceholder(t));
}
// Load the image
requestCreator.into(viewHolder.avatar);
} }
} }
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 +144,25 @@ public abstract class CardAdapter<T extends CardInterface> extends RecyclerView. ...@@ -116,10 +144,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
......
This diff is collapsed.
...@@ -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;
}
}
This diff is collapsed.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragment.BodyFragmentNew">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/body_picture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:transitionName="sharedAvatar" />
<android.support.v7.widget.RecyclerView
android:id="@+id/body_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<RelativeLayout
android:id="@+id/loadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:theme="@style/BlueAccent" />
</RelativeLayout>
</FrameLayout>
\ No newline at end of file
<?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>
\ No newline at end of file
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