301 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			301 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| package com.novelbook.android.Fragments;
 | |
| 
 | |
| import android.app.ProgressDialog;
 | |
| import android.os.Bundle;
 | |
| import android.support.v4.widget.SwipeRefreshLayout;
 | |
| import android.support.v7.widget.LinearLayoutManager;
 | |
| import android.text.TextUtils;
 | |
| import android.util.Log;
 | |
| import android.view.LayoutInflater;
 | |
| import android.view.View;
 | |
| import android.view.ViewGroup;
 | |
| import android.widget.AdapterView;
 | |
| import android.widget.Button;
 | |
| import android.widget.ImageButton;
 | |
| import android.widget.ListView;
 | |
| import android.widget.TextView;
 | |
| 
 | |
| import com.novelbook.android.MarkActivity;
 | |
| import com.novelbook.android.R;
 | |
| import com.novelbook.android.ReadActivity;
 | |
| import com.novelbook.android.adapter.CatalogueAdapter;
 | |
| import com.novelbook.android.db.Chapter;
 | |
| import com.novelbook.android.db.DownloadTask;
 | |
| import com.novelbook.android.db.Novel;
 | |
| import com.novelbook.android.netutils.NetUtil;
 | |
| import com.novelbook.android.utils.PageFactory;
 | |
| import com.umeng.analytics.MobclickAgent;
 | |
| 
 | |
| 
 | |
| import org.litepal.LitePal;
 | |
| 
 | |
| import java.io.File;
 | |
| import java.util.ArrayList;
 | |
| 
 | |
| import butterknife.BindView;
 | |
| import butterknife.ButterKnife;
 | |
| import butterknife.OnClick;
 | |
| 
 | |
| /**
 | |
|  * Created by Administrator on 2016/8/31 0031.
 | |
|  */
 | |
