884 lines
		
	
	
		
			30 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
		
		
			
		
	
	
			884 lines
		
	
	
		
			30 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
|  | package com.novelbook.android.Fragments; | |||
|  | 
 | |||
|  | import android.app.Activity; | |||
|  | import android.app.AlertDialog; | |||
|  | import android.content.Context; | |||
|  | import android.content.DialogInterface; | |||
|  | import android.content.Intent; | |||
|  | import android.os.Bundle; | |||
|  | import android.support.annotation.Nullable; | |||
|  | import android.support.design.widget.BottomSheetDialog; | |||
|  | import android.support.v4.widget.SwipeRefreshLayout; | |||
|  | import android.support.v7.widget.GridLayoutManager; | |||
|  | import android.support.v7.widget.RecyclerView; | |||
|  | import android.text.TextUtils; | |||
|  | import android.util.Log; | |||
|  | import android.view.KeyEvent; | |||
|  | import android.view.LayoutInflater; | |||
|  | import android.view.View; | |||
|  | import android.view.ViewGroup; | |||
|  | import android.widget.Button; | |||
|  | import android.widget.CheckBox; | |||
|  | import android.widget.CompoundButton; | |||
|  | import android.widget.FrameLayout; | |||
|  | import android.widget.ImageView; | |||
|  | import android.widget.LinearLayout; | |||
|  | import android.widget.RadioButton; | |||
|  | import android.widget.RadioGroup; | |||
|  | import android.widget.TextView; | |||
|  | import android.widget.Toast; | |||
|  | 
 | |||
|  | /*import com.androidquery.callback.AQuery2; | |||
|  | import com.novelbook.android.AD.toutiao.TTAdManagerHolder; | |||
|  | import com.novelbook.android.BuildConfig;*/ | |||
|  | import com.novelbook.android.Main2Activity; | |||
|  | import com.novelbook.android.R; | |||
|  | import com.novelbook.android.db.Chapter; | |||
|  | import com.novelbook.android.db.DownloadTask; | |||
|  | import com.novelbook.android.db.Novel; | |||
|  | 
 | |||
|  | import com.novelbook.android.db.SiteRule; | |||
|  | import com.novelbook.android.filechooser.FileChooserActivity; | |||
|  | import com.novelbook.android.netsubscribe.BookSubscribe; | |||
|  | import com.novelbook.android.netutils.HttpMethods; | |||
|  | import com.novelbook.android.netutils.NetUtil; | |||
|  | import com.novelbook.android.netutils.OnSuccessAndFaultListener; | |||
|  | import com.novelbook.android.netutils.OnSuccessAndFaultSub; | |||
|  | import com.novelbook.android.utils.CommonUtil; | |||
|  | import com.novelbook.android.utils.Config; | |||
|  | import com.novelbook.android.utils.Constants; | |||
|  | import com.novelbook.android.utils.FileUtils; | |||
|  | import com.novelbook.android.utils.Fileutil; | |||
|  | import com.novelbook.android.utils.GsonUtil; | |||
|  | import com.novelbook.android.utils.ImageUtil; | |||
|  | import com.novelbook.android.utils.MyImageLoader; | |||
|  | import com.novelbook.android.utils.OnItemClickListener; | |||
|  | import com.novelbook.android.adapter.BookListAdapter; | |||
|  | import com.umeng.analytics.MobclickAgent; | |||
|  | 
 | |||
|  | import org.json.JSONObject; | |||
|  | import org.litepal.LitePal; | |||
|  | 
 | |||
|  | import java.io.File; | |||
|  | import java.io.IOException; | |||
|  | import java.util.ArrayList; | |||
|  | import java.util.Date; | |||
|  | import java.util.List; | |||
|  | 
 | |||
|  | import butterknife.BindView; | |||
|  | import butterknife.ButterKnife; | |||
|  | import butterknife.OnClick; | |||
|  | import okhttp3.Call; | |||
|  | import okhttp3.Callback; | |||
|  | import okhttp3.Request; | |||
|  | import okhttp3.Response; | |||
|  | import okhttp3.ResponseBody; | |||
|  | 
 | |||
|  | 
 | |||
