Android Open Source - hpush Dailies Lst Fragment






From Project

Back to project page hpush.

License

The source code is released under:

MIT License

If you think the Android project hpush 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.hpush.app.fragments;
//  ww w .  j av a 2  s.  c  o m
import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.os.AsyncTaskCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chopping.application.BasicPrefs;
import com.chopping.fragments.BaseFragment;
import com.github.ksoichiro.android.observablescrollview.ObservableRecyclerView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.hpush.R;
import com.hpush.app.adapters.DailiesListAdapter;
import com.hpush.bus.BookmarkMessageEvent;
import com.hpush.bus.BookmarkedEvent;
import com.hpush.bus.ShowActionBar;
import com.hpush.data.DailyListItem;
import com.hpush.db.DB;
import com.hpush.db.DB.Sort;
import com.hpush.utils.Prefs;

import de.greenrobot.event.EventBus;

/**
 * Show all {@link com.hpush.data.Daily}s.
 *
 * @author Xinyue Zhao
 */
public final class DailiesLstFragment extends BaseFragment implements ObservableScrollViewCallbacks {

  /**
   * Main layout for this component.
   */
  private static final int LAYOUT = R.layout.fragment_dailies_list;


  /**
   * Database.
   */
  private DB mDB;
  /**
   * View shows all data.
   */
  private ObservableRecyclerView mRv;
  /**
   * {@link android.support.v7.widget.RecyclerView.Adapter} for the {@link #mRv}.
   */
  private DailiesListAdapter mAdp;

  //------------------------------------------------
  //Subscribes, event-handlers
  //------------------------------------------------

  /**
   * Handler for {@link com.hpush.bus.BookmarkMessageEvent}.
   *
   * @param e
   *     Event {@link com.hpush.bus.BookmarkMessageEvent}.
   */
  public void onEvent(BookmarkMessageEvent e) {
    if(e.getMessageListItem() instanceof DailyListItem) {
      //Bookmark on the site self.
      bookmarkOneItem((DailyListItem) e.getMessageListItem());
    } else {
      //Bookmark from a webview shows details.
      List<DailyListItem> list = mAdp.getMessages();
      for(DailyListItem item : list) {
        if(item.getId() == e.getMessageListItem().getId()) {
          bookmarkOneItem(item);
          EventBus.getDefault().removeAllStickyEvents();
          break;
        }
      }
    }
  }

  //------------------------------------------------
  public static DailiesLstFragment newInstance(Context context) {
    return (DailiesLstFragment) DailiesLstFragment.instantiate(context, DailiesLstFragment.class.getName());
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(LAYOUT, container, false);
  }


  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    setErrorHandlerAvailable(false);

    mDB = DB.getInstance(getActivity().getApplication());
    mRv = (ObservableRecyclerView) view.findViewById(R.id.daily_rv);
    if(getResources().getBoolean(R.bool.landscape)) {
      mRv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
    } else {
      mRv.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
    mRv.setHasFixedSize(false);
    mRv.setScrollViewCallbacks(this);


    loadDailies();
  }

  @Override
  protected BasicPrefs getPrefs() {
    return Prefs.getInstance(getActivity().getApplication());
  }



  /**
   * Get all data.
   */
  private void loadDailies() {
    AsyncTask<Void, List<DailyListItem>, List<DailyListItem>> task = new AsyncTask<Void, List<DailyListItem>, List<DailyListItem>>() {
      @Override
      protected List<DailyListItem> doInBackground(Void... params) {
        return mDB.getDailies(Sort.DESC);
      }

      @Override
      protected void onPostExecute(List<DailyListItem> dailies) {
        super.onPostExecute(dailies);
        if (mAdp == null) {
          mAdp = new DailiesListAdapter(dailies );
          mRv.setAdapter(mAdp);
        } else {
          mAdp.setMessages(dailies);
          mAdp.notifyDataSetChanged();
        }
      }
    };
    AsyncTaskCompat.executeParallel(task);
  }

