From 838b2ed10a2a15aa4b461751f6a30418f66bc3bf Mon Sep 17 00:00:00 2001 From: nikol412 Date: Wed, 3 Mar 2021 22:50:27 +0600 Subject: [PATCH] #6 added design for bottom nav dialog --- .../CreateTaskBottomSheetDialogFragment.kt | 42 +++++++++++++++++-- .../CreateTaskBottomSheetDialogViewModel.kt | 15 +++++++ app/src/main/res/drawable/ic_add.xml | 10 +++++ .../main/res/layout/fragment_create_task.xml | 20 ++++++--- app/src/main/res/layout/fragment_home.xml | 3 +- app/src/main/res/values/styles.xml | 2 +- 6 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 app/src/main/res/drawable/ic_add.xml diff --git a/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogFragment.kt b/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogFragment.kt index 07c11b3..19af39f 100644 --- a/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogFragment.kt +++ b/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogFragment.kt @@ -1,5 +1,6 @@ package com.example.timetracker.ui.fragment.home.createTask +import android.app.DatePickerDialog import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -9,6 +10,7 @@ import androidx.fragment.app.viewModels import com.example.timetracker.R import com.example.timetracker.databinding.FragmentCreateTaskBinding import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import java.util.* class CreateTaskBottomSheetDialogFragment : BottomSheetDialogFragment() { @@ -18,6 +20,25 @@ class CreateTaskBottomSheetDialogFragment : BottomSheetDialogFragment() { lateinit var binding: FragmentCreateTaskBinding + private val datePicker by lazy { + val calendar = Calendar.getInstance() + val datePicker = DatePickerDialog( + requireContext(), + dateListener, + calendar.get(Calendar.YEAR), + calendar.get(Calendar.MONTH), + calendar.get(Calendar.DAY_OF_MONTH) + ) + datePicker.datePicker.minDate = Calendar.getInstance().timeInMillis + + return@lazy datePicker + } + + val dateListener = + DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth -> + viewModel.taskDate.value = "$dayOfMonth/$month/$year" + } + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -27,11 +48,24 @@ class CreateTaskBottomSheetDialogFragment : BottomSheetDialogFragment() { binding.vm = viewModel binding.lifecycleOwner = this - binding.buttonCreateTask.setOnClickListener { - - this.dismiss() - } + subscribeViewModelVariables() return binding.root } + + fun subscribeViewModelVariables() { + viewModel.event.observe(viewLifecycleOwner, { event -> + when (event) { + CreateTaskEvent.ADD_TASK -> this.dismiss() + CreateTaskEvent.DATE -> showDatePicker() + } + }) + } + + + private fun showDatePicker() { + datePicker.show() + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt b/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt index 8e04b61..e5bc3f2 100644 --- a/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt +++ b/app/src/main/java/com/example/timetracker/ui/fragment/home/createTask/CreateTaskBottomSheetDialogViewModel.kt @@ -3,6 +3,7 @@ package com.example.timetracker.ui.fragment.home.createTask import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.example.timetracker.App +import com.example.timetracker.common.live.SingleLiveEvent import com.example.timetracker.ui.base.BaseViewModel class CreateTaskBottomSheetDialogViewModel: BaseViewModel() { @@ -11,7 +12,21 @@ class CreateTaskBottomSheetDialogViewModel: BaseViewModel() { val taskDescription = MutableLiveData("") val taskDate = MutableLiveData("") + val event = SingleLiveEvent() + init { App.appComponent?.inject(this) } + + fun onCreateTaskClick() { + event.value = CreateTaskEvent.ADD_TASK + } + + fun onDateClick() { + event.value = CreateTaskEvent.DATE + } +} + +enum class CreateTaskEvent { + ADD_TASK, DATE } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_add.xml b/app/src/main/res/drawable/ic_add.xml new file mode 100644 index 0000000..eb23254 --- /dev/null +++ b/app/src/main/res/drawable/ic_add.xml @@ -0,0 +1,10 @@ + + + diff --git a/app/src/main/res/layout/fragment_create_task.xml b/app/src/main/res/layout/fragment_create_task.xml index c77d247..293e7b0 100644 --- a/app/src/main/res/layout/fragment_create_task.xml +++ b/app/src/main/res/layout/fragment_create_task.xml @@ -10,15 +10,18 @@ + android:layout_height="match_parent" + android:layout_marginHorizontal="16dp" + android:paddingBottom="40dp"> + app:layout_constraintEnd_toEndOf="parent" + android:onClick="@{() -> vm.onCreateTaskClick()}"/> @@ -36,7 +40,9 @@ app:layout_constraintTop_toBottomOf="@+id/edittext_task_name" android:layout_marginTop="10dp" android:background="@color/colorPrimaryDark" - android:text="@={vm.taskDate}"/> + android:text="@={vm.taskDate}" + android:maxLines="1" + android:onClick="@{() -> vm.onDateClick()}"/> diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 47fc982..0b93d6a 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -12,7 +12,8 @@ + android:layout_height="match_parent" + android:layout_marginBottom="?actionBarSize"> -