diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b8504cd..72329a3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -80,7 +80,6 @@ - { - /*Intent intent = new Intent(context, BookInfoActivity.class); + /*Intent intent = new Intent(context, BookDetailedActivity.class); intent.putExtra(APPCONST.BOOK, mBook); context.startActivity(intent);*/ }); diff --git a/app/src/main/java/xyz/fycz/myreader/greendao/service/BookMarkService.java b/app/src/main/java/xyz/fycz/myreader/greendao/service/BookMarkService.java index 4d6afcb..2b14049 100644 --- a/app/src/main/java/xyz/fycz/myreader/greendao/service/BookMarkService.java +++ b/app/src/main/java/xyz/fycz/myreader/greendao/service/BookMarkService.java @@ -8,6 +8,7 @@ import xyz.fycz.myreader.greendao.entity.Chapter; import xyz.fycz.myreader.greendao.entity.SearchHistory; import xyz.fycz.myreader.greendao.gen.BookDao; import xyz.fycz.myreader.greendao.gen.BookMarkDao; +import xyz.fycz.myreader.greendao.gen.ChapterDao; import xyz.fycz.myreader.greendao.gen.SearchHistoryDao; import xyz.fycz.myreader.util.DateHelper; import xyz.fycz.myreader.util.StringHelper; @@ -114,8 +115,11 @@ public class BookMarkService extends BaseService { * @param bookId */ public void deleteBookALLBookMarkById(String bookId) { - String sel = "delete from book_mark where book_id = ?"; - rawQuery(sel, new String[]{bookId}); + GreenDaoManager.getInstance().getSession().getBookMarkDao() + .queryBuilder() + .where(BookMarkDao.Properties.BookId.eq(bookId)) + .buildDelete() + .executeDeleteWithoutDetachingEntities(); } /** * 批量删除书签 diff --git a/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java b/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java index 211035c..c7a2e3b 100644 --- a/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java +++ b/app/src/main/java/xyz/fycz/myreader/greendao/service/ChapterService.java @@ -117,8 +117,11 @@ public class ChapterService extends BaseService { * @param bookId */ public void deleteBookALLChapterById(String bookId) { - String sel = "delete from chapter where book_id = ?"; - rawQuery(sel, new String[]{bookId}); + GreenDaoManager.getInstance().getSession().getChapterDao() + .queryBuilder() + .where(ChapterDao.Properties.BookId.eq(bookId)) + .buildDelete() + .executeDeleteWithoutDetachingEntities(); deleteAllChapterCacheFile(bookId); } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookDetailedActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookDetailedActivity.java index f88ccec..9165794 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookDetailedActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookDetailedActivity.java @@ -9,19 +9,20 @@ import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.view.View; -import android.widget.FrameLayout; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.TextView; +import android.widget.*; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.Toolbar; +import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import butterknife.BindView; +import butterknife.OnClick; import com.bumptech.glide.Glide; import com.bumptech.glide.load.resource.bitmap.RoundedCorners; import com.bumptech.glide.request.RequestOptions; import xyz.fycz.myreader.R; +import xyz.fycz.myreader.application.MyApplication; import xyz.fycz.myreader.base.BaseActivity2; +import xyz.fycz.myreader.base.adapter.BaseListAdapter; import xyz.fycz.myreader.callback.ResultCallback; import xyz.fycz.myreader.common.APPCONST; import xyz.fycz.myreader.crawler.BookInfoCrawler; @@ -31,8 +32,12 @@ import xyz.fycz.myreader.creator.ChangeSourceDialog; import xyz.fycz.myreader.creator.DialogCreator; import xyz.fycz.myreader.enums.BookSource; import xyz.fycz.myreader.greendao.entity.Book; +import xyz.fycz.myreader.greendao.entity.Chapter; import xyz.fycz.myreader.greendao.service.BookService; +import xyz.fycz.myreader.greendao.service.ChapterService; import xyz.fycz.myreader.ui.read.ReadActivity; +import xyz.fycz.myreader.ui.read.catalog.CatalogActivity; +import xyz.fycz.myreader.ui.read.catalog.ChapterTitleAdapter; import xyz.fycz.myreader.util.StringHelper; import xyz.fycz.myreader.util.TextHelper; import xyz.fycz.myreader.util.utils.NetworkUtils; @@ -78,14 +83,26 @@ public class BookDetailedActivity extends BaseActivity2 { private Book mBook; private ArrayList aBooks; private BookService mBookService; + private ChapterService mChapterService; private ReadCrawler mReadCrawler; + private DetailCatalogAdapter mCatalogAdapter; + private ArrayList mChapters = new ArrayList<>(); + private ArrayList mNewestChapters = new ArrayList<>(); + private boolean isCollected; + @SuppressLint("HandlerLeak") private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: - initBookInfo(); + if (!"本地书籍".equals(mBook.getType())) { + mChapters.clear(); + mNewestChapters.clear(); + initBookInfo(); + initChapters(); + mCatalogAdapter.notifyDataSetChanged(); + } break; case 2: createChangeSourceDia(); @@ -110,13 +127,17 @@ public class BookDetailedActivity extends BaseActivity2 { protected void initData(Bundle savedInstanceState) { super.initData(savedInstanceState); mBookService = BookService.getInstance(); + mChapterService = ChapterService.getInstance(); aBooks = (ArrayList) getIntent().getSerializableExtra(APPCONST.SEARCH_BOOK_BEAN); if (aBooks != null) { mBook = aBooks.get(0); } else { mBook = (Book) getIntent().getSerializableExtra(APPCONST.BOOK); } - mReadCrawler = ReadCrawlerUtil.getReadCrawler(mBook.getSource()); + isCollected = isBookCollected(); + if (isCollected) { + mChapters = (ArrayList) mChapterService.findBookAllChapterByBookId(mBook.getId()); + } } @Override @@ -129,8 +150,22 @@ public class BookDetailedActivity extends BaseActivity2 { protected void initWidget() { super.initWidget(); initBookInfo(); + + //catalog + mCatalogAdapter = new DetailCatalogAdapter(); + bookDetailRvCatalog.setLayoutManager(new LinearLayoutManager(this)); + bookDetailRvCatalog.setAdapter(mCatalogAdapter); + + initChapters(); + + mCatalogAdapter.setOnItemClickListener((view, pos) -> { + mBook.setHisttoryChapterNum(mChapters.size() - pos - 1); + mBook.setLastReadPosition(0); + goReadActivity(); + }); + mTvDisclaimer.setOnClickListener(v -> DialogCreator.createAssetTipDialog(this, "免责声明", "disclaimer.fy")); - if (isBookCollected()) { + if (isCollected) { bookDetailTvAdd.setText("移除书籍"); bookDetailTvOpen.setText("继续阅读"); } @@ -141,40 +176,25 @@ public class BookDetailedActivity extends BaseActivity2 { protected void initClick() { super.initClick(); flAddBookcase.setOnClickListener(view -> { - if (!isBookCollected()) { + if (!isCollected) { mBookService.addBook(mBook); + isCollected = true; TextHelper.showText("成功加入书架"); bookDetailTvAdd.setText("移除书籍"); } else { mBookService.deleteBookById(mBook.getId()); + isCollected = false; + mBook.setHisttoryChapterNum(0); + mBook.setLastReadPosition(0); TextHelper.showText("成功移除书籍"); bookDetailTvAdd.setText("加入书架"); bookDetailTvOpen.setText("开始阅读"); } }); flOpenBook.setOnClickListener(view -> { - final boolean isCollected; - if (isBookCollected()) { - isCollected = true; - } else { - mBookService.addBook(mBook); - isCollected = false; - CommonApi.getBookChapters(mBook.getChapterUrl(), mReadCrawler, new ResultCallback() { - @Override - public void onFinish(Object o, int code) { - mBookService.updateEntity(mBook); - } - - @Override - public void onError(Exception e) { - } - }); - } - Intent intent = new Intent(this, ReadActivity.class); - intent.putExtra(APPCONST.BOOK, mBook); - intent.putExtra("isCollected", isCollected); - startActivityForResult(intent, APPCONST.REQUEST_READ); + goReadActivity(); }); + } @Override @@ -224,7 +244,7 @@ public class BookDetailedActivity extends BaseActivity2 { } private void initOtherInfo() { - mTvDesc.setText(mBook.getDesc()); + mTvDesc.setText("\t\t\t\t" + mBook.getDesc()); mTvType.setText(mBook.getType()); Glide.with(this) .load(mBook.getImgUrl()) @@ -236,7 +256,7 @@ public class BookDetailedActivity extends BaseActivity2 { } private void createChangeSourceDia() { - if (aBooks == null){ + if (aBooks == null) { mHandler.sendMessage(mHandler.obtainMessage(3)); return; } @@ -256,7 +276,6 @@ public class BookDetailedActivity extends BaseActivity2 { .setTitle("切换书源") .setCancelable(true) .setSingleChoiceItems(sources, checkedItem, (dialog1, which) -> { - boolean isBookCollected = isBookCollected(); if (finalCheckedItem == which) { dialog1.dismiss(); return; @@ -268,12 +287,12 @@ public class BookDetailedActivity extends BaseActivity2 { bookTem.setType(book.getType()); bookTem.setDesc(book.getDesc()); bookTem.setSource(book.getSource()); - if (isBookCollected) { + if (isCollected) { mBookService.updateBook(mBook, bookTem); } mBook = bookTem; mHandler.sendMessage(mHandler.obtainMessage(1)); - if (isBookCollected) { + if (isCollected) { DialogCreator.createTipDialog(this, "换源成功,由于不同书源的章节数量不一定相同,故换源后历史章节可能出错!"); } @@ -282,6 +301,44 @@ public class BookDetailedActivity extends BaseActivity2 { dialog.show(); } + private void initChapters() { + if (mChapters.size() == 0 && !"本地书籍".equals(mBook.getType())) { + mReadCrawler = ReadCrawlerUtil.getReadCrawler(mBook.getSource()); + CommonApi.getBookChapters(mBook.getChapterUrl(), mReadCrawler, new ResultCallback() { + @Override + public void onFinish(Object o, int code) { + mChapters = (ArrayList) o; + int end = Math.max(0, mChapters.size() - 10); + for (int i = mChapters.size() - 1; i >= end; i--) { + mNewestChapters.add(mChapters.get(i)); + } + MyApplication.runOnUiThread(() -> mCatalogAdapter.refreshItems(mNewestChapters)); + } + + @Override + public void onError(Exception e) { + + } + }); + } else { + int end = Math.max(0, mChapters.size() - 10); + for (int i = mChapters.size() - 1; i >= end; i--) { + mNewestChapters.add(mChapters.get(i)); + mCatalogAdapter.refreshItems(mNewestChapters); + } + } + } + + private void goReadActivity(){ + if (!isCollected) { + mBookService.addBook(mBook); + } + Intent intent = new Intent(this, ReadActivity.class); + intent.putExtra(APPCONST.BOOK, mBook); + intent.putExtra("isCollected", isCollected); + startActivityForResult(intent, APPCONST.REQUEST_READ); + } + /********************************Event***************************************/ @Override public boolean onCreateOptionsMenu(Menu menu) { @@ -299,7 +356,7 @@ public class BookDetailedActivity extends BaseActivity2 { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_change_source: //换源 - if (!NetworkUtils.isNetWorkAvailable()){ + if (!NetworkUtils.isNetWorkAvailable()) { TextHelper.showText("无网络连接!"); return true; } @@ -323,13 +380,19 @@ public class BookDetailedActivity extends BaseActivity2 { } break; case R.id.action_reload: //重新加载 - initWidget(); - processLogic(); + if (!"本地书籍".equals(mBook.getType())) { + mChapters.clear(); + mNewestChapters.clear(); + initWidget(); + processLogic(); + } break; case R.id.action_open_link: //打开链接 - Uri uri = Uri.parse(mBook.getChapterUrl()); - Intent intent = new Intent(Intent.ACTION_VIEW, uri); - startActivity(intent); + if (!"本地书籍".equals(mBook.getType())) { + Uri uri = Uri.parse(mBook.getChapterUrl()); + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + startActivity(intent); + } break; default: break; @@ -337,16 +400,52 @@ public class BookDetailedActivity extends BaseActivity2 { return super.onOptionsItemSelected(item); } + /** + * 展开简介 + */ + @OnClick(R.id.book_detail_tv_desc) + protected void showMoreDesc() { + if (mTvDesc.getMaxLines() == 5) + mTvDesc.setMaxLines(15); + else + mTvDesc.setMaxLines(5); + } + /** + * 章节列表 + */ + @OnClick(R.id.book_detail_tv_catalog_more) + public void goToMoreChapter() { + Intent intent = new Intent(this, CatalogActivity.class); + intent.putExtra(APPCONST.BOOK, mBook); + startActivityForResult(intent, APPCONST.REQUEST_CHAPTER_PAGE); + } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == APPCONST.REQUEST_READ) { - if (data == null) { - return; - } - boolean isCollected = data.getBooleanExtra(APPCONST.RESULT_IS_COLLECTED, false); - if (isCollected) { - bookDetailTvAdd.setText("移除书籍"); - bookDetailTvOpen.setText("继续阅读"); + if (resultCode == RESULT_OK) { + switch (requestCode) { + case APPCONST.REQUEST_READ: + if (data == null) { + return; + } + boolean isCollected = data.getBooleanExtra(APPCONST.RESULT_IS_COLLECTED, false); + int lastReadPosition = data.getIntExtra(APPCONST.RESULT_LAST_READ_POSITION, 0); + if (isCollected) { + bookDetailTvAdd.setText("移除书籍"); + bookDetailTvOpen.setText("继续阅读"); + this.isCollected = true; + mBook.setLastReadPosition(lastReadPosition); + }else { + mBook.setHisttoryChapterNum(0); + mBook.setLastReadPosition(0); + } + mCatalogAdapter.notifyDataSetChanged(); + break; + case APPCONST.REQUEST_CHAPTER_PAGE: + int[] chapterAndPage = data.getIntArrayExtra(APPCONST.CHAPTER_PAGE); + mBook.setHisttoryChapterNum(chapterAndPage[0]); + mBook.setLastReadPosition(chapterAndPage[1]); + goReadActivity(); + break; } } } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookInfoActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookInfoActivity.java deleted file mode 100644 index a44374e..0000000 --- a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookInfoActivity.java +++ /dev/null @@ -1,134 +0,0 @@ -package xyz.fycz.myreader.ui.bookinfo; - -import android.content.Intent; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import androidx.core.content.ContextCompat; -import android.widget.*; - -import xyz.fycz.myreader.R; -import xyz.fycz.myreader.base.BaseActivity; - -import butterknife.ButterKnife; -import butterknife.BindView; -import xyz.fycz.myreader.common.APPCONST; - -public class BookInfoActivity extends BaseActivity { - - @BindView(R.id.ll_title_back) - LinearLayout llTitleBack; - @BindView(R.id.tv_title_text) - TextView tvTitleText; - @BindView(R.id.system_title) - LinearLayout systemTitle; - @BindView(R.id.iv_book_img) - ImageView ivBookImg; - @BindView(R.id.tv_book_name) - TextView tvBookName; - @BindView(R.id.tv_book_author) - TextView tvBookAuthor; - @BindView(R.id.tv_book_type) - TextView tvBookType; - @BindView(R.id.tv_book_desc) - TextView tvBookDesc; - @BindView(R.id.btn_add_bookcase) - Button btnAddBookcase; - @BindView(R.id.btn_read_book) - Button btnReadBook; - @BindView(R.id.tv_book_newest_chapter) - TextView tvBookNewestChapter; - @BindView(R.id.tv_disclaimer) - TextView tvDisclaimer; - @BindView(R.id.tv_book_source) - TextView tvBookSource; - @BindView(R.id.btn_change_source) - Button btnChangeSource; - @BindView(R.id.pb_loading) - ProgressBar pbLoading; - - private BookInfoPresenter mBookInfoPresenter; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_book_info); - ButterKnife.bind(this); - setStatusBar(R.color.sys_line); - mBookInfoPresenter = new BookInfoPresenter(this); - mBookInfoPresenter.start(); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if (requestCode == APPCONST.REQUEST_READ) { - if (data == null) { - return; - } - boolean isCollected = data.getBooleanExtra(APPCONST.RESULT_IS_COLLECTED, false); - if (isCollected) { - getBtnAddBookcase().setText("移除书籍"); - getBtnReadBook().setText("继续阅读"); - } - } - } - - public LinearLayout getLlTitleBack() { - return llTitleBack; - } - - public TextView getTvTitleText() { - return tvTitleText; - } - - public LinearLayout getSystemTitle() { - return systemTitle; - } - - public ImageView getIvBookImg() { - return ivBookImg; - } - - public TextView getTvBookName() { - return tvBookName; - } - - public TextView getTvBookAuthor() { - return tvBookAuthor; - } - - public TextView getTvBookType() { - return tvBookType; - } - - public TextView getTvBookDesc() { - return tvBookDesc; - } - - public Button getBtnAddBookcase() { - return btnAddBookcase; - } - - public Button getBtnReadBook() { - return btnReadBook; - } - - public TextView getTvBookNewestChapter() { - return tvBookNewestChapter; - } - - public TextView getTvDisclaimer() { - return tvDisclaimer; - } - - public TextView getTvBookSource() { - return tvBookSource; - } - - public Button getBtnChangeSource() { - return btnChangeSource; - } - - public ProgressBar getPbLoading() { - return pbLoading; - } -} diff --git a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookInfoPresenter.java b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookInfoPresenter.java deleted file mode 100644 index 7eae1fe..0000000 --- a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/BookInfoPresenter.java +++ /dev/null @@ -1,282 +0,0 @@ -package xyz.fycz.myreader.ui.bookinfo; - -import android.annotation.SuppressLint; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Handler; -import android.os.Message; -import android.view.View; - -import androidx.appcompat.app.AlertDialog; - -import com.bumptech.glide.Glide; -import com.bumptech.glide.load.resource.bitmap.RoundedCorners; -import com.bumptech.glide.request.RequestOptions; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; - -import xyz.fycz.myreader.R; -import xyz.fycz.myreader.base.BasePresenter; -import xyz.fycz.myreader.callback.ResultCallback; -import xyz.fycz.myreader.common.APPCONST; -import xyz.fycz.myreader.crawler.BookInfoCrawler; -import xyz.fycz.myreader.crawler.ReadCrawler; -import xyz.fycz.myreader.crawler.ReadCrawlerUtil; -import xyz.fycz.myreader.creator.ChangeSourceDialog; -import xyz.fycz.myreader.creator.DialogCreator; -import xyz.fycz.myreader.enums.BookSource; -import xyz.fycz.myreader.greendao.entity.Book; -import xyz.fycz.myreader.greendao.service.BookService; -import xyz.fycz.myreader.ui.read.ReadActivity; -import xyz.fycz.myreader.util.StringHelper; -import xyz.fycz.myreader.util.TextHelper; -import xyz.fycz.myreader.util.utils.NetworkUtils; -import xyz.fycz.myreader.webapi.CommonApi; - - -public class BookInfoPresenter implements BasePresenter { - - private BookInfoActivity mBookInfoActivity; - private Book mBook; - private ArrayList aBooks; - private BookService mBookService; - private ReadCrawler mReadCrawler; - - @SuppressLint("HandlerLeak") - private Handler mHandler = new Handler() { - @Override - public void handleMessage(Message msg) { - switch (msg.what) { - case 1: - initBookInfo(); - break; - case 2: - createChangeSourceDia(); - break; - case 3: - mBookInfoActivity.getPbLoading().setVisibility(View.GONE); - DialogCreator.createTipDialog(mBookInfoActivity, "未搜索到该书籍,书源加载失败!"); - break; - case 4: - initOtherInfo(); - break; - } - } - }; - - public BookInfoPresenter(BookInfoActivity bookInfoActivity) { - mBookInfoActivity = bookInfoActivity; - mBookService = BookService.getInstance(); - } - - @Override - public void start() { - aBooks = (ArrayList) mBookInfoActivity.getIntent().getSerializableExtra(APPCONST.SEARCH_BOOK_BEAN); - if (aBooks != null) { - mBook = aBooks.get(0); - } else { - mBook = (Book) mBookInfoActivity.getIntent().getSerializableExtra(APPCONST.BOOK); - } - init(); - } - - private void init() { - mBookInfoActivity.getBtnChangeSource().setOnClickListener(v -> { - if (!NetworkUtils.isNetWorkAvailable()){ - TextHelper.showText("无网络连接!"); - return; - } - mBookInfoActivity.getPbLoading().setVisibility(View.VISIBLE); - if (aBooks == null) { - ChangeSourceDialog csd = new ChangeSourceDialog(mBookInfoActivity, mBook); - csd.init(new ResultCallback() { - @Override - public void onFinish(Object o, int code) { - aBooks = (ArrayList) o; - mHandler.sendMessage(mHandler.obtainMessage(2)); - } - - @Override - public void onError(Exception e) { - mHandler.sendMessage(mHandler.obtainMessage(3)); - } - }); - } else { - createChangeSourceDia(); - } - }); - mBookInfoActivity.getTvDisclaimer().setOnClickListener(v -> DialogCreator.createAssetTipDialog(mBookInfoActivity, "免责声明", "disclaimer.fy")); - if (isBookCollected()) { - mBookInfoActivity.getBtnAddBookcase().setText("移除书籍"); - mBookInfoActivity.getBtnReadBook().setText("继续阅读"); - } else { - mBookInfoActivity.getBtnAddBookcase().setText("加入书架"); - mBookInfoActivity.getBtnReadBook().setText("开始阅读"); - } - mBookInfoActivity.getLlTitleBack().setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - mBookInfoActivity.finish(); - } - }); - mBookInfoActivity.getBtnAddBookcase().setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (!isBookCollected()) { - mBookService.addBook(mBook); - TextHelper.showText("成功加入书架"); - mBookInfoActivity.getBtnAddBookcase().setText("移除书籍"); - } else { - mBookService.deleteBookById(mBook.getId()); - TextHelper.showText("成功移除书籍"); - mBookInfoActivity.getBtnAddBookcase().setText("加入书架"); - mBookInfoActivity.getBtnReadBook().setText("开始阅读"); - } - - } - }); - mReadCrawler = ReadCrawlerUtil.getReadCrawler(mBook.getSource()); - mBookInfoActivity.getBtnReadBook().setOnClickListener(view -> { - final boolean isCollected; - if (isBookCollected()) { - isCollected = true; - } else { - mBookService.addBook(mBook); - isCollected = false; - CommonApi.getBookChapters(mBook.getChapterUrl(), mReadCrawler, new ResultCallback() { - @Override - public void onFinish(Object o, int code) { - mBookService.updateEntity(mBook); - } - - @Override - public void onError(Exception e) { - } - }); - } - Intent intent = new Intent(mBookInfoActivity, ReadActivity.class); - intent.putExtra(APPCONST.BOOK, mBook); - intent.putExtra("isCollected", isCollected); - mBookInfoActivity.startActivityForResult(intent, APPCONST.REQUEST_READ); - }); - initBookInfo(); - } - - @SuppressLint("SetTextI18n") - private void initBookInfo() { - mBookInfoActivity.getTvTitleText().setText(mBook.getName()); - mBookInfoActivity.getTvBookAuthor().setText(mBook.getAuthor()); - if (StringHelper.isEmpty(mBook.getImgUrl())) { - mBook.setImgUrl(""); - } - assert mBook.getNewestChapterTitle() != null; - mBookInfoActivity.getTvBookNewestChapter().setText("最新章节:" + mBook.getNewestChapterTitle().replace("最近更新 ", "")); - mBookInfoActivity.getTvBookDesc().setText(""); - mBookInfoActivity.getTvBookType().setText(""); - mBookInfoActivity.getTvBookName().setText(mBook.getName()); - if (!"null".equals(mBook.getSource())) { - mBookInfoActivity.getTvBookSource().setText("书源:" + BookSource.fromString(mBook.getSource()).text); - } - ReadCrawler rc = ReadCrawlerUtil.getReadCrawler(mBook.getSource()); - if (rc instanceof BookInfoCrawler && StringHelper.isEmpty(mBook.getImgUrl())) { - BookInfoCrawler bic = (BookInfoCrawler) rc; - CommonApi.getBookInfo(mBook, bic, new ResultCallback() { - @Override - public void onFinish(Object o, int code) { - mHandler.sendMessage(mHandler.obtainMessage(4)); - } - - @Override - public void onError(Exception e) { - - } - }); - } else { - initOtherInfo(); - } - } - - private void initOtherInfo() { - mBookInfoActivity.getTvBookDesc().setText(mBook.getDesc()); - mBookInfoActivity.getTvBookType().setText(mBook.getType()); - Glide.with(mBookInfoActivity) - .load(mBook.getImgUrl()) - .error(R.mipmap.no_image) - .placeholder(R.mipmap.no_image) - //设置圆角 - .apply(RequestOptions.bitmapTransform(new RoundedCorners(8))) - .into(mBookInfoActivity.getIvBookImg()); - } - - private boolean isBookCollected() { - Book book = mBookService.findBookByAuthorAndName(mBook.getName(), mBook.getAuthor()); - if (book == null) { - return false; - } else { - mBook = book; - return true; - } - } - - private void createChangeSourceDia() { - if (aBooks == null){ - mHandler.sendMessage(mHandler.obtainMessage(3)); - return; - } - mBookInfoActivity.getPbLoading().setVisibility(View.GONE); - CharSequence[] sources = new CharSequence[aBooks.size()]; - int checkedItem = 0; - for (int i = 0; i < sources.length; i++) { - sources[i] = BookSource.fromString(aBooks.get(i).getSource()).text - + "\n" + aBooks.get(i).getNewestChapterTitle(); - if (sources[i].equals(BookSource.fromString(mBook.getSource()).text - + "\n" + aBooks.get(i).getNewestChapterTitle())) { - checkedItem = i; - } - } - final int finalCheckedItem = checkedItem; - AlertDialog dialog = new AlertDialog.Builder(mBookInfoActivity) - .setTitle("切换书源") - .setCancelable(true) - .setSingleChoiceItems(sources, checkedItem, (dialog1, which) -> { - boolean isBookCollected = isBookCollected(); -// mBookService.deleteBook(mBook); - if (finalCheckedItem == which) { - dialog1.dismiss(); - return; - } - Book book = aBooks.get(which); - Book bookTem = new Book(mBook); - bookTem.setChapterUrl(book.getChapterUrl()); - bookTem.setImgUrl(book.getImgUrl()); - bookTem.setType(book.getType()); - bookTem.setDesc(book.getDesc()); - bookTem.setSource(book.getSource()); - if (isBookCollected) { - mBookService.updateBook(mBook, bookTem); - } - mBook = bookTem; - mHandler.sendMessage(mHandler.obtainMessage(1)); - if (isBookCollected) { - DialogCreator.createTipDialog(mBookInfoActivity, - "换源成功,由于不同书源的章节数量不一定相同,故换源后历史章节可能出错!"); - } - dialog1.dismiss(); - }).create(); - dialog.show(); - /*try { - Field mAlert = AlertDialog.class.getDeclaredField("mAlert"); - mAlert.setAccessible(true); - Object mAlertController = mAlert.get(dialog); - Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView"); - mMessage.setAccessible(true); - TextView mMessageView = (TextView) mMessage.get(mAlertController); - mMessageView.setTextSize(5); - } catch (IllegalAccessException | NoSuchFieldException e) { - e.printStackTrace(); - }*/ - } -} diff --git a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/CatalogHolder.java b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/CatalogHolder.java new file mode 100644 index 0000000..e67d1b6 --- /dev/null +++ b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/CatalogHolder.java @@ -0,0 +1,36 @@ +package xyz.fycz.myreader.ui.bookinfo; + +import android.widget.TextView; +import xyz.fycz.myreader.R; +import xyz.fycz.myreader.base.ViewHolderImpl; +import xyz.fycz.myreader.greendao.entity.Chapter; +import xyz.fycz.myreader.greendao.service.ChapterService; + +import java.io.File; + +/** + * @author fengyue + * @date 2020/8/17 15:07 + */ +public class CatalogHolder extends ViewHolderImpl { + private TextView tvTitle; + @Override + protected int getItemLayoutId() { + return R.layout.listview_chapter_title_item; + } + + @Override + public void initView() { + tvTitle = findById(R.id.tv_chapter_title); + } + + @Override + public void onBind(Chapter data, int pos) { + if (ChapterService.isChapterCached(data.getBookId(), data.getTitle())) { + tvTitle.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(R.drawable.selector_category_load), null, null, null); + } else { + tvTitle.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(R.drawable.selector_category_unload), null, null, null); + } + tvTitle.setText(data.getTitle()); + } +} diff --git a/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/DetailCatalogAdapter.java b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/DetailCatalogAdapter.java new file mode 100644 index 0000000..7365215 --- /dev/null +++ b/app/src/main/java/xyz/fycz/myreader/ui/bookinfo/DetailCatalogAdapter.java @@ -0,0 +1,16 @@ +package xyz.fycz.myreader.ui.bookinfo; + +import xyz.fycz.myreader.base.IViewHolder; +import xyz.fycz.myreader.base.adapter.BaseListAdapter; +import xyz.fycz.myreader.greendao.entity.Chapter; + +/** + * @author fengyue + * @date 2020/8/17 15:06 + */ +public class DetailCatalogAdapter extends BaseListAdapter { + @Override + protected IViewHolder createViewHolder(int viewType) { + return new CatalogHolder(); + } +} diff --git a/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcaseDetailedAdapter.java b/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcaseDetailedAdapter.java index a869f92..3bb3c14 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcaseDetailedAdapter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcaseDetailedAdapter.java @@ -1,7 +1,6 @@ package xyz.fycz.myreader.ui.home.bookcase; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.view.LayoutInflater; import android.view.View; @@ -25,7 +24,6 @@ import xyz.fycz.myreader.common.APPCONST; import xyz.fycz.myreader.creator.DialogCreator; import xyz.fycz.myreader.greendao.entity.Book; import xyz.fycz.myreader.ui.bookinfo.BookDetailedActivity; -import xyz.fycz.myreader.ui.bookinfo.BookInfoActivity; import xyz.fycz.myreader.ui.read.ReadActivity; import xyz.fycz.myreader.util.StringHelper; import xyz.fycz.myreader.util.TextHelper; diff --git a/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcasePresenter.java b/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcasePresenter.java index 13ddf6b..c173563 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcasePresenter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/home/bookcase/BookcasePresenter.java @@ -131,6 +131,7 @@ public class BookcasePresenter implements BasePresenter { case 4: showErrorLoadingBooks(); if (MyApplication.isApkInDebug(mMainActivity)) { + MyApplication.runOnUiThread(() -> mBookcaseAdapter.notifyDataSetChanged()); downloadAll(false); } break; @@ -711,7 +712,9 @@ public class BookcasePresenter implements BasePresenter { return; } if (mChapters.size() == 0) { - TextHelper.showText("《" + book.getName() + "》章节目录为空,缓存失败,请刷新后重试"); + if (!isDownloadAll) { + TextHelper.showText("《" + book.getName() + "》章节目录为空,缓存失败,请刷新后重试"); + } return; } //取消之前下载 diff --git a/app/src/main/java/xyz/fycz/myreader/ui/home/bookstore/BookStorePresenter.java b/app/src/main/java/xyz/fycz/myreader/ui/home/bookstore/BookStorePresenter.java index 325fd5c..a9f172b 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/home/bookstore/BookStorePresenter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/home/bookstore/BookStorePresenter.java @@ -16,7 +16,6 @@ import xyz.fycz.myreader.common.URLCONST; import xyz.fycz.myreader.entity.bookstore.BookType; import xyz.fycz.myreader.greendao.entity.Book; import xyz.fycz.myreader.ui.bookinfo.BookDetailedActivity; -import xyz.fycz.myreader.ui.bookinfo.BookInfoActivity; import xyz.fycz.myreader.ui.home.MainActivity; import xyz.fycz.myreader.util.TextHelper; import xyz.fycz.myreader.webapi.BookStoreApi; diff --git a/app/src/main/java/xyz/fycz/myreader/ui/read/ReadActivity.java b/app/src/main/java/xyz/fycz/myreader/ui/read/ReadActivity.java index b821fe8..e799a6f 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/read/ReadActivity.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/read/ReadActivity.java @@ -111,9 +111,12 @@ public class ReadActivity extends BaseActivity { } private void exit(){ - // 返回给BookDetail。 + // 返回给BookDetail Intent result = new Intent(); result.putExtra(APPCONST.RESULT_IS_COLLECTED, mReadPresenter.isCollected()); + if (mReadPresenter.getmPageLoader() != null) { + result.putExtra(APPCONST.RESULT_LAST_READ_POSITION, mReadPresenter.getmPageLoader().getPagePos()); + } setResult(AppCompatActivity.RESULT_OK, result); super.onBackPressed(); } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/read/ReadPresenter.java b/app/src/main/java/xyz/fycz/myreader/ui/read/ReadPresenter.java index 14767d0..b035623 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/read/ReadPresenter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/read/ReadPresenter.java @@ -205,7 +205,6 @@ public class ReadPresenter implements BasePresenter { public ReadPresenter(ReadActivity readActivity) { mReadActivity = readActivity; mBookService = BookService.getInstance(); - ; mChapterService = ChapterService.getInstance(); mBookMarkService = BookMarkService.getInstance(); mSetting = SysManager.getSetting(); diff --git a/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogFragment.java b/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogFragment.java index 03cf10d..9c716ad 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogFragment.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogFragment.java @@ -5,6 +5,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; +import android.widget.ProgressBar; import android.widget.RelativeLayout; import androidx.fragment.app.Fragment; import butterknife.BindView; @@ -23,6 +24,8 @@ public class CatalogFragment extends Fragment { ListView lvChapterList; @BindView(R.id.change_sort) FloatingActionButton fcChangeSort; + @BindView(R.id.pb_loading) + ProgressBar pbLoading; Unbinder unbinder; @@ -64,4 +67,8 @@ public class CatalogFragment extends Fragment { public CatalogPresenter getmCatalogPresent() { return mCatalogPresent; } + + public ProgressBar getPbLoading() { + return pbLoading; + } } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogPresenter.java b/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogPresenter.java index d950b31..8365d05 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogPresenter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/CatalogPresenter.java @@ -5,12 +5,17 @@ import android.content.Intent; import android.view.View; import android.widget.AdapterView; import xyz.fycz.myreader.R; +import xyz.fycz.myreader.application.MyApplication; import xyz.fycz.myreader.application.SysManager; import xyz.fycz.myreader.base.BasePresenter; +import xyz.fycz.myreader.callback.ResultCallback; import xyz.fycz.myreader.common.APPCONST; +import xyz.fycz.myreader.crawler.ReadCrawlerUtil; import xyz.fycz.myreader.greendao.entity.Book; import xyz.fycz.myreader.greendao.entity.Chapter; import xyz.fycz.myreader.greendao.service.ChapterService; +import xyz.fycz.myreader.util.TextHelper; +import xyz.fycz.myreader.webapi.CommonApi; import java.util.ArrayList; import java.util.Collections; @@ -50,7 +55,32 @@ public class CatalogPresenter implements BasePresenter { changeChapterSort(); } }); - initChapterTitleList(); + mChapters = (ArrayList) mChapterService.findBookAllChapterByBookId(mBook.getId()); + if (mChapters.size() != 0) { + initChapterTitleList(); + }else { + mCatalogFragment.getPbLoading().setVisibility(View.VISIBLE); + CommonApi.getBookChapters(mBook.getChapterUrl(), ReadCrawlerUtil.getReadCrawler(mBook.getSource()), + new ResultCallback() { + @Override + public void onFinish(Object o, int code) { + mChapters = (ArrayList) o; + + MyApplication.runOnUiThread(() -> { + mCatalogFragment.getPbLoading().setVisibility(View.GONE); + initChapterTitleList(); + }); + + } + + @Override + public void onError(Exception e) { + e.printStackTrace(); + TextHelper.showText("章节目录加载失败!"); + MyApplication.runOnUiThread(() -> mCatalogFragment.getPbLoading().setVisibility(View.GONE)); + } + }); + } mCatalogFragment.getLvChapterList().setOnItemClickListener((adapterView, view, i, l) -> { Chapter chapter = mChapterTitleAdapter.getItem(i); final int position; @@ -76,7 +106,6 @@ public class CatalogPresenter implements BasePresenter { * 初始化章节目录 */ private void initChapterTitleList() { - mChapters = (ArrayList) mChapterService.findBookAllChapterByBookId(mBook.getId()); //初始化倒序章节 mConvertChapters.addAll(mChapters); Collections.reverse(mConvertChapters); diff --git a/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/ChapterTitleAdapter.java b/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/ChapterTitleAdapter.java index 7664568..9f92a38 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/ChapterTitleAdapter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/read/catalog/ChapterTitleAdapter.java @@ -124,7 +124,6 @@ public class ChapterTitleAdapter extends ArrayAdapter { }; } - public List getmList() { return mList; } diff --git a/app/src/main/java/xyz/fycz/myreader/ui/search/SearchBookPrensenter.java b/app/src/main/java/xyz/fycz/myreader/ui/search/SearchBookPrensenter.java index 4987ce3..5e1f3cc 100644 --- a/app/src/main/java/xyz/fycz/myreader/ui/search/SearchBookPrensenter.java +++ b/app/src/main/java/xyz/fycz/myreader/ui/search/SearchBookPrensenter.java @@ -12,18 +12,12 @@ import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; -import android.widget.AdapterView; -import android.widget.TextView; -import androidx.annotation.NonNull; -import com.scwang.smartrefresh.layout.api.RefreshLayout; -import com.scwang.smartrefresh.layout.listener.OnRefreshListener; import xyz.fycz.myreader.application.MyApplication; import xyz.fycz.myreader.crawler.*; import xyz.fycz.myreader.entity.SearchBookBean; import xyz.fycz.myreader.mulvalmap.ConcurrentMultiValueMap; import xyz.fycz.myreader.ui.bookinfo.BookDetailedActivity; -import xyz.fycz.myreader.ui.bookinfo.BookInfoActivity; import xyz.fycz.myreader.R; import xyz.fycz.myreader.base.BasePresenter; import xyz.fycz.myreader.callback.ResultCallback; @@ -39,9 +33,6 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; - -import me.gujun.android.taggroup.TagGroup; public class SearchBookPrensenter implements BasePresenter { diff --git a/app/src/main/java/xyz/fycz/myreader/webapi/CommonApi.java b/app/src/main/java/xyz/fycz/myreader/webapi/CommonApi.java index f757c58..46fb623 100644 --- a/app/src/main/java/xyz/fycz/myreader/webapi/CommonApi.java +++ b/app/src/main/java/xyz/fycz/myreader/webapi/CommonApi.java @@ -28,7 +28,6 @@ public class CommonApi extends BaseApi { @Override public void onError(Exception e) { callback.onError(e); - } }); } diff --git a/app/src/main/res/layout/activity_book_info.xml b/app/src/main/res/layout/activity_book_info.xml deleted file mode 100644 index 20811a5..0000000 --- a/app/src/main/res/layout/activity_book_info.xml +++ /dev/null @@ -1,213 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -