Skip to content
This repository has been archived by the owner on Oct 17, 2023. It is now read-only.

Commit

Permalink
优化卡顿问题
Browse files Browse the repository at this point in the history
  • Loading branch information
fengyuecanzhu committed Jun 2, 2021
1 parent 0cf2304 commit b22541d
Show file tree
Hide file tree
Showing 15 changed files with 395 additions and 446 deletions.
57 changes: 0 additions & 57 deletions app/src/main/java/xyz/fycz/myreader/application/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import xyz.fycz.myreader.entity.Setting;
import xyz.fycz.myreader.model.sourceAnalyzer.BookSourceManager;
import xyz.fycz.myreader.ui.activity.MainActivity;
import xyz.fycz.myreader.ui.dialog.APPDownloadTip;
import xyz.fycz.myreader.ui.dialog.DialogCreator;
import xyz.fycz.myreader.ui.fragment.BookcaseFragment;
import xyz.fycz.myreader.util.help.SSLSocketClient;
Expand Down Expand Up @@ -369,62 +368,6 @@ public static void checkVersionByServer(final AppCompatActivity activity, final
});
}

/**
* App自动升级
*
* @param activity
* @param versionCode
*/
public void updateApp(final AppCompatActivity activity, final String url, final int versionCode, String message,
final boolean isForceUpdate, final BookcaseFragment mBookcaseFragment) {
//String version = (versionCode / 100 % 10) + "." + (versionCode / 10 % 10) + "." + (versionCode % 10);
String cancelTitle;
if (isForceUpdate) {
cancelTitle = "退出";
} else {
cancelTitle = "忽略此版本";
}
if (mBookcaseFragment == null) {
DialogCreator.createCommonDialog(activity, "发现新版本:", message, true, "取消", "立即更新", null,
(dialog, which) -> goDownload(activity, url));
return;
}

DialogCreator.createThreeButtonDialog(activity, "发现新版本:", message, !isForceUpdate, cancelTitle, "直接下载",
"浏览器下载", (dialog, which) -> {
if (isForceUpdate) {
activity.finish();
}
}, (dialog, which) -> {
if (activity instanceof MainActivity) {
MainActivity mainActivity = (MainActivity) activity;
mainActivity.getViewPagerMain().setCurrentItem(0);
String filePath = APPCONST.UPDATE_APK_FILE_DIR + "FYReader.apk";
if (apkInfo(filePath) == versionCode) {
mainActivity.installApk(FileUtils.getFile(filePath), isForceUpdate);
return;
}
}
if (url == null || "".equals(url)) {
ToastUtils.showError("获取链接失败,请前往浏览器下载!");
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(URLCONST.APP_DIR_UR));
activity.startActivity(intent);
if (isForceUpdate) {
activity.finish();
}
} else {
APPDownloadTip downloadTip = new APPDownloadTip(url, mBookcaseFragment, activity, isForceUpdate);
downloadTip.downloadApp();
}
}, (dialog, which) -> {
goDownload(activity, url);
if (isForceUpdate) {
activity.finish();
}
});
}