  /**
   * Bookmark one item.
   *
   * @param itemToBookmark
   *     The item to bookmark.
   */
  private void bookmarkOneItem(final DailyListItem itemToBookmark) {
    AsyncTaskCompat.executeParallel(new AsyncTask<List<DailyListItem>, Void, Void>() {
      @Override
      protected Void doInBackground(
          List<DailyListItem>... params) {
        List<DailyListItem> data = params[0];
        for (DailyListItem obj:data) {
          if (obj.getId() == itemToBookmark.getId()) {
            mDB.removeMessage(obj == null ? null : obj.getMessage());
            mDB.addBookmark(obj.getMessage());
          }
        }
        itemToBookmark.setBookmarked(true);
        return null;
      }

      @Override
      protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        mAdp.notifyDataSetChanged();
        EventBus.getDefault().post(new BookmarkedEvent());
      }
    }, mAdp.getMessages());
  }

  @Override
  public void onScrollChanged(int i, boolean b, boolean b2) {

  }

  @Override
  public void onDownMotionEvent() {

  }

  @Override
  public void onUpOrCancelMotionEvent(ScrollState scrollState) {
    if (scrollState == ScrollState.UP) {
      EventBus.getDefault().post(new ShowActionBar(false));
    } else if (scrollState == ScrollState.DOWN) {
      EventBus.getDefault().post(new ShowActionBar(true));
    }
  }
}




Java Source Code List

com.hpush.app.App.java
com.hpush.app.activities.BasicActivity.java
com.hpush.app.activities.DailiesActivity.java
com.hpush.app.activities.MainActivity.java
com.hpush.app.activities.SettingActivity.java
com.hpush.app.activities.WebViewActivity.java
com.hpush.app.adapters.DailiesListAdapter.java
com.hpush.app.adapters.MainViewPagerAdapter.java
com.hpush.app.adapters.MessagesListAdapter.java
com.hpush.app.fragments.AboutDialogFragment.java
com.hpush.app.fragments.AdFragment.java
com.hpush.app.fragments.AppListImpFragment.java
com.hpush.app.fragments.BookmarksListFragment.java
com.hpush.app.fragments.DailiesLstFragment.java
com.hpush.app.fragments.GPlusFragment.java
com.hpush.app.fragments.MessagesListFragment.java
com.hpush.bus.BookmarkAllEvent.java
com.hpush.bus.BookmarkMessageEvent.java
com.hpush.bus.BookmarkedEvent.java
com.hpush.bus.ClickMessageCommentsEvent.java
com.hpush.bus.ClickMessageEvent.java
com.hpush.bus.ClickMessageLinkEvent.java
com.hpush.bus.DeleteAccountEvent.java
com.hpush.bus.EULAConfirmedEvent.java
com.hpush.bus.EULARejectEvent.java
com.hpush.bus.EditSettingsEvent.java
com.hpush.bus.GCMRegistedEvent.java
com.hpush.bus.InsertAccountEvent.java
com.hpush.bus.LoadAllEvent.java
com.hpush.bus.LoginedGPlusEvent.java
com.hpush.bus.LogoutGPlusEvent.java
com.hpush.bus.RemoveAllEvent.java
com.hpush.bus.SelectMessageEvent.java
com.hpush.bus.ShareMessageEvent.java
com.hpush.bus.ShowActionBar.java
com.hpush.bus.SortAllEvent.java
com.hpush.bus.UpdateCurrentTotalMessagesEvent.java
com.hpush.data.DailyListItem.java
com.hpush.data.Daily.java
com.hpush.data.FunctionType.java
com.hpush.data.MessageListItem.java
com.hpush.data.Message.java
com.hpush.data.Status.java
com.hpush.data.SyncList.java
com.hpush.db.BookmarksTbl.java
com.hpush.db.DB.java
com.hpush.db.DailyTbl.java
com.hpush.db.DatabaseHelper.java
com.hpush.db.MessagesTbl.java
com.hpush.gcm.ChangeSettingsTask.java
com.hpush.gcm.GcmBroadcastReceiver.java
com.hpush.gcm.GcmIntentService.java
com.hpush.gcm.RegGCMTask.java
com.hpush.gcm.SyncTask.java
com.hpush.gcm.UnregGCMTask.java
com.hpush.utils.Prefs.java
com.hpush.utils.Utils.java
com.hpush.views.FontTextView.java
com.hpush.views.OnViewAnimatedClickedListener2.java
com.hpush.views.OnViewAnimatedClickedListener3.java
com.hpush.views.OnViewAnimatedClickedListener.java
com.hpush.views.SortActionViewProvider.java
com.hpush.views.WebViewEx.java