ViewForumExpandableListDataAdapter.java :  » App » alldroid » org » alldroid » forum » data » Android Open Source

Android Open Source » App » alldroid 
alldroid » org » alldroid » forum » data » ViewForumExpandableListDataAdapter.java
/**
 *
 */
package org.alldroid.forum.data;

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

import org.alldroid.forum.R;
import org.alldroid.forum.ViewForumActivity;
import org.alldroid.forum.ViewTopicActivity;
import org.alldroid.forum.objects.Forum;
import org.alldroid.forum.objects.ForumCollectionCache;
import org.alldroid.forum.objects.ForumTopic;
import org.alldroid.forum.objects.ForumTopicList;
import org.alldroid.forum.preferences.Preferences;
import org.alldroid.forum.service.BaseForumService;
import org.alldroid.forum.utils.DateHelper;
import org.xmlrpc.android.XmlRpcCallCompletedListener;
import org.xmlrpc.android.XmlRpcException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;

/**
 * @author trr4rac
 */
public class ViewForumExpandableListDataAdapter extends BaseExpandableListAdapter {
  
  protected static final String TAG = ViewForumExpandableListDataAdapter.class.getSimpleName ( );

  
  private List<String> groups;
  private Context context;
  private String parentForumId;
  private BaseForumService service;
  private int totalTopics;
  private ExpandableListView list;
  private Preferences preferences;
  private List<Forum> subforums;
  private List<ForumTopic> topics;
  private XmlRpcCallCompletedListener callback;
  private boolean isLoading;
  
  /**
   *
   */
  public ViewForumExpandableListDataAdapter ( final Activity context, ExpandableListView list, String parentForumId, BaseForumService service ) {
    this.setGroups ( new ArrayList<String> ( ) );
    this.setParentForumId ( parentForumId );
    this.setContext ( context );
    this.setService ( service );
    this.setListView ( list );
    this.setPreferences ( Preferences.create ( context ) );
    list.setSelector ( R.drawable.listitemselected );
    this.setSubforums ( new ArrayList<Forum> ( ) );
    this.setTopics ( new ArrayList<ForumTopic> ( ) );
    
    View loading = context.findViewById ( R.id.loading );
    if ( loading != null ) {
      loading.setVisibility ( View.VISIBLE );
      list.setEmptyView ( loading );
    }
    
    View err = context.findViewById ( R.id.error );
    if ( err != null ) {
      err.setVisibility ( View.GONE );
    }
    
    list.setOnChildClickListener ( new OnChildClickListener ( ) {
      
      @Override
      public boolean onChildClick ( ExpandableListView parent, View v, int groupPosition, int childPosition, long id ) {
        BaseExpandableListAdapter ba = ViewForumExpandableListDataAdapter.this;
        if ( groupPosition == 0 ) {
          try {
            Forum forum = (Forum)ba.getChild ( groupPosition, childPosition );
            if ( forum != null ) {
              Intent intent = new Intent ( ViewForumExpandableListDataAdapter.this.getContext ( ), ViewForumActivity.class );
              Bundle extras = new Bundle ( );
              extras.putString ( ViewForumActivity.EXTRA_FORUMID, forum.getId ( ) );
              extras.putString ( ViewForumActivity.EXTRA_FORUMNAME, forum.getName ( ) );
              intent.putExtras ( extras );
              ViewForumExpandableListDataAdapter.this.getContext ( ).startActivity ( intent );
            }
          } catch ( Exception ex ) {
            Log.e ( TAG, ex.getMessage ( ), ex );
          }
          return true;
        } else if ( groupPosition == 1 ) {
          ForumTopic topic = (ForumTopic)ba.getChild ( groupPosition, childPosition );
          if ( topic != null ) {
            Intent intent = new Intent ( ViewForumExpandableListDataAdapter.this.getContext ( ), ViewTopicActivity.class );
            Bundle extras = new Bundle ( );
            extras.putString ( ViewTopicActivity.EXTRA_TOPICID, topic.getId ( ) );
            extras.putString ( ViewTopicActivity.EXTRA_TOPICNAME, topic.getTitle ( ) );
            intent.putExtras ( extras );
            ViewForumExpandableListDataAdapter.this.getContext ( ).startActivity ( intent );
          }
          return true;
        }
        return false;
      }
      
    } );
    
  }
  
  public void load ( ) {
    loadData ( );
  }
  
  private void loadData ( ) {
    
    list.setAdapter ( this );
    
    Bundle state = new Bundle ( );
    state.putString ( BaseForumService.STATE_FORUMID, this.getParentForumId ( ) );
    state.putString ( BaseForumService.STATE_GROUPFORUMS, this.getContext ( ).getString ( R.string.forumview_forumsgroup_title ) );
    state.putString ( BaseForumService.STATE_GROUPTOPICS, this.getContext ( ).getString ( R.string.forumview_topicsgroup_title ) );
    
    try {
      this.getService ( ).getTopicsAsync ( parentForumId, 0, 19, this.getCallback ( state ) );
    } catch ( XmlRpcException ex ) {
      Log.e ( TAG, ex.getMessage ( ), ex );
    }
  }
  