public void updateApp2(final AppCompatActivity activity, final String url, final int versionCode, String message,
final boolean isForceUpdate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import xyz.fycz.myreader.util.ToastUtils;
import xyz.fycz.myreader.util.utils.AdUtils;
import xyz.fycz.myreader.util.utils.GsonExtensionsKt;
import xyz.fycz.myreader.widget.NoScrollViewPager;

import static androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

Expand Down Expand Up @@ -231,7 +232,7 @@ private void reLoadFragment() {
mMineFragment = (MineFragment) fragments.get(2);
}

public ViewPager getViewPagerMain() {
public NoScrollViewPager getViewPagerMain() {
return binding.viewPagerMain;
}

Expand All @@ -250,10 +251,6 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean isEdit = mBookcaseFragment.getmBookcasePresenter() != null && mBookcaseFragment.getmBookcasePresenter().ismEditState();
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
menu.findItem(R.id.action_refresh).setVisible(!isEdit);
}
if (binding.viewPagerMain.getCurrentItem() == 0) {
if (mBookcaseFragment.getmBookcasePresenter() != null && mBookcaseFragment.getmBookcasePresenter().ismEditState()) {
menu.findItem(R.id.action_finish).setVisible(true);
Expand Down Expand Up @@ -295,8 +292,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
getSupportActionBar().setSubtitle(groupName);
});
}
} else if (itemId == R.id.action_refresh) {
mBookcaseFragment.getmBookcasePresenter().initNoReadNum();
} else if (itemId == R.id.action_edit) {
if (mBookcaseFragment.getmBookcasePresenter().canEditBookcase()) {
invalidateOptionsMenu();
Expand Down
112 changes: 66 additions & 46 deletions app/src/main/java/xyz/fycz/myreader/ui/adapter/BookcaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import android.view.View;
import android.widget.*;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kongzue.dialogx.dialogs.BottomDialog;
import com.kongzue.dialogx.dialogs.BottomMenu;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.interfaces.OnMenuItemSelectListener;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
Expand All @@ -20,6 +25,7 @@
import xyz.fycz.myreader.R;
import xyz.fycz.myreader.application.App;
import xyz.fycz.myreader.common.APPCONST;
import xyz.fycz.myreader.ui.adapter.helper.ItemTouchCallback;
import xyz.fycz.myreader.ui.dialog.DialogCreator;
import xyz.fycz.myreader.ui.dialog.MyAlertDialog;
import xyz.fycz.myreader.widget.CoverImageView;
Expand All @@ -38,7 +44,7 @@
* @author fengyue
* @date 2020/4/19 11:23
*/
public abstract class BookcaseAdapter extends DragAdapter {
public abstract class BookcaseAdapter extends RecyclerView.Adapter<BookcaseAdapter.ViewHolder> {

private final Map<String, Boolean> isLoading = new HashMap<>();
private final Map<String, Boolean> mCheckMap = new LinkedHashMap<>();
Expand All @@ -59,7 +65,7 @@ public abstract class BookcaseAdapter extends DragAdapter {
App.getmContext().getResources().getString(R.string.menu_book_cache),
App.getmContext().getResources().getString(R.string.menu_book_delete)
};

protected ItemTouchCallback.OnItemTouchListener itemTouchCallbackListener;

public BookcaseAdapter(Context context, int textViewResourceId, ArrayList<Book> objects
, boolean editState, BookcasePresenter bookcasePresenter, boolean isGroup) {
Expand All @@ -75,25 +81,10 @@ public BookcaseAdapter(Context context, int textViewResourceId, ArrayList<Book>


@Override
public void onDataModelMove(int from, int to) {
Book b = list.remove(from);
list.add(to, b);
for (int i = 0; i < list.size(); i++) {
if (!isGroup) {
list.get(i).setSortCode(i);
}else {
list.get(i).setGroupSort(i);
}
}
mBookService.updateBooks(list);
}

@Override
public int getCount() {
public int getItemCount() {
return list.size();
}

@Override
public Book getItem(int position) {
return list.get(position);
}
Expand All @@ -103,6 +94,16 @@ public long getItemId(int position) {
return !isGroup ? list.get(position).getSortCode() : list.get(position).getGroupSort();
}

public void onDataMove() {
for (int i = 0; i < list.size(); i++) {
if (!isGroup) {
list.get(i).setSortCode(i);
} else {
list.get(i).setGroupSort(i);
}
}
mBookService.updateBooks(list);
}

public void remove(Book item) {
list.remove(item);
Expand All @@ -124,9 +125,9 @@ protected void showDeleteBookDialog(final Book book) {
ToastUtils.showSuccess("书籍删除成功!");
mBookcasePresenter.init();
}, null);
}else {
} else {
DialogCreator.createCommonDialog(mContext, "删除/移除书籍", "您是希望删除《" + book.getName() + "》及其所有缓存还是从分组中移除该书籍(不会删除书籍)呢?",
true, "删除书籍", "从分组中移除",(dialogInterface, i) -> {
true, "删除书籍", "从分组中移除", (dialogInterface, i) -> {
remove(book);
ToastUtils.showSuccess("书籍删除成功!");
mBookcasePresenter.init();
Expand All @@ -151,6 +152,8 @@ public void setmEditState(boolean mEditState) {
mCheckMap.put(book.getId(), false);
}
mCheckedCount = 0;
} else {
onDataMove();
}
this.mEditState = mEditState;
notifyDataSetChanged();
Expand All @@ -173,6 +176,10 @@ public boolean isBookLoading(String bookID) {
return isLoading.get(bookID);
}

public ItemTouchCallback.OnItemTouchListener getItemTouchCallbackListener() {
return itemTouchCallbackListener;
}

public boolean unionChapterCathe(Book book) throws IOException {
ArrayList<Chapter> chapters = (ArrayList<Chapter>) mChapterService.findBookAllChapterByBookId(book.getId());
BufferedReader br = null;
Expand Down Expand Up @@ -317,39 +324,52 @@ public void onOneItemSelect(BottomMenu dialog, CharSequence text, int which) {
selectedIndex = which;
}
}).setOkButton("确定", (baseDialog, v) -> {
switch (selectedIndex) {
case 0:
begin[0] = book.getHisttoryChapterNum();
end[0] = book.getHisttoryChapterNum() + 50;
break;
case 1:
begin[0] = book.getHisttoryChapterNum() - 50;
end[0] = book.getHisttoryChapterNum() + 50;
break;
case 2:
begin[0] = book.getHisttoryChapterNum();
end[0] = 99999;
break;
case 3:
begin[0] = 0;
end[0] = 99999;
break;
}
Thread downloadThread = new Thread(() -> {
ArrayList<Chapter> chapters = (ArrayList<Chapter>) mChapterService.findBookAllChapterByBookId(book.getId());
mBookcasePresenter.addDownload(book, chapters, begin[0], end[0], false);
});
mBookcasePresenter.getEs().submit(downloadThread);
return false;
}).setCancelButton(R.string.cancel);
switch (selectedIndex) {
case 0:
begin[0] = book.getHisttoryChapterNum();
end[0] = book.getHisttoryChapterNum() + 50;
break;
case 1:
begin[0] = book.getHisttoryChapterNum() - 50;
end[0] = book.getHisttoryChapterNum() + 50;
break;
case 2:
begin[0] = book.getHisttoryChapterNum();
end[0] = 99999;
break;
case 3:
begin[0] = 0;
end[0] = 99999;
break;
}
Thread downloadThread = new Thread(() -> {
ArrayList<Chapter> chapters = (ArrayList<Chapter>) mChapterService.findBookAllChapterByBookId(book.getId());
mBookcasePresenter.addDownload(book, chapters, begin[0], end[0], false);
});
mBookcasePresenter.getEs().submit(downloadThread);
return false;
}).setCancelButton(R.string.cancel);
}

public void refreshBook(String chapterUrl){
for (int i = 0; i < list.size(); i++) {
if (Objects.equals(list.get(i).getChapterUrl(), chapterUrl)) {
notifyItemChanged(i);
}
}
}

static class ViewHolder {

static class ViewHolder extends RecyclerView.ViewHolder {
CheckBox cbBookChecked;
CoverImageView ivBookImg;
TextView tvBookName;
BadgeView tvNoReadNum;
ProgressBar pbLoading;

public ViewHolder(@NonNull @NotNull View itemView) {
super(itemView);
}
}

public void setOnBookCheckedListener(OnBookCheckedListener listener) {
Expand Down
Loading

0 comments on commit b22541d

Please sign in to comment.