Untitled

mail@pastecode.io avatar
unknown
kotlin
2 years ago
5.8 kB
6
Indexable
Never
package com.muzafferatmaca.ditennewcalender.ui.report.screen

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.muzafferatmaca.ditennewcalender.R
import com.muzafferatmaca.ditennewcalender.baseclass.BaseFragment
import com.muzafferatmaca.ditennewcalender.databinding.FragmentWantsNewBinding
import com.muzafferatmaca.ditennewcalender.datamodel.report.EventIdRequestString
import com.muzafferatmaca.ditennewcalender.datamodel.report.GetEmailReceiversResponseItem
import com.muzafferatmaca.ditennewcalender.datamodel.report.Receiver
import com.muzafferatmaca.ditennewcalender.dataviewmodel.report.EmailReceiversViewModel
import com.muzafferatmaca.ditennewcalender.ui.activities.ReportActivity
import com.muzafferatmaca.ditennewcalender.utils.LoginConstants


class WantsNewFragment : BaseFragment<FragmentWantsNewBinding>() {
    override val layoutId: Int = R.layout.fragment_wants_new

    var employeeTitle : String? = null
    var employeeId : Int? = null
    var requestEmail : String? = null
    var requestFullName : String? = null
    var eventID : Int? = null
    var eventReportOpinion : String? = null

    private lateinit var receiver : Receiver
    private lateinit var eventIdRequest: EventIdRequestString
    private lateinit var emailReceiversViewModel: EmailReceiversViewModel

    private lateinit var spinnerMailAdapter : ArrayAdapter<String>
    private val emailReceiversArrayList = ArrayList<GetEmailReceiversResponseItem>()
    private val spinnerArrayList :ArrayList<String> = arrayListOf()
    private var receiverEmailMap = mutableMapOf<String,String>()
    private var receiverEmpIdMap = mutableMapOf<String,Int>()
    private val receiverArrayList = ArrayList<Receiver>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        eventID = activity?.intent?.getIntExtra("eventID",0)

        emailReceiversViewModel = ViewModelProvider(this)[EmailReceiversViewModel::class.java]

        setButton()
        observe()


    }
    private fun observe(){
        eventIdRequest = EventIdRequestString(eventID.toString())
        emailReceiversViewModel.getEmailReceiver(eventIdRequest)

        emailReceiversViewModel.loadingEmailReceivers.observe(viewLifecycleOwner){loading->
            loading?.let {

            }
        }
        emailReceiversViewModel.errorEmailReceivers.observe(viewLifecycleOwner){error ->
            error?.let {

            }
        }
        emailReceiversViewModel.emailReceiversLiveData.observe(viewLifecycleOwner){data ->
            emailReceiversArrayList.clear()
            data?.let {
                emailReceiversArrayList.addAll(it)
            }
            getData()
        }
    }

    private fun getData(){
        spinnerArrayList.clear()

        for (data in emailReceiversArrayList){
            val email = data.email
            val empID = data.empId
            val posName = data.posName
            val name = data.name

            if (name != null) {
                spinnerArrayList.add(name)
                receiverEmailMap[name] = email!!
                receiverEmpIdMap[name] = empID!!
            }

        }


        binding.requestTitleEditText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                employeeTitle = p0?.toString() ?: ""
            }

            override fun afterTextChanged(p0: Editable?) {}

        })

        binding.requestDescriptionEditText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                eventReportOpinion = p0?.toString() ?: ""
            }

            override fun afterTextChanged(p0: Editable?) {}

        })


        spinnerMailAdapter = ArrayAdapter(requireContext(),android.R.layout.simple_dropdown_item_1line,android.R.id.text1,spinnerArrayList)
        binding.whoSendRequestSpinner.adapter = spinnerMailAdapter

        binding.whoSendRequestSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                val selectedSpinner = spinnerArrayList[p2]
                requestFullName = spinnerArrayList[p2]
                requestEmail = receiverEmailMap[selectedSpinner]
                employeeId = receiverEmpIdMap[selectedSpinner]
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {

            }

        }



    }

    private fun setButton(){
        binding.wantsNewBackButton.setOnClickListener {
            requireActivity().onBackPressed()
        }
        binding.wantsNewNextButton.setOnClickListener {
            receiverArrayList.clear()
            receiver = Receiver(employeeId,requestEmail,requestFullName,employeeTitle,eventReportOpinion)
            receiverArrayList.add(receiver)
            ReportActivity.reportSaveDataViewModel.wantsNew.value = receiverArrayList
            val action = WantsNewFragmentDirections.actionWantsNewFragmentToWantsCoaFragment()
            Navigation.findNavController(it).navigate(action)

        }
    }

}