From 3275404818afd46efdbbf06737e6b12f695c6ba7 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 17 Nov 2019 17:09:31 +0800 Subject: [PATCH] up --- .../source/debug/RssSourceDebugActivity.kt | 67 ++++++++++++++++++- .../rss/source/debug/RssSourceDebugAdapter.kt | 29 ++++++++ .../rss/source/debug/RssSourceDebugModel.kt | 47 +++++++++++++ 3 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt create mode 100644 app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt index 3c476c921..d886c8bb5 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt @@ -1,16 +1,79 @@ package io.legado.app.ui.rss.source.debug import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R -import io.legado.app.base.BaseActivity +import io.legado.app.base.VMBaseActivity +import io.legado.app.lib.theme.ATH +import io.legado.app.lib.theme.accentColor +import io.legado.app.ui.qrcode.QrCodeActivity +import io.legado.app.utils.getViewModel +import io.legado.app.utils.gone +import kotlinx.android.synthetic.main.activity_source_debug.* +import kotlinx.android.synthetic.main.view_search.* +import kotlinx.coroutines.launch +import org.jetbrains.anko.startActivityForResult +import org.jetbrains.anko.toast -class RssSourceDebugActivity : BaseActivity(R.layout.activity_source_debug) { +class RssSourceDebugActivity : VMBaseActivity(R.layout.activity_source_debug) { + override val viewModel: RssSourceDebugModel + get() = getViewModel(RssSourceDebugModel::class.java) + + private lateinit var adapter: RssSourceDebugAdapter + private val qrRequestCode = 101 override fun onActivityCreated(savedInstanceState: Bundle?) { + initRecyclerView() + initSearchView() + viewModel.observe { state, msg -> + launch { + adapter.addItem(msg) + if (state == -1 || state == 1000) { + rotate_loading.hide() + } + } + } + viewModel.initData(intent.getStringExtra("key")) { + startSearch() + } + } + + private fun initRecyclerView() { + ATH.applyEdgeEffectColor(recycler_view) + adapter = RssSourceDebugAdapter(this) + recycler_view.layoutManager = LinearLayoutManager(this) + recycler_view.adapter = adapter + rotate_loading.loadingColor = accentColor + } + private fun initSearchView() { + search_view.gone() } + override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.source_debug, menu) + return super.onCompatCreateOptionsMenu(menu) + } + + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.action_scan -> { + startActivityForResult(qrRequestCode) + } + } + return super.onCompatOptionsItemSelected(item) + } + private fun startSearch() { + adapter.clearItems() + viewModel.startDebug({ + rotate_loading.show() + }, { + toast("未获取到源") + }) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt new file mode 100644 index 000000000..a5e4eecbf --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugAdapter.kt @@ -0,0 +1,29 @@ +package io.legado.app.ui.rss.source.debug + +import android.content.Context +import android.view.View +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_log.view.* + +class RssSourceDebugAdapter(context: Context) : + SimpleRecyclerAdapter(context, R.layout.item_log) { + override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { + holder.itemView.apply { + if (text_view.getTag(R.id.tag1) == null) { + val listener = object : View.OnAttachStateChangeListener { + override fun onViewAttachedToWindow(v: View) { + text_view.isCursorVisible = false + text_view.isCursorVisible = true + } + + override fun onViewDetachedFromWindow(v: View) {} + } + text_view.addOnAttachStateChangeListener(listener) + text_view.setTag(R.id.tag1, listener) + } + text_view.text = item + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt new file mode 100644 index 000000000..0c8483497 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt @@ -0,0 +1,47 @@ +package io.legado.app.ui.rss.source.debug + +import android.app.Application +import io.legado.app.App +import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.RssSource +import io.legado.app.model.SourceDebug + +class RssSourceDebugModel(application: Application) : BaseViewModel(application), + SourceDebug.Callback { + + private var rssSource: RssSource? = null + + private var callback: ((Int, String) -> Unit)? = null + + fun initData(sourceUrl: String?, finally: () -> Unit) { + sourceUrl?.let { + execute { + rssSource = App.db.rssSourceDao().getByKey(sourceUrl) + }.onFinally { + finally() + } + } + } + + fun observe(callback: (Int, String) -> Unit) { + this.callback = callback + } + + fun startDebug(start: (() -> Unit)? = null, error: (() -> Unit)? = null) { + rssSource?.let { + start?.invoke() + SourceDebug.callback = this + SourceDebug.startDebug(it) + } ?: error?.invoke() + } + + override fun printLog(state: Int, msg: String) { + callback?.invoke(state, msg) + } + + override fun onCleared() { + super.onCleared() + SourceDebug.cancelDebug(true) + } + +}