Skip to content

Commit

Permalink
fix: crash with zero page count while loading failed
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHubZJY committed Aug 14, 2022
1 parent 077d085 commit 0ebcba7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
46 changes: 20 additions & 26 deletions pdfview/src/main/java/com/zjy/pdfview/PdfView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.zjy.pdfview;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static com.zjy.pdfview.constants.Constants.DOWNLOAD_ACTION;
import static com.zjy.pdfview.download.DownloadService.DOWNLOAD_URL_KEY;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
Expand All @@ -12,15 +17,12 @@
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.recyclerview.widget.RecyclerView;

import com.zjy.pdfview.adapter.PdfPageAdapter;
Expand All @@ -41,11 +43,6 @@
import java.util.ArrayList;
import java.util.List;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static com.zjy.pdfview.constants.Constants.DOWNLOAD_ACTION;
import static com.zjy.pdfview.download.DownloadService.DOWNLOAD_URL_KEY;

/**
* Date: 2021/1/27
* Author: Yang
Expand Down Expand Up @@ -144,12 +141,9 @@ public void onPageSelected(int position, boolean isBottom) {
});
contentRv.setLayoutManager(pageLayoutManager);

loadingLayout.setLoadLayoutListener(new PdfLoadingLayout.LoadLayoutListener() {
@Override
public void clickRetry() {
if (!TextUtils.isEmpty(pdfUrl)) {
loadPdf(pdfUrl);
}
loadingLayout.setLoadLayoutListener(() -> {
if (!TextUtils.isEmpty(pdfUrl)) {
loadPdf(pdfUrl);
}
});

Expand All @@ -159,19 +153,17 @@ public void clickRetry() {

getOperateView().addOperateListener(this);

scrollSlider.setScrollSlideListener(new ScrollSlider.ScrollSlideListener() {
@Override
public boolean scrolling(int scrollY) {
int pageItemHeight = contentRv.getHeight() / pageCount;
int scrollIndex = (int) scrollY / pageItemHeight;
if(scrollIndex >= 0 && scrollIndex < pageLayoutManager.getItemCount()) {
scrollSlider.setTranslationY(scrollY - scrollY % pageItemHeight);
currentIndex = scrollIndex;
pageLayoutManager.scrollToPosition(currentIndex);
getOperateView().setPageIndexText(generatePageIndexText());
}
return true;
scrollSlider.setScrollSlideListener(scrollY -> {
if (pageCount == 0) return true;
int pageItemHeight = contentRv.getHeight() / pageCount;
int scrollIndex = scrollY / pageItemHeight;
if(scrollIndex >= 0 && scrollIndex < pageLayoutManager.getItemCount()) {
scrollSlider.setTranslationY(scrollY - scrollY % pageItemHeight);
currentIndex = scrollIndex;
pageLayoutManager.scrollToPosition(currentIndex);
getOperateView().setPageIndexText(generatePageIndexText());
}
return true;
});
}

Expand Down Expand Up @@ -342,6 +334,8 @@ protected Boolean doInBackground(Void... voids) {
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
scrollSlider.setVisibility(pageCount == 0 ? GONE : VISIBLE);

if (result) {
getOperateView().setPageIndexText(generatePageIndexText());
pageAdapter.notifyDataSetChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class PdfPageAdapter extends RecyclerView.Adapter<PdfPageAdapter.ViewHolder> {

private Context context;
private List<Bitmap> pageList;
final private Context context;
final private List<Bitmap> pageList;

public PdfPageAdapter(Context context, List<Bitmap> pageList) {
this.context = context;
Expand Down
14 changes: 8 additions & 6 deletions pdfview/src/main/java/com/zjy/pdfview/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ public static File writeAssetsToFile(Context context, String assetPath) throws I
String fileName = assetPath.substring(assetPath.lastIndexOf("/") + 1);
File resultFile = checkCacheSize(context, assetPath);
if (!resultFile.exists()) {
resultFile.createNewFile();
boolean createResult = resultFile.createNewFile();
if (!createResult) {
return null;
}
}
InputStream is = context.getAssets().open(fileName);
FileOutputStream fos = null;
try {
byte[] data = new byte[2048];
int nbread = 0;
int readBuffer;
fos = new FileOutputStream(resultFile);
while ((nbread = is.read(data)) > -1) {
fos.write(data, 0, nbread);
while ((readBuffer = is.read(data)) > -1) {
fos.write(data, 0, readBuffer);
}
} catch (Exception ex) {
PdfLog.logError("Exception: " + ex);
Expand Down Expand Up @@ -75,8 +78,7 @@ private static File checkCacheSize(Context context, String url) {
}
File[] cacheList = folder.listFiles();
if (cacheList != null && cacheList.length >= 10) {
for (int i = 0; i < cacheList.length; i++) {
File childFile = cacheList[i];
for (File childFile : cacheList) {
childFile.delete();
}
}
Expand Down

0 comments on commit 0ebcba7

Please sign in to comment.