Search in sources :

Example 6 with RetrofitInterface

use of app.insti.api.RetrofitInterface in project IITB-App by wncc.

the class ComplaintsHomeFragment method onCreateView.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_complaints_home, container, false);
    RecyclerView recyclerViewHome = view.findViewById(R.id.recyclerViewHome);
    homeListAdapter = new ComplaintsAdapter(getActivity(), uID, uProfileUrl);
    swipeContainer = view.findViewById(R.id.swipeContainer);
    error_message_home = view.findViewById(R.id.error_message_home);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    recyclerViewHome.setLayoutManager(llm);
    recyclerViewHome.setHasFixedSize(true);
    recyclerViewHome.setAdapter(homeListAdapter);
    recyclerViewHome.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (networkBusy || currentIndex == -1)
                return;
            if (!recyclerView.canScrollVertically(1)) {
                networkBusy = true;
                swipeContainer.setRefreshing(true);
                RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
                retrofitInterface.getAllComplaints(Utils.getSessionIDHeader(), currentIndex, 5).enqueue(new Callback<List<Venter.Complaint>>() {

                    @Override
                    public void onResponse(Call<List<Venter.Complaint>> call, Response<List<Venter.Complaint>> response) {
                        if (response.isSuccessful()) {
                            if (response.body() != null && !response.body().isEmpty()) {
                                complaints.addAll(response.body());
                                initialiseRecyclerView(complaints);
                                currentIndex += 5;
                            } else {
                                currentIndex = -1;
                            }
                        }
                        networkBusy = false;
                        swipeContainer.setRefreshing(false);
                    }

                    @Override
                    public void onFailure(Call<List<Venter.Complaint>> call, Throwable t) {
                        networkBusy = false;
                        swipeContainer.setRefreshing(false);
                    }
                });
            }
        }
    });
    swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            callServerToGetNearbyComplaints();
        }
    });
    swipeContainer.setColorSchemeResources(R.color.colorPrimary);
    if (!isCalled) {
        swipeContainer.post(new Runnable() {

            @Override
            public void run() {
                swipeContainer.setRefreshing(true);
                callServerToGetNearbyComplaints();
            }
        });
        isCalled = true;
    }
    return view;
}
Also used : Call(retrofit2.Call) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) SwipeRefreshLayout(androidx.swiperefreshlayout.widget.SwipeRefreshLayout) ComplaintsAdapter(app.insti.adapter.ComplaintsAdapter) Response(retrofit2.Response) Callback(retrofit2.Callback) RecyclerView(androidx.recyclerview.widget.RecyclerView) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 7 with RetrofitInterface

use of app.insti.api.RetrofitInterface in project IITB-App by wncc.

the class ComplaintsHomeFragment method callServerToGetNearbyComplaints.

private void callServerToGetNearbyComplaints() {
    try {
        RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
        retrofitInterface.getAllComplaints(Utils.getSessionIDHeader(), 0, 5).enqueue(new Callback<List<Venter.Complaint>>() {

            @Override
            public void onResponse(@NonNull Call<List<Venter.Complaint>> call, @NonNull Response<List<Venter.Complaint>> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null && !(response.body().isEmpty())) {
                        complaints = response.body();
                        currentIndex = complaints.size();
                        initialiseRecyclerView(complaints);
                    } else {
                        error_message_home.setVisibility(View.VISIBLE);
                        error_message_home.setText(getString(R.string.no_complaints));
                    }
                }
                swipeContainer.setRefreshing(false);
            }

            @Override
            public void onFailure(@NonNull Call<List<Venter.Complaint>> call, @NonNull Throwable t) {
                swipeContainer.setRefreshing(false);
                error_message_home.setVisibility(View.VISIBLE);
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        swipeContainer.setRefreshing(false);
    }
}
Also used : Venter(app.insti.api.model.Venter) List(java.util.List) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 8 with RetrofitInterface

use of app.insti.api.RetrofitInterface in project IITB-App by wncc.

the class BodyFragment method updateBody.

private void updateBody() {
    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()) {
                Body bodyResponse = response.body();
                Utils.bodyCache.updateCache(response.body());
                if (!bodyDisplayed) {
                    body = bodyResponse;
                    displayBody();
                }
                bodySwipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFailure(Call<Body> call, Throwable t) {
            bodySwipeRefreshLayout.setRefreshing(false);
        }
    });
}
Also used : Body(app.insti.api.model.Body) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 9 with RetrofitInterface

