Commit b853b15c authored by Varun Patil's avatar Varun Patil

MAJOR REFACOR: move getSessionIDHeader and getRetrofitInterface to Utils

parent a3e5d0d6
......@@ -11,6 +11,7 @@ import com.google.gson.Gson;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Body;
import app.insti.api.model.Event;
import app.insti.api.model.User;
......@@ -19,6 +20,8 @@ import app.insti.fragment.EventFragment;
import app.insti.fragment.UserFragment;
public final class Utils {
private static String sessionId;
private static RetrofitInterface retrofitInterface;
public static UpdatableList<Event> eventCache = new UpdatableList<>();
public static final void loadImageWithPlaceholder(final ImageView imageView, final String url) {
......@@ -79,4 +82,20 @@ public final class Utils {
userFragment.setArguments(bundle);
updateFragment(userFragment, fragmentActivity);
}
public static void setSessionId(String sessionId1) {
sessionId = sessionId1;
}
public static String getSessionIDHeader() {
return "sessionid=" + sessionId;
}
public static RetrofitInterface getRetrofitInterface() {
return retrofitInterface;
}
public static void setRetrofitInterface(RetrofitInterface retrofitInterface) {
Utils.retrofitInterface = retrofitInterface;
}
}
......@@ -43,6 +43,7 @@ import java.util.List;
import app.insti.Constants;
import app.insti.R;
import app.insti.SessionManager;
import app.insti.Utils;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface;
import app.insti.api.ServiceGenerator;
......@@ -90,15 +91,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private boolean showNotifications = false;
private BackHandledFragment selectedFragment;
private Menu menu;
private RetrofitInterface retrofitInterface;
/** which menu item should be checked on activity start */
private int initMenuChecked = R.id.nav_feed;
public RetrofitInterface getRetrofitInterface() {
return retrofitInterface;
}
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
......@@ -115,7 +111,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
super.onCreate(savedInstanceState);
ServiceGenerator serviceGenerator = new ServiceGenerator(getApplicationContext());
this.retrofitInterface = serviceGenerator.getRetrofitInterface();
Utils.setRetrofitInterface(serviceGenerator.getRetrofitInterface());
/* Make notification channel on oreo */
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
......@@ -151,8 +147,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
private void fetchNotifications() {
RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.getNotifications(getSessionIDHeader()).enqueue(new EmptyCallback<List<Notification>>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getNotifications(Utils.getSessionIDHeader()).enqueue(new EmptyCallback<List<Notification>>() {
@Override
public void onResponse(Call<List<Notification>> call, Response<List<Notification>> response) {
if (response.isSuccessful()) {
......@@ -181,7 +177,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private void checkLatestVersion() {
final int versionCode = getCurrentVersion();
if (versionCode == 0) { return; }
RetrofitInterface retrofitInterface = getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getLatestVersion().enqueue(new EmptyCallback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
......@@ -247,7 +243,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
/* Mark the notification read */
final String notificationId = bundle.getString(FCM_BUNDLE_NOTIFICATION_ID);
if (notificationId != null) {
getRetrofitInterface().markNotificationRead(getSessionIDHeader(), notificationId).enqueue(new EmptyCallback<Void>());
Utils.getRetrofitInterface().markNotificationRead(Utils.getSessionIDHeader(), notificationId).enqueue(new EmptyCallback<Void>());
}
/* Follow the notification */
......@@ -323,8 +319,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
/** Open the event fragment from the provided id */
private void openEventFragment(String id) {
RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.getEvent(getSessionIDHeader(), id).enqueue(new EmptyCallback<Event>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getEvent(Utils.getSessionIDHeader(), id).enqueue(new EmptyCallback<Event>() {
@Override
public void onResponse(Call<Event> call, Response<Event> response) {
EventFragment eventFragment = new EventFragment();
......@@ -366,6 +362,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
super.onStart();
initNavigationView();
if (session.isLoggedIn()) {
Utils.setSessionId(session.getSessionID());
currentUser = User.fromString(session.pref.getString(Constants.CURRENT_USER, ""));
updateNavigationView();
updateFCMId();
......@@ -380,9 +377,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
final String fcmId = instanceIdResult.getToken();
RetrofitInterface retrofitInterface = getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.patchUserMe(getSessionIDHeader(), new UserFCMPatchRequest(fcmId, getCurrentVersion())).enqueue(new EmptyCallback<User>() {
retrofitInterface.patchUserMe(Utils.getSessionIDHeader(), new UserFCMPatchRequest(fcmId, getCurrentVersion())).enqueue(new EmptyCallback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
......@@ -597,10 +594,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
}
public String getSessionIDHeader() {
return "sessionid=" + session.getSessionID();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
......
......@@ -26,6 +26,7 @@ import com.google.gson.Gson;
import app.insti.Constants;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Body;
......@@ -77,7 +78,7 @@ public class AddEventFragment extends BaseFragment {
webView.setWebViewClient(new MyWebViewClient());
CookieManager cookieManager = CookieManager.getInstance();
String cookieString = ((MainActivity) getActivity()).getSessionIDHeader();
String cookieString = Utils.getSessionIDHeader();
cookieManager.setCookie(host, cookieString);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().flush();
......@@ -166,8 +167,8 @@ public class AddEventFragment extends BaseFragment {
if (url.contains("/event/")) {
url = url.substring(url.lastIndexOf("/") + 1);
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.getEvent(((MainActivity) getActivity()).getSessionIDHeader(), url).enqueue(new Callback<Event>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getEvent(Utils.getSessionIDHeader(), url).enqueue(new Callback<Event>() {
@Override
public void onResponse(Call<Event> call, Response<Event> response) {
if (response.isSuccessful()) {
......@@ -184,8 +185,8 @@ public class AddEventFragment extends BaseFragment {
} else if (url.contains("/org/")) {
url = url.substring(url.lastIndexOf("/") + 1);
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.getBody(((MainActivity) getActivity()).getSessionIDHeader(), url).enqueue(new Callback<Body>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getBody(Utils.getSessionIDHeader(), url).enqueue(new Callback<Body>() {
@Override
public void onResponse(Call<Body> call, Response<Body> response) {
if (response.isSuccessful()) {
......
......@@ -146,8 +146,8 @@ public class BodyFragment extends BackHandledFragment {
}
private void updateBody() {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.getBody(((MainActivity) getActivity()).getSessionIDHeader(), min_body.getBodyID()).enqueue(new Callback<Body>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getBody(Utils.getSessionIDHeader(), min_body.getBodyID()).enqueue(new Callback<Body>() {
@Override
public void onResponse(Call<Body> call, Response<Body> response) {
if (response.isSuccessful()) {
......@@ -223,8 +223,8 @@ public class BodyFragment extends BackHandledFragment {
followButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.updateBodyFollowing(((MainActivity) getActivity()).getSessionIDHeader(), body.getBodyID(), body.getBodyUserFollows() ? 0 : 1).enqueue(new Callback<Void>() {
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()) {
......
......@@ -29,6 +29,7 @@ import java.util.List;
import java.util.TimeZone;
import app.insti.Constants;
import app.insti.Utils;
import app.insti.interfaces.ItemClickListener;
import app.insti.R;
import app.insti.activity.MainActivity;
......@@ -118,8 +119,8 @@ public class CalendarFragment extends BaseFragment {
final String oneMonthBack = isoFormatter.format(oneMonthBackDate).toString();
final String oneMonthOn = isoFormatter.format(oneMonthOnDate).toString();
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.getEventsBetweenDates(((MainActivity) getActivity()).getSessionIDHeader(), oneMonthBack, oneMonthOn).enqueue(new Callback<NewsFeedResponse>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getEventsBetweenDates(Utils.getSessionIDHeader(), oneMonthBack, oneMonthOn).enqueue(new Callback<NewsFeedResponse>() {
@Override
public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
if (response.isSuccessful()) {
......
......@@ -275,8 +275,8 @@ public class EventFragment extends BackHandledFragment {
@Override
public void onClick(View view) {
final int endStatus = event.getEventUserUes() == status ? 0 : status;
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.updateUserEventStatus(((MainActivity) getActivity()).getSessionIDHeader(), event.getEventID(), endStatus).enqueue(new Callback<Void>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.updateUserEventStatus(Utils.getSessionIDHeader(), event.getEventID(), endStatus).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
......
......@@ -18,6 +18,7 @@ import java.util.ArrayList;
import java.util.List;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.adapter.BodyAdapter;
import app.insti.adapter.FeedAdapter;
......@@ -77,7 +78,7 @@ public class ExploreFragment extends Fragment {
super.onStart();
// Initialize
sessionId = ((MainActivity) getActivity()).getSessionIDHeader();
sessionId = Utils.getSessionIDHeader();
initRecyclerViews();
Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
......@@ -85,7 +86,7 @@ public class ExploreFragment extends Fragment {
// Get all bodies
if (allBodies.size() == 0) {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getAllBodies(sessionId).enqueue(new EmptyCallback<List<Body>>() {
@Override
public void onResponse(Call<List<Body>> call, Response<List<Body>> response) {
......@@ -136,7 +137,7 @@ public class ExploreFragment extends Fragment {
currentQuery = query;
// Make request
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.search(sessionId, query).enqueue(new EmptyCallback<ExploreResponse>() {
@Override
public void onResponse(Call<ExploreResponse> call, Response<ExploreResponse> response) {
......
......@@ -100,8 +100,8 @@ public class FeedFragment extends BaseFragment {
private void updateFeed() {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.getNewsFeed(((MainActivity)getActivity()).getSessionIDHeader()).enqueue(new Callback<NewsFeedResponse>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getNewsFeed(Utils.getSessionIDHeader()).enqueue(new Callback<NewsFeedResponse>() {
@Override
public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
if (response.isSuccessful()) {
......
......@@ -87,6 +87,7 @@ import java.util.regex.Pattern;
import app.insti.Constants;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.api.RetrofitInterface;
import app.insti.api.model.Venue;
......@@ -226,7 +227,7 @@ public class MapFragment extends Fragment implements TextWatcher,
}
private void getAPILocations() {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getAllVenues().enqueue(new Callback<List<Venue>>() {
@Override
public void onResponse(Call<List<Venue>> call, Response<List<Venue>> response) {
......
......@@ -23,6 +23,7 @@ import java.util.Locale;
import app.insti.ActivityBuffer;
import app.insti.Constants;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.adapter.MessMenuAdapter;
import app.insti.api.RetrofitInterface;
......@@ -107,7 +108,7 @@ public class MessMenuFragment extends BaseFragment {
}
private void updateMessMenu(final String hostel) {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getInstituteMessMenu("sessionid=" + getArguments().getString(Constants.SESSION_ID)).enqueue(new Callback<List<HostelMessMenu>>() {
@Override
public void onResponse(Call<List<HostelMessMenu>> call, Response<List<HostelMessMenu>> response) {
......
......@@ -59,8 +59,8 @@ public class NotificationsFragment extends BaseFragment {
Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("Notifications");
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
retrofitInterface.getNotifications(((MainActivity) getActivity()).getSessionIDHeader()).enqueue(new Callback<List<Notification>>() {
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getNotifications(Utils.getSessionIDHeader()).enqueue(new Callback<List<Notification>>() {
@Override
public void onResponse(Call<List<Notification>> call, Response<List<Notification>> response) {
if (response.isSuccessful()) {
......@@ -89,8 +89,8 @@ public class NotificationsFragment extends BaseFragment {
Notification notification = notifications.get(position);
/* Mark notification read */
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
String sessId = ((MainActivity) getActivity()).getSessionIDHeader();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
String sessId = Utils.getSessionIDHeader();
retrofitInterface.markNotificationRead(sessId, notification.getNotificationId().toString()).enqueue(new EmptyCallback<Void>());
/* Open event */
......
......@@ -20,6 +20,7 @@ import java.util.Objects;
import app.insti.ActivityBuffer;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.api.RetrofitInterface;
import app.insti.interfaces.Browsable;
......@@ -55,8 +56,8 @@ public abstract class RecyclerViewFragment<T extends Browsable, S extends Recycl
final String requestSearchQuery = searchQuery;
// Make the request
String sessionIDHeader = ((MainActivity) getActivity()).getSessionIDHeader();
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
String sessionIDHeader = Utils.getSessionIDHeader();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
Call<List<T>> call = getCall(retrofitInterface, sessionIDHeader, 0);
call.enqueue(new Callback<List<T>>() {
@Override
......@@ -129,8 +130,8 @@ public abstract class RecyclerViewFragment<T extends Browsable, S extends Recycl
LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
if (((layoutManager.getChildCount() + layoutManager.findFirstVisibleItemPosition()) > (layoutManager.getItemCount() - 5)) && (!loading) && (!allLoaded)) {
loading = true;
String sessionIDHeader = ((MainActivity) getActivity()).getSessionIDHeader();
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
String sessionIDHeader = Utils.getSessionIDHeader();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
Call<List<T>> call = getCall(retrofitInterface, sessionIDHeader, getPostCount());
call.enqueue(new Callback<List<T>>() {
@Override
......
......@@ -56,7 +56,7 @@ public class SettingsFragment extends Fragment {
populateViews();
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getUser("sessionid=" + getArguments().getString(Constants.SESSION_ID), userID).enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
......@@ -132,7 +132,7 @@ public class SettingsFragment extends Fragment {
logoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.logout("sessionid=" + getArguments().getString(Constants.SESSION_ID)).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
......
......@@ -108,7 +108,7 @@ public class UserFragment extends BackHandledFragment {
Bundle bundle = getArguments();
String userID = bundle.getString(Constants.USER_ID);
RetrofitInterface retrofitInterface = ((MainActivity) getActivity()).getRetrofitInterface();
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getUser("sessionid=" + getArguments().getString(Constants.SESSION_ID), userID).enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
......
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