  private XmlRpcCallCompletedListener getCallback ( final Bundle state ) {
    if ( callback == null ) {
      callback = new XmlRpcCallCompletedListener ( this.getService ( ).getXmlrpcClient ( ) ) {
        
        @Override
        public void onCompletion ( Object result ) {
          // add the default groups
          // this is here so the loading view will display until the data is retrieved
          ViewForumExpandableListDataAdapter.this.getGroups ( ).add ( state.getString ( BaseForumService.STATE_GROUPFORUMS ) );
          ViewForumExpandableListDataAdapter.this.getGroups ( ).add ( state.getString ( BaseForumService.STATE_GROUPTOPICS ) );
          
          ForumTopicList ftl = ViewForumExpandableListDataAdapter.this.getService ( ).getTopicsAsyncEnd ( result );
          if ( ftl != null ) {
            ViewForumExpandableListDataAdapter.this.getTopics ( ).addAll ( ftl.getTopics ( ) );
            ViewForumExpandableListDataAdapter.this.setTotalTopics ( ftl.getTotalTopics ( ) );
            // TODO figure out lazy loading so when the user scrolls to the last item, the next
            // "page" of data loads
            
            if ( ViewForumExpandableListDataAdapter.this.getPreferences ( ).getBoolean ( R.string.viewforum_expandsubforms_key, true ) ) {
              ViewForumExpandableListDataAdapter.this.getListView ( ).expandGroup ( 1 );
            }
          }
          
          if ( state.containsKey ( BaseForumService.STATE_FORUMID ) ) {
            List<Forum> tforums = ForumCollectionCache.getInstance ( ).getChildren ( state.getString ( BaseForumService.STATE_FORUMID ) );
            if ( tforums != null ) {
              ViewForumExpandableListDataAdapter.this.getSubforums ( ).addAll ( tforums );
            }
          }
          
          if ( ViewForumExpandableListDataAdapter.this.getPreferences ( ).getBoolean ( R.string.viewforum_expandsubforms_key, true ) ) {
            ViewForumExpandableListDataAdapter.this.getListView ( ).expandGroup ( 0 );
          }
          
          ViewForumExpandableListDataAdapter.this.notifyDataSetChanged ( );
          
        }
      };
      callback.setState ( state );
    }
    return callback;
  }
  
  public ExpandableListView getListView ( ) {
    return this.list;
  }
  
