Commit 8b816b0d authored by Varun Patil's avatar Varun Patil

Use CardAdapter for NotificationsAdapter

parent 98b30cf8
......@@ -76,7 +76,14 @@ public abstract class CardAdapter<T> extends RecyclerView.Adapter<CardAdapter<T>
Utils.loadImageWithPlaceholder(viewHolder.bigPicture, getBigImageUrl(t));
} else {
// Build basic request
RequestCreator requestCreator = Picasso.get().load(Utils.resizeImageUrl(getAvatarUrl(t)));
RequestCreator requestCreator;
if (getAvatarUrl(t) != null)
requestCreator = Picasso.get().load(Utils.resizeImageUrl(getAvatarUrl(t)));
else if (getAvatarPlaceholder() != 0) {
requestCreator = Picasso.get().load(getAvatarPlaceholder());
} else {
return;
}
// Check if we have a placeholder
if (getAvatarPlaceholder() != 0) {
......
package app.insti.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import java.util.List;
import app.insti.R;
import app.insti.Utils;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Event;
import app.insti.api.model.NewsArticle;
import app.insti.api.model.Notification;
import app.insti.api.model.PlacementBlogPost;
import app.insti.interfaces.ItemClickListener;
import app.insti.fragment.NewsFragment;
import app.insti.fragment.PlacementBlogFragment;
import app.insti.fragment.TrainingBlogFragment;
public class NotificationsAdapter extends RecyclerView.Adapter<NotificationsAdapter.Viewholder> {
private List<Notification> notifications;
private Context context;
private ItemClickListener itemClickListener;
public class NotificationsAdapter extends CardAdapter<Notification> {
public NotificationsAdapter(List<Notification> notifications, ItemClickListener itemClickListener) {
this.notifications = notifications;
this.itemClickListener = itemClickListener;
private final String TYPE_EVENT = "event";
private final String TYPE_NEWSENTRY = "newsentry";
private final String TYPE_BLOG = "blogentry";
private Gson gson;
public NotificationsAdapter(List<Notification> notifications, Fragment fragment) {
super(notifications, fragment);
gson = new Gson();
}
@Override
public Viewholder onCreateViewHolder(ViewGroup viewGroup, int i) {
context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View notificationView = inflater.inflate(R.layout.feed_card, viewGroup, false);
final Viewholder notificationsViewHolder = new Viewholder(notificationView);
notificationView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemClickListener.onItemClick(v, notificationsViewHolder.getAdapterPosition());
public void onClick(Notification notification, FragmentActivity fragmentActivity) {
/* Mark notification read */
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
String sessId = Utils.getSessionIDHeader();
retrofitInterface.markNotificationRead(sessId, notification.getNotificationId().toString()).enqueue(new EmptyCallback<Void>());
/* Open event */
if (isEvent(notification)) {
Gson gson = new Gson();
Event event = gson.fromJson(gson.toJson(notification.getNotificationActor()), Event.class) ;
Utils.openEventFragment(event, fragmentActivity);
} else if (isNews(notification)) {
NewsFragment newsFragment = new NewsFragment();
Utils.updateFragment(newsFragment, fragmentActivity);
} else if (isBlogPost(notification)) {
Gson gson = new Gson();
PlacementBlogPost post = gson.fromJson(gson.toJson(notification.getNotificationActor()), PlacementBlogPost.class);
if (post.getLink().contains("training")) {
Utils.updateFragment(new TrainingBlogFragment(), fragmentActivity);
} else {
Utils.updateFragment(new PlacementBlogFragment(), fragmentActivity);
}
});
return notificationsViewHolder;
}
}
@Override
public void onBindViewHolder(Viewholder viewholder, int i) {
Gson gson = new Gson();
Notification appNotification = notifications.get(i);
viewholder.notificationVerb.setText(appNotification.getNotificationVerb());
if (appNotification.getNotificationActorType().contains("event")) {
Event event = gson.fromJson(gson.toJson(appNotification.getNotificationActor()), Event.class);
Picasso.get().load(
Utils.resizeImageUrl(event.getEventImageURL())
).into(viewholder.notificationPicture);
viewholder.notificationTitle.setText(event.getEventName());
} else if (appNotification.getNotificationActorType().contains("newsentry")) {
NewsArticle article = gson.fromJson(gson.toJson(appNotification.getNotificationActor()), NewsArticle.class);
Picasso.get().load(
Utils.resizeImageUrl(article.getBody().getBodyImageURL())
).into(viewholder.notificationPicture);
viewholder.notificationTitle.setText(article.getTitle());
} else if (appNotification.getNotificationActorType().contains("blogentry")) {
PlacementBlogPost post = gson.fromJson(gson.toJson(appNotification.getNotificationActor()), PlacementBlogPost.class);
Picasso.get().load(R.drawable.lotus_sq).into(viewholder.notificationPicture);
viewholder.notificationTitle.setText(post.getTitle());
public String getTitle(Notification notification) {
if (isEvent(notification)) {
return getEvent(notification).getEventName();
} else if (isNews(notification)) {
return getNews(notification).getTitle();
} else if (isBlogPost(notification)) {
return getBlogPost(notification).getTitle();
}
return "Notification";
}
@Override
public int getItemCount() {
return notifications.size();
public String getSubtitle(Notification notification) {
return notification.getNotificationVerb();
}
public class Viewholder extends RecyclerView.ViewHolder {
private TextView notificationTitle;
private ImageView notificationPicture;
private TextView notificationVerb;
@Override
public String getAvatarUrl(Notification notification) {
if (isEvent(notification)) {
return getEvent(notification).getEventImageURL();
} else if (isNews(notification)) {
return getNews(notification).getBody().getBodyImageURL();
}
return null;
}
public Viewholder(View itemView) {
super(itemView);
@Override
public int getAvatarPlaceholder() {
return R.drawable.lotus_sq;
}
notificationPicture = (ImageView) itemView.findViewById(R.id.object_picture);
notificationTitle = (TextView) itemView.findViewById(R.id.object_title);
notificationVerb = (TextView) itemView.findViewById(R.id.object_subtitle);
}
private boolean isEvent(Notification notification) {
return notification.getNotificationActorType().contains(TYPE_EVENT);
}
private boolean isNews(Notification notification) {
return notification.getNotificationActorType().contains(TYPE_NEWSENTRY);
}
private boolean isBlogPost(Notification notification) {
return notification.getNotificationActorType().contains(TYPE_BLOG);
}
private Event getEvent(Notification notification) {
return gson.fromJson(gson.toJson(notification.getNotificationActor()), Event.class);
}
private NewsArticle getNews(Notification notification) {
return gson.fromJson(gson.toJson(notification.getNotificationActor()), NewsArticle.class);
}
private PlacementBlogPost getBlogPost(Notification notification) {
return gson.fromJson(gson.toJson(notification.getNotificationActor()), PlacementBlogPost.class);
}
}
......@@ -10,19 +10,13 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import java.util.List;
import app.insti.R;
import app.insti.Utils;
import app.insti.adapter.NotificationsAdapter;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Event;
import app.insti.api.model.Notification;
import app.insti.api.model.PlacementBlogPost;
import app.insti.interfaces.ItemClickListener;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -78,36 +72,7 @@ public class NotificationsFragment extends BaseFragment {
/* Hide loader */
getActivity().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
NotificationsAdapter notificationsAdapter = new NotificationsAdapter(notifications, new ItemClickListener() {
@Override
public void onItemClick(View v, int position) {
/* Get the notification */
Notification notification = notifications.get(position);
/* Mark notification read */
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
String sessId = Utils.getSessionIDHeader();
retrofitInterface.markNotificationRead(sessId, notification.getNotificationId().toString()).enqueue(new EmptyCallback<Void>());
/* Open event */
if (notification.getNotificationActorType().contains("event")) {
Gson gson = new Gson();
Event event = gson.fromJson(gson.toJson(notification.getNotificationActor()), Event.class) ;
Utils.openEventFragment(event, getActivity());
} else if (notification.getNotificationActorType().contains("newsentry")) {
NewsFragment newsFragment = new NewsFragment();
Utils.updateFragment(newsFragment, getActivity());
} else if (notification.getNotificationActorType().contains("blogentry")) {
Gson gson = new Gson();
PlacementBlogPost post = gson.fromJson(gson.toJson(notification.getNotificationActor()), PlacementBlogPost.class);
if (post.getLink().contains("training")) {
Utils.updateFragment(new TrainingBlogFragment(), getActivity());
} else {
Utils.updateFragment(new PlacementBlogFragment(), getActivity());
}
}
}
});
NotificationsAdapter notificationsAdapter = new NotificationsAdapter(notifications, this);
notificationsRecyclerView = (RecyclerView) getActivity().findViewById(R.id.notifications_recycler_view);
notificationsRecyclerView.setAdapter(notificationsAdapter);
notificationsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
......
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