From 24b22ea2e142954088c61ae1f217d16312550462 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Wed, 31 Jul 2019 10:21:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/base/adapter/CommonRecyclerAdapter.kt | 43 ++++++------ .../java/io/legado/app/help/EventMessage.kt | 50 ++++++++++++++ .../app/ui/sourcedebug/SourceDebugActivity.kt | 65 +++++-------------- .../app/ui/sourcedebug/SourceDebugAdapter.kt | 31 ++------- .../app/ui/sourcedebug/SourceDebugModel.kt | 51 +++++++++++++++ .../app/ui/sourceedit/SourceEditActivity.kt | 2 +- 6 files changed, 146 insertions(+), 96 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/EventMessage.kt create mode 100644 app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugModel.kt diff --git a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt index bd0cea427..30f8f794f 100644 --- a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt @@ -53,11 +53,11 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec } fun > addItemViewDelegate(viewType: Int, delegate: DELEGATE) { - itemDelegates.put(viewType, delegate) + itemDelegates[viewType] = delegate } fun > addItemViewDelegate(delegate: DELEGATE) { - itemDelegates.put(itemDelegates.size, delegate) + itemDelegates[itemDelegates.size] = delegate } fun > addItemViewDelegates(vararg delegates: DELEGATE) { @@ -148,26 +148,15 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec synchronized(lock) { val oldSize = getActualItemCount() if (this.items.add(item)) { - if (oldSize == 0) { - notifyDataSetChanged() - } else { - notifyItemInserted(oldSize + getHeaderCount()) - } + notifyItemInserted(oldSize + getHeaderCount()) } } } fun addItems(position: Int, newItems: List) { synchronized(lock) { - val oldSize = getActualItemCount() - if (position in 0 until oldSize) { - if (if (oldSize == 0) this.items.addAll(newItems) else this.items.addAll(position, newItems)) { - if (oldSize == 0) { - notifyDataSetChanged() - } else { - notifyItemRangeChanged(position + getHeaderCount(), newItems.size) - } - } + if (this.items.addAll(position, newItems)) { + notifyItemRangeInserted(position + getHeaderCount(), newItems.size) } } } @@ -176,33 +165,32 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec synchronized(lock) { val oldSize = getActualItemCount() if (this.items.addAll(newItems)) { - if (oldSize == 0) { - notifyDataSetChanged() - } else { - notifyItemRangeChanged(oldSize + getHeaderCount(), newItems.size) - } + notifyItemRangeInserted(oldSize + getHeaderCount(), newItems.size) } } } fun removeItem(position: Int) { synchronized(lock) { - if (this.items.removeAt(position) != null) + if (this.items.removeAt(position) != null) { notifyItemRemoved(position + getHeaderCount()) + } } } fun removeItem(item: ITEM) { synchronized(lock) { - if (this.items.remove(item)) + if (this.items.remove(item)) { notifyItemRemoved(this.items.indexOf(item) + getHeaderCount()) + } } } fun removeItems(items: List) { synchronized(lock) { - if (this.items.removeAll(items)) + if (this.items.removeAll(items)) { notifyDataSetChanged() + } } } @@ -234,6 +222,13 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec } } + fun clearItems() { + synchronized(lock) { + this.items.clear() + notifyDataSetChanged() + } + } + fun isEmpty(): Boolean { return items.isEmpty() } diff --git a/app/src/main/java/io/legado/app/help/EventMessage.kt b/app/src/main/java/io/legado/app/help/EventMessage.kt new file mode 100644 index 000000000..fe9c80994 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/EventMessage.kt @@ -0,0 +1,50 @@ +package io.legado.app.help + +import android.text.TextUtils + +import java.util.Arrays + +class EventMessage { + + var what: Int?=null + var tag: String? = null + var obj: Any? = null + + fun isFrom(tag: String): Boolean { + return TextUtils.equals(this.tag, tag) + } + + fun maybeFrom(vararg tags: String): Boolean { + return listOf(*tags).contains(tag) + } + + companion object { + + fun obtain(tag: String): EventMessage { + val message = EventMessage() + message.tag = tag + return message + } + + fun obtain(what: Int): EventMessage { + val message = EventMessage() + message.what = what + return message + } + + fun obtain(what: Int, obj: Any): EventMessage { + val message = EventMessage() + message.what = what + message.obj = obj + return message + } + + fun obtain(tag: String, obj: Any): EventMessage { + val message = EventMessage() + message.tag = tag + message.obj = obj + return message + } + } + +} diff --git a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt index be5966534..3de2e2fd0 100644 --- a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugActivity.kt @@ -3,48 +3,43 @@ package io.legado.app.ui.sourcedebug import android.os.Bundle import android.view.View import androidx.appcompat.widget.SearchView -import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager -import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseActivity -import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ThemeStore -import io.legado.app.model.WebBook import io.legado.app.model.webbook.SourceDebug import io.legado.app.utils.getViewModel -import io.legado.app.utils.isAbsUrl import kotlinx.android.synthetic.main.activity_source_debug.* import kotlinx.android.synthetic.main.view_title_bar.* -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.launch import org.jetbrains.anko.toast -class SourceDebugActivity : BaseActivity(), SourceDebug.Callback { +class SourceDebugActivity : BaseActivity() { - override val viewModel: AndroidViewModel - get() = getViewModel(AndroidViewModel::class.java) + override val viewModel: SourceDebugModel + get() = getViewModel(SourceDebugModel::class.java) override val layoutID: Int get() = R.layout.activity_source_debug private lateinit var adapter: SourceDebugAdapter - private var bookSource: BookSource? = null - override fun onActivityCreated(viewModel: AndroidViewModel, savedInstanceState: Bundle?) { - launch(IO) { - intent.getStringExtra("key")?.let { - bookSource = App.db.bookSourceDao().findByKey(it) - } - } + override fun onActivityCreated(viewModel: SourceDebugModel, savedInstanceState: Bundle?) { + viewModel.init(intent.getStringExtra("key")) initRecyclerView() initSearchView() + viewModel.logs.observe(this, Observer { + adapter.addItem(it.obj as String) + if (it.what == -1 || it.what == 1000) { + rotate_loading.hide() + } + }) } private fun initRecyclerView() { ATH.applyEdgeEffectColor(recycler_view) - adapter = SourceDebugAdapter() + adapter = SourceDebugAdapter(this) recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.adapter = adapter rotate_loading.loadingColor = ThemeStore.accentColor(this) @@ -69,36 +64,12 @@ class SourceDebugActivity : BaseActivity(), SourceDebug.Callba } private fun startSearch(key: String) { - adapter.logList.clear() - adapter.notifyDataSetChanged() - bookSource?.let { - SourceDebug.debugSource = it.bookSourceUrl + adapter.clearItems() + viewModel.startDebug(key, { rotate_loading.show() - if (key.isAbsUrl()) { - val book = Book() - book.origin = it.bookSourceUrl - book.bookUrl = key - SourceDebug.printLog(it.bookSourceUrl, 1, "开始访问$key") - SourceDebug(WebBook(it), this) - .infoDebug(book) - } else { - SourceDebug.printLog(it.bookSourceUrl, 1, "开始搜索关键字$key") - SourceDebug(WebBook(it), this) - .searchDebug(key) - } - } ?: toast("未获取到书源") - } - - override fun printLog(state: Int, msg: String) { - launch { - synchronized(this) { - adapter.logList.add(msg) - adapter.notifyItemChanged(adapter.logList.size - 1) - if (state == -1 || state == 1000) { - rotate_loading.hide() - } - } - } + }, { + toast("未获取到书源") + }) } override fun onDestroy() { diff --git a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugAdapter.kt b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugAdapter.kt index 0cde606b0..b8b821ea1 100644 --- a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugAdapter.kt @@ -1,32 +1,15 @@ package io.legado.app.ui.sourcedebug -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.recyclerview.widget.RecyclerView +import android.content.Context import io.legado.app.R +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.SimpleRecyclerAdapter import kotlinx.android.synthetic.main.item_source_debug.view.* -class SourceDebugAdapter : RecyclerView.Adapter() { - - val logList = arrayListOf() - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { - return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_source_debug, parent, false)) - } - - override fun onBindViewHolder(holder: MyViewHolder, position: Int) { - holder.bind(logList[position]) - } - - override fun getItemCount(): Int { - return logList.size - } - - class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { - - fun bind(log: String) = with(itemView) { - text_view.text = log +class SourceDebugAdapter(context: Context) : SimpleRecyclerAdapter(context, R.layout.item_source_debug) { + override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { + holder.itemView.apply { + text_view.text = item } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugModel.kt new file mode 100644 index 000000000..fe3316548 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/sourcedebug/SourceDebugModel.kt @@ -0,0 +1,51 @@ +package io.legado.app.ui.sourcedebug + +import android.app.Application +import androidx.lifecycle.MutableLiveData +import io.legado.app.App +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookSource +import io.legado.app.help.EventMessage +import io.legado.app.model.WebBook +import io.legado.app.model.webbook.SourceDebug +import io.legado.app.utils.isAbsUrl + +class SourceDebugModel(application: Application) : BaseViewModel(application), SourceDebug.Callback { + + val logs: MutableLiveData = MutableLiveData() + + private var bookSource: BookSource? = null + + fun init(sourceUrl: String?) { + sourceUrl?.let { + //优先使用这个,不会抛出异常 + execute { + bookSource = App.db.bookSourceDao().findByKey(sourceUrl) + } + } + } + + fun startDebug(key: String, start: (() -> Unit)? = null, error: (() -> Unit)? = null) { + bookSource?.let { + start?.let { it() } + SourceDebug.debugSource = it.bookSourceUrl + if (key.isAbsUrl()) { + val book = Book() + book.origin = it.bookSourceUrl + book.bookUrl = key + SourceDebug.printLog(it.bookSourceUrl, 1, "开始访问$key") + SourceDebug(WebBook(it), this) + .infoDebug(book) + } else { + SourceDebug.printLog(it.bookSourceUrl, 1, "开始搜索关键字$key") + SourceDebug(WebBook(it), this) + .searchDebug(key) + } + } ?: error?.let { it() } + } + + override fun printLog(state: Int, msg: String) { + logs.postValue(EventMessage.obtain(state, msg)) + } +} diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index a290ffd45..4f1e8a344 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -81,7 +81,7 @@ class SourceEditActivity : BaseActivity(false), KeyboardToo toast("书源名称和URL不能为空") } else { viewModel.save(bookSource) { - startActivity(Pair("key", bookSource.bookSourceUrl)) + startActivity("key" to bookSource.bookSourceUrl) } } }