Commit 4dff5f87 authored by Varun Patil's avatar Varun Patil

Add CardInterface to extend for card

parent 8b816b0d
...@@ -25,6 +25,7 @@ public final class Utils { ...@@ -25,6 +25,7 @@ public final class Utils {
public static UpdatableList<Event> eventCache = new UpdatableList<>(); public static UpdatableList<Event> eventCache = new UpdatableList<>();
private static String sessionId; private static String sessionId;
private static RetrofitInterface retrofitInterface; private static RetrofitInterface retrofitInterface;
public static Gson gson;
public static final void loadImageWithPlaceholder(final ImageView imageView, final String url) { public static final void loadImageWithPlaceholder(final ImageView imageView, final String url) {
Picasso.get() Picasso.get()
...@@ -114,4 +115,8 @@ public final class Utils { ...@@ -114,4 +115,8 @@ public final class Utils {
context.startActivity(browse); context.startActivity(browse);
} }
} }
public static void makeGson() {
Utils.gson = new Gson();
}
} }
...@@ -116,6 +116,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -116,6 +116,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
ServiceGenerator serviceGenerator = new ServiceGenerator(getApplicationContext()); ServiceGenerator serviceGenerator = new ServiceGenerator(getApplicationContext());
Utils.setRetrofitInterface(serviceGenerator.getRetrofitInterface()); Utils.setRetrofitInterface(serviceGenerator.getRetrofitInterface());
Utils.makeGson();
/* Make notification channel on oreo */ /* Make notification channel on oreo */
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
createNotificationChannel(); createNotificationChannel();
......
...@@ -18,19 +18,4 @@ public class BodyAdapter extends CardAdapter<Body> { ...@@ -18,19 +18,4 @@ public class BodyAdapter extends CardAdapter<Body> {
public void onClick(Body body, FragmentActivity fragmentActivity) { public void onClick(Body body, FragmentActivity fragmentActivity) {
Utils.openBodyFragment(body, fragmentActivity); Utils.openBodyFragment(body, fragmentActivity);
} }
@Override
public String getTitle(Body body) {
return body.getBodyName();
}
@Override
public String getSubtitle(Body body) {
return body.getBodyShortDescription();
}
@Override
public String getAvatarUrl(Body body) {
return body.getBodyImageURL();
}
} }
...@@ -18,18 +18,15 @@ import java.util.List; ...@@ -18,18 +18,15 @@ import java.util.List;
import app.insti.R; import app.insti.R;
import app.insti.Utils; import app.insti.Utils;
import app.insti.interfaces.CardInterface;
public abstract class CardAdapter<T> extends RecyclerView.Adapter<CardAdapter<T>.ViewHolder> { public abstract class CardAdapter<T extends CardInterface> extends RecyclerView.Adapter<CardAdapter<T>.ViewHolder> {
private List<T> tList; private List<T> tList;
private Fragment mFragment; private Fragment mFragment;
public abstract void onClick(T t, FragmentActivity fragmentActivity); public abstract void onClick(T t, FragmentActivity fragmentActivity);
public abstract String getAvatarUrl(T t);
public abstract String getTitle(T t);
public abstract String getSubtitle(T t);
public String getBigImageUrl(T t) { public String getBigImageUrl(T t) {
return null; return null;
} }
...@@ -64,8 +61,8 @@ public abstract class CardAdapter<T> extends RecyclerView.Adapter<CardAdapter<T> ...@@ -64,8 +61,8 @@ public abstract class CardAdapter<T> extends RecyclerView.Adapter<CardAdapter<T>
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
T t = tList.get(i); T t = tList.get(i);
viewHolder.title.setText(getTitle(t)); viewHolder.title.setText(t.getTitle());
viewHolder.subtitle.setText(getSubtitle(t)); viewHolder.subtitle.setText(t.getSubtitle());
if (getBigImageUrl(t) != null) { if (getBigImageUrl(t) != null) {
// Show big image, hide avatar // Show big image, hide avatar
...@@ -77,8 +74,8 @@ public abstract class CardAdapter<T> extends RecyclerView.Adapter<CardAdapter<T> ...@@ -77,8 +74,8 @@ public abstract class CardAdapter<T> extends RecyclerView.Adapter<CardAdapter<T>
} else { } else {
// Build basic request // Build basic request
RequestCreator requestCreator; RequestCreator requestCreator;
if (getAvatarUrl(t) != null) if (t.getAvatarUrl() != null)
requestCreator = Picasso.get().load(Utils.resizeImageUrl(getAvatarUrl(t))); requestCreator = Picasso.get().load(Utils.resizeImageUrl(t.getAvatarUrl()));
else if (getAvatarPlaceholder() != 0) { else if (getAvatarPlaceholder() != 0) {
requestCreator = Picasso.get().load(getAvatarPlaceholder()); requestCreator = Picasso.get().load(getAvatarPlaceholder());
} else { } else {
......
...@@ -3,15 +3,10 @@ package app.insti.adapter; ...@@ -3,15 +3,10 @@ package app.insti.adapter;
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 java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
import app.insti.Utils; import app.insti.Utils;
import app.insti.api.model.Event; import app.insti.api.model.Event;
import app.insti.api.model.Venue;
public class FeedAdapter extends CardAdapter<Event> { public class FeedAdapter extends CardAdapter<Event> {
...@@ -24,65 +19,6 @@ public class FeedAdapter extends CardAdapter<Event> { ...@@ -24,65 +19,6 @@ public class FeedAdapter extends CardAdapter<Event> {
Utils.openEventFragment(event, fragmentActivity); Utils.openEventFragment(event, fragmentActivity);
} }
@Override
public String getTitle(Event event) {
return event.getEventName();
}
@Override
public String getSubtitle(Event event)
{
String subtitle = "";
Date startTime = event.getEventStartTime();
Date endTime = event.getEventEndTime();
Date timeNow = Calendar.getInstance().getTime();
boolean eventStarted = timeNow.compareTo(startTime) > 0;
boolean eventEnded = timeNow.compareTo(endTime) > 0;
if (eventEnded)
subtitle += "Event ended | ";
else if(eventStarted)
{
long difference = endTime.getTime() - timeNow.getTime();
long minutes = difference / (60 * 1000 ) % 60;
long hours = difference / (60 * 60 * 1000) % 24;
long days = difference / (24 * 60 * 60 * 1000);
String timeDiff = "";
if (days > 0)
timeDiff += Long.toString(days) + "D ";
if (hours > 0)
timeDiff += Long.toString(hours) + "H ";
timeDiff += Long.toString(minutes) + "M";
subtitle += "Ends in " + timeDiff + " | " ;
}
Timestamp timestamp = event.getEventStartTime();
if (timestamp != null) {
Date Date = new Date(timestamp.getTime());
SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("dd MMM");
SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("HH:mm");
subtitle += simpleDateFormatDate.format(Date) + " | " + simpleDateFormatTime.format(Date);
}
StringBuilder eventVenueName = new StringBuilder();
for (Venue venue : event.getEventVenues()) {
eventVenueName.append(", ").append(venue.getVenueShortName());
}
if (!eventVenueName.toString().equals(""))
subtitle += " | " + eventVenueName.toString().substring(2);
return subtitle;
}
@Override
public String getAvatarUrl(Event event) {
return event.getEventImageURL();
}
@Override @Override
public String getBigImageUrl(Event event) { public String getBigImageUrl(Event event) {
if (event.isEventBigImage()) { if (event.isEventBigImage()) {
......
...@@ -12,7 +12,6 @@ import app.insti.Utils; ...@@ -12,7 +12,6 @@ import app.insti.Utils;
import app.insti.api.EmptyCallback; 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.Event;
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.fragment.NewsFragment; import app.insti.fragment.NewsFragment;
...@@ -20,16 +19,8 @@ import app.insti.fragment.PlacementBlogFragment; ...@@ -20,16 +19,8 @@ import app.insti.fragment.PlacementBlogFragment;
import app.insti.fragment.TrainingBlogFragment; import app.insti.fragment.TrainingBlogFragment;
public class NotificationsAdapter extends CardAdapter<Notification> { public class NotificationsAdapter extends CardAdapter<Notification> {
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) { public NotificationsAdapter(List<Notification> notifications, Fragment fragment) {
super(notifications, fragment); super(notifications, fragment);
gson = new Gson();
} }
@Override @Override
...@@ -40,14 +31,14 @@ public class NotificationsAdapter extends CardAdapter<Notification> { ...@@ -40,14 +31,14 @@ public class NotificationsAdapter extends CardAdapter<Notification> {
retrofitInterface.markNotificationRead(sessId, notification.getNotificationId().toString()).enqueue(new EmptyCallback<Void>()); retrofitInterface.markNotificationRead(sessId, notification.getNotificationId().toString()).enqueue(new EmptyCallback<Void>());
/* Open event */ /* Open event */
if (isEvent(notification)) { if (notification.isEvent()) {
Gson gson = new Gson(); Gson gson = new Gson();
Event event = gson.fromJson(gson.toJson(notification.getNotificationActor()), Event.class) ; Event event = gson.fromJson(gson.toJson(notification.getNotificationActor()), Event.class) ;
Utils.openEventFragment(event, fragmentActivity); Utils.openEventFragment(event, fragmentActivity);
} else if (isNews(notification)) { } else if (notification.isNews()) {
NewsFragment newsFragment = new NewsFragment(); NewsFragment newsFragment = new NewsFragment();
Utils.updateFragment(newsFragment, fragmentActivity); Utils.updateFragment(newsFragment, fragmentActivity);
} else if (isBlogPost(notification)) { } else if (notification.isBlogPost()) {
Gson gson = new Gson(); Gson gson = new Gson();
PlacementBlogPost post = gson.fromJson(gson.toJson(notification.getNotificationActor()), PlacementBlogPost.class); PlacementBlogPost post = gson.fromJson(gson.toJson(notification.getNotificationActor()), PlacementBlogPost.class);
if (post.getLink().contains("training")) { if (post.getLink().contains("training")) {
...@@ -58,59 +49,8 @@ public class NotificationsAdapter extends CardAdapter<Notification> { ...@@ -58,59 +49,8 @@ public class NotificationsAdapter extends CardAdapter<Notification> {
} }
} }
@Override
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 String getSubtitle(Notification notification) {
return notification.getNotificationVerb();
}
@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 getAvatarPlaceholder() { public int getAvatarPlaceholder() {
return R.drawable.lotus_sq; return R.drawable.lotus_sq;
} }
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);
}
} }
...@@ -11,7 +11,6 @@ import app.insti.api.model.Role; ...@@ -11,7 +11,6 @@ import app.insti.api.model.Role;
public class RoleAdapter extends CardAdapter<Role> { public class RoleAdapter extends CardAdapter<Role> {
public RoleAdapter(List<Role> roleList, Fragment mFragment) { public RoleAdapter(List<Role> roleList, Fragment mFragment) {
super(roleList, mFragment); super(roleList, mFragment);
} }
...@@ -20,19 +19,4 @@ public class RoleAdapter extends CardAdapter<Role> { ...@@ -20,19 +19,4 @@ public class RoleAdapter extends CardAdapter<Role> {
public void onClick(Role role, FragmentActivity fragmentActivity) { public void onClick(Role role, FragmentActivity fragmentActivity) {
Utils.openBodyFragment(role.getRoleBodyDetails(), fragmentActivity); Utils.openBodyFragment(role.getRoleBodyDetails(), fragmentActivity);
} }
@Override
public String getTitle(Role role) {
return role.getRoleBodyDetails().getBodyName();
}
@Override
public String getSubtitle(Role role) {
return role.getRoleName();
}
@Override
public String getAvatarUrl(Role role) {
return role.getRoleBodyDetails().getBodyImageURL();
}
} }
...@@ -20,25 +20,6 @@ public class UserAdapter extends CardAdapter<User> { ...@@ -20,25 +20,6 @@ public class UserAdapter extends CardAdapter<User> {
Utils.openUserFragment(user, fragmentActivity); Utils.openUserFragment(user, fragmentActivity);
} }
@Override
public String getTitle(User user) {
return user.getUserName();
}
@Override
public String getSubtitle(User user) {
if (user.getCurrentRole() == null || user.getCurrentRole().equals("")) {
return user.getUserLDAPId();
} else {
return user.getCurrentRole();
}
}
@Override
public String getAvatarUrl(User user) {
return user.getUserProfilePictureUrl();
}
@Override @Override
public int getAvatarPlaceholder() { public int getAvatarPlaceholder() {
return R.drawable.user_placeholder; return R.drawable.user_placeholder;
......
...@@ -6,7 +6,9 @@ import com.google.gson.annotations.SerializedName; ...@@ -6,7 +6,9 @@ import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
public class Body { import app.insti.interfaces.CardInterface;
public class Body implements CardInterface {
@SerializedName("id") @SerializedName("id")
private String bodyID; private String bodyID;
...@@ -181,4 +183,16 @@ public class Body { ...@@ -181,4 +183,16 @@ public class Body {
public void setBodyRoles(List<Role> bodyRoles) { public void setBodyRoles(List<Role> bodyRoles) {
this.bodyRoles = bodyRoles; this.bodyRoles = bodyRoles;
} }
public String getTitle() {
return getBodyName();
}
public String getSubtitle() {
return getBodyShortDescription();
}
public String getAvatarUrl() {
return getBodyImageURL();
}
} }
\ No newline at end of file
...@@ -6,10 +6,15 @@ import com.google.gson.Gson; ...@@ -6,10 +6,15 @@ import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
public class Event { import app.insti.interfaces.CardInterface;
public class Event implements CardInterface {
@NonNull() @NonNull()
@SerializedName("id") @SerializedName("id")
private String eventID; private String eventID;
...@@ -237,4 +242,60 @@ public class Event { ...@@ -237,4 +242,60 @@ public class Event {
public int hashCode() { public int hashCode() {
return Objects.hash(eventID); return Objects.hash(eventID);
} }
public String getTitle() {
return getEventName();
}
public String getSubtitle()
{
String subtitle = "";
Date startTime = getEventStartTime();
Date endTime = getEventEndTime();
Date timeNow = Calendar.getInstance().getTime();
boolean eventStarted = timeNow.compareTo(startTime) > 0;
boolean eventEnded = timeNow.compareTo(endTime) > 0;
if (eventEnded)
subtitle += "Event ended | ";
else if(eventStarted)
{
long difference = endTime.getTime() - timeNow.getTime();
long minutes = difference / (60 * 1000 ) % 60;
long hours = difference / (60 * 60 * 1000) % 24;
long days = difference / (24 * 60 * 60 * 1000);
String timeDiff = "";
if (days > 0)
timeDiff += Long.toString(days) + "D ";
if (hours > 0)
timeDiff += Long.toString(hours) + "H ";
timeDiff += Long.toString(minutes) + "M";
subtitle += "Ends in " + timeDiff + " | " ;
}
Timestamp timestamp = getEventStartTime();
if (timestamp != null) {
Date Date = new Date(timestamp.getTime());
SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("dd MMM");
SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("HH:mm");
subtitle += simpleDateFormatDate.format(Date) + " | " + simpleDateFormatTime.format(Date);
}
StringBuilder eventVenueName = new StringBuilder();
for (Venue venue : getEventVenues()) {
eventVenueName.append(", ").append(venue.getVenueShortName());
}
if (!eventVenueName.toString().equals(""))
subtitle += " | " + eventVenueName.toString().substring(2);
return subtitle;
}
public String getAvatarUrl() {
return getEventImageURL();
}
} }
...@@ -2,11 +2,19 @@ package app.insti.api.model; ...@@ -2,11 +2,19 @@ package app.insti.api.model;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
public class Notification { import app.insti.Utils;
@NonNull() import app.insti.interfaces.CardInterface;
public class Notification implements CardInterface {
private final String TYPE_EVENT = "event";
private final String TYPE_NEWSENTRY = "newsentry";
private final String TYPE_BLOG = "blogentry";
@NonNull()
@SerializedName("id") @SerializedName("id")
private Integer notificationId; private Integer notificationId;
...@@ -70,4 +78,55 @@ public class Notification { ...@@ -70,4 +78,55 @@ public class Notification {
public void setNotificationActor(Object notificationActor) { public void setNotificationActor(Object notificationActor) {
this.notificationActor = notificationActor; this.notificationActor = notificationActor;
} }
public String getTitle() {
if (isEvent()) {
return getEvent().getEventName();
} else if (isNews()) {
return getNews().getTitle();
} else if (isBlogPost()) {
return getBlogPost().getTitle();
}
return "Notification";
}
public String getSubtitle() {
return getNotificationVerb();
}
public String getAvatarUrl() {
if (isEvent()) {
return getEvent().getEventImageURL();
} else if (isNews()) {
return getNews().getBody().getBodyImageURL();
}
return null;
}
public boolean isEvent() {
return getNotificationActorType().contains(TYPE_EVENT);
}
public boolean isNews() {
return getNotificationActorType().contains(TYPE_NEWSENTRY);
}
public boolean isBlogPost() {
return getNotificationActorType().contains(TYPE_BLOG);
}
public Event getEvent() {
Gson gson = Utils.gson;
return gson.fromJson(gson.toJson(getNotificationActor()), Event.class);
}
public NewsArticle getNews() {
Gson gson = Utils.gson;
return gson.fromJson(gson.toJson(getNotificationActor()), NewsArticle.class);
}
public PlacementBlogPost getBlogPost() {
Gson gson = Utils.gson;
return gson.fromJson(gson.toJson(getNotificationActor()), PlacementBlogPost.class);
}
} }
...@@ -6,7 +6,9 @@ import com.google.gson.annotations.SerializedName; ...@@ -6,7 +6,9 @@ import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
public class Role { import app.insti.interfaces.CardInterface;
public class Role implements CardInterface {
@NonNull() @NonNull()
@SerializedName("id") @SerializedName("id")
private String roleID; private String roleID;
...@@ -118,4 +120,16 @@ public class Role { ...@@ -118,4 +120,16 @@ public class Role {
public void setRoleUsersDetail(List<User> roleUsersDetail) { public void setRoleUsersDetail(List<User> roleUsersDetail) {
this.roleUsersDetail = roleUsersDetail; this.roleUsersDetail = roleUsersDetail;
} }
public String getTitle() {
return getRoleBodyDetails().getBodyName();
}
public String getSubtitle() {
return getRoleName();
}
public String getAvatarUrl() {
return getRoleBodyDetails().getBodyImageURL();
}
} }
...@@ -7,7 +7,9 @@ import com.google.gson.annotations.SerializedName; ...@@ -7,7 +7,9 @@ import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
public class User { import app.insti.interfaces.CardInterface;
public class User implements CardInterface {
@NonNull() @NonNull()
@SerializedName("id") @SerializedName("id")
private String userID; private String userID;
...@@ -235,4 +237,20 @@ public class User { ...@@ -235,4 +237,20 @@ public class User {
public String toString() { public String toString() {
return new Gson().toJson(this); return new Gson().toJson(this);
} }
public String getTitle() {
return getUserName();
}
public String getSubtitle() {
if (getCurrentRole() == null || getCurrentRole().equals("")) {
return getUserLDAPId();
} else {
return getCurrentRole();
}
}
public String getAvatarUrl() {
return getUserProfilePictureUrl();
}
} }
package app.insti.interfaces;
public interface CardInterface {
String getTitle();
String getSubtitle();
String getAvatarUrl();
}
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