2019-04-02 17:41:53 +08:00
package com.novelbook.android.Fragments ;
import android.app.AlertDialog ;
import android.content.Context ;
import android.content.DialogInterface ;
import android.content.Intent ;
import android.content.res.Resources ;
2019-04-06 23:04:42 +08:00
import android.graphics.pdf.PdfDocument ;
2019-04-02 17:41:53 +08:00
import android.net.Uri ;
import android.os.Bundle ;
2019-04-11 18:05:41 +08:00
import android.os.Handler ;
import android.os.Message ;
2019-04-02 17:41:53 +08:00
import android.support.annotation.Nullable ;
import android.support.design.widget.TabLayout ;
import android.support.v4.app.Fragment ;
import android.support.v4.app.FragmentActivity ;
import android.support.v4.app.FragmentManager ;
import android.support.v4.app.FragmentTransaction ;
2019-04-16 23:11:00 +08:00
import android.support.v4.widget.SwipeRefreshLayout ;
2019-04-02 17:41:53 +08:00
import android.support.v7.widget.RecyclerView ;
import android.text.TextUtils ;
import android.util.Log ;
import android.util.TypedValue ;
import android.view.LayoutInflater ;
import android.view.View ;
import android.view.ViewGroup ;
import android.widget.ImageView ;
import android.widget.LinearLayout ;
import android.widget.TextView ;
import android.widget.Toast ;
2019-04-11 18:05:41 +08:00
import com.bumptech.glide.Glide ;
2019-04-06 23:04:42 +08:00
import com.google.gson.Gson ;
2019-04-12 23:02:31 +08:00
import com.google.gson.JsonArray ;
import com.google.gson.JsonElement ;
import com.google.gson.JsonObject ;
import com.google.gson.JsonParser ;
2019-04-10 22:19:06 +08:00
import com.novelbook.android.Activity_base ;
2019-04-02 17:41:53 +08:00
import com.novelbook.android.Activity_paihangbang ;
import com.novelbook.android.Activity_shudan ;
import com.novelbook.android.BookActivity ;
import com.novelbook.android.R ;
import com.novelbook.android.ReadActivity ;
import com.novelbook.android.adapter.BookListAdapter ;
2019-04-11 23:24:50 +08:00
import com.novelbook.android.bean.NovelBlock ;
2019-04-03 16:21:00 +08:00
import com.novelbook.android.db.Novel ;
2019-04-06 23:04:42 +08:00
import com.novelbook.android.netsubscribe.BookSubscribe ;
2019-04-20 00:26:49 +08:00
import com.novelbook.android.netutils.NetUtil ;
2019-04-06 23:04:42 +08:00
import com.novelbook.android.netutils.OnSuccessAndFaultListener ;
import com.novelbook.android.netutils.OnSuccessAndFaultSub ;
2019-04-11 18:05:41 +08:00
import com.novelbook.android.utils.ImageUtil ;
import com.novelbook.android.utils.MyImageLoader ;
2019-04-02 17:41:53 +08:00
import com.novelbook.android.utils.OnItemClickListener ;
2019-04-06 23:04:42 +08:00
import com.novelbook.android.utils.PageFactory ;
2019-04-11 18:05:41 +08:00
import com.youth.banner.loader.ImageLoader ;
2019-04-02 17:41:53 +08:00
import org.litepal.LitePal ;
2019-04-06 23:04:42 +08:00
import org.w3c.dom.Text ;
2019-04-02 17:41:53 +08:00
import java.io.File ;
import java.lang.reflect.Field ;
import java.util.ArrayList ;
2019-04-12 23:02:31 +08:00
import java.util.Iterator ;
2019-04-02 17:41:53 +08:00
import java.util.List ;
import butterknife.BindView ;
import butterknife.ButterKnife ;
/ * *
* A simple { @link Fragment } subclass .
* /
public abstract class BasicFragment extends Fragment {
public static String TAG = " need to be replaced " ;
2019-04-16 23:11:00 +08:00
protected View rootView ;
2019-04-17 23:28:18 +08:00
protected int pageNo = 1 ;
protected int pageCount ;
2019-04-02 17:41:53 +08:00
protected abstract int getLayoutRes ( ) ;
protected abstract void initData ( ) ;
2019-04-11 18:05:41 +08:00
protected abstract void fillData ( ) ;
2019-04-02 17:41:53 +08:00
protected void initListener ( ) { }
protected abstract void initViews ( ) ;
2019-04-06 23:04:42 +08:00
protected Gson gson = new Gson ( ) ;
2019-04-11 18:05:41 +08:00
// protected MyImageLoader imgloader = new MyImageLoader();
2019-04-02 17:41:53 +08:00
// Main2Activity activity;
FragmentActivity activity ;
2019-04-16 23:11:00 +08:00
@Nullable
@BindView ( R . id . swipeLayout )
SwipeRefreshLayout mSwipeRefresh ;
2019-04-02 17:41:53 +08:00
public BasicFragment ( ) {
// Required empty public constructor
}
@Override
public void onCreate ( Bundle savedInstanceState ) {
super . onCreate ( savedInstanceState ) ;
setFTag ( ) ;
// Log.d(TAG,"fragment onCreate ");
// activity =(Main2Activity)getActivity();
activity = getActivity ( ) ;
2019-04-11 18:05:41 +08:00
processArguments ( ) ;
2019-04-02 17:41:53 +08:00
// QLog.getLogger().e(dTAG,TAG+" "+ "onCreate");
// mediumFontSize = activity.getMiddleSizeFont();
}
2019-04-11 18:05:41 +08:00
protected abstract void processArguments ( ) ;
2019-04-02 17:41:53 +08:00
@Override
public View onCreateView ( LayoutInflater inflater , ViewGroup container ,
Bundle savedInstanceState ) {
Log . d ( TAG , " onCreateView " ) ;
View view = inflater . inflate ( getLayoutRes ( ) , container , false ) ;
rootView = view ;
// 初始化View注入
ButterKnife . bind ( this , view ) ;
initListener ( ) ;
2019-04-11 18:05:41 +08:00
initData ( ) ;
2019-04-02 17:41:53 +08:00
initViews ( ) ;
2019-04-16 23:11:00 +08:00
initSwipeRefreshLayout ( ) ;
2019-04-02 17:41:53 +08:00
return view ;
// Inflate the layout for this fragment
}
public String getCurrentTag ( ) {
setFTag ( ) ;
return TAG ;
}
public abstract void setFTag ( ) ;
2019-04-11 18:05:41 +08:00
Handler handler = new Handler ( ) {
@Override
public void handleMessage ( Message msg ) {
2019-04-20 00:26:49 +08:00
2019-04-11 18:05:41 +08:00
int wt = msg . what ;
if ( msg . what = = 1 ) {
fillData ( ) ;
} else if ( msg . what = = 2 ) //
{
2019-04-18 23:34:19 +08:00
// Toast.makeText(getActivity(), "数据请求失败 ", Toast.LENGTH_LONG).show();
2019-04-11 18:05:41 +08:00
} else if ( msg . what = = 3 ) {
2019-04-18 23:34:19 +08:00
// Toast.makeText(getActivity(), " ", Toast.LENGTH_LONG).show();
2019-04-11 18:05:41 +08:00
}
2019-04-20 00:26:49 +08:00
if ( getActivity ( ) ! = null & & ! NetUtil . isNetworkConnected ( ) ) {
Toast . makeText ( getActivity ( ) , " 好像没有网络啦 " , Toast . LENGTH_LONG ) . show ( ) ;
}
2019-04-11 18:05:41 +08:00
hideProgress ( ) ;
}
} ;
2019-04-02 17:41:53 +08:00
//--------------------------------------------------------------book list adapter------------------------------------------------
/ * interface mOnItemClickListener
{
void onItemClick ( View view , int position ) ;
void onItemLongClick ( View view , int position ) ;
void onLinearOutClick ( View view , int bookId ) ;
}
* /
2019-04-03 16:21:00 +08:00
protected List < Novel > initData ( List < Novel > mDatas , char x )
2019-04-02 17:41:53 +08:00
{
2019-04-03 16:21:00 +08:00
mDatas = new ArrayList < Novel > ( ) ;
2019-04-02 17:41:53 +08:00
for ( int i = 'A' ; i < x ; i + + )
{
2019-04-03 16:21:00 +08:00
Novel bk = new Novel ( ) ;
2019-04-10 22:19:06 +08:00
bk . setNovelId ( i ) ;
2019-04-02 17:41:53 +08:00
bk . setAuthor ( " 金庸 " ) ;
2019-04-05 23:59:31 +08:00
bk . setName ( " 射雕英雄传 " + ( char ) i ) ;
2019-04-03 16:21:00 +08:00
bk . setNovelType ( " 武侠 " ) ;
2019-04-09 22:32:02 +08:00
bk . setDesc ( " 南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事南宋时期的武林故事 " ) ;
2019-04-02 17:41:53 +08:00
mDatas . add ( bk ) ;
}
return mDatas ;
}
2019-04-10 22:19:06 +08:00
void showBookDetail ( Novel book ) {
2019-04-11 18:05:41 +08:00
2019-04-02 17:41:53 +08:00
Intent intent = new Intent ( activity , BookActivity . class ) ;
2019-04-10 22:19:06 +08:00
intent . putExtra ( BookActivity . EXTRA_BOOK , book ) ;
2019-04-02 17:41:53 +08:00
startActivity ( intent ) ;
}
2019-04-06 23:04:42 +08:00
void getBookInfo ( Novel novel ) {
2019-04-09 22:32:02 +08:00
int novelId = novel . getNovelId ( ) ;
2019-04-06 23:04:42 +08:00
long id = novel . getId ( ) ;
2019-04-02 17:41:53 +08:00
2019-04-06 23:04:42 +08:00
BookSubscribe . getNovel ( novel . getNovelId ( ) , new OnSuccessAndFaultSub ( new OnSuccessAndFaultListener ( ) {
@Override
public void onSuccess ( String result ) {
//成功
Novel nv ;
nv = gson . fromJson ( result , Novel . class ) ;
2019-04-11 18:05:41 +08:00
if ( id > 0 ) {
nv . update ( id ) ;
nv = LitePal . find ( Novel . class , id ) ;
} else {
nv . save ( ) ;
}
2019-04-16 23:11:00 +08:00
Log . d ( TAG , String . format ( " prepare book %s ,lastchapt %s, last pos %s ,开始prepare. " , nv . getName ( ) , nv . getLastReadChapt ( ) , nv . getLastReadPos ( ) ) ) ;
2019-04-15 23:26:24 +08:00
// final Novel novel = nv;
/ * new Thread ( ) {
@Override
public void run ( ) {
super . run ( ) ;
}
} . start ( ) ; * /
2019-04-10 22:19:06 +08:00
2019-04-07 23:49:08 +08:00
PageFactory . getInstance ( ) . prepareBook ( nv ) ; //打开本地小说内容
2019-04-15 23:26:24 +08:00
Log . d ( TAG , String . format ( " prepare book %s 开始open book. " , nv . getName ( ) ) ) ;
ReadActivity . openBook ( nv , activity ) ;
2019-04-06 23:04:42 +08:00
}
@Override
public void onFault ( String errorMsg ) {
//失败
Toast . makeText ( getActivity ( ) , " Novel 请求失败: " + errorMsg , Toast . LENGTH_SHORT ) . show ( ) ;
2019-04-07 23:49:08 +08:00
PageFactory . getInstance ( ) . prepareBook ( novel ) ; //打开本地小说内容
2019-04-10 22:19:06 +08:00
ReadActivity . openBook ( novel , activity ) ;
2019-04-06 23:04:42 +08:00
}
} , getActivity ( ) ) ) ;
}
2019-04-10 22:19:06 +08:00
void showProgressDialog ( boolean flag , String msg ) {
2019-04-18 23:34:19 +08:00
if ( getActivity ( ) = = null ) {
return ;
}
if ( mSwipeRefresh = = null | | ! mSwipeRefresh . isRefreshing ( ) ) {
2019-04-16 23:11:00 +08:00
( ( Activity_base ) getActivity ( ) ) . showProgressDialog ( flag , msg ) ;
}
2019-04-18 23:34:19 +08:00
2019-04-10 22:19:06 +08:00
}
void hideProgress ( ) {
2019-04-18 23:34:19 +08:00
if ( getActivity ( ) = = null ) {
return ;
}
2019-04-16 23:11:00 +08:00
if ( mSwipeRefresh ! = null & & mSwipeRefresh . isRefreshing ( ) ) {
mSwipeRefresh . setRefreshing ( false ) ;
}
2019-04-10 22:19:06 +08:00
( ( Activity_base ) getActivity ( ) ) . hideProgress ( ) ;
2019-04-16 23:11:00 +08:00
2019-04-10 22:19:06 +08:00
}
2019-04-16 23:11:00 +08:00
void initSwipeRefreshLayout ( ) {
if ( mSwipeRefresh = = null ) { return ; }
mSwipeRefresh . setColorSchemeResources ( R . color . colorPrimary , R . color . colorPrimaryDark ) ;
mSwipeRefresh . setOnRefreshListener ( new SwipeRefreshLayout . OnRefreshListener ( ) {
@Override
public void onRefresh ( ) {
// 开始刷新,设置当前为刷新状态
//swipeRefreshLayout.setRefreshing(true);
2019-04-17 23:28:18 +08:00
pageNo = 1 ;
2019-04-16 23:11:00 +08:00
initData ( ) ;
// TODO 获取数据
}
} ) ;
}
2019-04-03 16:21:00 +08:00
void openBook ( Novel book , BookListAdapter mAdapter ) {
2019-04-02 17:41:53 +08:00
//从新从数据库抓取该书的最新阅读进度
2019-04-03 16:21:00 +08:00
Novel book1 = ( Novel ) LitePal . find ( Novel . class , book . getId ( ) ) ;
2019-04-02 17:41:53 +08:00
if ( book1 ! = null ) { //读取本地最新进度
2019-04-03 16:21:00 +08:00
book . setLastReadPos ( book1 . getLastReadPos ( ) ) ;
book . setLastReadChapt ( book1 . getLastReadChapt ( ) ) ;
2019-04-02 17:41:53 +08:00
}
2019-04-05 23:59:31 +08:00
Toast . makeText ( activity , book . getName ( ) + " 加载 " , Toast . LENGTH_SHORT ) . show ( ) ;
2019-04-02 17:41:53 +08:00
2019-04-06 23:04:42 +08:00
if ( ! book . isLocalBook ( ) ) {
2019-04-10 22:19:06 +08:00
2019-04-06 23:04:42 +08:00
getBookInfo ( book ) ;
2019-04-07 23:49:08 +08:00
2019-04-10 22:19:06 +08:00
/ *
PageFactory pageFactory = PageFactory . getInstance ( ) ;
showProgressDialog ( false , " 读取目录信息 " ) ;
int slept = 0 ;
while ( slept < 100 | | pageFactory . getChapters ( ) = = null | | pageFactory . getChapters ( ) . size ( ) = = 0 ) {
try {
Thread . sleep ( 50 ) ;
slept + + ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
}
}
hideProgress ( ) ;
* /
//ReadActivity.openBook(book ,activity);
2019-04-06 23:04:42 +08:00
return ;
2019-04-02 17:41:53 +08:00
}
2019-04-06 23:04:42 +08:00
final String path = book . getNovelPath ( ) ;
2019-04-02 17:41:53 +08:00
File file = new File ( path ) ;
if ( ! file . exists ( ) ) {
new AlertDialog . Builder ( activity )
. setTitle ( activity . getString ( R . string . app_name ) )
. setMessage ( path + " 文件不存在,是否删除该书本? " )
. setPositiveButton ( " 删除 " , new DialogInterface . OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog , int which ) {
2019-04-06 23:04:42 +08:00
LitePal . deleteAll ( Novel . class , " id = ? " , book . getId ( ) + " " ) ;
if ( mAdapter ! = null ) { mAdapter . setBookList ( LitePal . where ( " isOnShelf=? " , " 1 " ) . find ( Novel . class ) ) ; }
2019-04-02 17:41:53 +08:00
}
} ) . setCancelable ( true ) . show ( ) ;
return ;
}
2019-04-06 23:04:42 +08:00
2019-04-05 23:59:31 +08:00
ReadActivity . openBook ( book , activity ) ;
2019-04-02 17:41:53 +08:00
}
void showShudanDetail ( int shuandanId ) {
Intent intent = new Intent ( activity , Activity_shudan . class ) ;
intent . putExtra ( " NAME " , shuandanId ) ;
startActivity ( intent ) ;
}
//-------------------------------------------------------------------------------------book list adapter end--------------------------------------------------
2019-04-11 23:24:50 +08:00
void showPaihangbang ( Object obj ) { //show paihangbang activity
2019-04-02 17:41:53 +08:00
2019-04-11 23:24:50 +08:00
NovelBlock nb = ( NovelBlock ) obj ;
2019-04-02 17:41:53 +08:00
Intent intent = new Intent ( activity , Activity_paihangbang . class ) ;
2019-04-12 23:02:31 +08:00
String title = TextUtils . isEmpty ( nb . getTitle ( ) ) ? nb . getName ( ) : nb . getTitle ( ) ;
intent . putExtra ( Activity_paihangbang . EXTR_FN , nb . getFn ( ) ) ;
intent . putExtra ( Activity_paihangbang . EXTR_TITLE , title ) ;
2019-04-02 17:41:53 +08:00
startActivity ( intent ) ;
}
2019-04-12 23:02:31 +08:00
void showPaihangbang ( NovelBlock nb ) { //show paihangbang activity
Intent intent = new Intent ( activity , Activity_paihangbang . class ) ;
String title = TextUtils . isEmpty ( nb . getTitle ( ) ) ? nb . getName ( ) : nb . getTitle ( ) ;
intent . putExtra ( Activity_paihangbang . EXTR_FN , nb . getFn ( ) ) ;
intent . putExtra ( Activity_paihangbang . EXTR_TITLE , title ) ;
intent . putExtra ( Activity_paihangbang . EXTR_BANGDAN , title ) ;
startActivity ( intent ) ;
}
2019-04-11 18:05:41 +08:00
protected void loadImageView ( String url , ImageView imageView ) {
ImageUtil . loadImage ( getActivity ( ) , url , imageView ) ;
}
2019-04-12 23:02:31 +08:00
2019-04-14 23:17:31 +08:00
public void reload ( ) {
initViews ( ) ;
}
2019-04-17 23:28:18 +08:00
public void loadData ( ) {
initData ( ) ;
} //on tab changed
2019-04-02 17:41:53 +08:00
}