Exemplo n.º 1
0
  public StoriesResponse getStarredStories(String pageNumber) {
    final APIClient client = new APIClient(context);
    final ValueMultimap values = new ValueMultimap();
    if (!TextUtils.isEmpty(pageNumber)) {
      values.put(APIConstants.PARAMETER_PAGE_NUMBER, "" + pageNumber);
    }
    final APIResponse response = client.get(APIConstants.URL_STARRED_STORIES, values);

    StoriesResponse storiesResponse = gson.fromJson(response.responseString, StoriesResponse.class);
    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {
      if (TextUtils.equals(pageNumber, "1")) {
        contentResolver.delete(FeedProvider.STARRED_STORIES_URI, null, null);
      }
      for (Story story : storiesResponse.stories) {
        contentResolver.insert(FeedProvider.STARRED_STORIES_URI, story.getValues());
        insertComments(story);
      }
      for (UserProfile user : storiesResponse.users) {
        contentResolver.insert(FeedProvider.USERS_URI, user.getValues());
      }
      return storiesResponse;
    } else {
      return null;
    }
  }
Exemplo n.º 2
0
  public SocialFeedResponse getSharedStoriesForFeeds(String[] feedIds, String pageNumber) {
    final APIClient client = new APIClient(context);
    final ValueMultimap values = new ValueMultimap();
    for (String feedId : feedIds) {
      values.put(APIConstants.PARAMETER_FEEDS, feedId);
    }
    if (!TextUtils.isEmpty(pageNumber)) {
      values.put(APIConstants.PARAMETER_PAGE_NUMBER, "" + pageNumber);
    }

    final APIResponse response = client.get(APIConstants.URL_SHARED_RIVER_STORIES, values);

    SocialFeedResponse storiesResponse =
        gson.fromJson(response.responseString, SocialFeedResponse.class);
    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {

      // If we've successfully retrieved the latest stories for all shared feeds (the first page),
      // delete all previous shared feeds
      if (TextUtils.equals(pageNumber, "1")) {
        Uri storyUri = FeedProvider.ALL_STORIES_URI;
        contentResolver.delete(storyUri, null, null);
      }

      for (Story story : storiesResponse.stories) {
        for (String userId : story.sharedUserIds) {
          Uri storySocialUri =
              FeedProvider.SOCIALFEED_STORIES_URI.buildUpon().appendPath(userId).build();
          contentResolver.insert(storySocialUri, story.getValues());
        }

        Uri storyUri = FeedProvider.FEED_STORIES_URI.buildUpon().appendPath(story.feedId).build();
        contentResolver.insert(storyUri, story.getValues());

        insertComments(story);
      }

      for (UserProfile user : storiesResponse.userProfiles) {
        contentResolver.insert(FeedProvider.USERS_URI, user.getValues());
      }

      if (storiesResponse != null && storiesResponse.feeds != null) {
        for (Feed feed : storiesResponse.feeds) {
          contentResolver.insert(FeedProvider.FEEDS_URI, feed.getValues());
        }
      }

      return storiesResponse;
    } else {
      return null;
    }
  }
Exemplo n.º 3
0
  public SocialFeedResponse getStoriesForSocialFeed(
      String userId, String username, String pageNumber) {
    final APIClient client = new APIClient(context);
    final ContentValues values = new ContentValues();
    values.put(APIConstants.PARAMETER_USER_ID, userId);
    values.put(APIConstants.PARAMETER_USERNAME, username);
    if (!TextUtils.isEmpty(pageNumber)) {
      values.put(APIConstants.PARAMETER_PAGE_NUMBER, "" + pageNumber);
    }
    Uri feedUri =
        Uri.parse(APIConstants.URL_SOCIALFEED_STORIES)
            .buildUpon()
            .appendPath(userId)
            .appendPath(username)
            .build();
    final APIResponse response = client.get(feedUri.toString(), values);
    SocialFeedResponse socialFeedResponse =
        gson.fromJson(response.responseString, SocialFeedResponse.class);
    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {

      Uri storySocialUri =
          FeedProvider.SOCIALFEED_STORIES_URI.buildUpon().appendPath(userId).build();
      if (TextUtils.equals(pageNumber, "1")) {
        contentResolver.delete(storySocialUri, null, null);
      }

      for (Story story : socialFeedResponse.stories) {
        insertComments(story);

        Uri storyUri = FeedProvider.FEED_STORIES_URI.buildUpon().appendPath(story.feedId).build();
        contentResolver.insert(storyUri, story.getValues());
        contentResolver.insert(storySocialUri, story.getValues());
      }

      if (socialFeedResponse.userProfiles != null) {
        for (UserProfile user : socialFeedResponse.userProfiles) {
          contentResolver.insert(FeedProvider.USERS_URI, user.getValues());
        }
      }

      for (Feed feed : socialFeedResponse.feeds) {
        contentResolver.insert(FeedProvider.FEEDS_URI, feed.getValues());
      }
      return socialFeedResponse;
    } else {
      return null;
    }
  }
