rawetew

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

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.android.kepr.R
import com.android.kepr.data.db.AgentDatabase
import com.android.kepr.data.db.AppExecutors
import com.android.kepr.data.db.dao.DaoAccess
import com.android.kepr.data.domain.apiresponse.AssetDataObject
import com.android.kepr.data.domain.apiresponse.BuildingDataObject
import com.android.kepr.data.domain.apiresponse.RoomDataObject
import com.android.kepr.pagination.MainLoadStateAdapter
import com.android.kepr.ui.assets.adapter.ParticularAssetsViewListingAdapter
import com.android.kepr.ui.assets.adapter.SubListingAdapter
import com.android.kepr.ui.assets.createassets.CreateAssetsScreenActivity
import com.android.kepr.ui.assets.viewallassetspagination.AllAssetsViewModel
import com.android.kepr.ui.assets.viewallassetspagination.AssetsViewAllAssetsViewModelFactory
import com.android.kepr.ui.assets.viewallassetspagination.ViewAllAssetsPagingAdaptor
import com.android.kepr.ui.base.BaseFragment
import com.android.kepr.ui.scanner.ScannerModeFragment
import com.android.kepr.ui.scanner.ScannerSelfAssignmentActivity
import com.android.kepr.utils.CommonStrings
import com.android.kepr.utils.PowerMenuUtils
import com.android.kepr.utils.Utils
import com.android.kepr.utils.dialogs.DialogScanMode
import com.android.kepr.utils.dialogs.DialogUpdateBuildingAndRoom
import com.android.kepr.utils.dialogs.DialogVerifyStatus
import com.skydoves.powermenu.OnDismissedListener
import com.skydoves.powermenu.OnMenuItemClickListener
import com.skydoves.powermenu.PowerMenu
import com.skydoves.powermenu.PowerMenuItem
import kotlinx.android.synthetic.main.fragent_view_all_assets.*
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class AssestTabsFragment : BaseFragment(R.layout.fragent_view_all_assets),
    SubListingAdapter.ItemClickListener, DialogScanMode.ItemClickListener,
    DialogUpdateBuildingAndRoom.ItemClickListener, DialogVerifyStatus.ItemClickListener {

    private lateinit var layoutManager: LinearLayoutManager
    private lateinit var dao: DaoAccess
    private var finalPosition: Int = -1
    private var hamburgerMenu: PowerMenu? = null

    private val viewModel: AllAssetsViewModel by viewModels {
        AssetsViewAllAssetsViewModelFactory(
            dao
        )
    }


    private lateinit var viewAllAssetsPagingAdaptor: ViewAllAssetsPagingAdaptor

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dao = AgentDatabase.getInstance(activity).daoAccess()
        iv_back.isVisible = false
        layoutManager = LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false)
        rv_assets_listing.layoutManager = layoutManager

        viewAllAssetsPagingAdaptor =
            ViewAllAssetsPagingAdaptor(
                requireActivity(),
                this
            )

        rv_assets_listing.adapter =
            viewAllAssetsPagingAdaptor.withLoadStateFooter(MainLoadStateAdapter())

        lifecycleScope.launch {
            viewModel.data.collectLatest {
                viewAllAssetsPagingAdaptor.submitData(it)
            }

        }

        hamburgerMenu = PowerMenuUtils.getHamburgerPowerMenu(
            requireActivity(), this, onHamburgerItemClickListener, onHamburgerMenuDismissedListener
        )

        rl_more.setOnClickListener(listener)
        iv_back.setOnClickListener(listener)
        tv_action_continue.setOnClickListener(listener)
    }

    private fun onHamburger() {
        if (hamburgerMenu!!.isShowing) {
            hamburgerMenu!!.dismiss()
            return
        }
        hamburgerMenu!!.showAsDropDown(rl_more)
    }
    private lateinit var assetListData: List<AssetDataObject>

    private val onHamburgerItemClickListener =
        OnMenuItemClickListener<PowerMenuItem> { position, _ ->
            hamburgerMenu!!.selectedPosition = position
            assetListData   =
                AgentDatabase.getInstance(context).daoAccess()
                    .getAssetAccordingToBuildingAndRoom(
                        buildingObj.buildingId,
                        roomObj.roomId.toString()
                    )

            Log.d("assetListData" , "assetListData" +assetListData.size)
            when (position) {
                0 -> {
                    if (finalPosition > -1) {



                        val dialogVerifyStatus =
                            DialogVerifyStatus.newInstance(
                                this,
                                0,
                                0,
                                assetListData,
                                finalPosition,
                                buildingObj,
                                roomObj,
                                CommonStrings.Keys.TaskDetailType.STATE_VERIFY
                            )
                        dialogVerifyStatus.show(childFragmentManager, "CustomDialog")
                        hamburgerMenu!!.dismiss()
                    } else {
                        Utils.showToastForFragments(
                            requireActivity(),
                            "Please select atleast one asset"
                        )
                    }


                }
                1 -> {
                    if (finalPosition > -1) {
                        var dialog = DialogUpdateBuildingAndRoom.newInstance(
                            this,
                            0,
                            0,
                            assetListData,
                            finalPosition,
                            buildingObj,
                            roomObj
                        ).show(childFragmentManager, "CustomDialog")

                        hamburgerMenu!!.dismiss()
                    } else {
                        Utils.showToastForFragments(
                            requireActivity(),
                            "Please select atleast one asset"
                        )
                    }
                }
                2 -> {
                    // Export functionality coming soon
                }
                3 -> {
                    val intent = Intent(activity, CreateAssetsScreenActivity::class.java)
                    startActivity(intent)
                    hamburgerMenu!!.dismiss()
                }
                4 -> {
                    // navigate to filter screen


                }
                5 -> {
                    // navigate to scan mode
                }
                6 -> {
                    if (finalPosition > -1) {
                        val dialogVerifyStatus =
                            DialogVerifyStatus.newInstance(
                                this,
                                0,
                                0,
                                assetListData,
                                finalPosition,
                                buildingObj,
                                roomObj,
                                CommonStrings.Keys.TaskDetailType.STATE_CHANGE
                            )
                        dialogVerifyStatus.show(childFragmentManager, "CustomDialog")
                        hamburgerMenu!!.dismiss()
                    } else {
                        Utils.showToastForFragments(
                            requireActivity(),
                            "Please select atleast one asset"
                        )
                    }

                }
                7 -> {
                    /*  val manager = requireActivity().supportFragmentManager
                      val trans = manager.beginTransaction()
                      trans.remove(
                          AssetsFragment.newInstance(
                              buildingObj,
                              roomObj,
                              assetListData,
                              buildingPosition,
                              roomPosition
                          )
                      )
                      trans.commit()*/
                    fragmentManager?.doTransaction {
                        add(
                            R.id.fl_scanner,
                            AssetsSummaryFragment.newInstance()
                        )
                    }
                    hamburgerMenu!!.dismiss()
                }
                8 -> {
                    /* val manager = requireActivity().supportFragmentManager
                     val trans = manager.beginTransaction()
                     trans.remove(
                         AssetsFragment.newInstance(
                             buildingObj,
                             roomObj,
                             assetListData,
                             buildingPosition,
                             roomPosition
                         )
                     )
                     trans.commit()*/
                    fragmentManager?.doTransaction {
                        add(
                            R.id.fl_scanner,
                            AssetsRoomStatusListingFragment.newInstance()
                        )
                    }
                    hamburgerMenu!!.dismiss()
                }
            }
        }
    private val onHamburgerMenuDismissedListener =
        OnDismissedListener { Log.d("Test", "onDismissed hamburger menu") }

    //2
    companion object {
        fun newInstance(): AssestTabsFragment {
            return AssestTabsFragment()
        }
    }

    override fun onViewDetailsClick(position: Int, name: String) {
        val manager = requireActivity().supportFragmentManager
        val trans = manager.beginTransaction()
        trans.remove(
            AssetsFragment.newInstance(
                buildingObj,
                roomObj,
                assetListData,
                0,
                0
            )
        )
        trans.commit()
        fragmentManager?.doTransaction {
            replace(
                R.id.fl_scanner,
                AssetsViewFragment.newInstance(
                    buildingObj.buildingName + " " + roomObj
                        .roomNumber, assetListData[position]
                )
            )
        }

    }
    lateinit var buildingObj : BuildingDataObject
    lateinit var roomObj : RoomDataObject

    override fun onCheckBoxClick(position: Int, updatedBuildingId: String?, toString: String) {
        finalPosition = position

        AppExecutors.getInstance().diskIO()
            .execute {
                try {
                    if (updatedBuildingId != null) {
                        buildingObj = dao.getbuildingDataObject(updatedBuildingId)
                    }
                    Log.d("building_obj" , "buildingObj" +buildingObj)
                    if (toString != null) {
                        roomObj = dao.getRoomDataObject(toString)
                    }
                    Log.d("roomObj" , "roomObj" +roomObj)

                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
    }

    override fun onScannerViaRFIDClick() {
        parentFragmentManager?.doTransaction {
            add(
                R.id.fl_scanner,
                ScannerModeFragment.newInstance()
            )
        }

    }

    override fun onBarCodeClick() {
        val intent = Intent(activity, ScannerSelfAssignmentActivity::class.java)
        startActivity(intent)
    }

    override fun onPhoneScannerClick() {
        val intent = Intent(activity, ScannerSelfAssignmentActivity::class.java)
        startActivity(intent)
    }

    override fun onCancel() {
    }

    private lateinit var dataList: List<AssetDataObject>
    override fun onSave(
        buildingDataObj: BuildingDataObject,
        roomDataObj: RoomDataObject,
        assetListData: List<AssetDataObject>
    ) {
        activity?.runOnUiThread {
            progressBar.isVisible = true
        }
        Log.d("building_obj", "" + buildingDataObj + " " + roomDataObj + " " + assetListData.size)
        AppExecutors.getInstance().diskIO()
            .execute {
                try {

                    dataList = AgentDatabase.getInstance(activity).daoAccess()
                        .getAssetAccordingToBuildingAndRoom(
                            "%" + buildingDataObj.buildingId + "%",
                            "%" + roomDataObj.roomId.toString() + "%"
                        )
                    Log.d("dataList", "dataList" + dataList.size)
                    activity?.runOnUiThread {
                        progressBar.isVisible = false
                        if (dataList.isNotEmpty()) {
                            rv_assets_listing.isVisible = true
                            tv_no_assets_found.isVisible = false
                            tv_action_continue.isVisible = true

                            rv_assets_listing.adapter =
                                viewAllAssetsPagingAdaptor.withLoadStateFooter(MainLoadStateAdapter())
                         /*   assetsViewListingAdapter = ParticularAssetsViewListingAdapter(
                                requireActivity(),
                                this,
                                buildingDataObj,
                                roomDataObj, dataList as ArrayList<AssetDataObject>
                            )
                            rv_assets_listing.adapter = assetsViewListingAdapter*/
                        } else {
                            rv_assets_listing.isVisible = false
                            tv_no_assets_found.isVisible = true
                            // tv_action_continue.isVisible = false
                        }

                    }
                } catch (e: Exception) {
                    e.printStackTrace()

                }
            }


    }


    private val listener = View.OnClickListener { view ->
        when (view.id) {
            R.id.rl_more -> {
                onHamburger()
            }
            R.id.iv_back -> {
                fragmentManager?.popBackStack()
            }
            R.id.tv_action_continue -> {
                var dialog = DialogScanMode.newInstance(
                    this
                ).show(childFragmentManager, "CustomDialog")
            }
        }
    }


    override fun onResume() {
        super.onResume()

    }

    override fun onCancelClick() {

    }

    override fun onVerifyClick() {

    }
}


data class AllAssetsAccordingToAllRooms(
    val roomNumber: String?,
    val buildingName: String?,
    val buildingId: String?,
    val roomId: String?,
    val roomName: String?
)

fun FragmentManager.doTransaction(
    func: FragmentTransaction.() ->
    FragmentTransaction
) {
    beginTransaction().func().addToBackStack(null).commit()
}