Android Open Source - droidcon Comments Activity






From Project

Back to project page droidcon.

License

The source code is released under:

Apache License

If you think the Android project droidcon listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.timehop.droidcon2014retrofitsample;
//from www . j  a v a 2  s . c  om
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.timehop.droidcon2014retrofitsample.data.reddit.RedditService;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditComment;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditLink;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditListing;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObject;
import com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditResponse;
import com.timehop.droidcon2014retrofitsample.ui.CommentsAdapter;

import java.util.List;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class CommentsActivity extends Activity {
  public static final String EXTRA_LINK_ID = "link_id";
  public static final String EXTRA_SUBREDDIT = "subreddit";
  private ListView mListView;
  private CommentsAdapter mAdapter;
  private ViewGroup mContentView;
  private ProgressDialog mProgressDialog;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_comments);
    String subreddit = getIntent().getStringExtra(EXTRA_SUBREDDIT);
    String linkId = getIntent().getStringExtra(EXTRA_LINK_ID);

    mListView = (ListView) findViewById(android.R.id.list);
    mAdapter = new CommentsAdapter(this);
    mListView.setAdapter(mAdapter);
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setCancelable(false);
    mProgressDialog.setMessage("Loading comments...");
    mProgressDialog.show();
    RedditService.Implementation.get().getComments(subreddit, linkId,
        new Callback<List<RedditResponse<RedditListing>>>() {
          @Override
          public void success(List<RedditResponse<RedditListing>> listings, Response response) {
            if (isDestroyed()) return;
            mProgressDialog.dismiss();
            findViewById(R.id.divider).setVisibility(View.VISIBLE);
            onListingsReceived(listings);
          }

          @Override
          public void failure(RetrofitError error) {
            if (isDestroyed()) return;
            mProgressDialog.dismiss();
            new AlertDialog.Builder(CommentsActivity.this)
                .setMessage("Loading failed :(")
                .setCancelable(false)
                .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    finish();
                  }
                })
                .show();
          }
        });
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mProgressDialog.dismiss();
  }

  private void onListingsReceived(List<RedditResponse<RedditListing>> listings) {
    RedditObject linkObject = listings.get(0).getData().getChildren().get(0);
    RedditLink link = (RedditLink) linkObject;
    TextView scoreTextView = (TextView) findViewById(R.id.score_textview);
    TextView titleTextView = (TextView) findViewById(R.id.title_textview);
    TextView authorTextView = (TextView) findViewById(R.id.author_textview);

    scoreTextView.setText(String.valueOf(link.getScore()));
    titleTextView.setText(String.valueOf(link.getTitle()));
    authorTextView.setText(String.valueOf(link.getAuthor()));

    addCommentsToAdapter(listings.get(1).getData().getChildren());
  }

  private void addCommentsToAdapter(List<RedditObject> children) {
    for (RedditObject child : children) {
      if (child instanceof RedditComment) {
        RedditComment comment = (RedditComment) child;
        addCommentToAdapter(comment);
      }
    }
  }

  private void addCommentToAdapter(RedditComment comment) {
    mAdapter.add(comment);
    if (comment.getReplies() != null) {
      RedditListing repliesListing = (RedditListing) comment.getReplies();
      for (RedditObject redditObject : repliesListing.getChildren()) {
        // replies can be of type 'more' but we're not going to worry about that here
        if (redditObject instanceof RedditComment) {
          RedditComment childComment = (RedditComment) redditObject;
          // increment the depth of the child so it can be indented
          childComment.setDepth(comment.getDepth() + 1);
          addCommentToAdapter(childComment);
        }
      }
    }
  }
}




Java Source Code List

com.timehop.droidcon2014retrofitsample.CommentsActivity.java
com.timehop.droidcon2014retrofitsample.MainActivity.java
com.timehop.droidcon2014retrofitsample.SubredditActivity.java
com.timehop.droidcon2014retrofitsample.VenueSearchActivity.java
com.timehop.droidcon2014retrofitsample.data.SynchronousExecutor.java
com.timehop.droidcon2014retrofitsample.data.foursquare.FoursquareService.java
com.timehop.droidcon2014retrofitsample.data.foursquare.FoursquareTests.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareCredentials.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareErrorHandler.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareException.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareRequestInterceptor.java
com.timehop.droidcon2014retrofitsample.data.foursquare.api.FoursquareResponse.java
com.timehop.droidcon2014retrofitsample.data.foursquare.legacy.VenueSearchTask.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Category.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Contact.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Icon.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Location.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Menu.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Meta.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Price.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.ResponseWrapper.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Stats.java
com.timehop.droidcon2014retrofitsample.data.foursquare.model.Venue.java
com.timehop.droidcon2014retrofitsample.data.reddit.DateTimeDeserializer.java
com.timehop.droidcon2014retrofitsample.data.reddit.MockRedditService.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditObjectDeserializer.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditService.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditTests.java
com.timehop.droidcon2014retrofitsample.data.reddit.RedditType.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.MockModelHelper.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditAccount.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditComment.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditLink.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditListing.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditMore.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObjectWrapper.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditObject.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditResponse.java
com.timehop.droidcon2014retrofitsample.data.reddit.model.RedditSubmission.java
com.timehop.droidcon2014retrofitsample.ui.CommentsAdapter.java
com.timehop.droidcon2014retrofitsample.ui.LinkAdapter.java
com.timehop.droidcon2014retrofitsample.ui.VenueAdapter.java