Exemplo n.º 4
0
  public StoriesResponse getStoriesForFeed(
      String feedId, String pageNumber, StoryOrder order, ReadFilter filter) {
    final APIClient client = new APIClient(context);
    final ContentValues values = new ContentValues();
    Uri feedUri = Uri.parse(APIConstants.URL_FEED_STORIES).buildUpon().appendPath(feedId).build();
    values.put(APIConstants.PARAMETER_FEEDS, feedId);
    values.put(APIConstants.PARAMETER_PAGE_NUMBER, pageNumber);
    values.put(APIConstants.PARAMETER_ORDER, order.getParameterValue());
    values.put(APIConstants.PARAMETER_READ_FILTER, filter.getParameterValue());

    final APIResponse response = client.get(feedUri.toString(), values);
    Uri storyUri = FeedProvider.FEED_STORIES_URI.buildUpon().appendPath(feedId).build();

    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {
      if (TextUtils.equals(pageNumber, "1")) {
        contentResolver.delete(storyUri, null, null);
      }
      StoriesResponse storiesResponse =
          gson.fromJson(response.responseString, StoriesResponse.class);

      Uri classifierUri = FeedProvider.CLASSIFIER_URI.buildUpon().appendPath(feedId).build();

      contentResolver.delete(classifierUri, null, null);

      for (ContentValues classifierValues : storiesResponse.classifiers.getContentValues()) {
        contentResolver.insert(classifierUri, classifierValues);
      }

      for (Story story : storiesResponse.stories) {
        contentResolver.insert(storyUri, story.getValues());
        insertComments(story);
      }

      for (UserProfile user : storiesResponse.users) {
        contentResolver.insert(FeedProvider.USERS_URI, user.getValues());
      }

      return storiesResponse;
    } else {
      return null;
    }
  }
