Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
6.3 kB
1
Indexable
Never
package com.android.kepr.ui.assets.assetsdetatilfrags

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import com.android.kepr.R
import com.android.kepr.data.db.AgentDatabase
import com.android.kepr.data.db.dao.DaoAccess
import com.android.kepr.data.domain.apiresponse.AssetDataObject
import com.android.kepr.data.domain.apiresponse.FetchNotesUnderAssetsDataResponseItem
import com.android.kepr.ui.base.BaseFragment
import com.android.kepr.utils.CommonStrings
import com.android.kepr.utils.LocalStorage
import com.android.kepr.utils.Utils
import com.android.kepr.utils.Utils.Companion.argument
import com.android.kepr.utils.Utils.Companion.value
import kotlinx.android.synthetic.main.fragment_asset_detail_notes.*

class AssetsDetailsNotesFragment : BaseFragment(R.layout.fragment_asset_detail_notes) {

    private val assetDataObject: AssetDataObject by argument(ARG_ASSETS_OBJ)
    private lateinit var dao: DaoAccess


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dao = AgentDatabase.getInstance(activity).daoAccess()
        val assetNotesListData: List<FetchNotesUnderAssetsDataResponseItem> =
            dao.getAssetsNotesData(assetDataObject.assetId.toString())
        et_notes.isEnabled = false
        et_notes.setTextColor(requireActivity().resources.getColor(R.color.light_grey))


        if (assetNotesListData.isNotEmpty()) {
            Log.e("asset notes", ""+assetNotesListData[0])
            cv_top.isVisible = true
            tv_no_data_found.isVisible = false

            LocalStorage.with(context)?.save(
                CommonStrings.Keys.Prefs.ASSET_HISTORY_NOTES,
                assetNotesListData[0].notes
            )

            et_notes.value =
                if (assetNotesListData == null || assetNotesListData[0].notes.isNullOrEmpty()) "" else assetNotesListData[0].notes.toString()

            tvUser.text =
                if (assetNotesListData == null || assetNotesListData[0].lastEditedByUserName.isNullOrEmpty()) "N/A" else assetNotesListData[0].lastEditedByUserName
            tvTimeStamp.text =
                if (assetNotesListData == null || assetNotesListData[0].lastEditedAt.isNullOrEmpty()) {
                    "N/A"
                } else {
                    Utils.convertTZToLocal(
                        requireActivity(),
                        assetNotesListData[0].lastEditedAt.toString().replace("T", " "),
                        CommonStrings.DateFormat.MY_CUSTOM_FORMAT_SENDING
                    )
                }

            et_notes.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                }

                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(
                    s: CharSequence?,
                    start: Int,
                    before: Int,
                    count: Int
                ) {
                    if (s?.length!! > 0) {
                        LocalStorage.with(context)?.save(
                            CommonStrings.Keys.Prefs.ASSET_HISTORY_NOTES,
                            s.toString()
                        )
                    }
                    if (s.isEmpty()) {
                        LocalStorage.with(context)?.save(
                            CommonStrings.Keys.Prefs.ASSET_HISTORY_NOTES,
                            ""
                        )
                    }

                }
            })

        } else {


            cv_top.isVisible = true



            tvUser.text = LocalStorage.with(requireActivity())?.getString(CommonStrings.Keys.Prefs.USER_NAME,"")

            var nTime  =Utils.getTime("EST").toString()
            tvTimeStamp.text = Utils.convertTZToLocal(
                requireActivity(),
                nTime.replace("T", " "),
                CommonStrings.DateFormat.MY_CUSTOM_FORMAT_SENDING
            )

            et_notes.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {
                }

                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {
                }

                override fun onTextChanged(
                    s: CharSequence?,
                    start: Int,
                    before: Int,
                    count: Int
                ) {
                    if (s?.length!! > 0) {
                        LocalStorage.with(context)?.save(
                            CommonStrings.Keys.Prefs.ASSET_HISTORY_NOTES,
                            s.toString()
                        )
                    }
                    if (s.isEmpty()) {
                        LocalStorage.with(context)?.save(
                            CommonStrings.Keys.Prefs.ASSET_HISTORY_NOTES,
                            ""
                        )
                    }

                }
            })

        }


    }

    fun clickAble(value: Boolean) {
        activity?.runOnUiThread {
            et_notes.isEnabled = value
            if (value) {
                et_notes.requestFocus()
                et_notes.setTextColor(requireActivity().resources.getColor(R.color.text_color_black))
                et_notes.setSelection(et_notes.length())
            }
        }
    }

    companion object {
        private const val ARG_ASSETS_OBJ = "assetDataObj"
        fun newInstance(assetDataObject: AssetDataObject) = AssetsDetailsNotesFragment().apply {
            arguments = bundleOf(
                ARG_ASSETS_OBJ to assetDataObject
            )
        }
    }
}