Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
5.6 kB
1
Indexable
Never
package com.example.alarmclock

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.NumberPicker
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import com.example.alarmclock.databinding.BottomSheetDialogBinding
import com.example.alarmclock.databinding.DescBottomSheetBinding
import com.example.alarmclock.databinding.FragmentUpdateAlarmBinding
import com.example.alarmclock.utils.Alarm
import com.example.alarmclock.viewmodel.CreateAlarmViewModel
import com.example.alarmclock.viewmodel.UpdateAlarmViewModel
import com.google.android.material.bottomsheet.BottomSheetDialog
import java.text.SimpleDateFormat
import java.util.Date

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [UpdateAlarmFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class UpdateAlarmFragment : Fragment() {
    private lateinit var alarm: Alarm
    private val TAG = "UpdateAlarmFragment"
    private var _binding: FragmentUpdateAlarmBinding? = null
    private val binding get() = _binding!!
    var recurring = false
    var monday = false
    var tuesday = false
    var wednesday = false
    var thursday = false
    var friday = false
    var saturday = false
    var sunday = false
    var isRun = false
    var count = 0
    private var desc: String? = null
    var vibration = false

    private var updateAlarmViewModel: UpdateAlarmViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        updateAlarmViewModel = ViewModelProvider(this)[UpdateAlarmViewModel::class.java]
        arguments?.let {
            alarm = it.getParcelable("alarm")!!
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
//        return inflater.inflate(R.layout.fragment_update_alarm, container, false)
        _binding = FragmentUpdateAlarmBinding.inflate(layoutInflater, container, false)
        val view = binding.root
        arguments?.let {
            alarm = it.getParcelable("alarm")!!
            hourPickerSetValue(alarm)
            minutePickerSetValue(alarm)

        }
        setListeners()
        alarm?.let { loadAlarm(it) }
        return view
    }

    private fun loadAlarm(alarm: Alarm) {
        binding.numberPicker3.value = alarm.hour
        binding.numberPicker4.value = alarm.minute
//        binding.desc.text = alarm.desc
        // Set other values like vibration, days of the week, etc.
    }


    @SuppressLint("SimpleDateFormat")
    private fun minutePickerSetValue(alarm:Alarm) {
        binding.numberPicker4.setOnLongPressUpdateInterval(100)
        binding.numberPicker4.wrapSelectorWheel = true
        binding.numberPicker4.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        binding.numberPicker4.isSoundEffectsEnabled = true
        binding.numberPicker4.minValue = 0
        binding.numberPicker4.maxValue = 59
        binding.numberPicker4.setFormatter { p0 ->
            var value = ""
            value = if (p0 < 10) {
                "0$p0"
            } else {
                p0.toString()
            }
            value
        }

        binding.numberPicker4.value = alarm.minute
    }

    @SuppressLint("SimpleDateFormat")
    private fun hourPickerSetValue(alarm: Alarm) {
        binding.numberPicker3.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS
        binding.numberPicker3.isSoundEffectsEnabled = true
        binding.numberPicker3.wrapSelectorWheel = true
        binding.numberPicker3.minValue = 0
        binding.numberPicker3.maxValue = 23
        binding.numberPicker3.setFormatter { p0 ->
            var value = ""
            value = if (p0 < 10) {
                "0$p0"
            } else {
                p0.toString()
            }
            value
        }

        binding.numberPicker3.value = alarm.hour
    }

    private fun setListeners() {
        binding.saveBtn1.setOnClickListener {
            alarm?.let {
                it.hour = binding.numberPicker3.value
                it.minute = binding.numberPicker4.value

                // Update other values like vibration, days of the week, etc.

                updateAlarmViewModel?.update(it)
                findNavController().popBackStack()
            }
        }

        binding.closeBtn1.setOnClickListener {
            requireActivity().onBackPressed()
        }



        // Set other listeners...
    }



    companion object {
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment UpdateAlarmFragment.
         */
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            UpdateAlarmFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}
Leave a Comment