diff --git a/README.md b/README.md
index 0a94bcb..0e37adb 100644
--- a/README.md
+++ b/README.md
@@ -28,7 +28,7 @@ Kt、AndroidX、Retrofit、Okhttp3、Glide、LitePal等
| --- | --- | --- |
| | | |
| | | |
-| | | |
+| | https://github.com/390057892/reader/blob/master/screenshot/night.jpg | https://github.com/390057892/reader/blob/master/screenshot/edit.jpg |
diff --git a/app/src/main/java/com/novel/read/activity/NovelBookTypeListActivity.kt b/app/src/main/java/com/novel/read/activity/NovelBookTypeListActivity.kt
index 2720057..3a324fc 100644
--- a/app/src/main/java/com/novel/read/activity/NovelBookTypeListActivity.kt
+++ b/app/src/main/java/com/novel/read/activity/NovelBookTypeListActivity.kt
@@ -63,7 +63,7 @@ class NovelBookTypeListActivity : NovelBaseActivity() {
}
private fun getData() {
- AccountManager.getInstance().getSearchBookList(mCategoryId, "", page)
+ mCategoryId?.let { AccountManager.getInstance().getSearchBookList(it, "", page) }
}
@Subscribe
diff --git a/app/src/main/java/com/novel/read/activity/NovelRankListActivity.kt b/app/src/main/java/com/novel/read/activity/NovelRankListActivity.kt
index 1c68b92..3a8409a 100644
--- a/app/src/main/java/com/novel/read/activity/NovelRankListActivity.kt
+++ b/app/src/main/java/com/novel/read/activity/NovelRankListActivity.kt
@@ -24,8 +24,8 @@ class NovelRankListActivity : NovelBaseActivity() {
private var mList: MutableList = ArrayList()
private var page = 1
private var loadSize: Int = 0
- private var type: String? = null
- private var sex: String? = null
+ private var type: String = ""
+ private var sex: String = ""
override val layoutId: Int get() = R.layout.activity_rank_list
diff --git a/app/src/main/java/com/novel/read/activity/NovelReadActivity.kt b/app/src/main/java/com/novel/read/activity/NovelReadActivity.kt
index 7aa96fc..a747b79 100644
--- a/app/src/main/java/com/novel/read/activity/NovelReadActivity.kt
+++ b/app/src/main/java/com/novel/read/activity/NovelReadActivity.kt
@@ -74,7 +74,7 @@ class NovelReadActivity : NovelBaseActivity(), DownloadService.OnDownloadListene
private val isRegistered = false
private var mCollBook: CollBookBean? = null
- private var mBookId: String? = null
+ private var mBookId: String = ""
@SuppressLint("HandlerLeak")
private val mHandler = object : Handler() {
diff --git a/app/src/main/java/com/novel/read/event/DeleteBookSignEvent.kt b/app/src/main/java/com/novel/read/event/DeleteBookSignEvent.kt
index fc49e8e..ee62dbd 100644
--- a/app/src/main/java/com/novel/read/event/DeleteBookSignEvent.kt
+++ b/app/src/main/java/com/novel/read/event/DeleteBookSignEvent.kt
@@ -3,4 +3,8 @@ package com.novel.read.event
import com.mango.mangolib.event.BaseEvent
import com.mango.mangolib.http.ErrorResponse
-class DeleteBookSignEvent(result: ErrorResponse) : BaseEvent(result)
+class DeleteBookSignEvent : BaseEvent{
+ constructor(result: ErrorResponse) : super(result) {}
+
+ constructor() {}
+}
diff --git a/app/src/main/java/com/novel/read/event/HotSearchEvent.kt b/app/src/main/java/com/novel/read/event/HotSearchEvent.kt
index e152f31..06b220e 100644
--- a/app/src/main/java/com/novel/read/event/HotSearchEvent.kt
+++ b/app/src/main/java/com/novel/read/event/HotSearchEvent.kt
@@ -2,9 +2,15 @@ package com.novel.read.event
import com.mango.mangolib.event.BaseEvent
import com.novel.read.model.protocol.HotSearchResp
+import com.novel.read.model.protocol.SearchResp
/**
* create by zlj on 2019/6/18
* describe:
*/
-class HotSearchEvent(result: HotSearchResp) : BaseEvent(result)
+class HotSearchEvent : BaseEvent{
+
+ constructor(result: HotSearchResp) : super(result) {}
+
+ constructor() {}
+}
diff --git a/app/src/main/java/com/novel/read/event/HumanEvent.kt b/app/src/main/java/com/novel/read/event/HumanEvent.kt
index e7f196f..43025b1 100644
--- a/app/src/main/java/com/novel/read/event/HumanEvent.kt
+++ b/app/src/main/java/com/novel/read/event/HumanEvent.kt
@@ -8,6 +8,9 @@ import com.novel.read.model.protocol.RecommendListResp
* create by 赵利君 on 2019/6/18
* describe:
*/
-class HumanEvent(result: RecommendListResp) : BaseEvent(result) {
+class HumanEvent : BaseEvent {
+ constructor(result: RecommendListResp) : super(result) {}
+
+ constructor() {}
}
diff --git a/app/src/main/java/com/novel/read/fragment/BookListFragment.kt b/app/src/main/java/com/novel/read/fragment/BookListFragment.kt
index 02e1055..4ce552e 100644
--- a/app/src/main/java/com/novel/read/fragment/BookListFragment.kt
+++ b/app/src/main/java/com/novel/read/fragment/BookListFragment.kt
@@ -21,9 +21,9 @@ class BookListFragment : NovelBaseFragment() {
private var mAdapter: RankListAdapter? = null
private var mList: MutableList = ArrayList()
- private var sex: String? = null
- private var dateType: String? = null
- private var type: String? = null
+ private var sex: String = ""
+ private var dateType: String = ""
+ private var type: String = ""
private var page = 1
private var loadSize: Int = 0
diff --git a/app/src/main/java/com/novel/read/http/AccountManager.java b/app/src/main/java/com/novel/read/http/AccountManager.java
deleted file mode 100644
index cb12edd..0000000
--- a/app/src/main/java/com/novel/read/http/AccountManager.java
+++ /dev/null
@@ -1,313 +0,0 @@
-package com.novel.read.http;
-
-import android.content.Context;
-import android.text.TextUtils;
-import android.util.Log;
-
-import com.mango.mangolib.event.EventManager;
-import com.mango.mangolib.http.ErrorResponse;
-import com.mango.mangolib.http.MyRequestType;
-import com.mango.mangolib.http.ServiceCallback;
-import com.novel.read.event.AddBookSignEvent;
-import com.novel.read.event.BookArticleEvent;
-import com.novel.read.event.DeleteBookSignEvent;
-import com.novel.read.event.ErrorChapterEvent;
-import com.novel.read.event.FinishChapterEvent;
-import com.novel.read.event.GetBookDetailEvent;
-import com.novel.read.event.GetBookSignEvent;
-import com.novel.read.event.GetCategoryTypeEvent;
-import com.novel.read.event.GetRecommendBookEvent;
-import com.novel.read.event.HotSearchEvent;
-import com.novel.read.event.LoginEvent;
-import com.novel.read.event.SearchListEvent;
-import com.novel.read.event.VersionEvent;
-import com.novel.read.http.service.AccountService;
-import com.novel.read.model.db.ChapterInfoBean;
-import com.novel.read.model.db.dbManage.BookRepository;
-import com.novel.read.model.protocol.BookArticleResp;
-import com.novel.read.model.protocol.BookDetailResp;
-import com.novel.read.model.protocol.CategoryTypeResp;
-import com.novel.read.model.protocol.HotSearchResp;
-import com.novel.read.model.protocol.MarkResp;
-import com.novel.read.model.protocol.RankByUpadateResp;
-import com.novel.read.model.protocol.RecommendBookResp;
-import com.novel.read.model.protocol.RecommendListResp;
-import com.novel.read.model.protocol.SearchResp;
-import com.novel.read.model.protocol.UidResp;
-import com.novel.read.model.protocol.VersionResp;
-import com.novel.read.utlis.LogUtils;
-import com.novel.read.utlis.PhoneUtils;
-import com.novel.read.widget.page.TxtChapter;
-
-import org.reactivestreams.Subscriber;
-import org.reactivestreams.Subscription;
-
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import io.reactivex.Single;
-import io.reactivex.android.schedulers.AndroidSchedulers;
-import io.reactivex.schedulers.Schedulers;
-import okhttp3.MediaType;
-import okhttp3.RequestBody;
-import retrofit2.Call;
-import retrofit2.Callback;
-
-/**
- * Created by zlj on 2017/2/14.
- */
-
-public class AccountManager {
-
- private AccountService accountService;
-
- private AccountManager() {
- accountService = ServiceGenerator.createService(AccountService.class, MyRequestType.URL_TEXT);
- }
-
- private static AccountManager instance = null;
-
- public static synchronized AccountManager getInstance() {
- if (instance == null) {
- instance = new AccountManager();
- }
- return instance;
- }
-
-
- public void getRecommendBook(String bookId, String limit) {
- HashMap map = new HashMap<>();
- map.put("book_id", bookId);
- Call call = accountService.getRecommendBook(getUrlString(Urls.getRecommend, map));
- call.enqueue(new ServiceCallback<>(GetRecommendBookEvent.class));
- }
-
- public void getBookDetail(String bookId) {
- HashMap map = new HashMap<>();
- map.put("book_id", bookId);
- Call call = accountService.getBookDetail(getUrlString(Urls.getBookDetail, map));
- call.enqueue(new ServiceCallback<>(GetBookDetailEvent.class));
- }
-
- public Single getBookDetails(String bookId) { //rxjava 获取多书籍详情 合并请求调用
- HashMap map = new HashMap<>();
- map.put("book_id", bookId);
- return accountService.getBookDetails(getUrlString(Urls.getBookDetail, map));
- }
-
- public void getCategoryType() {
- HashMap map = new HashMap<>();
- Call call = accountService.getCategoryType(getUrlString(Urls.getCategoryType, map));
- call.enqueue(new ServiceCallback<>(GetCategoryTypeEvent.class));
- }
-
- public void getHotSearch() {
- HashMap map = new HashMap<>();
- Call call = accountService.getHotSearch(getUrlString(Urls.getHotSearch, map));
- call.enqueue(new ServiceCallback<>(HotSearchEvent.class));
- }
-
- public void getBookArticle(String bookId, String hasContent, String page, String limit) {
- HashMap map = new HashMap<>();
- map.put("book_id", bookId);
- map.put("has_content", hasContent);
- map.put("page", page);
- map.put("limit", limit);
- Call call = accountService.getBookArticle(getUrlString(Urls.getBookArticle, map));
- call.enqueue(new ServiceCallback<>(BookArticleEvent.class));
- }
-
-
- public void getSearchBookList(String category_id, String key, int page) {
- HashMap map = new HashMap<>();
- if (!TextUtils.isEmpty(category_id)) {
- map.put("category_id", category_id);
- } else {
- map.put("category_id", "0");
- }
- if (!TextUtils.isEmpty(key)) {
- map.put("key", key);
- }
- map.put("page", String.valueOf(page));
- Call call = accountService.getSearchList(getUrlString(Urls.getBookList, map));
- call.enqueue(new ServiceCallback<>(SearchListEvent.class));
- }
-
- public void getRecommendList(String listType, Callback callback) {
- HashMap map = new HashMap<>();
- map.put("type", listType);
- Call call = accountService.getRecommendList(getUrlString(Urls.getRecommendList, map));
- call.enqueue(callback);
-
- }
-
- public void getRankByUpdate(int page, int limit, Callback callback) {
- HashMap map = new HashMap<>();
- if (!TextUtils.isEmpty(String.valueOf(page))) {
- map.put("page", String.valueOf(page));
- }
- if (limit != 0) {
- map.put("limit", String.valueOf(limit));
- }
- Call call = accountService.getRankByUpdate(getUrlString(Urls.getRankByUpdate, map));
- call.enqueue(callback);
- }
-
- public void getRankList(String type, String sex, String dateType, String page, Callback callback) {
- HashMap map = new HashMap<>();
- map.put("type", type);
- map.put("gender", sex);
- map.put("date_type", dateType);
- map.put("page", page);
- Call call = accountService.getRankList(getUrlString(Urls.getRankList, map));
- call.enqueue(callback);
- }
-
- public void checkVersion(int versionCode) {
- HashMap map = new HashMap<>();
- map.put("version", String.valueOf(versionCode));
-// map.put("shell", Constant.shell);
- Call call = accountService.checkVersion(getUrlString(Urls.checkVersion, map));
- call.enqueue(new ServiceCallback<>(VersionEvent.class));
- }
-
- public void addSign(String bookId, String articleId, String content) {
- HashMap map = new HashMap<>();
- map.put("book_id", bookId);
- map.put("article_id", articleId);
- map.put("content", content);
- Call call = accountService.addSign(Urls.addBookSign, mapToBody(map));
- call.enqueue(new ServiceCallback<>(AddBookSignEvent.class));
- }
-
- public void deleteSign(String signIds) {
- HashMap map = new HashMap<>();
- map.put("sign_ids", signIds);
- Call call = accountService.deleteSign(Urls.deleteSign, mapToBody(map));
- call.enqueue(new ServiceCallback<>(DeleteBookSignEvent.class));
- }
-
- public void getSignList(String bookId) {
- HashMap map = new HashMap<>();
- map.put("book_id", bookId);
- Call call = accountService.getSignList(getUrlString(Urls.getBookSign, map));
- call.enqueue(new ServiceCallback<>(GetBookSignEvent.class));
- }
-
- public void login(Context mContext) {
- HashMap map = new HashMap<>();
- Log.e("getUniquePsuedoID", "login: " + PhoneUtils.getUniquePsuedoID());
- map.put("code", PhoneUtils.getUniquePsuedoID());
- Call call = accountService.login(Urls.login, mapToBody(map));
- call.enqueue(new ServiceCallback<>(LoginEvent.class));
- }
-
- private Subscription mChapterSub;
-
- public void getBookArticleDetail(String bookId, List bookChapters) {
- int size = bookChapters.size();
- //取消上次的任务,防止多次加载
- if (mChapterSub != null) {
- mChapterSub.cancel();
- }
- List> chapterInfos = new ArrayList<>(bookChapters.size());
- ArrayDeque titles = new ArrayDeque<>(bookChapters.size());
-
- // 将要下载章节,转换成网络请求。
- for (int i = 0; i < size; ++i) {
- TxtChapter bookChapter = bookChapters.get(i);
- // 网络中获取数据
- Single chapterInfoSingle = getChapterInfo(bookChapter.getChapterId());
- chapterInfos.add(chapterInfoSingle);
- titles.add(bookChapter.getTitle());
- }
-
- Single.concat(chapterInfos)
- .subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Subscriber() {
- String title = titles.poll();
-
- @Override
- public void onSubscribe(Subscription s) {
- s.request(Integer.MAX_VALUE);
- mChapterSub = s;
- }
-
- @Override
- public void onNext(ChapterInfoBean chapterInfoBean) {
- //存储数据
- BookRepository.getInstance().saveChapterInfo(
- bookId, title, chapterInfoBean.getBody()
- );
- EventManager.Companion.getInstance().postEvent(new FinishChapterEvent());
- //将获取到的数据进行存储
- title = titles.poll();
- }
-
- @Override
- public void onError(Throwable t) {
- //只有第一个加载失败才会调用errorChapter
- if (bookChapters.get(0).getTitle().equals(title)) {
- EventManager.Companion.getInstance().postEvent(new ErrorChapterEvent());
- }
- LogUtils.e(t);
- }
-
- @Override
- public void onComplete() {
- }
- }
- );
- }
-
- /**
- * 注意这里用的是同步请求
- */
- public Single getChapterInfo(String id) {
- HashMap map = new HashMap<>();
- map.put("article_id", id);
- return accountService.getBookArticleDetail(getUrlString(Urls.getDetail, map))
- .map(bean -> bean.getArticle().get(0));
- }
-
- /**
- * get方法拼接字符串
- */
- private String getUrlString(String path, HashMap query) {
- String mypath = path;
- if (query != null && query.size() > 0) {
- StringBuilder pathWithQuery = new StringBuilder(path);
- if (!path.contains("?")) {
- pathWithQuery.append("?");
- } else {
- pathWithQuery.append("&");
- }
-
- for (Map.Entry stringStringEntry : query.entrySet()) {
- String key = (String) ((Map.Entry) stringStringEntry).getKey();
- String val = (String) ((Map.Entry) stringStringEntry).getValue();
- pathWithQuery.append(key);
- pathWithQuery.append("=");
- pathWithQuery.append(val);
- pathWithQuery.append("&");
- }
-
- pathWithQuery.deleteCharAt(pathWithQuery.length() - 1);
- mypath = pathWithQuery.toString();
- }
-
- return mypath;
- }
-
- /**
- * 减少请求info类的数量,直接用map替代实体类,返回body
- */
- private RequestBody mapToBody(HashMap map) {
- return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), ServiceGenerator.formatResponse(map));
- }
-
-}
diff --git a/app/src/main/java/com/novel/read/http/AccountManager.kt b/app/src/main/java/com/novel/read/http/AccountManager.kt
new file mode 100644
index 0000000..fa3c37f
--- /dev/null
+++ b/app/src/main/java/com/novel/read/http/AccountManager.kt
@@ -0,0 +1,309 @@
+package com.novel.read.http
+
+import android.content.Context
+import android.text.TextUtils
+import android.util.Log
+
+import com.mango.mangolib.event.EventManager
+import com.mango.mangolib.http.MyRequestType
+import com.mango.mangolib.http.ServiceCallback
+import com.novel.read.event.AddBookSignEvent
+import com.novel.read.event.BookArticleEvent
+import com.novel.read.event.DeleteBookSignEvent
+import com.novel.read.event.ErrorChapterEvent
+import com.novel.read.event.FinishChapterEvent
+import com.novel.read.event.GetBookDetailEvent
+import com.novel.read.event.GetBookSignEvent
+import com.novel.read.event.GetCategoryTypeEvent
+import com.novel.read.event.GetRecommendBookEvent
+import com.novel.read.event.HotSearchEvent
+import com.novel.read.event.LoginEvent
+import com.novel.read.event.SearchListEvent
+import com.novel.read.event.VersionEvent
+import com.novel.read.http.service.AccountService
+import com.novel.read.model.db.ChapterInfoBean
+import com.novel.read.model.db.dbManage.BookRepository
+import com.novel.read.model.protocol.BookDetailResp
+import com.novel.read.model.protocol.RankByUpadateResp
+import com.novel.read.model.protocol.RecommendListResp
+import com.novel.read.utlis.LogUtils
+import com.novel.read.utlis.PhoneUtils
+import com.novel.read.widget.page.TxtChapter
+
+import org.reactivestreams.Subscriber
+import org.reactivestreams.Subscription
+
+import java.util.ArrayDeque
+import java.util.ArrayList
+import java.util.HashMap
+
+import io.reactivex.Single
+import io.reactivex.android.schedulers.AndroidSchedulers
+import io.reactivex.schedulers.Schedulers
+import okhttp3.MediaType
+import okhttp3.RequestBody
+import retrofit2.Callback
+import java.util.Map
+
+/**
+ * Created by zlj on 2017/2/14.
+ */
+
+class AccountManager private constructor() {
+
+ private val accountService: AccountService
+
+ private var mChapterSub: Subscription? = null
+
+ init {
+ accountService = ServiceGenerator.createService(AccountService::class.java, MyRequestType.URL_TEXT)
+ }
+
+ fun getRecommendBook(bookId: String, limit: String) {
+ val map = HashMap()
+ map["book_id"] = bookId
+ val call = accountService.getRecommendBook(getUrlString(Urls.getRecommend, map))
+ call.enqueue(ServiceCallback(GetRecommendBookEvent::class.java))
+ }
+
+ fun getBookDetail(bookId: String) {
+ val map = HashMap()
+ map["book_id"] = bookId
+ val call = accountService.getBookDetail(getUrlString(Urls.getBookDetail, map))
+ call.enqueue(ServiceCallback(GetBookDetailEvent::class.java))
+ }
+
+ fun getBookDetails(bookId: String): Single { //rxjava 获取多书籍详情 合并请求调用
+ val map = HashMap()
+ map["book_id"] = bookId
+ return accountService.getBookDetails(getUrlString(Urls.getBookDetail, map))
+ }
+
+ fun getCategoryType() {
+ val map = HashMap()
+ val call = accountService.getCategoryType(getUrlString(Urls.getCategoryType, map))
+ call.enqueue(ServiceCallback(GetCategoryTypeEvent::class.java))
+ }
+
+ fun getHotSearch() {
+ val map = HashMap()
+ val call = accountService.getHotSearch(getUrlString(Urls.getHotSearch, map))
+ call.enqueue(ServiceCallback(HotSearchEvent::class.java))
+ }
+
+ fun getBookArticle(bookId: String, hasContent: String, page: String, limit: String) {
+ val map = HashMap()
+ map["book_id"] = bookId
+ map["has_content"] = hasContent
+ map["page"] = page
+ map["limit"] = limit
+ val call = accountService.getBookArticle(getUrlString(Urls.getBookArticle, map))
+ call.enqueue(ServiceCallback(BookArticleEvent::class.java))
+ }
+
+
+ fun getSearchBookList(category_id: String, key: String, page: Int) {
+ val map = HashMap()
+ if (!TextUtils.isEmpty(category_id)) {
+ map["category_id"] = category_id
+ } else {
+ map["category_id"] = "0"
+ }
+ if (!TextUtils.isEmpty(key)) {
+ map["key"] = key
+ }
+ map["page"] = page.toString()
+ val call = accountService.getSearchList(getUrlString(Urls.getBookList, map))
+ call.enqueue(ServiceCallback(SearchListEvent::class.java))
+ }
+
+ fun getRecommendList(listType: String, callback: Callback) {
+ val map = HashMap()
+ map["type"] = listType
+ val call = accountService.getRecommendList(getUrlString(Urls.getRecommendList, map))
+ call.enqueue(callback)
+
+ }
+
+ fun getRankByUpdate(page: Int, limit: Int, callback: Callback) {
+ val map = HashMap()
+ if (!TextUtils.isEmpty(page.toString())) {
+ map["page"] = page.toString()
+ }
+ if (limit != 0) {
+ map["limit"] = limit.toString()
+ }
+ val call = accountService.getRankByUpdate(getUrlString(Urls.getRankByUpdate, map))
+ call.enqueue(callback)
+ }
+
+ fun getRankList(
+ type: String,
+ sex: String,
+ dateType: String,
+ page: String,
+ callback: Callback
+ ) {
+ val map = HashMap()
+ map["type"] = type
+ map["gender"] = sex
+ map["date_type"] = dateType
+ map["page"] = page
+ val call = accountService.getRankList(getUrlString(Urls.getRankList, map))
+ call.enqueue(callback)
+ }
+
+ fun checkVersion(versionCode: Int) {
+ val map = HashMap()
+ map["version"] = versionCode.toString()
+ // map.put("shell", Constant.shell);
+ val call = accountService.checkVersion(getUrlString(Urls.checkVersion, map))
+ call.enqueue(ServiceCallback(VersionEvent::class.java))
+ }
+
+ fun addSign(bookId: String, articleId: String, content: String) {
+ val map = HashMap()
+ map["book_id"] = bookId
+ map["article_id"] = articleId
+ map["content"] = content
+ val call = accountService.addSign(Urls.addBookSign, mapToBody(map))
+ call.enqueue(ServiceCallback(AddBookSignEvent::class.java))
+ }
+
+ fun deleteSign(signIds: String) {
+ val map = HashMap()
+ map["sign_ids"] = signIds
+ val call = accountService.deleteSign(Urls.deleteSign, mapToBody(map))
+ call.enqueue(ServiceCallback(DeleteBookSignEvent::class.java))
+ }
+
+ fun getSignList(bookId: String) {
+ val map = HashMap()
+ map["book_id"] = bookId
+ val call = accountService.getSignList(getUrlString(Urls.getBookSign, map))
+ call.enqueue(ServiceCallback(GetBookSignEvent::class.java))
+ }
+
+ fun login(mContext: Context) {
+ val map = HashMap()
+ Log.e("getUniquePsuedoID", "login: " + PhoneUtils.getUniquePsuedoID())
+ map["code"] = PhoneUtils.getUniquePsuedoID()
+ val call = accountService.login(Urls.login, mapToBody(map))
+ call.enqueue(ServiceCallback(LoginEvent::class.java))
+ }
+
+ fun getBookArticleDetail(bookId: String?, bookChapters: List) {
+ val size = bookChapters.size
+ //取消上次的任务,防止多次加载
+ if (mChapterSub != null) {
+ mChapterSub!!.cancel()
+ }
+ val chapterInfos = ArrayList>(bookChapters.size)
+ val titles = ArrayDeque(bookChapters.size)
+
+ // 将要下载章节,转换成网络请求。
+ for (i in 0 until size) {
+ val bookChapter = bookChapters[i]
+ // 网络中获取数据
+ val chapterInfoSingle = getChapterInfo(bookChapter.chapterId)
+ chapterInfos.add(chapterInfoSingle)
+ titles.add(bookChapter.title)
+ }
+
+ Single.concat(chapterInfos)
+ .subscribeOn(Schedulers.io())
+ .observeOn(AndroidSchedulers.mainThread())
+ .subscribe(object : Subscriber {
+ internal var title = titles.poll()
+
+ override fun onSubscribe(s: Subscription) {
+ s.request(Integer.MAX_VALUE.toLong())
+ mChapterSub = s
+ }
+
+ override fun onNext(chapterInfoBean: ChapterInfoBean) {
+ //存储数据
+ BookRepository.getInstance().saveChapterInfo(
+ bookId, title, chapterInfoBean.body
+ )
+ EventManager.instance.postEvent(FinishChapterEvent())
+ //将获取到的数据进行存储
+ title = titles.poll()
+ }
+
+ override fun onError(t: Throwable) {
+ //只有第一个加载失败才会调用errorChapter
+ if (bookChapters[0].title == title) {
+ EventManager.instance.postEvent(ErrorChapterEvent())
+ }
+ LogUtils.e(t)
+ }
+
+ override fun onComplete() {}
+ }
+ )
+ }
+
+ /**
+ * 注意这里用的是同步请求
+ */
+ fun getChapterInfo(id: String): Single {
+ val map = HashMap()
+ map["article_id"] = id
+ return accountService.getBookArticleDetail(getUrlString(Urls.getDetail, map))
+ .map { bean -> bean.article[0] }
+ }
+
+ /**
+ * get方法拼接字符串
+ */
+ private fun getUrlString(path: String, query: HashMap?): String {
+ var mypath = path
+ if (query != null && query.size > 0) {
+ val pathWithQuery = StringBuilder(path)
+ if (!path.contains("?")) {
+ pathWithQuery.append("?")
+ } else {
+ pathWithQuery.append("&")
+ }
+
+ for (stringStringEntry in query.entries) {
+ val key = (stringStringEntry as Map.Entry<*, *>).key as String
+ val `val` = (stringStringEntry as Map.Entry<*, *>).value as String
+ pathWithQuery.append(key)
+ pathWithQuery.append("=")
+ pathWithQuery.append(`val`)
+ pathWithQuery.append("&")
+ }
+
+ pathWithQuery.deleteCharAt(pathWithQuery.length - 1)
+ mypath = pathWithQuery.toString()
+ }
+
+ return mypath
+ }
+
+ /**
+ * 减少请求info类的数量,直接用map替代实体类,返回body
+ */
+ private fun mapToBody(map: HashMap): RequestBody {
+ return RequestBody.create(
+ MediaType.parse("application/json; charset=utf-8"),
+ ServiceGenerator.formatResponse(map)
+ )
+ }
+
+ companion object {
+
+ private var instance: AccountManager? = null
+
+ @Synchronized
+ fun getInstance(): AccountManager {
+ if (instance == null) {
+ instance = AccountManager()
+ }
+ return instance as AccountManager
+ }
+ }
+
+}
diff --git a/app/src/main/java/com/novel/read/http/service/AccountService.java b/app/src/main/java/com/novel/read/http/service/AccountService.java
deleted file mode 100644
index 69f9a17..0000000
--- a/app/src/main/java/com/novel/read/http/service/AccountService.java
+++ /dev/null
@@ -1,94 +0,0 @@
-package com.novel.read.http.service;
-
-import com.mango.mangolib.http.ErrorResponse;
-import com.novel.read.model.protocol.BookArticleResp;
-import com.novel.read.model.protocol.BookDetailResp;
-import com.novel.read.model.protocol.CategoryTypeResp;
-import com.novel.read.model.protocol.ChapterInfoPackage;
-import com.novel.read.model.protocol.HotSearchResp;
-import com.novel.read.model.protocol.MarkResp;
-import com.novel.read.model.protocol.RankByUpadateResp;
-import com.novel.read.model.protocol.RecommendBookResp;
-import com.novel.read.model.protocol.RecommendListResp;
-import com.novel.read.model.protocol.SearchResp;
-import com.novel.read.model.protocol.UidResp;
-import com.novel.read.model.protocol.VersionResp;
-
-import io.reactivex.Single;
-import okhttp3.RequestBody;
-import retrofit2.Call;
-import retrofit2.http.Body;
-import retrofit2.http.GET;
-import retrofit2.http.POST;
-import retrofit2.http.Url;
-
-/**
- * Created by Administrator on 2017/2/14.
- */
-
-public interface AccountService {
-
- //获取推荐书籍
- @GET
- Call getRecommendBook(@Url String url);
-
- //获取书籍详情
- @GET
- Call getBookDetail(@Url String url);
-
- //获取书籍详情
- @GET
- Single getBookDetails(@Url String url);
-
- //小说类型
- @GET
- Call getCategoryType(@Url String url);
-
- //热搜
- @GET
- Call getHotSearch(@Url String url);
-
- //书籍章节
- @GET
- Call getBookArticle(@Url String url);
-
- //搜索
- @GET
- Call getSearchList(@Url String url);
-
- //获取推荐
- @GET
- Call getRecommendList(@Url String url);
-
- //获取最新排行
- @GET
- Call getRankByUpdate(@Url String url);
-
- //获取排行
- @GET
- Call getRankList(@Url String url);
-
- //获取新版本
- @GET
- Call checkVersion(@Url String url);
-
- //添加标签
- @POST
- Call addSign(@Url String url, @Body RequestBody body);
-
- //删除标签
- @POST
- Call deleteSign(@Url String url, @Body RequestBody body);
-
- //获取标签
- @GET
- Call getSignList(@Url String url);
-
- //登录
- @POST
- Call login(@Url String url, @Body RequestBody body);
-
- //获取书籍章节详情
- @GET
- Single getBookArticleDetail(@Url String url);
-}
\ No newline at end of file
diff --git a/app/src/main/java/com/novel/read/http/service/AccountService.kt b/app/src/main/java/com/novel/read/http/service/AccountService.kt
new file mode 100644
index 0000000..173028f
--- /dev/null
+++ b/app/src/main/java/com/novel/read/http/service/AccountService.kt
@@ -0,0 +1,94 @@
+package com.novel.read.http.service
+
+import com.mango.mangolib.http.ErrorResponse
+import com.novel.read.model.protocol.BookArticleResp
+import com.novel.read.model.protocol.BookDetailResp
+import com.novel.read.model.protocol.CategoryTypeResp
+import com.novel.read.model.protocol.ChapterInfoPackage
+import com.novel.read.model.protocol.HotSearchResp
+import com.novel.read.model.protocol.MarkResp
+import com.novel.read.model.protocol.RankByUpadateResp
+import com.novel.read.model.protocol.RecommendBookResp
+import com.novel.read.model.protocol.RecommendListResp
+import com.novel.read.model.protocol.SearchResp
+import com.novel.read.model.protocol.UidResp
+import com.novel.read.model.protocol.VersionResp
+
+import io.reactivex.Single
+import okhttp3.RequestBody
+import retrofit2.Call
+import retrofit2.http.Body
+import retrofit2.http.GET
+import retrofit2.http.POST
+import retrofit2.http.Url
+
+/**
+ * Created by Administrator on 2017/2/14.
+ */
+
+interface AccountService {
+
+ //获取推荐书籍
+ @GET
+ fun getRecommendBook(@Url url: String): Call
+
+ //获取书籍详情
+ @GET
+ fun getBookDetail(@Url url: String): Call
+
+ //获取书籍详情
+ @GET
+ fun getBookDetails(@Url url: String): Single
+
+ //小说类型
+ @GET
+ fun getCategoryType(@Url url: String): Call
+
+ //热搜
+ @GET
+ fun getHotSearch(@Url url: String): Call
+
+ //书籍章节
+ @GET
+ fun getBookArticle(@Url url: String): Call
+
+ //搜索
+ @GET
+ fun getSearchList(@Url url: String): Call
+
+ //获取推荐
+ @GET
+ fun getRecommendList(@Url url: String): Call
+
+ //获取最新排行
+ @GET
+ fun getRankByUpdate(@Url url: String): Call
+
+ //获取排行
+ @GET
+ fun getRankList(@Url url: String): Call
+
+ //获取新版本
+ @GET
+ fun checkVersion(@Url url: String): Call
+
+ //添加标签
+ @POST
+ fun addSign(@Url url: String, @Body body: RequestBody): Call
+
+ //删除标签
+ @POST
+ fun deleteSign(@Url url: String, @Body body: RequestBody): Call
+
+ //获取标签
+ @GET
+ fun getSignList(@Url url: String): Call
+
+ //登录
+ @POST
+ fun login(@Url url: String, @Body body: RequestBody): Call
+
+ //获取书籍章节详情
+ @GET
+ fun getBookArticleDetail(@Url url: String): Single
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/novel/read/service/DownloadService.java b/app/src/main/java/com/novel/read/service/DownloadService.java
index 18c6d10..7998b5d 100644
--- a/app/src/main/java/com/novel/read/service/DownloadService.java
+++ b/app/src/main/java/com/novel/read/service/DownloadService.java
@@ -308,7 +308,7 @@ public class DownloadService extends BaseService {
final int[] result = {LOAD_NORMAL};
//问题:(这里有个问题,就是body其实比较大,如何获取数据流而不是对象,)是不是直接使用OkHttpClient交互会更好一点
- Disposable disposable = AccountManager.getInstance()
+ Disposable disposable = AccountManager.Companion.getInstance()
.getChapterInfo(bean.getId())
//表示在当前环境下执行
.subscribe(