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 package com.biganto.visual.roompark.presentation.screen.article
import PicassoImageGetter
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.text.Html import android.text.Html
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import androidx.core.os.bundleOf 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.BindView
import butterknife.OnClick import butterknife.OnClick
import com.biganto.visual.roompark.R import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.base.RoomParkApplication import com.biganto.visual.roompark.base.RoomParkApplication
import com.biganto.visual.roompark.base.RoomParkMainActivity import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.conductor.BigantoBaseController 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.biganto.visual.roompark.util.extensions.formatToSimple
import com.google.android.material.floatingactionbutton.FloatingActionButton import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.textview.MaterialTextView import com.google.android.material.textview.MaterialTextView
...@@ -53,12 +54,14 @@ class ArticleScreenController : ...@@ -53,12 +54,14 @@ class ArticleScreenController :
@BindView(R.id.articleHeaderBlock) @BindView(R.id.articleHeaderBlock)
lateinit var headerBlock: ViewGroup lateinit var headerBlock: ViewGroup
@BindView(R.id.articleContent)
lateinit var contentView: MaterialTextView
@BindView(R.id.articleCloseButton) @BindView(R.id.articleCloseButton)
lateinit var closeArticle: FloatingActionButton lateinit var closeArticle: FloatingActionButton
@BindView(R.id.articleBodyRecyclerView)
lateinit var articleRecyclerView: RecyclerView
val blurPreview:ImageView by lazy { val blurPreview:ImageView by lazy {
headerBlock.findViewById<ImageView>(R.id.articlePreviewBlurred) headerBlock.findViewById<ImageView>(R.id.articlePreviewBlurred)
...@@ -82,7 +85,11 @@ class ArticleScreenController : ...@@ -82,7 +85,11 @@ class ArticleScreenController :
} }
private fun bindRecycler() { private fun bindRecycler() {
articleRecyclerView.isNestedScrollingEnabled = true
articleRecyclerView.layoutManager =
LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
articleRecyclerView.adapter = HtmlPageAdapter()
articleRecyclerView.itemAnimator = null
} }
override fun onViewBound(v: View) { override fun onViewBound(v: View) {
...@@ -109,37 +116,32 @@ class ArticleScreenController : ...@@ -109,37 +116,32 @@ class ArticleScreenController :
title.text = viewState.item.title title.text = viewState.item.title
val imageGetter = PicassoImageGetter(contentView) // val imageGetter = PicassoImageGetter()
val parsedHtml = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
Html.fromHtml(viewState.item.htmlBody.replace("<br>","<br />"), val _escaped = Html.escapeHtml(viewState.item.htmlBody)
HtmlCompat.FROM_HTML_MODE_COMPACT val charset = Charsets.UTF_8
,
imageGetter val reg = "<img\\b(?=\\s)(?=(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"][^\\s>]*)*?\\ssrc=['\"]([^\"]*)['\"]?)(?:[^>=]|='[^']*'|=\"[^\"]*\"|=[^'\"\\s]*)*\"\\s?\\/?>"
// Html.ImageGetter { url -> .toRegex()
// val rUrl = "room-park.ru$url"
// Timber.d("trying to get image: $rUrl") val _esc =_escaped.toByteArray(charset).contentToString()
// Picasso.get() val z = viewState.item.htmlBody.replace("<\\br>","\n")
// .load(rUrl) .replace("<br>","\n")
// .get() .replace("</br>","\n").toString()
// .toDrawable(resources!!)
//// .current Timber.d(" escaped lines: ${z}")
// }
, null) val tags = z.lines().map{
else Html.fromHtml(viewState.item.htmlBody.replace("<br>","<br />") val r = reg.matchEntire(it)?.groupValues?.last()
, Timber.d("matched: $r")
imageGetter val result = if (r == null) HtmlTag.Text(it)
// Html.ImageGetter { url -> else HtmlTag.ImageSource(r)
// val rUrl = "room-park.ru$url" Timber.d(" result: ${result}")
// Timber.d("trying to get image: $rUrl") result
// Picasso.get()
// .load(rUrl) }.toList()
// .get()
// .toDrawable(resources!!) (articleRecyclerView.adapter as HtmlPageAdapter).setItems(tags)
//// .current
// }
, null)
contentView.text = parsedHtml
articleDate.text = viewState.item.published.formatToSimple 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 @@ ...@@ -19,11 +19,12 @@
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/default_image_placeholder" /> app:srcCompat="@drawable/default_image_placeholder" />
<ImageView <com.biganto.visual.roompark.util.view_utils.image_view.RoundedImageView
android:id="@+id/articlePreview" android:id="@+id/articlePreview"
app:image_corner_radius="4dp"
android:layout_width="125dp" android:layout_width="125dp"
android:layout_height="125dp" android:layout_height="125dp"
android:layout_marginStart="27dp" android:layout_marginStart="16dp"
android:layout_marginTop="44dp" android:layout_marginTop="44dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
......
...@@ -4,39 +4,34 @@ ...@@ -4,39 +4,34 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/articleHeaderBlock"
layout="@layout/feed_read_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
<com.google.android.material.textview.MaterialTextView <LinearLayout
android:id="@+id/articleTitle" android:id="@+id/article_container"
style="@style/Header_TextView.Main_Header"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:paddingStart="16dp" android:orientation="vertical">
android:paddingEnd="16dp"
android:text="В ЖК «РУМЯНЦЕВО-ПАРК» ИПОТЕЧНАЯ СТАВКА - 6,5%" />
<com.google.android.material.textview.MaterialTextView <include
android:id="@+id/articleContent" android:id="@+id/articleHeaderBlock"
android:layout_width="match_parent" layout="@layout/feed_read_header"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:paddingStart="16dp" android:layout_height="wrap_content"
android:paddingEnd="16dp" android:visibility="visible" />
android:text="С марта 2019 года для всех покупателей квартир в жилом комплексе бизнес-класса «Румянцево-Парк» от компании Lexion Development, доступна минимальная ипотечная ставка от банка-партнера ПАО Банк ВТБ в размере 6,5% по программе субсидирования ипотеки от застройщика.
Минимальная процентная ставка действительна при покупке любого типа квартир в жилом комплексе – от студий 23,8 кв. м до четырехкомнатных квартир площадью 102,7 кв. м. Минимальный первоначальный взнос составляет от 20%. <com.google.android.material.textview.MaterialTextView
android:id="@+id/articleTitle"
style="@style/Header_TextView.Main_Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="В ЖК «РУМЯНЦЕВО-ПАРК» ИПОТЕЧНАЯ СТАВКА - 6,5%" />
Более подробная информация доступна в офисе продаж по телефону: +7 (495) 127-86-86" /> <androidx.recyclerview.widget.RecyclerView
android:id="@+id/articleBodyRecyclerView"
android:layout_width="match_parent"
</LinearLayout> android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView> </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