LifecycleOwner migration in CustomView android

122 Views Asked by At

I am upgrading lifeCycle components in an old project that uses lifeCycleRegistry. How can I update below code

Below method is removed in new LifeCycleOwner interface.

Lifecycle getLifecycle()

Below class is RemoteConfig based TextView. How could we make lifeCycle aware.

I have seen a post that said remove it as it is implemented in Activity but what about custom Views.

RcTextView.kt

open class RcTextView : AppCompatTextView, LifecycleOwner {

    private var m_modifyingText = false

    var hasHtmlText = false
    var hasJsonValue = false
    private var lifecycleRegistry: LifecycleRegistry? = null
    private var remoteKeyText: RemoteKey? = null

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        if (!isInEditMode) {
            lifecycleRegistry = LifecycleRegistry(this)
            lifecycleRegistry?.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
            initRcKey(attrs)

            updateText()

            attrs?.let {
                val ta = context.obtainStyledAttributes(it, R.styleable.RcTextView, 0, 0)
                try {
                    hasJsonValue = ta.getBoolean(R.styleable.RcView_rcKeyJson, false)

                    ta.getBoolean(R.styleable.RcTextView_isUnderline, false).let { isUnderline ->
                        if (isUnderline) {
                            applyUnderline()
                        }
                    }
                } catch (exception: Exception) {
                    exception.printStackTrace()
                } finally {
                    ta.recycle()
                }
            }

            RemoteConfigRepository.loadingResult.observe(this, Observer {
                updateText()
            })
        }
    }

    fun setRcKey(remoteKey: RemoteKey?) {
        if(!isInEditMode){
            this.remoteKeyText = remoteKey
            updateText()
        }
    }

    private fun initRcKey(attrs: AttributeSet?) {
        attrs?.let {
            val ta = context.obtainStyledAttributes(it, R.styleable.RcView, 0, 0)
            try {
                ta.getString(R.styleable.RcView_rcKey)?.let { rcKey ->
                    remoteKeyText = RemoteKey.valueOf(rcKey)
                }
            } catch (exception: Exception) {
                exception.printStackTrace()
            } finally {
                ta.recycle()
            }
        }
    }

    private fun updateText() {
        remoteKeyText?.let {
            setRcText(RemoteConfigRepository.getString(it))
        }
    }

    private fun setRcText(rawText: String) {
        var strValue = rawText
        if (hasJsonValue) {
            strValue = parseValue(rawText)
        }
        if (rawText.isHtml()) {
            hasHtmlText = false
            setHtmlText(strValue)
//            val typeface = ResourcesCompat.getFont(context, R.font.century_gothic_std_regular)
//            val boldTypeface = ResourcesCompat.getFont(context, R.font.century_gothic_std_bold)
//            setTypeface(typeface)
//            setTypeface(boldTypeface, Typeface.BOLD)
        } else {
            text = strValue
        }
    }

    private fun parseValue(rawText: String): String {
        return try {
            val json = RemoteConfigRepository.getString(RemoteKey.url_config)
            val moshi: Moshi = Moshi.Builder().build()
            val jsonAdapter = moshi.adapter(RcString::class.java)
            val region = TenantUtils.getSelectedTenant(context)
            jsonAdapter.fromJson(json)?.getValue(region.tenantName) ?: rawText
        } catch (ex: Exception) {
            rawText
        }
    }

    private fun applyUnderline() {
        addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                //Do nothing here... we don't care
            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                //Do nothing here... we don't care
            }

            override fun afterTextChanged(s: Editable) {
                if (m_modifyingText) return
                underlineText()
            }
        })
        underlineText()
    }

    override fun getLifecycle(): Lifecycle {
        return lifecycleRegistry!!
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        lifecycleRegistry?.handleLifecycleEvent(Lifecycle.Event.ON_START)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        lifecycleRegistry?.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    }

    private fun underlineText() {
        if (m_modifyingText) return

        m_modifyingText = true

        val content = SpannableString(text)
        content.setSpan(UnderlineSpan(), 0, content.length, 0)
        text = content
        m_modifyingText = false
    }
}

I simply want to upgrade life cycle components but without effecting above class

0

There are 0 best solutions below