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