Skip to content

Commit

Permalink
收藏->缓存全本
Browse files Browse the repository at this point in the history
  • Loading branch information
smuyyh committed Sep 26, 2016
1 parent 89e208c commit 7a77f09
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 5 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

<uses-permission android:name="android.permission.GET_TASKS"/>

<application
android:name=".ReaderApplication"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public synchronized void addToDownloadQueue(DownloadQueue downloadQueue) {
// 添加到下载队列
downloadQueues.add(downloadQueue);
// 当前队列里有其他任务,才提示
if (downloadQueues.size() > 1)
if (downloadQueues.size() >= 1)
ToastUtils.showSingleToast("成功加入缓存队列");
}
// 从队列顺序取出第一条下载
Expand Down Expand Up @@ -190,6 +190,7 @@ protected void onPostExecute(Integer failureCount) {
private int download(String url, final String bookId, final int chapter) {

final int[] result = {-1};
final BookPageFactory factory = new BookPageFactory(bookId);

bookApi.getChapterRead(url).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Expand All @@ -198,6 +199,7 @@ private int download(String url, final String bookId, final int chapter) {
public void onNext(ChapterRead data) {
if (data.chapter != null) {
post(new DownloadProgress(bookId, chapter));
factory.append(data.chapter, chapter);
result[0] = 1;
} else {
result[0] = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,8 @@ private void toggleReadBar() { // 切换工具栏 隐藏/显示 状态

@Override
public void showError() {

ToastUtils.showSingleToast("加载失败");
hideDialog();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.justwayward.reader.ui.contract;

import com.justwayward.reader.base.BaseContract;
import com.justwayward.reader.bean.BookToc;
import com.justwayward.reader.bean.Recommend;

import java.util.List;
Expand All @@ -13,6 +14,8 @@ public interface RecommendContract {

interface View extends BaseContract.BaseView {
void showRecommendList(List<Recommend.RecommendBooks> list);

void showBookToc(String bookId, List<BookToc.mixToc.Chapters> list);
}

interface Presenter<T> extends BaseContract.BasePresenter<T> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.justwayward.reader.ui.fragment;

import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -18,12 +22,17 @@
import com.justwayward.reader.R;
import com.justwayward.reader.base.BaseRVFragment;
import com.justwayward.reader.base.Constant;
import com.justwayward.reader.bean.BookToc;
import com.justwayward.reader.bean.Recommend;
import com.justwayward.reader.bean.support.DownloadComplete;
import com.justwayward.reader.bean.support.DownloadQueue;
import com.justwayward.reader.bean.support.RefreshCollectionsEvent;
import com.justwayward.reader.component.AppComponent;
import com.justwayward.reader.component.DaggerMainComponent;
import com.justwayward.reader.manager.CollectionsManager;
import com.justwayward.reader.service.DownloadBookService;
import com.justwayward.reader.ui.activity.BookDetailActivity;
import com.justwayward.reader.ui.activity.MainActivity;
import com.justwayward.reader.ui.activity.ReadActivity;
import com.justwayward.reader.ui.contract.RecommendContract;
import com.justwayward.reader.ui.easyadapter.RecommendAdapter;
Expand Down Expand Up @@ -119,6 +128,19 @@ public void showRecommendList(List<Recommend.RecommendBooks> list) {
mAdapter.addAll(list);
}

@Override
public void showBookToc(String bookId, List<BookToc.mixToc.Chapters> list) {
DownloadBookService.post(new DownloadQueue(bookId, list, 1, list.size()));
dismissDialog();
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void downloadComplete(DownloadComplete complete) {
if (isForeground(getActivity().getClass().getName())) {
ToastUtils.showSingleToast("缓存完成");
}
}

@Override
public void onItemClick(int position) {
//批量管理时,屏蔽点击事件
Expand Down Expand Up @@ -167,8 +189,8 @@ public void onClick(DialogInterface dialog, int which) {
break;
case 3:
//缓存全本
//TODO 缓存全本
ToastUtils.showToast("正在拼命开发中...");
showDialog();
mPresenter.getTocList(mAdapter.getItem(position)._id);
break;
case 4:
//删除
Expand Down Expand Up @@ -313,6 +335,7 @@ public void RefreshList(RefreshCollectionsEvent event) {
@Override
public void showError() {
loaddingError();
dismissDialog();
}

@Override
Expand Down Expand Up @@ -354,4 +377,22 @@ public void onDestroyView() {
EventBus.getDefault().unregister(this);
}

private boolean isForeground(String className) {
if (TextUtils.isEmpty(className)) {
return false;
}

ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
if (list != null && list.size() > 0) {
ComponentName cpn = list.get(0).topActivity;
if (MainActivity.class.getName().contains(cpn.getClassName())) {
return true;
}
}

return false;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void getChapterRead(String url, final int chapter) {
public void onNext(ChapterRead data) {
if (data.chapter != null && mView != null) {
mView.showChapterRead(data.chapter, chapter);
} else {
mView.showError();
}
}

Expand All @@ -94,6 +96,7 @@ public void onCompleted() {
@Override
public void onError(Throwable e) {
LogUtils.e("onError: " + e);
mView.showError();
}
});
addSubscrebe(rxSubscription);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.justwayward.reader.ui.presenter;

import android.content.Context;

import com.justwayward.reader.api.BookApi;
import com.justwayward.reader.base.RxPresenter;
import com.justwayward.reader.bean.BookToc;
import com.justwayward.reader.bean.Recommend;
import com.justwayward.reader.ui.contract.RecommendContract;
import com.justwayward.reader.utils.ACache;
import com.justwayward.reader.utils.LogUtils;
import com.justwayward.reader.utils.RxUtil;
import com.justwayward.reader.utils.StringUtils;
Expand All @@ -16,17 +20,20 @@
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
* @author yuyh.
* @date 2016/8/3.
*/
public class RecommendPresenter extends RxPresenter<RecommendContract.View> implements RecommendContract.Presenter<RecommendContract.View> {

private Context mContext;
private BookApi bookApi;

@Inject
public RecommendPresenter(BookApi bookApi) {
public RecommendPresenter(Context mContext, BookApi bookApi) {
this.mContext = mContext;
this.bookApi = bookApi;
}

Expand Down Expand Up @@ -63,4 +70,29 @@ public void onError(Throwable e) {
});
addSubscrebe(rxSubscription);
}

public void getTocList(final String bookId) {
bookApi.getBookToc(bookId, "chapters").subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<BookToc>() {
@Override
public void onNext(BookToc data) {
ACache.get(mContext).put(bookId + "bookToc", data);
List<BookToc.mixToc.Chapters> list = data.mixToc.chapters;
if (list != null && !list.isEmpty() && mView != null) {
mView.showBookToc(bookId, list);
}
}

@Override
public void onCompleted() {
}

@Override
public void onError(Throwable e) {
LogUtils.e("onError: " + e);
mView.showError();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public class BookPageFactory {

private static LRUMap<String, ArrayList<String>> cache = new LRUMap<>(10);

public BookPageFactory(String bookId){
this.bookId = bookId;
}

public BookPageFactory(String bookId, int lineHeight) {
this.bookId = bookId;
mWidth = ScreenUtils.getScreenWidth();
Expand Down

0 comments on commit 7a77f09

Please sign in to comment.