| public class CatalogFragment extends BasicFragment implements MarkActivity.Sortcat {
 | |
|     public static final String TAG=CatalogFragment.class.getSimpleName();
 | |
|     public static final String ARGUMENT = "argument";
 | |
| 
 | |
|     private PageFactory pageFactory;
 | |
|     ArrayList<Chapter> catalogueList = new ArrayList<>();
 | |
|     ArrayList<Integer> catalogCached = new ArrayList<>();
 | |
|     @BindView(R.id.lv_catalogue)
 | |
|     ListView lv_catalogue;
 | |
|     @BindView(R.id.btnRefresh)
 | |
|     Button btnRefresh;
 | |
| /*    @BindView(R.id.tvTitle)
 | |
|     TextView tvTitle;
 | |
|     @BindView(R.id.tvChapts)
 | |
|     TextView tvChapters;
 | |
|     @BindView(R.id.btnSort)
 | |
|     ImageButton ibSort;*/
 | |
| 
 | |
|     CatalogueAdapter catalogueAdapter;
 | |
|     protected   void processArguments(){
 | |
|         if (getArguments() != null) {
 | |
|             Bundle bundle = getArguments() ;
 | |
| 
 | |
| 
 | |
| 
 | |
|         }
 | |
|     }
 | |
|     boolean isAsc = true;
 | |
|     boolean isFirstLoad = true;
 | |
|     @Override
 | |
|     protected  void fillData() {
 | |
|         catalogueList.clear();
 | |
|         catalogueList.addAll(pageFactory.getChapters());
 | |
| 
 | |
|        // int currentChp =pageFactory.getCurrentChapter()-1;
 | |
|         int currentChp =pageFactory.getNovle().getLastReadChapt()-1;
 | |
|         if(!isAsc){
 | |
|             catalogueList =revertArray();
 | |
|             currentChp = catalogueList.size() -currentChp-1;
 | |
| 
 | |
|         }
 | |
|         catalogueAdapter.setData(catalogueList);
 | |
|         catalogueAdapter.setCharter(currentChp+1);
 | |
|         catalogueAdapter.setCahedChapters(catalogCached);
 | |
|         catalogueAdapter.notifyDataSetChanged();
 | |
| 
 | |
|        int count= lv_catalogue.getCount();
 | |
|        if(count >=pageFactory.getCurrentChapter()&& isFirstLoad){
 | |
|            lv_catalogue.setSelection(currentChp);
 | |
|            isFirstLoad =false;
 | |
|        }
 | |
| 
 | |
|       /* tvTitle.setText(pageFactory.getBookName());
 | |
|        tvChapters.setText(String.format("共%s章",catalogueList.size()));*/
 | |
| 
 | |
|       if(catalogueList.size() ==0) {
 | |
|           btnRefresh.setVisibility(View.VISIBLE);
 | |
|       }else btnRefresh.setVisibility(View.GONE);
 | |
| 
 | |
|        
 | |
|     }
 | |
| 
 | |
| @OnClick(R.id.btnRefresh)
 | |
|     void refresh(View view) {
 | |
|     pageFactory.refreshCate();
 | |
|     loadData();
 | |
|     btnRefresh.setVisibility(View.GONE);
 | |
| }
 | |
| 
 | |
|     ArrayList<Chapter> revertArray(){
 | |
|         ArrayList<Chapter> tmp = new ArrayList<>();
 | |
| 
 | |
|         for(int i = catalogueList.size()-1;i>=0;i--){
 | |
|             tmp.add(catalogueList.get(i));
 | |
| 
 | |
|         }
 | |
| 
 | |
|         return tmp;
 | |
|  
 | |
|     }
 | |
| 
 | |
|    /* @OnClick(R.id.btnSort)
 | |
|     void Onclick(View view){
 | |
|         if(view.getId() == R.id.btnSort){
 | |
|             Log.d(TAG, "Onclick: sorted");
 | |
|             catalogueList = getArray();
 | |
|             catalogueAdapter.notifyDataSetChanged();
 | |
|         }
 | |
|         
 | |
|     }*/
 | |
|     @Override
 | |
|     protected int getLayoutRes() {
 | |
|         return R.layout.fragment_catalog;
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     protected void initData() {
 | |
| 
 | |
|         pageFactory = PageFactory.getInstance(activity.getApplicationContext());
 | |
|        loadData();
 | |
| 
 | |
| 
 | |
|     }
 | |
| 
 | |
|     public void loadData(){
 | |
|         showProgressDialog(false,"正在读取目录数据,请稍等");
 | |
| 
 | |
|         catalogueList.clear();
 | |
|         catalogueAdapter = new CatalogueAdapter(getContext(), catalogueList);
 | |
| 
 | |
|         lv_catalogue.setAdapter(catalogueAdapter);
 | |
|         Log.d(TAG, String .format("prepare book: start to open book cate of  %s" ,  pageFactory.getNovle().getName()));
 | |
| 
 | |
|         new Thread() {
 | |
|             @Override
 | |
|             public void run() {
 | |
|              //   ArrayList<Chapter> list  =(ArrayList<Chapter>) LitePal.where("novelId=? and domain =? " ,pageFactory.getNovle().getId()+"",pageFactory.getSite().getDomain()).find(Chapter.class);
 | |
| 
 | |
|                 String domain =pageFactory.getNovle().getDomain();
 | |
|                 if(TextUtils.isEmpty(domain)){
 | |
|                   domain="";
 | |
|                 }
 | |
| 
 | |
|                 ArrayList<Chapter> list  = new ArrayList<Chapter>();
 | |
|                 if(!TextUtils.isEmpty(domain)) {
 | |
|                     list = (ArrayList<Chapter>) LitePal.where("novelId=? and domain =? ", pageFactory.getNovle().getId() + "", domain).find(Chapter.class);
 | |
|                   }
 | |
|                 File file;
 | |
|                 for(Chapter cp : list){
 | |
|                     if(!TextUtils.isEmpty(cp.getChapterPath())) {
 | |
|                         String filename =pageFactory.getChapterFileName(cp.getIndex());
 | |
|                         file = new File(filename);
 | |
|                         if(file.exists()) {
 | |
|                             Log.d(TAG, String .format("prepare book: load cats,chapter %s ,id %s,file %s is exist" ,cp.getChapterName(),cp.getIndex() , filename));
 | |
| 
 | |
|                             catalogCached.add(cp.getIndex());
 | |
|                         }
 | |
|                     }
 | |
|                 }
 | |
| 
 | |
|                 int maxSlept =NetUtil.isNetworkConnected()?250 :100;
 | |
|                 int slepttime = 0;
 | |
|                 while (  slepttime < maxSlept && ( pageFactory.isReadingCatalogs()  || pageFactory.getChapters().size() == 0)) {
 | |
|                     try {
 | |
|                         Log.d(TAG, String .format("prepare book:  to open book cate to sleep   %s" , 50));
 | |
|                         sleep(50);
 | |
|                         slepttime++;
 | |
|                     } catch (InterruptedException e) {
 | |
| 
 | |
|                         e.printStackTrace();
 | |
|                     }
 | |
|                 }
 | |
|                 Log.d(TAG, String .format("prepare book:to open book cate slept  %s" , slepttime*50));
 | |
|               /*  if ( !pageFactory.isReadingCatalogs() && pageFactory.getChapters().size() == 0) {
 | |
| 
 | |
|                     handler.sendEmptyMessage(2);
 | |
|                 }else{
 | |
|                     handler.sendEmptyMessage(1);
 | |
|                 }*/
 | |
|                 handler.sendEmptyMessage(1);
 | |
| 
 | |
|             }
 | |
|         }.start();
 | |
| 
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     protected void initListener() {
 | |
|         lv_catalogue.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 | |
|             @Override
 | |
|             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 | |
|               //  showProgressDialog(true,"请稍等");
 | |
|                 Log.d(TAG, String .format("prepare book: getPageWidget is null? %s",pageFactory.getPageWidget()==null));
 | |
|              //   if(pageFactory.getPageWidget()==null){
 | |
| 
 | |
|                   Novel novel = pageFactory.getNovle();
 | |
|                   Chapter cp = catalogueList.get(position);
 | |
|                 Log.d(TAG, String .format("prepare book: catalog list postion %s, cp.index %s ,cp.name %s ",position,cp.getIndex(),cp.getChapterName()));
 | |
|                   if(novel!=null){
 | |
| 
 | |
|                       novel.setLastReadChapt(cp.getIndex());
 | |
|                       novel.setLastReadPos(1);
 | |
|                       ReadActivity.openBook(novel, getActivity());
 | |
| 
 | |
|             }
 | |
| 
 | |
| 
 | |
|                 getActivity().finish();
 | |
| 
 | |
|             }
 | |
|         });
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     protected void initViews() {
 | |
|         lv_catalogue.setFastScrollEnabled(true);
 | |
|         ((MarkActivity) getActivity()).setSortcat(this);
 | |
| 
 | |
|         //lv_catalogue.setFastScrollStyle(R.style.FastScrollTheme); //不起作用
 | |
|     }
 | |
|     void initSwipeRefreshLayout(){
 | |
|         super.initSwipeRefreshLayout();
 | |
|         mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
 | |
|             @Override
 | |
|             public void onRefresh() {
 | |
| 
 | |
|                 // 开始刷新,设置当前为刷新状态
 | |
|                 //swipeRefreshLayout.setRefreshing(true);
 | |
|                // isAsc =true;
 | |
|                 refresh(btnRefresh);
 | |
| 
 | |
|             }
 | |
|         });
 | |