|  | public class Fragment_Shelf extends BasicFragment { | |||
|  |     public static final String TAG = Fragment_Shelf.class.getSimpleName(); | |||
|  |     private boolean[] flag;//此处添加一个boolean类型的数组 | |||
|  |     private boolean isSelectAll = true; | |||
|  |     private MyImageLoader loader = new MyImageLoader(); | |||
|  |     @BindView(R.id.llShelfBottom) | |||
|  |     LinearLayout llShelfBottom; | |||
|  |     @BindView(R.id.id_recyclerview) | |||
|  |     RecyclerView mRecyclerView; | |||
|  |     // private List<Novel> mDatas; | |||
|  |     private BookListAdapter mAdapter; | |||
|  |     List<Novel> lstUpdate = new ArrayList<Novel>(); | |||
|  |     private List<Novel> bookLists; | |||
|  |     // private ShelfAdapter adapter; | |||
|  |     private String noveIds; | |||
|  |     BottomSheetDialog bottomSheetDialog; | |||
|  | 
 | |||
|  |     @BindView(R.id.banner_container) | |||
|  |     FrameLayout mBannerContainer; | |||
|  | 
 | |||
|  |     private  Config config  ; | |||
|  |     public Fragment_Shelf() { | |||
|  |         // Required empty public constructor | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     public static Fragment_Shelf newInstance(String param1, String param2) { | |||
|  |         Fragment_Shelf fragment = new Fragment_Shelf(); | |||
|  |         Bundle args = new Bundle(); | |||
|  | 
 | |||
|  |         fragment.setArguments(args); | |||
|  |         return fragment; | |||
|  |     } | |||
|  | 
 | |||
|  |     private void loadNovelsOnShelf() { | |||
|  | 
 | |||
|  |         Log.d(TAG, String.format("db count info: chapters %s,novels %s,downtask  %s,siterule %s,maxsize %s", | |||
|  |                 LitePal.count(Chapter.class),  LitePal.count(Novel.class),LitePal.count(DownloadTask.class),LitePal.count(SiteRule.class) | |||
|  |                 ,Fileutil.formatFileSize( LitePal.getDatabase().getMaximumSize()) | |||
|  |         )); | |||
|  | 
 | |||
|  |      /*   try{ | |||
|  |             throw new Exception("show trace"); | |||
|  |         }catch (Exception e){ | |||
|  |             Log.e(TAG, "loadNovelsOnShelf: ",e); | |||
|  |         }*/ | |||
|  | 
 | |||
|  |         bookLists = Novel.getNovelsOnShelf(); | |||
|  |         noveIds = ""; | |||
|  |         int i=0; | |||
|  |         for (Novel novel : bookLists) { | |||
|  |              if (novel.isFinished() || novel.isLocalBook() || novel.isUpdated()  && (new Date().getTime() - novel.getLastVisit() > 1*3600*1000 ) ) { | |||
|  |                 //if(!novel.getName().equals("剑来")) | |||
|  |                 continue; | |||
|  |             } | |||
|  |             if(i>Constants.MAX_SHELF_CNT){ | |||
|  |                 break; | |||
|  |             } | |||
|  |             noveIds += novel.getNovelId() + ","; | |||
|  |             i++; | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     void test(int maxAge) { | |||
|  | 
 | |||
|  |         String url = "http://xiaoshuofenxiang.com/api/r/need-update"; | |||
|  |         Request request = new Request.Builder().url(url)   // .header("User-Agent", "OkHttp Example") | |||
|  |                 .removeHeader("Pragma") | |||
|  |                 .header("Cache-Control", "public, max-age=" + maxAge) | |||
|  |                 .build(); | |||
|  | 
 | |||
|  |         if (maxAge == -1) { | |||
|  |             request = new Request.Builder().url(url).build(); | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         HttpMethods.getOkClient().newCall(request).enqueue(new Callback() { | |||
|  |             @Override | |||
|  |             public void onFailure(Call call, IOException e) { | |||
|  | 
 | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onResponse(Call call, Response response) throws IOException { | |||
|  |                 ResponseBody body = response.body(); | |||
|  |                 Log.d(TAG, "onResponse:test " + body.string()); | |||
|  |                 handler.sendEmptyMessage(1); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     protected int getLayoutRes() { | |||
|  |         return R.layout.fragment_fragment__shelf; | |||
|  |     } | |||
|  | 
 | |||
|  |     /** | |||
|  |      * to get updated info from server,TODO: put it in service ,scheduled | |||
|  |      */ | |||
|  |     private void getUpdatedData() { | |||
|  | 
 | |||
|  |         loadNovelsOnShelf(); | |||
|  |         List<Integer> novelIds; | |||
|  |         OnSuccessAndFaultListener successAndFaultListener = new OnSuccessAndFaultListener() { | |||
|  |             @Override | |||
|  |             public void onSuccess(String result) { | |||
|  | 
 | |||
|  |                 // mFirstPage= gson.fromJson(result, FirstPage.class); | |||
|  |                 try { | |||
|  | 
 | |||
|  |                     lstUpdate = GsonUtil.parserJsonArray(result, Constants.BLOCK_TITLE_NOVELS); | |||
|  |                     if (lstUpdate.size() > 0) { | |||
|  |                         for (Novel novel2 : lstUpdate) { | |||
|  |                             novel2.checkAndUpdate(); | |||
|  |                         } | |||
|  |                     } | |||
|  |                    loadNovelsOnShelf(); | |||
|  | 
 | |||
|  |                    /* for(Novel nove : bookLists){ | |||
|  | 
 | |||
|  |                         nove.testUpdate(); | |||
|  |                     }*/ | |||
|  | 
 | |||
|  |                 } catch (Exception e) { | |||
|  |                     e.printStackTrace(); | |||
|  |                 } | |||
|  |                 handler.sendEmptyMessage(1); | |||
|  | 
 | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onFault(String errorMsg) { | |||
|  |                 //失败 | |||
|  |                 Log.d(TAG, "error on get novel update list: " + errorMsg); | |||
|  |                 handler.sendEmptyMessage(1); | |||
|  |             } | |||
|  |         }; | |||
|  | 
 | |||
|  | 
 | |||
|  |         if (TextUtils.isEmpty(noveIds)) { | |||
|  |             loadNovelsOnShelf(); | |||
|  |         } | |||
|  |         if (!TextUtils.isEmpty(noveIds)) { | |||
|  |             BookSubscribe.getNovelsByIds(noveIds, new OnSuccessAndFaultSub(successAndFaultListener, getActivity())); | |||
|  |         } else { | |||
|  |             handler.sendEmptyMessage(1); | |||
|  |         } | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void initData() { | |||
|  | 
 | |||
|  | 
 | |||
|  |         // mDatas = initData(mDatas,'X'); | |||
|  | 
 | |||
|  |         // bookLists = LitePal.where("isOnShelf=? or novelId=? ","1","").find(Novel.class); | |||
|  |         loadNovelsOnShelf(); | |||
|  |         // getUpdatedData(); | |||
|  |         flag = new boolean[bookLists.size()]; | |||
|  | 
 | |||
|  |        /* if(bookLists.size()>0) { //TODO: to remove | |||
|  |             bookLists.get(0).setUpdated(true); | |||
|  |            // bookLists.get(bookLists.size()-1).setUpdated(true); | |||
|  |         }*/ | |||
|  | 
 | |||
|  |         mAdapter = new BookListAdapter(activity, bookLists, R.layout.recycle_list_item, new OnItemClickListener() { | |||
|  |             private long lastTimeMillis; | |||
|  |             private static final long MIN_CLICK_INTERVAL = 1000; | |||
|  | 
 | |||
|  |             protected boolean isTimeEnabled() { | |||
|  |                 long currentTimeMillis = System.currentTimeMillis(); | |||
|  |                 if ((currentTimeMillis - lastTimeMillis) > MIN_CLICK_INTERVAL) { | |||
|  |                     lastTimeMillis = currentTimeMillis; | |||
|  |                     return true; | |||
|  |                 } | |||
|  |                 return false; | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onItemClick(View view, int position) { | |||
|  |                 if (isTimeEnabled()) { | |||
|  |                     Novel book = bookLists.get(position); | |||
|  |                     openBook(book, mAdapter); | |||
|  |                 } else { | |||
|  |                     Log.d(TAG, "onclickInterval: bad click"); | |||
|  |                 } | |||
|  | 
 | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onItemLongClick(View view, int position) { | |||
|  |                 initDialog(position); | |||
|  |                 // mAdapter.removeData(position); | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onLinearOutClick(View view, int position, int llId) { | |||
|  | 
 | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |         mAdapter.setNorecord(R.string.noRecordInshelf); | |||
|  | 
 | |||
|  | 
 | |||
|  |         ((Main2Activity) activity).setShelfFragment(this); | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     void initSwipeRefreshLayout() { | |||
|  |         super.initSwipeRefreshLayout(); | |||
|  |         mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { | |||
|  |             @Override | |||
|  |             public void onRefresh() { | |||
|  | 
 | |||
|  |                 // 开始刷新,设置当前为刷新状态 | |||
|  |                 //swipeRefreshLayout.setRefreshing(true); | |||
|  |                 pageNo = 1; | |||
|  |                 getUpdatedData(); | |||
|  | 
 | |||
|  | 
 | |||
|  |             } | |||
|  |         }); | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void initViews() { | |||
|  | 
 | |||
|  |         initReceyleView(); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     protected void processArguments() { | |||
|  |         if (getArguments() != null) { | |||
|  |             Bundle bundle = getArguments(); | |||
|  | 
 | |||
|  | 
 | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     protected void fillData() { | |||
|  |        /* for(Novel nv :bookLists){ | |||
|  |             nv.testUpdate(); | |||
|  |         }*/ | |||
|  | 
 | |||
|  |         mAdapter.setData(bookLists); | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     public void initReceyleView() { | |||
|  |         // initData(); | |||
|  |         //  mRecyclerView.setLayoutManager(new LinearLayoutManager(this.activity)); | |||
|  |         mRecyclerView.setLayoutManager(new GridLayoutManager(activity, 3)); | |||
|  |         mRecyclerView.setAdapter(mAdapter); | |||
|  |         llShelfBottom.setVisibility(View.GONE); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  |     public void showShelfMoreSheet() { | |||
|  |         BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this.activity); | |||
|  |         bottomSheetDialog.setContentView(R.layout.fragment_shelf_more_sheet); | |||
|  | //给布局设置透明背景色 | |||
|  |         bottomSheetDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet) | |||
|  |                 .setBackgroundColor(getResources().getColor(android.R.color.transparent)); | |||
|  | 
 | |||
|  |         Button btnZhengli = bottomSheetDialog.findViewById(R.id.btnZhengli); | |||
|  |         btnZhengli.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 bottomSheetDialog.dismiss(); | |||
|  |                 initZhengliDialog(); | |||
|  |             } | |||
|  |         }); | |||
|  |         Button btnImport = bottomSheetDialog.findViewById(R.id.btnImport); | |||
|  |         btnImport.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 bottomSheetDialog.dismiss(); | |||
|  |                 Intent intent = new Intent(activity, FileChooserActivity.class); | |||
|  |                 startActivityForResult(intent, Activity.RESULT_FIRST_USER); | |||
|  |                 //  startActivity(intent); | |||
|  |             } | |||
|  |         }); | |||
|  |         Button btnCancel = bottomSheetDialog.findViewById(R.id.btnCancel); | |||
|  |         btnCancel.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 bottomSheetDialog.dismiss(); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |         if(config==null){ | |||
|  |             config =Config.getInstance(); | |||
|  |         } | |||
|  | 
 | |||
|  |         RadioGroup rgShelfOrder =  bottomSheetDialog.findViewById(R.id.rgShelfOrder); | |||
|  |         Constants.SHELF_ORDER = config.getShelfOrderOption(); | |||
|  |         rgShelfOrder.clearCheck(); | |||
|  |         ((RadioButton) rgShelfOrder.getChildAt(Constants.SHELF_ORDER-1)).setChecked(true); | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |         RadioButton radioButton1 =  bottomSheetDialog.findViewById(R.id.radioButton1); | |||
|  |         RadioButton radioButton2 =  bottomSheetDialog.findViewById(R.id.radioButton2); | |||
|  |         RadioButton radioButton3 =  bottomSheetDialog.findViewById(R.id.radioButton3); | |||
|  |         RadioButton radioButton4 =  bottomSheetDialog.findViewById(R.id.radioButton4); | |||
|  |         radioButton1.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 setShelfOrder(1); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |         radioButton2.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 setShelfOrder(2); | |||
|  |             } | |||
|  |         }); | |||
|  |         radioButton3.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 setShelfOrder(3); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  |         radioButton4.setOnClickListener(new View.OnClickListener() { | |||
|  |             @Override | |||
|  |             public void onClick(View view) { | |||
|  |                 setShelfOrder(4); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  | 
 | |||
|  |         bottomSheetDialog.show(); | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     } | |||
|  |     private void setShelfOrder(int option) { | |||
|  | 
 | |||
|  |         Log.d(TAG, "setShelfOrder: new sex option:" +option); | |||
|  |         if(option!= Constants.SHELF_ORDER) { | |||
|  |             Constants.SHELF_ORDER = option; | |||
|  |             config.setShelfOrderOption(option); | |||
|  |             getUpdatedData(); | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onActivityResult(int requestCode, int resultCode, Intent data) { | |||
|  |         //  bookLists = LitePal.findAll(Novel.class); | |||
|  |         //   mAdapter.notifyDataSetChanged(); | |||
|  |         initViews(); | |||
|  |     } | |||
|  | 
 | |||
|  |     private void initDialog(int position) { | |||
|  | 
 | |||
|  |         bottomSheetDialog = new BottomSheetDialog(this.activity); | |||
|  |         bottomSheetDialog.setContentView(R.layout.fragment_shelf_botoomsheetdialog); | |||
|  | //给布局设置透明背景色 | |||
|  |         bottomSheetDialog.getDelegate().findViewById(android.support.design.R.id.design_bottom_sheet) | |||
|  |                 .setBackgroundColor(getResources().getColor(android.R.color.transparent)); | |||
|  | 
 | |||
|  |         TextView tvTitle = (TextView) bottomSheetDialog.findViewById(R.id.bdTitle); | |||
|  |         TextView tvLatestChapt = (TextView) bottomSheetDialog.findViewById(R.id.txtLatestCate); | |||
|  |         TextView tvAuthor = (TextView) bottomSheetDialog.findViewById(R.id.tvAuthor); | |||
|  |         tvTitle.setText(bookLists.get(position).getName()); | |||
|  |         if (!bookLists.get(position).isLocalBook()) { | |||
|  | 
 | |||
|  |             tvAuthor.setText(bookLists.get(position).getAuthor()); | |||
|  |             TextView tvLatest = (TextView) bottomSheetDialog.findViewById(R.id.txtLatestUpdate); | |||
|  | 
 | |||
|  |             tvLatestChapt.setText(bookLists.get(position).getChapterName()); | |||
|  |             tvLatest.setText(CommonUtil.getDateString(bookLists.get(position).getLastUpdateTime())); | |||
|  | 
 | |||
|  |             TextView tvLastVisit = (TextView) bottomSheetDialog.findViewById(R.id.txtLatestVisit);; | |||
|  |             tvLastVisit.setText( CommonUtil.getDateString( bookLists.get(position).getLastVisit())); | |||
|  |             TextView tvLastReadChpapt = (TextView) bottomSheetDialog.findViewById(R.id.txtLatestReadChapter);; | |||
|  |             tvLastReadChpapt.setText(   bookLists.get(position).getLastReadChaptName()) ; | |||
|  | 
 | |||
|  |         } else { | |||
|  |             tvLatestChapt.setText("本地导入"); | |||
|  |             tvAuthor.setText(""); | |||
|  |         } | |||
|  | 
 | |||
|  |         ImageView imageView = (ImageView) bottomSheetDialog.findViewById(R.id.imageG1); | |||
|  |         ImageUtil.loadImage(activity, bookLists.get(position).getCover(), imageView); | |||
|  | 
 | |||
|  |         LinearLayout lldetail = (LinearLayout) bottomSheetDialog.findViewById(R.id.llBookdetail); | |||
|  |         if (!bookLists.get(position).isLocalBook()) | |||
|  |             lldetail.setOnClickListener(new View.OnClickListener() { | |||
|  |                 public void onClick(View v) { | |||
|  |                     showBookDetail(bookLists.get(position)); | |||
|  |                 } | |||
|  |             }); | |||
|  |         TextView tvCache = (TextView) bottomSheetDialog.findViewById(R.id.tvCache); | |||
|  | 
 | |||
|  |         tvCache.setText(FileUtils.getCacheSizeK(bookLists.get(position).getId())); | |||
|  | 
 | |||
|  |         if (!bookLists.get(position).isLocalBook()) { | |||
|  |             LinearLayout llClearCache = (LinearLayout) bottomSheetDialog.findViewById(R.id.llClearCache); | |||
|  | 
 | |||
|  |             llClearCache.setOnClickListener(new View.OnClickListener() { | |||
|  |                 public void onClick(View v) { | |||
|  |                     FileUtils.clearChapterCache(bookLists.get(position).getId()); | |||
|  |                     long size = FileUtils.getCacheSize(bookLists.get(position).getId()); | |||
|  |                     size = size / 1024; | |||
|  |                     tvCache.setText(String.format("%sK", size)); | |||
|  |                     Toast.makeText(activity, "缓存清除成功", Toast.LENGTH_SHORT); | |||
|  | 
 | |||
|  |                 } | |||
|  |             }); | |||
|  |         } else { | |||
|  |             TextView tvClearCache = (TextView) bottomSheetDialog.findViewById(R.id.tvClearCache); | |||
|  |             tvClearCache.setText("导入文本"); | |||
|  |         } | |||
|  | 
 | |||
|  |         LinearLayout lltop = (LinearLayout) bottomSheetDialog.findViewById(R.id.llTop); | |||
|  |         TextView tvTop = (TextView) bottomSheetDialog.findViewById(R.id.tvTop); | |||
|  |         ImageView imgTop = (ImageView) bottomSheetDialog.findViewById(R.id.imageTop); | |||
|  |         if (bookLists.get(position).isTop()) { | |||
|  | 
 | |||
|  |             tvTop.setText("取消置顶"); | |||
|  |             imgTop.setImageResource(R.mipmap.star_inactive); | |||
|  | 
 | |||
|  |         } else { | |||
|  |             tvTop.setText("置顶显示"); | |||
|  |             imgTop.setImageResource(R.mipmap.star_active); | |||
|  |         } | |||
|  | 
 | |||
|  |         lltop.setOnClickListener(new View.OnClickListener() { | |||
|  |             public void onClick(View v) { | |||
|  |                 if (bookLists.get(position).isTop()) { | |||
|  |                     bookLists.get(position).setToDefault("isTop"); | |||
|  |                     tvTop.setText("置顶显示"); | |||
|  |                     imgTop.setImageResource(R.mipmap.star_active); | |||
|  | 
 | |||
|  |                 } else { | |||
|  |                     bookLists.get(position).setTop(true); | |||
|  |                     tvTop.setText("取消置顶"); | |||
|  |                     imgTop.setImageResource(R.mipmap.star_inactive); | |||
|  |                 } | |||
|  | 
 | |||
|  |                 bookLists.get(position).update(bookLists.get(position).getId()); | |||
|  | 
 | |||
|  |                 loadNovelsOnShelf(); | |||
|  |                 mAdapter.setData(bookLists); | |||
|  | 
 | |||
|  |                 //Toast.makeText(activity,"缓存清除成功",Toast.LENGTH_SHORT); | |||
|  |             } | |||
|  |         }); | |||
|  | 
 | |||
|  | 
 | |||
|  |         bottomSheetDialog.show(); | |||
|  |     } | |||
|  | 
 | |||
|  |     @BindView(R.id.btnDelete) | |||
|  |     Button btnDelete; | |||
|  |     @BindView(R.id.btnSelect) | |||
|  |     Button btnSelectAll; | |||
|  | 
 | |||
|  |     @OnClick({R.id.btnSelect, R.id.btnDelete}) | |||
|  |     void shelfZhengliSubmit(View view) { | |||
|  | 
 | |||
|  |         if (view.getId() == R.id.btnSelect) { | |||
|  | 
 | |||
|  |             if (isSelectAll) { | |||
|  |                 btnSelectAll.setText("全不选"); | |||
|  | 
 | |||
|  |             } else { | |||
|  |                 btnSelectAll.setText("全选"); | |||
|  |             } | |||
|  |             for (int i = 0; i < flag.length; i++) { | |||
|  |                 flag[i] = isSelectAll; | |||
|  |             } | |||
|  |             isSelectAll = !isSelectAll; | |||
|  |             mChkAdapter.notifyDataSetChanged(); | |||
|  |         } else if (view.getId() == R.id.btnDelete) { | |||
|  | 
 | |||
|  |             boolean isChecked = false; | |||
|  |             for(boolean b :flag){ | |||
|  |                 if(b) { | |||
|  |                     isChecked = true; | |||
|  |                     break; | |||
|  |                 } | |||
|  |             } | |||
|  |             if(!isChecked){ | |||
|  |                 new AlertDialog.Builder(activity) | |||
|  |                         .setTitle(this.getString(R.string.app_name)) | |||
|  |                         .setMessage( "您还没有选择要移除的书籍") | |||
|  |                         .setNegativeButton("确定", new DialogInterface.OnClickListener() { | |||
|  |                             @Override | |||
|  |                             public void onClick(DialogInterface dialog, int which) { | |||
|  |                                 return; | |||
|  | 
 | |||
|  | 
 | |||
|  |                             } | |||
|  |                         }) | |||
|  | 
 | |||
|  |                         .setCancelable(false).show(); | |||
|  |                 return; | |||
|  |             } | |||
|  |             new AlertDialog.Builder(activity) | |||
|  |                     .setTitle(this.getString(R.string.app_name)) | |||
|  |                     .setMessage( "您将从书架移除选中的书籍,是否继续?") | |||
|  |                     .setNegativeButton("取消", new DialogInterface.OnClickListener() { | |||
|  |                         @Override | |||
|  |                         public void onClick(DialogInterface dialog, int which) { | |||
|  |                             return; | |||
|  | 
 | |||
|  | 
 | |||
|  |                         } | |||
|  |                     }) | |||
|  |                     .setPositiveButton("确定", new DialogInterface.OnClickListener() { | |||
|  |                         @Override | |||
|  |                         public void onClick(DialogInterface dialog, int which) { | |||
|  |                             for (int i = 0; i < bookLists.size(); i++) { | |||
|  |                                 if (flag[i]) { | |||
|  |                                     Novel nv = bookLists.get(i); | |||
|  |                                     if (nv.isLocalBook()) { | |||
|  | 
 | |||
|  |                                         nv.delete(); | |||
|  |                                         LitePal.deleteAllAsync(Chapter.class, "novelId=?", nv.getId() + ""); | |||
|  |                                         //TODO: remove cache on a new thread | |||
|  |                                         FileUtils.clearChapterCache(nv.getId()); | |||
|  |                                     } else { | |||
|  |                                         Log.d(TAG, "shelfZhengliSubmit: to delete novel " + nv.getName()); | |||
|  |                                         nv.setToDefault("isOnShelf"); | |||
|  |                                         nv.setToDefault("isTop"); | |||
|  |                                         //nv.setOnShelf(false); | |||
|  |                                         nv.update(nv.getId()); | |||
|  |                                         // nv.update(nv.getId()); //not work,,,If you set a default value to a field, the corresponding  column won't be updated. | |||
|  |                                     } | |||
|  | 
 | |||
|  |                                     // flag = new boolean[bookLists.size()]; | |||
|  |                                     // bookLists.remove(i); | |||
|  |                                 } | |||
|  |                             } | |||
|  |                             btnSelectAll.setText("全选"); | |||
|  |                             bookLists = Novel.getNovelsOnShelf(); | |||
|  |                             flag = new boolean[bookLists.size()]; | |||
|  |                             zhengliShelf(); | |||
|  |                             initData(); | |||
|  |                         } | |||
|  |                     }) | |||
|  |                     .setCancelable(false).show(); | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  |          //   List<Integer> toRemove = new ArrayList<Integer>(); | |||
|  | 
 | |||
|  | 
 | |||
|  |             /*for (int i = 0; i < bookLists.size(); i++) { | |||
|  |                 if (flag[i]) { | |||
|  |                     bookLists.remove(i); | |||
|  |                     flag[i] =false; | |||
|  |                 } | |||
|  |             } | |||
|  |             */ | |||
|  | 
 | |||
|  |             // mChkAdapter.notifyDataSetChanged(); | |||
|  |         } | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     private void initZhengliDialog() { | |||
|  |         if( flag==null || flag.length ==0 ){ | |||
|  |             flag=  new boolean[bookLists.size()]; | |||
|  |         } | |||
|  |         if( flag==null || flag.length ==0 ){ | |||
|  |             new AlertDialog.Builder(activity) | |||
|  |                     .setTitle(this.getString(R.string.app_name)) | |||
|  |                     .setMessage( "您书架上还没有书籍可以整理") | |||
|  |                     .setNegativeButton("确定", new DialogInterface.OnClickListener() { | |||
|  |                         @Override | |||
|  |                         public void onClick(DialogInterface dialog, int which) { | |||
|  |                             return; | |||
|  | 
 | |||
|  | 
 | |||
|  |                         } | |||
|  |                     }) | |||
|  | 
 | |||
|  |                     .setCancelable(false).show(); | |||
|  |             return; | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         zhengliShelf(); | |||
|  |         llShelfBottom.setVisibility(View.VISIBLE); | |||
|  |         Main2Activity main2Activity = (Main2Activity) activity; | |||
|  |         main2Activity.switchShelfZhengli(false); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     CheckAdapter mChkAdapter; | |||
|  | 
 | |||
|  |     void zhengliShelf() { | |||
|  |         mChkAdapter = new CheckAdapter(activity, bookLists, R.layout.recycle_list_item, new OnItemClickListener() { | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onItemClick(View view, int position) { | |||
|  | 
 | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onItemLongClick(View view, int position) { | |||
|  | 
 | |||
|  |             } | |||
|  | 
 | |||
|  |             @Override | |||
|  |             public void onLinearOutClick(View view, int position, int llId) { | |||
|  | 
 | |||
|  |             } | |||
|  |         }); | |||
|  |         mRecyclerView.setLayoutManager(new GridLayoutManager(activity, 3)); | |||
|  |         mRecyclerView.setAdapter(mChkAdapter); | |||
|  | 
 | |||
|  |     } | |||
|  | 
 | |||
|  | 
 | |||
|  |     //--------------------整理 adapter--------------- | |||
|  | 
 | |||
|  |     class CheckAdapter extends RecyclerView.Adapter<CheckAdapter.MyViewHolder> { | |||
|  |         private final int EMPTY_VIEW = 1; | |||
|  |         private final int PROGRESS_VIEW = 2; | |||
|  |         private final int IMAGE_VIEW = 3; | |||
|  | 
 | |||
|  |         private Context context; | |||
|  |         private List<Novel> mDatas = new ArrayList<Novel>(); | |||
|  |         private OnItemClickListener mOnItemClickListener; | |||
|  |         private int listItemID; | |||
|  | 
 | |||
|  |         public CheckAdapter(Context context, List<Novel> mDatas, int listItemID, OnItemClickListener clickLitener) { | |||
|  |             this.context = context; | |||
|  |             this.mDatas = mDatas; | |||
|  |             this.mOnItemClickListener = clickLitener; | |||
|  |             this.listItemID = listItemID; | |||
|  |         } | |||
|  | 
 | |||
|  |         public CheckAdapter(Context context, OnItemClickListener clickLitener) { | |||
|  |             this.context = context; | |||
|  |             this.mOnItemClickListener = clickLitener; | |||
|  | 
 | |||
|  |         } | |||
|  | 
 | |||
|  |         @Override | |||
|  |         public int getItemViewType(int position) { | |||
|  |             if (mDatas.size() == 0) { | |||
|  |                 return EMPTY_VIEW; | |||
|  |             } else if (mDatas.get(position) == null) { | |||
|  |                 return PROGRESS_VIEW; | |||
|  |             } else { | |||
|  |                 return super.getItemViewType(position); | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         @Override | |||
|  |         public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |||
|  |             MyViewHolder holder = new MyViewHolder(LayoutInflater.from( | |||
|  |                     context).inflate(listItemID, parent, | |||
|  |                     false)); | |||
|  |             return holder; | |||
|  |         } | |||
|  | 
 | |||
|  | 
 | |||
|  |         public void setParameters(List<Novel> mDatas, int listItemID) { | |||
|  |             this.mDatas = mDatas; | |||
|  |             this.listItemID = listItemID; | |||
|  |         } | |||
|  | 
 | |||
|  |         public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) { | |||
|  |             this.mOnItemClickListener = mOnItemClickLitener; | |||
|  |         } | |||
|  | 
 | |||
|  |         @Override | |||
|  |         public void onBindViewHolder(MyViewHolder holder, int position) { | |||
|  | 
 | |||
|  |             holder.tvTitle.setText(mDatas.get(position).getName()); | |||
|  |             if (holder.tvAuthor != null) holder.tvAuthor.setText(mDatas.get(position).getAuthor()); | |||
|  |             if (holder.tvCate != null) holder.tvCate.setText(mDatas.get(position).getNovelType()); | |||
|  |             if (holder.tvDesc != null) holder.tvDesc.setText(mDatas.get(position).getDesc()); | |||
|  |             if (holder.checkBox != null) holder.checkBox.setVisibility(View.VISIBLE); | |||
|  |             if (holder.imageView != null && !TextUtils.isEmpty(mDatas.get(position).getCover())) { | |||
|  |                 loader.displayImage(context, NetUtil.getCoverUrl(mDatas.get(position).getCover()), holder.imageView); | |||
|  |             } | |||
|  |             if (mDatas.get(position).isTop()) { | |||
|  |                 holder.imageTop.setVisibility(View.VISIBLE); | |||
|  |             } else { | |||
|  |                 holder.imageTop.setVisibility(View.GONE); | |||
|  |             } | |||
|  |             // 如果设置了回调,则设置点击事件 | |||
|  |             if (mOnItemClickListener != null) { | |||
|  |                 holder.checkBox.setOnCheckedChangeListener(null);//先设置一次CheckBox的选中监听器,传入参数null | |||
|  |                 holder.checkBox.setChecked(flag[position]);//用数组中的值设置CheckBox的选中状态 | |||
|  | 
 | |||
|  |                 holder.itemView.setOnClickListener(new View.OnClickListener() { | |||
|  |                     @Override | |||
|  |                     public void onClick(View v) { | |||
|  |                         holder.checkBox.setChecked(!holder.checkBox.isChecked()); | |||
|  |                     } | |||
|  |                 }); | |||
|  | 
 | |||
|  |                 holder.itemView.setOnLongClickListener(new View.OnLongClickListener() { | |||
|  |                     @Override | |||
|  |                     public boolean onLongClick(View v) { | |||
|  |                         return false; | |||
|  |                     } | |||
|  |                 }); | |||
|  | 
 | |||
|  | 
 | |||
|  |                 //再设置一次CheckBox的选中监听器,当CheckBox的选中状态发生改变时,把改变后的状态储存在数组中 | |||
|  |                 holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |||
|  |                     @Override | |||
|  |                     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { | |||
|  |                         flag[position] = b; | |||
|  |                     } | |||
|  |                 }); | |||
|  | 
 | |||
|  |             } | |||
|  |         } | |||
|  | 
 | |||
|  |         @Override | |||
|  |         public int getItemCount() { | |||
|  |             return mDatas.size(); | |||
|  |         } | |||
|  | 
 | |||
|  |         public void addData(int position) { | |||
|  |             // mDatas.add(position, "Insert One"); | |||
|  |             notifyItemInserted(position); | |||
|  |         } | |||
|  | 
 | |||
|  |         public void removeData(int position) { | |||
|  |             mDatas.remove(position); | |||
|  |             notifyItemRemoved(position); | |||
|  |         } | |||
|  | 
 | |||
|  |         class MyViewHolder extends RecyclerView.ViewHolder { | |||
|  |             @BindView(R.id.id_check_box) | |||
|  |             CheckBox checkBox; | |||
|  |             @BindView(R.id.imageView) | |||
|  |             ImageView imageView; | |||
|  |             @BindView(R.id.imageTop) | |||
|  |             ImageView imageTop; | |||
|  | 
 | |||
|  |             @BindView(R.id.title) | |||
|  |             TextView tvTitle; | |||
|  |             @Nullable | |||
|  |             @BindView(R.id.author) | |||
|  |             TextView tvAuthor; | |||
|  |             @Nullable | |||
|  |             @BindView(R.id.category) | |||
|  |             TextView tvCate; | |||
|  |             @Nullable | |||
|  |             @BindView(R.id.desc) | |||
|  |             TextView tvDesc; | |||
|  | 
 | |||
|  |             public MyViewHolder(View view) { | |||
|  |                 super(view); | |||
|  |                 ButterKnife.bind(this, view); | |||
|  |                 //tvTitle = (TextView) view.findViewById(R.id.title); | |||
|  |                 // tvAuthor = (TextView) view.findViewById(R.id.author); | |||
|  | 
 | |||
|  |             } | |||
|  |         } | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onResume() { | |||
|  |         super.onResume(); | |||
|  |         getUpdatedData(); | |||
|  | 
 | |||
|  |         if (bottomSheetDialog != null) { | |||
|  |             bottomSheetDialog.hide(); | |||
|  |         } | |||
|  |         MobclickAgent.onPageStart(TAG); | |||
|  |     } | |||
|  | 
 | |||
|  |     @Override | |||
|  |     public void onPause() { | |||
|  |         super.onPause(); | |||
|  |         MobclickAgent.onPageEnd(TAG); | |||
|  |     } | |||
|  | 
 | |||
|  |     //----------toutiao ad | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | } | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 | |||
|  | 
 |