SPostContainer.java :  » UnTagged » snaio » edu » uta » cse » snaio » Android Open Source

Android Open Source » UnTagged » snaio 
snaio » edu » uta » cse » snaio » SPostContainer.java
/*
Copyright (c) 2010, SNAIO team
All rights reserved.
 */
package edu.uta.cse.snaio;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import edu.uta.cse.snaio.core.Constants;
import edu.uta.cse.snaio.core.model.post.IPost;
import edu.uta.cse.snaio.core.model.post.SPostView;

/**
 * The post container of wall screen
 * 
 * @author Tuan Nguyen - tnguyen@mavs.uta.edu
 */
public class SPostContainer extends LinearLayout {

  /**
   * Instantiates SPostContainer
   * 
   * @param context
   *            the context
   */
  public SPostContainer(Context context) {
    super(context);
  }

  /**
   * Adds the a view of a post to wall screen
   * 
   * @param postView
   *            a view of a post
   */
  public void addPost(SPostView postView) {
    boolean postAdded = false;
    // check if post is exit on screen
    SPostView existedPost = getTheSamePostOnWall(postView);
    // if the post there and it is updated
    if (existedPost != null) {
      // if this post is changed 
      if (!existedPost.getPost().getUpdatedDate()
          .equals(postView.getPost().getUpdatedDate())) {
        // try update new content.
        existedPost.update(postView);
      }
      postAdded = true;
    }
    if (!postAdded) {
      // get total posts on wall screen
      int childCount = getChildCount();
      SPostView sPostView = null;
      // process adding post view to wall screen
      for (int i = 0; i < childCount; i++) {
        sPostView = (SPostView) getChildAt(i);
        // add new post before old post
        if (sPostView.compareTo(postView) <= 0) {
          addView(postView, i);
          postAdded = true;
          break;
        }
      }
    }
    if (!postAdded) {
      // add new post on screen
      addView(postView);
    }
    // remove old posts on wall screen
    removeOldPosts();
  }

  /**
   * Removes the old views of posts on keep limit post on wall screen
   * 
   */
  private void removeOldPosts() {
    List<View> postViews = new ArrayList<View>();
    for (int i = Constants.MAX_ITEMS_ON_WALL_SCREEN; i < getChildCount(); i++) {
      postViews.add(getChildAt(i));
    }
    // Log.e(SPostContainer.class.getCanonicalName(), "currents:"
    // +getChildCount()+" will remove: "+postViews.size());
    for (View view : postViews) {
      removeView(view);
    }
    // Log.e(SPostContainer.class.getCanonicalName(), "after:"
    // +getChildCount());

  }

  /**
   * Check exist post,
   * 
   * @param postView
   *            the post view
   * @return true, if successful
   */
  private SPostView getTheSamePostOnWall(SPostView postView) {
    // get total current post on screen
    int childCount = getChildCount();
    IPost currentPost = null;
    IPost post = postView.getPost();
    SPostView sPostView = null;

    for (int i = 0; i < childCount; i++) {
      sPostView = (SPostView) getChildAt(i);
      currentPost = sPostView.getPost();
      if (currentPost.getPostId().equals(post.getPostId())) {
        return sPostView;
      }
    }
    return null;
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.