|     }
 | |
|     @Override
 | |
|     public void setFTag() {
 | |
| 
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * 用于从Activity传递数据到Fragment
 | |
|      * @param bookpath
 | |
|      * @return
 | |
|      */
 | |
|     public static CatalogFragment newInstance(String bookpath)
 | |
|     {
 | |
|         Bundle bundle = new Bundle();
 | |
|         bundle.putString(ARGUMENT, bookpath);
 | |
|         CatalogFragment catalogFragment = new CatalogFragment();
 | |
|         catalogFragment.setArguments(bundle);
 | |
|         return catalogFragment;
 | |
|     }
 | |
| 
 | |
| 
 | |
|     @Override
 | |
|     public void sortList() {
 | |
|         isAsc =!isAsc;
 | |
| 
 | |
|         fillData();
 | |
| 
 | |
| 
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     public void refresh() {
 | |
|         refresh(null);
 | |
|     }
 | |
| 
 | |
|     @Override
 | |
|     public void onResume() {
 | |
|         super.onResume();
 | |
|         MobclickAgent.onPageStart(TAG);
 | |
|     }
 | |
|     @Override
 | |
|     public void onPause(){
 | |
|         super.onPause();
 | |
|         MobclickAgent.onPageEnd(TAG);
 | |
|     }
 | |
| }
 |