Exemplo n.º 5
0
  public StoriesResponse getStoriesForFeeds(
      String[] feedIds, String pageNumber, StoryOrder order, ReadFilter filter) {
    final APIClient client = new APIClient(context);
    final ValueMultimap values = new ValueMultimap();
    for (String feedId : feedIds) {
      values.put(APIConstants.PARAMETER_FEEDS, feedId);
    }
    if (!TextUtils.isEmpty(pageNumber)) {
      values.put(APIConstants.PARAMETER_PAGE_NUMBER, "" + pageNumber);
    }
    values.put(APIConstants.PARAMETER_ORDER, order.getParameterValue());
    values.put(APIConstants.PARAMETER_READ_FILTER, filter.getParameterValue());
    final APIResponse response = client.get(APIConstants.URL_RIVER_STORIES, values);

    StoriesResponse storiesResponse = gson.fromJson(response.responseString, StoriesResponse.class);
    if (response.responseCode == HttpStatus.SC_OK && !response.hasRedirected) {
      if (TextUtils.equals(pageNumber, "1")) {
        Uri storyUri = FeedProvider.ALL_STORIES_URI;
        contentResolver.delete(storyUri, null, null);
      }

      for (Story story : storiesResponse.stories) {
        Uri storyUri = FeedProvider.FEED_STORIES_URI.buildUpon().appendPath(story.feedId).build();
        contentResolver.insert(storyUri, story.getValues());
        insertComments(story);
      }

      for (UserProfile user : storiesResponse.users) {
        contentResolver.insert(FeedProvider.USERS_URI, user.getValues());
      }

      return storiesResponse;
    } else {
      return null;
    }
  }
  @Override
  protected Void doInBackground(Void... arg0) {

    commentCursor =
        resolver.query(FeedProvider.COMMENTS_URI, null, null, new String[] {story.id}, null);

    publicCommentViews = new ArrayList<View>();
    friendCommentViews = new ArrayList<View>();

    while (commentCursor.moveToNext()) {
      final Comment comment = Comment.fromCursor(commentCursor);

      // skip public comments if they are disabled
      if (!comment.byFriend && !PrefsUtils.showPublicComments(context)) {
        continue;
      }

      Cursor userCursor =
          resolver.query(
              FeedProvider.USERS_URI,
              null,
              DatabaseConstants.USER_USERID + " IN (?)",
              new String[] {comment.userId},
              null);
      UserProfile commentUser = UserProfile.fromCursor(userCursor);
      userCursor.close();
      // rarely, we get a comment but never got the user's profile, so we can't display it
      if (commentUser == null) {
        Log.w(
            this.getClass().getName(),
            "cannot display comment from missing user ID: " + comment.userId);
        continue;
      }

      View commentView = inflater.inflate(R.layout.include_comment, null);
      commentView.setTag(COMMENT_VIEW_BY + comment.userId);

      TextView commentText = (TextView) commentView.findViewById(R.id.comment_text);

      commentText.setText(Html.fromHtml(comment.commentText));
      commentText.setTag(COMMENT_BY + comment.userId);

      ImageView commentImage = (ImageView) commentView.findViewById(R.id.comment_user_image);

      TextView commentSharedDate = (TextView) commentView.findViewById(R.id.comment_shareddate);
      commentSharedDate.setText(comment.sharedDate + " ago");
      commentSharedDate.setTag(COMMENT_DATE_BY + comment.userId);

      final FlowLayout favouriteContainer =
          (FlowLayout) commentView.findViewById(R.id.comment_favourite_avatars);
      final ImageView favouriteIcon =
          (ImageView) commentView.findViewById(R.id.comment_favourite_icon);
      final ImageView replyIcon = (ImageView) commentView.findViewById(R.id.comment_reply_icon);

      if (comment.likingUsers != null) {
        if (Arrays.asList(comment.likingUsers).contains(user.id)) {
          favouriteIcon.setImageResource(R.drawable.have_favourite);
        }

        for (String id : comment.likingUsers) {
          ImageView favouriteImage = new ImageView(context);

          Cursor likingUserCursor =
              resolver.query(
                  FeedProvider.USERS_URI,
                  null,
                  DatabaseConstants.USER_USERID + " IN (?)",
                  new String[] {id},
                  null);
          UserProfile user = UserProfile.fromCursor(likingUserCursor);
          likingUserCursor.close();

          imageLoader.displayImage(user.photoUrl, favouriteImage, 10f);
          favouriteImage.setTag(id);

          favouriteContainer.addView(favouriteImage);
        }

        favouriteIcon.setOnClickListener(
            new OnClickListener() {
              @Override
              public void onClick(View v) {
                if (!Arrays.asList(comment.likingUsers).contains(user.id)) {
                  new LikeCommentTask(
                          context,
                          apiManager,
                          favouriteIcon,
                          favouriteContainer,
                          story.id,
                          comment,
                          story.feedId,
                          user.id)
                      .execute();
                } else {
                  new UnLikeCommentTask(
                          context,
                          apiManager,
                          favouriteIcon,
                          favouriteContainer,
                          story.id,
                          comment,
                          story.feedId,
                          user.id)
                      .execute();
                }
              }
            });
      }

      replyIcon.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              if (story != null) {
                Cursor userCursor =
                    resolver.query(
                        FeedProvider.USERS_URI,
                        null,
                        DatabaseConstants.USER_USERID + " IN (?)",
                        new String[] {comment.userId},
                        null);
                UserProfile user = UserProfile.fromCursor(userCursor);
                userCursor.close();

                DialogFragment newFragment =
                    ReplyDialogFragment.newInstance(story, comment.userId, user.username);
                newFragment.show(manager, "dialog");
              }
            }
          });

      Cursor replies =
          resolver.query(
              FeedProvider.REPLIES_URI,
              null,
              null,
              new String[] {comment.id},
              DatabaseConstants.REPLY_DATE + " DESC");
      while (replies.moveToNext()) {
        Reply reply = Reply.fromCursor(replies);

        View replyView = inflater.inflate(R.layout.include_reply, null);
        TextView replyText = (TextView) replyView.findViewById(R.id.reply_text);
        replyText.setText(Html.fromHtml(reply.text));
        ImageView replyImage = (ImageView) replyView.findViewById(R.id.reply_user_image);

        // occasionally there was no reply user and this caused a force close
        Cursor replyCursor =
            resolver.query(
                FeedProvider.USERS_URI,
                null,
                DatabaseConstants.USER_USERID + " IN (?)",
                new String[] {reply.userId},
                null);
        if (replyCursor.getCount() > 0) {
          final UserProfile replyUser = UserProfile.fromCursor(replyCursor);
          imageLoader.displayImage(replyUser.photoUrl, replyImage);
          replyImage.setOnClickListener(
              new OnClickListener() {
                @Override
                public void onClick(View view) {
                  Intent i = new Intent(context, Profile.class);
                  i.putExtra(Profile.USER_ID, replyUser.userId);
                  context.startActivity(i);
                }
              });

          TextView replyUsername = (TextView) replyView.findViewById(R.id.reply_username);
          replyUsername.setText(replyUser.username);
        } else {
          TextView replyUsername = (TextView) replyView.findViewById(R.id.reply_username);
          replyUsername.setText(R.string.unknown_user);
        }
        replyCursor.close();

        TextView replySharedDate = (TextView) replyView.findViewById(R.id.reply_shareddate);
        replySharedDate.setText(reply.shortDate + " ago");

        ((LinearLayout) commentView.findViewById(R.id.comment_replies_container))
            .addView(replyView);
      }
      replies.close();

      TextView commentUsername = (TextView) commentView.findViewById(R.id.comment_username);
      commentUsername.setText(commentUser.username);
      String userPhoto = commentUser.photoUrl;

      TextView commentLocation = (TextView) commentView.findViewById(R.id.comment_location);
      if (!TextUtils.isEmpty(commentUser.location)) {
        commentLocation.setText(commentUser.location.toUpperCase());
      } else {
        commentLocation.setVisibility(View.GONE);
      }

      if (!TextUtils.isEmpty(comment.sourceUserId)) {
        commentImage.setVisibility(View.INVISIBLE);
        ImageView usershareImage =
            (ImageView) commentView.findViewById(R.id.comment_user_reshare_image);
        ImageView sourceUserImage =
            (ImageView) commentView.findViewById(R.id.comment_sharesource_image);
        sourceUserImage.setVisibility(View.VISIBLE);
        usershareImage.setVisibility(View.VISIBLE);
        commentImage.setVisibility(View.INVISIBLE);

        Cursor sourceUserCursor =
            resolver.query(
                FeedProvider.USERS_URI,
                null,
                DatabaseConstants.USER_USERID + " IN (?)",
                new String[] {comment.sourceUserId},
                null);
        if (sourceUserCursor.getCount() > 0) {
          UserProfile sourceUser = UserProfile.fromCursor(sourceUserCursor);
          sourceUserCursor.close();

          imageLoader.displayImage(sourceUser.photoUrl, sourceUserImage, 10f);
          imageLoader.displayImage(userPhoto, usershareImage, 10f);
        }
        sourceUserCursor.close();
      } else {
        imageLoader.displayImage(userPhoto, commentImage, 10f);
      }

      if (comment.byFriend) {
        friendCommentViews.add(commentView);
      } else {
        publicCommentViews.add(commentView);
      }

      commentImage.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View view) {
              Intent i = new Intent(context, Profile.class);
              i.putExtra(Profile.USER_ID, comment.userId);
              context.startActivity(i);
            }
          });
    }

    return null;
  }
  protected void onPostExecute(Void result) {
    if (viewHolder.get() != null) {
      FlowLayout sharedGrid =
          (FlowLayout) viewHolder.get().findViewById(R.id.reading_social_shareimages);
      FlowLayout commentGrid =
          (FlowLayout) viewHolder.get().findViewById(R.id.reading_social_commentimages);

      TextView friendCommentTotal =
          ((TextView) viewHolder.get().findViewById(R.id.reading_friend_comment_total));
      TextView publicCommentTotal =
          ((TextView) viewHolder.get().findViewById(R.id.reading_public_comment_total));

      ViewUtils.setupCommentCount(context, viewHolder.get(), commentCursor.getCount());
      ViewUtils.setupShareCount(context, viewHolder.get(), story.sharedUserIds.length);

      ArrayList<String> commentIds = new ArrayList<String>();
      commentCursor.moveToFirst();
      for (int i = 0; i < commentCursor.getCount(); i++) {
        commentIds.add(
            commentCursor.getString(
                commentCursor.getColumnIndex(DatabaseConstants.COMMENT_USERID)));
        commentCursor.moveToNext();
      }

      for (final String userId : story.sharedUserIds) {
        if (!commentIds.contains(userId)) {
          Cursor userCursor =
              resolver.query(
                  FeedProvider.USERS_URI,
                  null,
                  DatabaseConstants.USER_USERID + " IN (?)",
                  new String[] {userId},
                  null);
          if (userCursor.getCount() > 0) {
            UserProfile user = UserProfile.fromCursor(userCursor);
            userCursor.close();

            ImageView image =
                ViewUtils.createSharebarImage(context, imageLoader, user.photoUrl, user.userId);
            sharedGrid.addView(image);
          }
          userCursor.close();
        }
      }

      commentCursor.moveToFirst();

      for (int i = 0; i < commentCursor.getCount(); i++) {
        final Comment comment = Comment.fromCursor(commentCursor);

        Cursor userCursor =
            resolver.query(
                FeedProvider.USERS_URI,
                null,
                DatabaseConstants.USER_USERID + " IN (?)",
                new String[] {comment.userId},
                null);
        UserProfile user = UserProfile.fromCursor(userCursor);
        userCursor.close();

        ImageView image =
            ViewUtils.createSharebarImage(context, imageLoader, user.photoUrl, user.userId);
        commentGrid.addView(image);
        commentCursor.moveToNext();
      }

      if (publicCommentViews.size() > 0) {
        String commentCount = context.getString(R.string.public_comment_count);
        if (publicCommentViews.size() == 1) {
          commentCount = commentCount.substring(0, commentCount.length() - 1);
        }
        publicCommentTotal.setText(String.format(commentCount, publicCommentViews.size()));
        viewHolder
            .get()
            .findViewById(R.id.reading_public_comment_header)
            .setVisibility(View.VISIBLE);
      }

      if (friendCommentViews.size() > 0) {
        String commentCount = context.getString(R.string.friends_comments_count);
        if (friendCommentViews.size() == 1) {
          commentCount = commentCount.substring(0, commentCount.length() - 1);
        }
        friendCommentTotal.setText(String.format(commentCount, friendCommentViews.size()));
        viewHolder
            .get()
            .findViewById(R.id.reading_friend_comment_header)
            .setVisibility(View.VISIBLE);
      }

      for (int i = 0; i < publicCommentViews.size(); i++) {
        if (i == publicCommentViews.size() - 1) {
          publicCommentViews.get(i).findViewById(R.id.comment_divider).setVisibility(View.GONE);
        }
        ((LinearLayout) viewHolder.get().findViewById(R.id.reading_public_comment_container))
            .addView(publicCommentViews.get(i));
      }

      for (int i = 0; i < friendCommentViews.size(); i++) {
        if (i == friendCommentViews.size() - 1) {
          friendCommentViews.get(i).findViewById(R.id.comment_divider).setVisibility(View.GONE);
        }
        ((LinearLayout) viewHolder.get().findViewById(R.id.reading_friend_comment_container))
            .addView(friendCommentViews.get(i));
      }
    }
    commentCursor.close();
  }