Commit 198311a2 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

proper match regex sometimes , sometimes not..

parent f04a265c
package com.biganto.visual.roompark.presentation.screen.article
import PicassoImageGetter
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import butterknife.BindView
import butterknife.OnClick
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.base.RoomParkApplication
import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.conductor.BigantoBaseController
import com.biganto.visual.roompark.presentation.screen.article.util.HtmlPageAdapter
import com.biganto.visual.roompark.presentation.screen.article.util.HtmlTag
import com.biganto.visual.roompark.util.extensions.formatToSimple
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.textview.MaterialTextView
......@@ -53,12 +54,14 @@ class ArticleScreenController :
@BindView(R.id.articleHeaderBlock)
lateinit var headerBlock: ViewGroup
@BindView(R.id.articleContent)
lateinit var contentView: MaterialTextView
@BindView(R.id.articleCloseButton)
lateinit var closeArticle: FloatingActionButton
@BindView(R.id.articleBodyRecyclerView)
lateinit var articleRecyclerView: RecyclerView
val blurPreview:ImageView by lazy {
headerBlock.findViewById<ImageView>(R.id.articlePreviewBlurred)
......@@ -82,7 +85,11 @@ class ArticleScreenController :
}
private fun bindRecycler() {
articleRecyclerView.isNestedScrollingEnabled = true
articleRecyclerView.layoutManager =
LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
articleRecyclerView.adapter = HtmlPageAdapter()
articleRecyclerView.itemAnimator = null
}
override fun onViewBound(v: View) {
......@@ -109,37 +116,32 @@ class ArticleScreenController :
title.text = viewState.item.title
val imageGetter = PicassoImageGetter(contentView)
val parsedHtml = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
Html.fromHtml(viewState.item.htmlBody.replace("<br>","<br />"),
HtmlCompat.FROM_HTML_MODE_COMPACT
,
imageGetter
// Html.ImageGetter { url ->
// val rUrl = "room-park.ru$url"
// Timber.d("trying to get image: $rUrl")
// Picasso.get()
// .load(rUrl)
// .get()
// .toDrawable(resources!!)
//// .current
// }
, null)
else Html.fromHtml(viewState.item.htmlBody.replace("<br>","<br />")
,
imageGetter
// Html.ImageGetter { url ->
// val rUrl = "room-park.ru$url"
// Timber.d("trying to get image: $rUrl")
// Picasso.get()
// .load(rUrl)
// .get()
// .toDrawable(resources!!)
//// .current
// }
, null)
contentView.text = parsedHtml
// val imageGetter = PicassoImageGetter()
val _escaped = Html.escapeHtml(viewState.item.htmlBody)
val charset = Charsets.UTF_8
val reg = "<img\\b(?=\\s)(?=(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"][^\\s>]*)*?\\ssrc=['\"]([^\"]*)['\"]?)(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"\\s]*)*\"\\s?\\/?>"
.toRegex()
val _esc =_escaped.toByteArray(charset).contentToString()
val z = viewState.item.htmlBody.replace("<\\br>","\n")
.replace("<br>","\n")
.replace("</br>","\n").toString()
Timber.d(" escaped lines: ${z}")
val tags = z.lines().map{
val r = reg.matchEntire(it)?.groupValues?.last()
Timber.d("matched: $r")
val result = if (r == null) HtmlTag.Text(it)
else HtmlTag.ImageSource(r)
Timber.d(" result: ${result}")
result
}.toList()
(articleRecyclerView.adapter as HtmlPageAdapter).setItems(tags)
articleDate.text = viewState.item.published.formatToSimple
......
package com.biganto.visual.roompark.presentation.screen.article.util
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import butterknife.ButterKnife
import com.biganto.visual.roompark.R
import com.google.android.material.textview.MaterialTextView
import com.squareup.picasso.Picasso
import timber.log.Timber
/**
* Created by Vladislav Bogdashkin on 29.01.2020.
*/
sealed class HtmlTag {
class Text(val text:String):HtmlTag()
class ImageSource(val src:String):HtmlTag()
}
class HtmlPageAdapter : RecyclerView.Adapter<HtmlTagViewHolder<HtmlTag>>() {
private val list = mutableListOf<HtmlTag>()
override fun getItemViewType(position: Int): Int {
return when (list[position]){
is HtmlTag.Text -> HtmlViewType.TEXT.viewType
is HtmlTag.ImageSource -> HtmlViewType.IMG.viewType
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HtmlTagViewHolder<HtmlTag> {
val ret = when (HtmlViewType.fromInt(viewType)) {
HtmlViewType.TEXT ->
HtmlTextViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.htlm_text_viewholder, parent, false)
) as HtmlTagViewHolder<HtmlTag>
HtmlViewType.IMG ->
HtmlImageViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.htlm_image_viewholder, parent, false)
) as HtmlTagViewHolder<HtmlTag>
}
return ret
}
override fun onBindViewHolder(holder: HtmlTagViewHolder<HtmlTag>, position: Int) {
Timber.d(list[position].toString())
Timber.d(" holder: ${holder::class} - list: ${list[position]::class}")
Timber.d("position - $position")
holder.bindModel(list[position])
}
override fun getItemCount(): Int = list.size
fun setItems(items:List<HtmlTag>){
this.list.clear()
this.list.addAll(items)
notifyDataSetChanged()
}
}
abstract class HtmlTagViewHolder<M:HtmlTag>(itemView: View) : RecyclerView.ViewHolder(itemView){
abstract fun onViewBound(model: M)
fun bindModel(model: M){
ButterKnife.bind(this, itemView)
bindedModel = model
onViewBound(model)
}
protected lateinit var bindedModel: M
}
class HtmlTextViewHolder(itemView: View) : HtmlTagViewHolder<HtmlTag.Text>(itemView) {
override fun onViewBound(model: HtmlTag.Text) {
(itemView as MaterialTextView).text = model.text
}
}
class HtmlImageViewHolder(itemView: View) :HtmlTagViewHolder<HtmlTag.ImageSource>(itemView) {
override fun onViewBound(model: HtmlTag.ImageSource) {
Picasso.get().load(
"https://room-park.ru${model.src}"
)
.into(itemView as ImageView)
}
}
private enum class HtmlViewType(val viewType:Int){
TEXT(0),
IMG(1);
companion object {
fun fromInt(type: Int) = HtmlViewType.values().first { it.viewType == type }
}
}
......@@ -19,11 +19,12 @@
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/default_image_placeholder" />
<ImageView
<com.biganto.visual.roompark.util.view_utils.image_view.RoundedImageView
android:id="@+id/articlePreview"
app:image_corner_radius="4dp"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_marginStart="27dp"
android:layout_marginStart="16dp"
android:layout_marginTop="44dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
......
......@@ -4,7 +4,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
<LinearLayout
android:id="@+id/article_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
......@@ -25,18 +27,11 @@
android:paddingEnd="16dp"
android:text="В ЖК «РУМЯНЦЕВО-ПАРК» ИПОТЕЧНАЯ СТАВКА - 6,5%" />
<com.google.android.material.textview.MaterialTextView
android:id="@+id/articleContent"
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/articleBodyRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="С марта 2019 года для всех покупателей квартир в жилом комплексе бизнес-класса «Румянцево-Парк» от компании Lexion Development, доступна минимальная ипотечная ставка от банка-партнера ПАО Банк ВТБ в размере 6,5% по программе субсидирования ипотеки от застройщика.
Минимальная процентная ставка действительна при покупке любого типа квартир в жилом комплексе – от студий 23,8 кв. м до четырехкомнатных квартир площадью 102,7 кв. м. Минимальный первоначальный взнос составляет от 20%.
Более подробная информация доступна в офисе продаж по телефону: +7 (495) 127-86-86" />
</LinearLayout>
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<com.biganto.visual.roompark.util.view_utils.image_view.RoundedImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:image_corner_radius="4dp" />
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textview.MaterialTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Default_TextView.Header_Text"
android:minHeight="16dp"
/>
\ No newline at end of file
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