Commit 6dbbbb66 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

PicassoImageGetter

parent 9c0ec0ce
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<AndroidXmlCodeStyleSettings>
<option name="ARRANGEMENT_SETTINGS_MIGRATED_TO_191" value="true" />
</AndroidXmlCodeStyleSettings>
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
......
package com.biganto.visual.roompark.di.dagger
import android.app.Application
import com.biganto.visual.androidplayer.data.repository.local.ILocalStore
import com.biganto.visual.roompark.base.RoomParkApplication
import com.biganto.visual.roompark.data.local.LocalStorage
......@@ -57,6 +58,8 @@ interface AppComponent : AndroidInjector<RoomParkApplication>{
fun providedb():IDb
fun provideAppContext():Application
@Component.Factory
interface Factory{
......
package com.biganto.visual.roompark.presentation.screen.article
import PicassoImageGetter
import android.os.Build
import android.text.Html
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.graphics.drawable.toDrawable
import androidx.core.os.bundleOf
import androidx.core.text.HtmlCompat
import butterknife.BindView
......@@ -13,6 +14,7 @@ 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.util.view_utils.html.HtmlTagHandler
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.textview.MaterialTextView
import com.squareup.picasso.Picasso
......@@ -20,6 +22,7 @@ import jp.wasabeef.picasso.transformations.BlurTransformation
import timber.log.Timber
import javax.inject.Inject
/**
* Created by Vladislav Bogdashkin on 30.09.2019.
*/
......@@ -116,13 +119,39 @@ class ArticleScreenController :
private fun render(viewState: ArticleScreenViewState.ArticleLoaded) {
title.text = viewState.item.title
contentView.text = Html.fromHtml(viewState.item.htmlBody,
HtmlCompat.FROM_HTML_MODE_LEGACY, Html.ImageGetter { url ->
Picasso.get()
.load(url)
.get()
.toDrawable(resources!!)
},HtmlTa)
val imageGetter = PicassoImageGetter(contentView)
val parsedHtml = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
Html.fromHtml(viewState.item.htmlBody,
HtmlCompat.FROM_HTML_MODE_LEGACY
,
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
// }
, HtmlTagHandler())
else Html.fromHtml(viewState.item.htmlBody
,
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
// }
, HtmlTagHandler())
contentView.text = parsedHtml
articleDate.text = viewState.item.published.toString()
Picasso.get()
......@@ -146,41 +175,3 @@ class ArticleScreenController :
}
public class HtmlTagHandler implements Html.TagHandler {
@Override
public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
Log.e("Tag", tag);
if (tag.startsWith("b") ) {
Log.e("Tage", "Code tag encountered1");
if (opening) {
Log.e("Tage", "Code tag encountered1");
output.setSpan(new TypefaceSpan("monospace"), output.length(), output.length(), Spannable.SPAN_MARK_MARK);
} else {
Log.e("Tage", "Code tag encountered2");
Object obj = getLast(output, TypefaceSpan.class);
int where = output.getSpanStart(obj);
output.removeSpan(obj);
output.setSpan(new TypefaceSpan("monospace"), where, output.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
else {
Log.e("Tage", "Code Exited");
}
}
private Object getLast(Editable text, Class kind) {
Object[] objs = text.getSpans(0, text.length(), kind);
if (objs.length == 0) {
return null;
} else {
for (int i = objs.length; i > 0; i--) {
if (text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK) {
return objs[i - 1];
}
}
return null;
}
}
\ No newline at end of file
......@@ -2,10 +2,8 @@ package com.biganto.visual.roompark.presentation.screen.article
import android.content.Context
import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.data.data_provider.FeedsContractModule
import com.biganto.visual.roompark.di.dagger.AppComponent
import com.biganto.visual.roompark.di.dagger.PerScreen
import com.biganto.visual.roompark.domain.contract.FeedsContract
import dagger.Binds
import dagger.BindsInstance
import dagger.Component
......@@ -41,7 +39,7 @@ abstract class ArticleScreenModule{
@Binds
abstract fun provideContext(activity: RoomParkMainActivity): Context
@PerScreen
@Binds
abstract fun provideContract(impl: FeedsContractModule): FeedsContract
// @PerScreen
// @Binds
// abstract fun provideContract(impl: FeedsContractModule): FeedsContract
}
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.text.Html
import com.biganto.visual.roompark.R
import com.biganto.visual.roompark.base.RoomParkApplication
import com.google.android.material.textview.MaterialTextView
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import kotlin.math.min
class PicassoImageGetter(private val target: MaterialTextView) : Html.ImageGetter {
override fun getDrawable(source: String): Drawable {
val drawable = BitmapDrawablePlaceHolder()
Picasso.get()
.load("https://room-park.ru$source")
.placeholder(R.drawable.ic_deals)
.into(drawable)
return drawable
}
private inner class BitmapDrawablePlaceHolder : BitmapDrawable(), Target{
protected var _drawable: Drawable? = null
private val resources: Resources = RoomParkApplication.component.provideAppContext().resources
override fun draw(canvas: Canvas) {
if (_drawable != null) {
_drawable!!.draw(canvas)
}
}
fun setDrawable(drawable: Drawable) {
this._drawable = drawable
var width = drawable.intrinsicWidth
var height = drawable.intrinsicHeight
target.let {
val aspect = it.measuredWidthAndState.toFloat()/width.toFloat()
height = (height*min(aspect,1.0f)).toInt()
width = min(width,it.measuredWidthAndState)
drawable.setBounds(0, 0, width, height)
setBounds(0, 0, width, height)
it.text=it.text
}
}
override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
setDrawable(BitmapDrawable(resources, bitmap))
}
override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {}
override fun onPrepareLoad(placeHolderDrawable: Drawable) {
}
}
}
\ No newline at end of file
package com.biganto.visual.roompark.util.view_utils.html
import android.text.Editable
import android.text.Html
import android.text.Spannable
import android.text.Spanned
import android.text.style.TypefaceSpan
import org.xml.sax.XMLReader
import timber.log.Timber
/**
* Created by Vladislav Bogdashkin on 02.12.2019.
*/
class HtmlTagHandler : Html.TagHandler {
override fun handleTag(opening: Boolean, tag: String, output: Editable, xmlReader: XMLReader) {
Timber.e(tag,"Tag" )
if (tag.startsWith("b")) {
Timber.d( "Code tag encountered1")
if (opening) {
Timber.d("Code tag encountered1")
output.setSpan(
TypefaceSpan("monospace"),
output.length,
output.length,
Spannable.SPAN_MARK_MARK
)
} else {
Timber.d("Code tag encountered2")
val obj = getLast(output, TypefaceSpan::class.java)
val where = output.getSpanStart(obj)
output.removeSpan(obj)
output.setSpan(
TypefaceSpan("monospace"),
where,
output.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
} else {
Timber.d("Code Exited")
}
}
private fun getLast(text: Editable, kind: Class<*>): Any? {
val objs = text.getSpans(0, text.length, kind)
if (objs.isEmpty()) {
return null
} else {
for (i in objs.size downTo 1) {
if (text.getSpanFlags(objs[i - 1]) == Spannable.SPAN_MARK_MARK) {
return objs[i - 1]
}
}
return null
}
}
}
\ 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