Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
11 kB
1
Indexable
Never
package com.fairfairdepanneur

import android.app.Dialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.widget.TimePicker
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.setFragmentResult
import java.text.SimpleDateFormat
import java.util.*

class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {

    private val calendar = Calendar.getInstance()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val hourOfDay = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        return TimePickerDialog(requireActivity(),this, hourOfDay, minute, true)
    }

    override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay)
        calendar.set(Calendar.MINUTE, minute)

        val selectedTime = SimpleDateFormat("HH-mm", Locale.FRANCE).format(calendar.time)

        var selectedDateBundle = Bundle()
        selectedDateBundle.putString("SELECTED_TIME", selectedTime)

        setFragmentResult("REQUEST_CODE", selectedDateBundle)
    }
}














<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@color/white"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="8dp">

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/appointment_action_card"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="16dp">

                    <!-- Title, secondary and supporting text -->
                    <TextView
                        android:id="@+id/title_card_1_appointment_step_fragment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="0dp"
                        android:layout_marginTop="0dp"
                        android:text="@string/titleCard1AppointmentStepFragment"
                        android:textAppearance="?attr/textAppearanceTitleLarge"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/subtitle_card_1_appointment_step_fragment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="0dp"
                        android:layout_marginEnd="0dp"
                        android:layout_marginTop="8dp"
                        android:text="@string/subtitleCard1AppointmentStepFragment"
                        android:textAppearance="?attr/textAppearanceBodyMedium"
                        android:textColor="?android:attr/textColorSecondary"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/title_card_1_appointment_step_fragment" />

                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/call_action"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:backgroundTint="@color/secondaryColor"
                        android:text="@string/titleCallButtonAppointmentStepFragment"
                        android:textAllCaps="true"
                        android:textColor="@color/white99"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/subtitle_card_1_appointment_step_fragment" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </com.google.android.material.card.MaterialCardView>

            <com.google.android.material.card.MaterialCardView
                android:id="@+id/callActionCard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:checkable="true"
                android:clickable="true"
                android:focusable="true">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="16dp">

                    <!-- Title, secondary and supporting text -->
                    <TextView
                        android:id="@+id/title_card_2_appointment_step_fragment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/titleCard2AppointmentStepFragment"
                        android:textAppearance="?attr/textAppearanceTitleLarge"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/subtitle_card_2_appointment_step_fragment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp"
                        android:text="@string/subtitleCard2AppointmentStepFragment"
                        android:textAppearance="?attr/textAppearanceBodyMedium"
                        android:textColor="?android:attr/textColorSecondary"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/title_card_2_appointment_step_fragment" />

                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/set_appointment_action"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="16dp"
                        android:backgroundTint="@color/secondaryColor"
                        android:text="@string/titleSetAppointmentButtonAppointmentStepFragment"
                        android:textAllCaps="true"
                        android:textColor="@color/white99"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/subtitle_card_2_appointment_step_fragment" />

                </androidx.constraintlayout.widget.ConstraintLayout>

            </com.google.android.material.card.MaterialCardView>

        </LinearLayout>

    </ScrollView>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/next_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:backgroundTint="@color/secondaryColor"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        android:padding="0dp"
        android:textAllCaps="true"
        android:text="@string/titleNextButtonAppointmentStepFragment"
        android:textColor="@color/white99"
        app:cornerRadius="0dp"/>

</RelativeLayout>





















package com.fairfairdepanneur.ui

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.fairfairdepanneur.DatePickerFragment
import com.fairfairdepanneur.TimePickerFragment
import com.fairfairdepanneur.databinding.AppointmentStepFragmentBinding

class AppointmentStepFragment : Fragment() {

    private var _binding: AppointmentStepFragmentBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        _binding = AppointmentStepFragmentBinding.inflate(inflater)

        binding.apply {
            callAction.setOnClickListener {
                val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:06 95 23 45 24"))
                startActivity(intent)
            }

            setAppointmentAction.setOnClickListener {
                val supportFragmentManager = activity?.supportFragmentManager

                /**
                val datePickerFragment = DatePickerFragment()

                supportFragmentManager?.setFragmentResultListener(
                    "REQUEST_KEY",
                    viewLifecycleOwner
                ) { resultKey, bundle ->
                    if (resultKey == "REQUEST_KEY") {
                        val date = bundle.getString("SELECTED_DATE")
                        // Toast.LENGTH_LONG(date)
                    }
                }

                if (supportFragmentManager != null) {
                    datePickerFragment.show(supportFragmentManager, "DatePickerFragment")
                }
                **/

                val timePickerFragment = TimePickerFragment()

                supportFragmentManager?.setFragmentResultListener(
                    "REQUEST_KEY",
                    viewLifecycleOwner
                ) { resultKey, bundle ->
                    if (resultKey == "REQUEST_KEY") {
                        val time = bundle.getString("SELECTED_TIME")
                        // Toast.LENGTH_LONG(date)
                    }
                }

                if (supportFragmentManager != null) {
                    timePickerFragment.show(supportFragmentManager, "TimePickerFragment")
                }
            }

        }

        return binding.root
    }
}