use of app.insti.api.RetrofitInterface in project IITB-App by wncc.

the class CalendarFragment method updateEvents.

private void updateEvents(CalendarDay calendarDay, final boolean setToday) {
    // Do not make duplicate calls
    if (haveMonths.contains(calendarDay)) {
        if (!setToday) {
            return;
        } else {
            setupCalendar(true);
        }
    }
    haveMonths.add(calendarDay);
    // Parsers
    String ISO_FORMAT = "yyyy-MM-dd HH:mm:ss";
    final TimeZone utc = TimeZone.getTimeZone("UTC");
    final SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
    isoFormatter.setTimeZone(utc);
    // Get the start date
    final Date startDate;
    try {
        startDate = toDate(calendarDay);
    } catch (ParseException ignored) {
        return;
    }
    // Get start and end times
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal.add(Calendar.MONTH, -1);
    final Date oneMonthBackDate = cal.getTime();
    cal.add(Calendar.MONTH, 2);
    final Date oneMonthOnDate = cal.getTime();
    final String oneMonthBack = isoFormatter.format(oneMonthBackDate).toString();
    final String oneMonthOn = isoFormatter.format(oneMonthOnDate).toString();
    // Make the API call
    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()) {
                if (getActivity() == null || getView() == null)
                    return;
                // Concatenate the response
                NewsFeedResponse newsFeedResponse = response.body();
                List<Event> eventList = newsFeedResponse.getEvents();
                if (eventList == null)
                    return;
                // Concatenate
                for (Event event : eventList) {
                    if (!events.contains(event))
                        events.add(event);
                }
                setupCalendar(setToday);
            }
        }

        @Override
        public void onFailure(Call<NewsFeedResponse> call, Throwable t) {
            // Network Error
            Toast.makeText(getActivity(), "Failed to fetch events!", Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : Calendar(java.util.Calendar) Date(java.util.Date) TimeZone(java.util.TimeZone) NewsFeedResponse(app.insti.api.response.NewsFeedResponse) Event(app.insti.api.model.Event) List(java.util.List) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) RetrofitInterface(app.insti.api.RetrofitInterface)

Example 10 with RetrofitInterface

use of app.insti.api.RetrofitInterface in project IITB-App by wncc.

the class MapFragment method getAPILocations.

private void getAPILocations() {
    RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
    retrofitInterface.getAllVenues().enqueue(new Callback<List<Venue>>() {

        @Override
        public void onResponse(Call<List<Venue>> call, Response<List<Venue>> response) {
            if (response.isSuccessful()) {
                if (getActivity() == null || getView() == null || getContext() == null)
                    return;
                // Show the map and data
                venues = response.body();
                setupWithData(venues);
            }
        }

        @Override
        public void onFailure(Call<List<Venue>> call, Throwable t) {
        // Do nothing
        }
    });
}
Also used : ColorStateList(android.content.res.ColorStateList) ArrayList(java.util.ArrayList) List(java.util.List) RetrofitInterface(app.insti.api.RetrofitInterface)

Aggregations

RetrofitInterface (app.insti.api.RetrofitInterface)35 List (java.util.List)12 Venter (app.insti.api.model.Venter)6 Call (retrofit2.Call)6 Response (retrofit2.Response)6 Event (app.insti.api.model.Event)5 ArrayList (java.util.ArrayList)5 Callback (retrofit2.Callback)5 User (app.insti.api.model.User)4 DialogInterface (android.content.DialogInterface)3 TextView (android.widget.TextView)3 RecyclerView (androidx.recyclerview.widget.RecyclerView)3 Intent (android.content.Intent)2 Bundle (android.os.Bundle)2 View (android.view.View)2 AlertDialog (androidx.appcompat.app.AlertDialog)2 Toolbar (androidx.appcompat.widget.Toolbar)2 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)2 SessionManager (app.insti.SessionManager)2 UpdatableList (app.insti.UpdatableList)2