  protected void setListView ( ExpandableListView list ) {
    this.list = list;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getChild(int, int)
   */
  @Override
  public Object getChild ( int groupPosition, int childPosition ) {
    if ( groupPosition == 0 ) {
      return this.getSubforums ( ).get ( childPosition );
    } else if ( groupPosition == 1 ) {
      return this.getTopics ( ).get ( childPosition );
    }
    
    return null;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getChildId(int, int)
   */
  @Override
  public long getChildId ( int groupPosition, int childPosition ) {
    return childPosition;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getChildView ( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent ) {
    View v = convertView;
    
    if ( groupPosition == 0 ) {
      // if ( v == null ) {
      LayoutInflater vi = (LayoutInflater)this.getContext ( ).getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
      v = vi.inflate ( R.layout.forumlistitem, null );
      // }
      Forum forum = this.getSubforums ( ).get ( childPosition );
      if ( forum != null ) {
        TextView tv = (TextView)v.findViewById ( R.id.title );
        if ( tv != null ) {
          tv.setText ( forum.getName ( ) );
        }
        
        tv = (TextView)v.findViewById ( R.id.description );
        if ( tv != null ) {
          tv.setText ( forum.getDescription ( ) );
        }
      }
      
    } else if ( groupPosition == 1 ) {
      // if ( v == null ) {
      LayoutInflater vi = (LayoutInflater)this.getContext ( ).getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
      v = vi.inflate ( R.layout.topiclistitem, null );
      // }
      ForumTopic ftopic = this.getTopics ( ).get ( childPosition );
      if ( ftopic != null ) {
        TextView tv = (TextView)v.findViewById ( R.id.title );
        if ( tv != null ) {
          tv.setText ( ftopic.getTitle ( ) );
        }
        
        tv = (TextView)v.findViewById ( R.id.author );
        if ( tv != null ) {
          tv.setText ( ftopic.getAuthorName ( ) );
        }
        
        tv = (TextView)v.findViewById ( R.id.lastpostdate );
        if ( tv != null ) {
          tv.setText ( DateHelper.format ( DateHelper.SIMPLE, ftopic.getLastReplyDate ( ) ) );
        }
        
        // replies
        tv = (TextView)v.findViewById ( R.id.replies );
        if ( tv != null ) {
          tv.setText ( Integer.toString ( ftopic.getReplyCount ( ) ) );
        }
        
        //
        tv = (TextView)v.findViewById ( R.id.views );
        if ( tv != null ) {
          tv.setText ( Integer.toString ( ftopic.getViewCount ( ) ) );
        }
        
        ImageView iv = (ImageView)v.findViewById ( R.id.statusimage );
        if ( iv != null ) {
          if ( ftopic.isNewPost ( ) ) {
            iv.setImageResource ( R.drawable.forum_unread );
          } else {
            iv.setImageResource ( R.drawable.forum_read );
          }
        }
      }
    }
    return v;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
   */
  @Override
  public int getChildrenCount ( int groupPosition ) {
    
    if ( groupPosition == 0 ) {
      return this.getSubforums ( ).size ( );
    } else if ( groupPosition == 1 ) {
      return this.getTopics ( ).size ( );
    } else {
      return 0;
    }
    
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getGroup(int)
   */
  @Override
  public Object getGroup ( int groupPosition ) {
    return this.getGroups ( ).get ( groupPosition );
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getGroupCount()
   */
  @Override
  public int getGroupCount ( ) {
    return this.getGroups ( ).size ( );
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getGroupId(int)
   */
  @Override
  public long getGroupId ( int groupPosition ) {
    return groupPosition;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean, android.view.View,
   * android.view.ViewGroup)
   */
  @Override
  public View getGroupView ( int groupPosition, boolean isLastChild, View convertView, ViewGroup parent ) {
    View v = convertView;
    if ( v == null ) {
      LayoutInflater vi = (LayoutInflater)this.getContext ( ).getSystemService ( Context.LAYOUT_INFLATER_SERVICE );
      v = vi.inflate ( R.layout.viewforumgroup, null );
    }
    
    String title = this.getGroups ( ).get ( groupPosition );
    if ( title != null && title.length ( ) > 0 ) {
      TextView tv = (TextView)v.findViewById ( R.id.title );
      if ( tv != null ) {
        tv.setText ( title );
      }
    }
    
    return v;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#hasStableIds()
   */
  @Override
  public boolean hasStableIds ( ) {
    return false;
  }
  
  /*
   * (non-Javadoc)
   * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
   */
  @Override
  public boolean isChildSelectable ( int groupPosition, int childPosition ) {
    return true;
  }
  
  /**
   * @param groups
   *          the groups to set
   */
  private void setGroups ( List<String> groups ) {
    this.groups = groups;
  }
  
  /**
   * @return the groups
   */
  public List<String> getGroups ( ) {
    return groups;
  }
  
  /**
   * @param context
   *          the context to set
   */
  private void setContext ( Context context ) {
    this.context = context;
  }
  
  /**
   * @return the context
   */
  public Context getContext ( ) {
    return context;
  }
  
  /**
   * @param parentForumId
   *          the parentForumId to set
   */
  private void setParentForumId ( String parentForumId ) {
    this.parentForumId = parentForumId;
  }
  
  /**
   * @return the parentForumId
   */
  public String getParentForumId ( ) {
    return parentForumId;
  }
  
  /**
   * @param service
   *          the service to set
   */
  private void setService ( BaseForumService service ) {
    this.service = service;
  }
  
  /**
   * @return the service
   */
  public BaseForumService getService ( ) {
    return service;
  }
  
  /**
   * @param subforums
   *          the subforums to set
   */
  private void setSubforums ( List<Forum> subforums ) {
    this.subforums = subforums;
  }
  
  /**
   * @return the subforums
   */
  public List<Forum> getSubforums ( ) {
    return subforums;
  }
  
  /**
   * @param topics
   *          the topics to set
   */
  private void setTopics ( List<ForumTopic> topics ) {
    this.topics = topics;
  }
  
  /**
   * @return the topics
   */
  public List<ForumTopic> getTopics ( ) {
    return topics;
  }
  
  /**
   * @param totalTopics
   *          the totalTopics to set
   */
  private void setTotalTopics ( int totalTopics ) {
    this.totalTopics = totalTopics;
  }
  
  /**
   * @return the totalTopics
   */
  public int getTotalTopics ( ) {
    return totalTopics;
  }
  
  /**
   * @param preferences
   *          the preferences to set
   */
  protected void setPreferences ( Preferences preferences ) {
    this.preferences = preferences;
  }
  
  /**
   * @return the preferences
   */
  public Preferences getPreferences ( ) {
    return preferences;
  }
  
  /**
   * @param isLoading
   *          the isLoading to set
   */
  protected void setLoading ( boolean isLoading ) {
    this.isLoading = isLoading;
  }
  
  /**
   * @return the isLoading
   */
  public boolean isLoading ( ) {
    return isLoading;
  }
  
}
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.