Commit 7efedf04 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

move "md to spannable" parser from ui thread

parent 3fabf233
......@@ -10,7 +10,7 @@ data class TargetHeader(
val author: Author,
val icon: TargetHeaderIcon,
val title: TargetHeaderTitle,
val body: String?,
val body: CharSequence?,
val date: Date,
val target: AppTarget,
val targetId: Long,
......
......@@ -3,6 +3,8 @@ package ru.terrakok.gitlabclient.presentation.global
import io.reactivex.Single
import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer
import ru.noties.markwon.Markwon
import ru.noties.markwon.SpannableConfiguration
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
import javax.inject.Inject
......@@ -10,15 +12,30 @@ import javax.inject.Inject
* @author Konstantin Tskhovrebov (aka terrakok). Date: 28.05.17
*/
class MarkDownConverter @Inject constructor(
config: Config,
private val schedulers: SchedulersProvider
) {
private val parser: Parser = Parser.builder().build()
private val renderer: HtmlRenderer = HtmlRenderer.builder().build()
data class Config(
val parser: Parser,
val htmlRenderer: HtmlRenderer,
val spannableConfiguration: SpannableConfiguration
)
fun markdownToHtml(raw: String) = Single
private val parser = config.parser
private val renderer = config.htmlRenderer
private val mdConfig = config.spannableConfiguration
fun markdownToHtml(raw: String): Single<String> = Single
.fromCallable {
renderer.render(parser.parse(raw))
}
.subscribeOn(schedulers.computation())
.observeOn(schedulers.ui())
fun markdownToSpannable(raw: String): Single<CharSequence> = Single
.fromCallable {
Markwon.markdown(mdConfig, raw)
}
.subscribeOn(schedulers.computation())
.observeOn(schedulers.ui())
}
\ No newline at end of file
package ru.terrakok.gitlabclient.presentation.my.events
import com.arellomobile.mvp.InjectViewState
import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.Screens
import ru.terrakok.gitlabclient.entity.app.target.TargetHeader
import ru.terrakok.gitlabclient.extension.openInfo
import ru.terrakok.gitlabclient.model.interactor.event.EventInteractor
import ru.terrakok.gitlabclient.model.system.flow.FlowRouter
import ru.terrakok.gitlabclient.presentation.global.BasePresenter
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
import ru.terrakok.gitlabclient.presentation.global.GlobalMenuController
import ru.terrakok.gitlabclient.presentation.global.Paginator
import ru.terrakok.gitlabclient.presentation.global.*
import javax.inject.Inject
/**
......@@ -19,6 +15,7 @@ import javax.inject.Inject
@InjectViewState
class MyEventsPresenter @Inject constructor(
private val eventInteractor: EventInteractor,
private val mdConverter: MarkDownConverter,
private val menuController: GlobalMenuController,
private val errorHandler: ErrorHandler,
private val router: FlowRouter
......@@ -31,7 +28,17 @@ class MyEventsPresenter @Inject constructor(
}
private val paginator = Paginator(
{ eventInteractor.getEvents(it) },
{
eventInteractor.getEvents(it)
.toObservable()
.flatMapIterable { it }
.flatMap { item ->
mdConverter.markdownToSpannable(item.body.toString())
.map { md -> item.copy(body = md) }
.toObservable()
}
.toList()
},
object : Paginator.ViewController<TargetHeader> {
override fun showEmptyProgress(show: Boolean) {
viewState.showEmptyProgress(show)
......
......@@ -8,6 +8,7 @@ import ru.terrakok.gitlabclient.model.interactor.issue.IssueInteractor
import ru.terrakok.gitlabclient.model.system.flow.FlowRouter
import ru.terrakok.gitlabclient.presentation.global.BasePresenter
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
import ru.terrakok.gitlabclient.presentation.global.MarkDownConverter
import ru.terrakok.gitlabclient.presentation.global.Paginator
import javax.inject.Inject
......@@ -18,6 +19,7 @@ import javax.inject.Inject
class MyIssuesPresenter @Inject constructor(
initFilter: Filter,
private val issueInteractor: IssueInteractor,
private val mdConverter: MarkDownConverter,
private val errorHandler: ErrorHandler,
private val router: FlowRouter
) : BasePresenter<MyIssuesView>() {
......@@ -32,7 +34,17 @@ class MyIssuesPresenter @Inject constructor(
}
private val paginator = Paginator(
{ issueInteractor.getMyIssues(filter.createdByMe, filter.onlyOpened, it) },
{
issueInteractor.getMyIssues(filter.createdByMe, filter.onlyOpened, it)
.toObservable()
.flatMapIterable { it }
.flatMap { item ->
mdConverter.markdownToSpannable(item.body.toString())
.map { md -> item.copy(body = md) }
.toObservable()
}
.toList()
},
object : Paginator.ViewController<TargetHeader> {
override fun showEmptyProgress(show: Boolean) {
viewState.showEmptyProgress(show)
......
......@@ -8,6 +8,7 @@ import ru.terrakok.gitlabclient.model.interactor.mergerequest.MergeRequestIntera
import ru.terrakok.gitlabclient.model.system.flow.FlowRouter
import ru.terrakok.gitlabclient.presentation.global.BasePresenter
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
import ru.terrakok.gitlabclient.presentation.global.MarkDownConverter
import ru.terrakok.gitlabclient.presentation.global.Paginator
import javax.inject.Inject
......@@ -15,6 +16,7 @@ import javax.inject.Inject
class MyMergeRequestsPresenter @Inject constructor(
initFilter: Filter,
private val interactor: MergeRequestInteractor,
private val mdConverter: MarkDownConverter,
private val errorHandler: ErrorHandler,
private val router: FlowRouter
) : BasePresenter<MyMergeRequestListView>() {
......@@ -29,7 +31,17 @@ class MyMergeRequestsPresenter @Inject constructor(
}
private val paginator = Paginator(
{ interactor.getMyMergeRequests(filter.createdByMe, filter.onlyOpened, it) },
{
interactor.getMyMergeRequests(filter.createdByMe, filter.onlyOpened, it)
.toObservable()
.flatMapIterable { it }
.flatMap { item ->
mdConverter.markdownToSpannable(item.body.toString())
.map { md -> item.copy(body = md) }
.toObservable()
}
.toList()
},
object : Paginator.ViewController<TargetHeader> {
override fun showEmptyProgress(show: Boolean) {
viewState.showEmptyProgress(show)
......
......@@ -8,6 +8,7 @@ import ru.terrakok.gitlabclient.model.interactor.todo.TodoListInteractor
import ru.terrakok.gitlabclient.model.system.flow.FlowRouter
import ru.terrakok.gitlabclient.presentation.global.BasePresenter
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
import ru.terrakok.gitlabclient.presentation.global.MarkDownConverter
import ru.terrakok.gitlabclient.presentation.global.Paginator
import ru.terrakok.gitlabclient.toothpick.PrimitiveWrapper
import ru.terrakok.gitlabclient.toothpick.qualifier.TodoListPendingState
......@@ -18,8 +19,9 @@ import javax.inject.Inject
*/
@InjectViewState
class MyTodosPresenter @Inject constructor(
private @TodoListPendingState val pendingStateWrapper: PrimitiveWrapper<Boolean>,
@TodoListPendingState private val pendingStateWrapper: PrimitiveWrapper<Boolean>,
private val todoListInteractor: TodoListInteractor,
private val mdConverter: MarkDownConverter,
private val errorHandler: ErrorHandler,
private val router: FlowRouter
) : BasePresenter<MyTodoListView>() {
......@@ -33,7 +35,17 @@ class MyTodosPresenter @Inject constructor(
}
private val paginator = Paginator(
{ todoListInteractor.getMyTodos(isPending, it) },
{
todoListInteractor.getMyTodos(isPending, it)
.toObservable()
.flatMapIterable { it }
.flatMap { item ->
mdConverter.markdownToSpannable(item.body.toString())
.map { md -> item.copy(body = md) }
.toObservable()
}
.toList()
},
object : Paginator.ViewController<TargetHeader> {
override fun showEmptyProgress(show: Boolean) {
viewState.showEmptyProgress(show)
......
......@@ -2,11 +2,17 @@ package ru.terrakok.gitlabclient.toothpick.module
import android.content.Context
import android.content.res.AssetManager
import org.commonmark.parser.Parser
import org.commonmark.renderer.html.HtmlRenderer
import ru.noties.markwon.SpannableConfiguration
import ru.noties.markwon.spans.SpannableTheme
import ru.terrakok.cicerone.Cicerone
import ru.terrakok.cicerone.NavigatorHolder
import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.BuildConfig
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.entity.app.develop.AppInfo
import ru.terrakok.gitlabclient.extension.color
import ru.terrakok.gitlabclient.model.data.auth.AuthHolder
import ru.terrakok.gitlabclient.model.data.storage.Prefs
import ru.terrakok.gitlabclient.model.data.storage.RawAppData
......@@ -34,11 +40,21 @@ class AppModule(context: Context) : Module() {
bind(PrimitiveWrapper::class.java).withName(DefaultPageSize::class.java).toInstance(PrimitiveWrapper(20))
bind(SchedulersProvider::class.java).toInstance(AppSchedulers())
bind(ResourceManager::class.java).singletonInScope()
bind(MarkDownConverter::class.java).singletonInScope()
bind(Base64Tools::class.java).toInstance(Base64Tools())
bind(AssetManager::class.java).toInstance(context.assets)
bind(RawAppData::class.java)
bind(MarkDownConverter.Config::class.java).toInstance(MarkDownConverter.Config(
Parser.builder().build(),
HtmlRenderer.builder().build(),
SpannableConfiguration.builder(context)
.theme(SpannableTheme.builderWithDefaults(context)
.codeBackgroundColor(context.color(R.color.beige))
.build())
.build()
))
bind(MarkDownConverter::class.java).singletonInScope()
//Navigation
val cicerone = Cicerone.create(FlowRouter())
bind(Router::class.java).toInstance(cicerone.router)
......
......@@ -8,8 +8,6 @@ import com.hannesdorfmann.adapterdelegates3.AdapterDelegate
import kotlinx.android.synthetic.main.item_target_badge.view.*
import kotlinx.android.synthetic.main.item_target_header.view.*
import ru.noties.markwon.Markwon
import ru.noties.markwon.SpannableConfiguration
import ru.noties.markwon.spans.SpannableTheme
import ru.terrakok.gitlabclient.R
import ru.terrakok.gitlabclient.entity.app.target.TargetBadge
import ru.terrakok.gitlabclient.entity.app.target.TargetBadgeIcon
......@@ -43,7 +41,6 @@ class TargetHeaderAdapterDelegate(
private val clickListener: (TargetHeader) -> Unit
) : RecyclerView.ViewHolder(view) {
private lateinit var item: TargetHeader
private val mdConfig: SpannableConfiguration
init {
view.setOnClickListener { clickListener(item) }
......@@ -52,13 +49,6 @@ class TargetHeaderAdapterDelegate(
(1..5).forEach {
view.badgesContainer.inflate(R.layout.item_target_badge, true)
}
val mdTheme = SpannableTheme.builderWithDefaults(view.context)
.codeBackgroundColor(view.context.color(R.color.beige))
.build()
mdConfig = SpannableConfiguration.builder(view.context)
.theme(mdTheme)
.build()
}
fun bind(item: TargetHeader) {
......@@ -66,7 +56,7 @@ class TargetHeaderAdapterDelegate(
val res = view.resources
view.titleTextView.text = item.title.getHumanName(res)
Markwon.setMarkdown(view.descriptionTextView, mdConfig, item.body ?: "")
Markwon.setText(view.descriptionTextView, item.body ?: "")
view.descriptionTextView.movementMethod = null //disable internal link click
view.avatarImageView.loadRoundedImage(item.author.avatarUrl)
view.iconImageView.setImageResource(item.icon.getIcon())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment