Android Open Source - sms-smap-gateway U I Paging Controller






From Project

Back to project page sms-smap-gateway.

License

The source code is released under:

GNU General Public License

If you think the Android project sms-smap-gateway 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.android.smap.controllers;
//from w ww  .  ja  v  a2s . c  o  m
import android.content.Context;

import com.android.smap.api.ApiConstants;
import com.android.smap.api.requests.ApiRequest;
import com.android.volley.VolleyError;

/**
 * This controller provides the base logic for paging. Intended to be used with
 * VelocAdapter to display an endless list of incoming traffic updates
 * 
 * @param <T>
 *            Clean model type of each page item.
 * @param <V>
 *            Dirty raw model from feed of underlying concrete controller
 * @author matt witherow
 */

public abstract class UIPagingController<T, V> extends UIRequestController<V> {

  public interface PagingStatusIndicator {
    public void noMorePages();

    public void paging(boolean isPaging);
  }

  protected enum ControllerState {
    ERROR,
    FREE,
    BUSY
  }

  /**
   * From the newly requested page of the dirty model, we want only new items
   * (no duplicates). Save these new items as clean models to be appended to
   * the end of the list. We update the dirty model (super.setModel) to be the
   * current page (we dont append to create a massive dirty model that is
   * tiresome to sanitze)
   * 
   * @param Dirty
   *            model of concrete controller
   */
  protected abstract void appendNewPageItems();

  public static final int      PAGE_HEADSTART  = 3;
  private int            mPageSize    = ApiConstants.DEFAULT_PAGE_SIZE;
  private int            mPageNumber    = 1;
  private PagingStatusIndicator  mPagingIndicator;
  private ControllerState      mState      = ControllerState.FREE;
  private boolean          mMorePages    = true;

  public UIPagingController(Context context, ControllerListener listener,
      ControllerErrorListener errorListener,
      PagingStatusIndicator indicator) {
    super(context, listener, errorListener);
    mPagingIndicator = indicator;
    mState = ControllerState.FREE;
  }

  /**
   * This method provides the page request
   * 
   * @param pageSize
   *            size of each page
   * @param page
   *            current page number to fetch
   * @return
   */
  protected abstract ApiRequest<?> getPageRequest(int pageSize, int page);

  @Override
  protected ApiRequest<?> getRequest() {
    return getPageRequest(mPageSize, mPageNumber);
  }

  @Override
  public void setModel(V model) {
    super.setModel(model);
    appendNewPageItems();
    mPagingIndicator.paging(setBusy(false));
  }

  @Override
  public void onErrorResponse(VolleyError error) {
    super.onErrorResponse(error);
    mState = ControllerState.ERROR;
  }

  /**
   * This method is used to check if the controller is not busy and there are
   * more pages left to get
   * 
   * @return
   */
  public boolean canPage() {
    return (mMorePages && !isBusy());
  }

  /**
   * This method is used to check if the controller is currently fetching new
   * data
   * 
   * @return
   */
  public boolean isBusy() {
    return (mState == ControllerState.BUSY);
  }

  private boolean setBusy(boolean busy) {

    mState = busy ? ControllerState.BUSY : ControllerState.FREE;
    return busy;
  }

  /**
   * Starts a new page request, and notifies the controller's callback when
   * completed. Tells a paging delegate that it is busy paging.
   */
  public void performPageRequest() {
    if (canPage()) {
      incrementPageNumber();
      mPagingIndicator.paging(setBusy(true));
      super.start();
    }
  }

  /**
   * Sets whether or not the controller has reached the last page
   * 
   * @param atEnd
   */
  public void setAtEnd() {
    mMorePages = false;
    mPagingIndicator.paging(setBusy(false));
    if (mPagingIndicator != null) {
      mPagingIndicator.noMorePages();
    }
  }

  public void setPageSize(int size) {
    this.mPageSize = size;
  }

  public int getPageSize() {
    return mPageSize;
  }

  public int getPage() {
    return mPageNumber;
  }

  public void incrementPageNumber() {
    ++mPageNumber;
  }

  public void resetPager() {
    mPageNumber = 1;
    mMorePages = true;
    mPagingIndicator.paging(setBusy(false));

  }

  /**
   * Call this when network requests have been canceled to update the
   * controller's state
   */
  public void onStop() {
    setBusy(false);
  }

}




Java Source Code List

com.android.smap.AppConfig.java
com.android.smap.GatewayApp.java
com.android.smap.PhoneStateWrapper.java
com.android.smap.activities.BaseActivity.java
com.android.smap.activities.FragmentWrapperActivity.java
com.android.smap.activities.MainActivity.java
com.android.smap.adapters.MenuDrawerAdapter.java
com.android.smap.api.ApiConstants.java
com.android.smap.api.models.Gojo.java
com.android.smap.api.models.Node.java
com.android.smap.api.models.SmapException.java
com.android.smap.api.models.Status.java
com.android.smap.api.models.validator.ModelValidator.java
com.android.smap.api.models.validator.Required.java
com.android.smap.api.requests.ApiRequest.java
com.android.smap.api.requests.GojoRequest.java
com.android.smap.api.requests.StatusRequest.java
com.android.smap.commonsware.wakefull.AlarmReceiver.java
com.android.smap.commonsware.wakefull.WakefulIntentService.java
com.android.smap.controllers.ControllerErrorListener.java
com.android.smap.controllers.ControllerError.java
com.android.smap.controllers.ControllerFactory.java
com.android.smap.controllers.ControllerListener.java
com.android.smap.controllers.Controller.java
com.android.smap.controllers.EmailController.java
com.android.smap.controllers.GojoController.java
com.android.smap.controllers.UIPagingController.java
com.android.smap.controllers.UIRequestController.java
com.android.smap.fragments.BaseFragment.java
com.android.smap.fragments.HomeFragment.java
com.android.smap.models.MenuDrawerItem.java
com.android.smap.models.TextMessage.java
com.android.smap.samuel.Samuel.java
com.android.smap.sms.CellularModem.java
com.android.smap.sms.GatewayService.java
com.android.smap.sms.MonitorService.java
com.android.smap.sms.ScheduleMonitoringListener.java
com.android.smap.sms.ServiceRebooter.java
com.android.smap.ui.ArrayVelocAdapter.java
com.android.smap.ui.OnErrorTouchedListener.java
com.android.smap.ui.Operation.java
com.android.smap.ui.VelocAdapter.java
com.android.smap.ui.ViewBinder.java
com.android.smap.ui.ViewQuery.java
com.android.smap.ui.views.AnimatedNetworkImageView.java
com.android.smap.ui.views.ErrorView.java
com.android.smap.ui.views.FontableTextView.java
com.android.smap.utils.BitmapLruCache.java
com.android.smap.utils.MWAPNUtils.java
com.android.smap.utils.MWAnimUtil.java
com.android.smap.utils.MWAssetsUtils.java
com.android.smap.utils.MWConnectionUtils.java
com.android.smap.utils.MWEmailUtils.java
com.android.smap.utils.MWUiUtils.java
com.android.smap.utils.PreferenceWrapper.java
com.android.smap.utils.Refresher.java
com.android.smap.utils.UriBuilder.java