Search in sources :

Example 1 with FeedAdapter

use of in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter in project IITB-App by wncc.

the class FeedFragment method updateFeed.

private void updateFeed() {
    RetrofitInterface retrofitInterface = ServiceGenerator.createService(RetrofitInterface.class);
    retrofitInterface.getNewsFeed("sessionid=" + getArguments().getString(SESSION_ID)).enqueue(new Callback<NewsFeedResponse>() {

        @Override
        public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
            if (response.isSuccessful()) {
                NewsFeedResponse newsFeedResponse = response.body();
                final List<Event> events = newsFeedResponse.getEvents();
                FeedAdapter feedAdapter = new FeedAdapter(events, new ItemClickListener() {

                    @Override
                    public void onItemClick(View v, int position) {
                        String eventJson = new Gson().toJson(events.get(position));
                        Bundle bundle = new Bundle();
                        bundle.putString(Constants.EVENT_JSON, eventJson);
                        EventFragment eventFragment = new EventFragment();
                        eventFragment.setArguments(bundle);
                        FragmentManager manager = getActivity().getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        transaction.replace(R.id.framelayout_for_fragment, eventFragment, eventFragment.getTag());
                        transaction.commit();
                    }
                });
                feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
                feedRecyclerView.setAdapter(feedAdapter);
                feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                appDatabase.dbDao().deleteEvents();
                appDatabase.dbDao().insertEvents(events);
                // Server Error
                feedSwipeRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onFailure(Call<NewsFeedResponse> call, Throwable t) {
            // Network Error
            feedSwipeRefreshLayout.setRefreshing(false);
        }
    });
}
Also used : ItemClickListener(in.ac.iitb.gymkhana.iitbapp.ItemClickListener) Bundle(android.os.Bundle) Gson(com.google.gson.Gson) FeedAdapter(in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction) NewsFeedResponse(in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse) ArrayList(java.util.ArrayList) List(java.util.List) RecyclerView(android.support.v7.widget.RecyclerView) RetrofitInterface(in.ac.iitb.gymkhana.iitbapp.api.RetrofitInterface)

Example 2 with FeedAdapter

use of in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter in project IITB-App by wncc.

the class FeedFragment method onStart.

@Override
public void onStart() {
    super.onStart();
    appDatabase = AppDatabase.getAppDatabase(getContext());
    final List<Event> events = appDatabase.dbDao().getAllEvents();
    FeedAdapter feedAdapter = new FeedAdapter(events, new ItemClickListener() {

        @Override
        public void onItemClick(View v, int position) {
            String eventJson = new Gson().toJson(events.get(position));
            Bundle bundle = new Bundle();
            bundle.putString(Constants.EVENT_JSON, eventJson);
            EventFragment eventFragment = new EventFragment();
            eventFragment.setArguments(bundle);
            FragmentManager manager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.framelayout_for_fragment, eventFragment, eventFragment.getTag());
            transaction.commit();
        }
    });
    feedRecyclerView = (RecyclerView) getActivity().findViewById(R.id.feed_recycler_view);
    feedRecyclerView.setAdapter(feedAdapter);
    feedRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    // }
    updateFeed();
    feedSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.feed_swipe_refresh_layout);
    feedSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

        @Override
        public void onRefresh() {
            updateFeed();
        }
    });
}
Also used : ItemClickListener(in.ac.iitb.gymkhana.iitbapp.ItemClickListener) Bundle(android.os.Bundle) Gson(com.google.gson.Gson) FeedAdapter(in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) SwipeRefreshLayout(android.support.v4.widget.SwipeRefreshLayout) FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction) Event(in.ac.iitb.gymkhana.iitbapp.data.Event)

Aggregations

Bundle (android.os.Bundle)2 FragmentManager (android.support.v4.app.FragmentManager)2 FragmentTransaction (android.support.v4.app.FragmentTransaction)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 RecyclerView (android.support.v7.widget.RecyclerView)2 View (android.view.View)2 Gson (com.google.gson.Gson)2 ItemClickListener (in.ac.iitb.gymkhana.iitbapp.ItemClickListener)2 FeedAdapter (in.ac.iitb.gymkhana.iitbapp.adapter.FeedAdapter)2 SwipeRefreshLayout (android.support.v4.widget.SwipeRefreshLayout)1 RetrofitInterface (in.ac.iitb.gymkhana.iitbapp.api.RetrofitInterface)1 NewsFeedResponse (in.ac.iitb.gymkhana.iitbapp.api.model.NewsFeedResponse)1 Event (in.ac.iitb.gymkhana.iitbapp.data.Event)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1