Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.3 kB
1
Indexable
Never
package com.app.healthupp.presentation.page.nutrition.more

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
import com.app.healthupp.R
import com.app.healthupp.databinding.NutritionInfoDialogBinding
import com.app.healthupp.model.other.DayBalance
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class NutritionSpecificInfo : DialogFragment() {
    private var binding: NutritionInfoDialogBinding? = null
    private val viewModel: NutritionSpecificInfoViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.AppTheme_FullScreenDialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return NutritionInfoDialogBinding.inflate(layoutInflater).apply {
            binding = this
        }.root
    }

    @SuppressLint("WrongConstant")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val nutrition = arguments?.getParcelable<DayBalance>(ARGS)
        binding?.nutritionHeaderInfo?.text = nutrition?.name
        binding?.nutritionDescTv?.movementMethod = ScrollingMovementMethod()
        viewModel.getNutritionInfo(nutrition?.id ?: 0)
        viewModel.nutritionInfo.observe(viewLifecycleOwner){
            binding?.nutritionDescTv?.text = it
        }
        binding?.crossButtonIv?.setOnClickListener {
            dismissAllowingStateLoss()
        }
    }

    companion object {
        const val TAG = "NutritionSpecificInfo"
        private const val ARGS = "args"
        fun show(fragmentManager: FragmentManager, nutritionInfo: DayBalance) {
            NutritionSpecificInfo().apply {
                arguments = bundleOf(ARGS to nutritionInfo)
            }.show(fragmentManager, TAG)
        }
    }
}