pull/49/head
parent
acafc3dab1
commit
3275404818
@ -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<RssSourceDebugModel>(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<QrCodeActivity>(qrRequestCode) |
||||
} |
||||
} |
||||
return super.onCompatOptionsItemSelected(item) |
||||
} |
||||
|
||||
private fun startSearch() { |
||||
adapter.clearItems() |
||||
viewModel.startDebug({ |
||||
rotate_loading.show() |
||||
}, { |
||||
toast("未获取到源") |
||||
}) |
||||
} |
||||
} |
@ -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<String>(context, R.layout.item_log) { |
||||
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) { |
||||
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 |
||||
} |
||||
} |
||||
} |
@ -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) |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue