public void doAfterPostSuccess(String s, int articleId) {
   if (s.equals("IOException")) {
     Toast.makeText(
             getActivity().getApplicationContext(),
             "Server not available, please contact application owner",
             Toast.LENGTH_SHORT)
         .show();
     mAdapter.notifyDataSetChanged();
     swipeLayout.setRefreshing(false);
     return;
   }
   addNewArticleToUser(s, articleId);
   done++;
   Log.d("SVF", "Done " + done + " out of " + topics.length());
   if (done == topics.length()) {
     swipeLayout.setRefreshing(false);
     ValuesAndUtil.getInstance().saveUserData(user, getActivity().getApplicationContext());
     try {
       JSONArray sortedTopics =
           ValuesAndUtil.getInstance().sortTopicsArray(user.getJSONArray("topics"));
       user.put("topics", sortedTopics);
       ValuesAndUtil.getInstance().saveUserData(user, getActivity().getApplicationContext());
       mAdapter = new StoriesViewAdapter(sortedTopics);
       if (numUpdated == 0) {
         Toast.makeText(getActivity(), "No updates found for any stories", Toast.LENGTH_SHORT)
             .show();
       }
     } catch (JSONException e) {
       e.printStackTrace();
     }
     mRecyclerView.setAdapter(mAdapter);
     Log.d("SVF", "Finished updating all articles");
   }
 }
 public void addNewArticleToUser(String data, int articleId) {
   Log.d("SVF", "Modifying topic " + articleId);
   try {
     JSONObject article = new JSONObject(data);
     boolean found = article.getBoolean("found");
     if (found) {
       // update topic with new article; don't do anything if not found
       if (!ValuesAndUtil.getInstance().articleAlreadyExists(article, topics, articleId)) {
         article.remove("found");
         article.put("thumbsUp", false);
         article.put("thumbsDown", false);
         JSONObject topic = topics.getJSONObject(articleId);
         JSONArray timeline = topic.getJSONArray("timeline");
         timeline = ValuesAndUtil.getInstance().addToExistingJSON(timeline, 0, article);
         topic.put("timeline", timeline);
         topic.put("lastTimeStamp", article.getLong("date"));
         topic.put("lastSignature", article.getString("signature"));
         topic.put("lastUpdated", System.currentTimeMillis());
         Log.d("SVF", topic.toString(2));
         topics.put(articleId, topic);
         user.put("topics", topics);
         ValuesAndUtil.getInstance().saveUserData(user, getActivity().getApplicationContext());
         mAdapter.notifyItemChanged(articleId);
         numUpdated++;
       }
     }
   } catch (JSONException e) {
     e.printStackTrace();
   }
 }
  @Override
  public View onCreateView(
      LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_stories_view, container, false);
    getActivity().setTitle(getResources().getString(R.string.title_activity_news_home_screen));

    swipeLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_layout);
    swipeLayout.setOnRefreshListener(this);

    TextView noTopicsText = (TextView) rootView.findViewById(R.id.no_topics_textview);
    user = ValuesAndUtil.getInstance().loadUserData(getActivity().getApplicationContext());

    boolean hasTopics = false;
    try {
      hasTopics = user.has("topics") && user.getJSONArray("topics").length() > 0;
    } catch (JSONException e) {
      e.printStackTrace();
    }
    Log.d("SVF", "Has topics = " + hasTopics);
    if (!hasTopics) {
      noTopicsText.setText(getString(R.string.no_topics_available));
      swipeLayout.setVisibility(View.INVISIBLE);
    } else {
      Log.d("SVF", "Topics were not null");
      swipeLayout.setVisibility(View.VISIBLE);
      try {
        topics = user.getJSONArray("topics");
        setUpList(rootView, topics);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }

    return rootView;
  }
 @Override
 public void onStart() {
   super.onStart();
   Log.d("SVF", "Called onStart in StoriesViewFragment");
   user = ValuesAndUtil.getInstance().loadUserData(getActivity().getApplicationContext());
   topics = user.optJSONArray("topics");
 }
  public void onResume() {
    super.onResume();
    Log.d("SVF", "Called onResume in StoriesViewFragment");
    user = ValuesAndUtil.getInstance().loadUserData(getActivity().getApplicationContext());

    try {
      if (user.has("topics") && user.getJSONArray("topics").length() > 0) {
        topics = user.getJSONArray("topics");
        mRecyclerView.invalidate();
        JSONArray sortedTopics =
            ValuesAndUtil.getInstance().sortTopicsArray(user.getJSONArray("topics"));
        user.put("topics", sortedTopics);
        ValuesAndUtil.getInstance().saveUserData(user, getActivity().getApplicationContext());
        mAdapter = new StoriesViewAdapter(sortedTopics);
        mRecyclerView.setAdapter(mAdapter);
      }
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }
 @Override
 public void onPause() {
   super.onPause();
   Log.d("SVF", "Called onPause in StoriesViewFragment");
   ValuesAndUtil.getInstance().saveUserData(user, getActivity().getApplicationContext());
 }
 @Override
 protected String doInBackground(String... strings) {
   id = strings[1];
   return ValuesAndUtil.getInstance()
       .doPostRequest(ValuesAndUtil.SERVER_NEW_ARTICLES_URL, strings[0]);
 }