pull/32/head
parent
55857cce15
commit
24b22ea2e1
@ -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 |
||||
} |
||||
} |
||||
|
||||
} |
@ -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<SourceDebugAdapter.MyViewHolder>() { |
||||
|
||||
val logList = arrayListOf<String>() |
||||
|
||||
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<String>(context, R.layout.item_source_debug) { |
||||
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { |
||||
holder.itemView.apply { |
||||
text_view.text = item |
||||
} |
||||
} |
||||
} |
@ -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<EventMessage> = 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)) |
||||
} |
||||
} |
Loading…
Reference in new issue