commit
6aaae52450
@ -1,119 +0,0 @@ |
|||||||
package net.pokeranalytics.android.ui.fragment.components |
|
||||||
|
|
||||||
import android.app.DatePickerDialog |
|
||||||
import android.app.TimePickerDialog |
|
||||||
import android.os.Bundle |
|
||||||
import android.text.format.DateFormat |
|
||||||
import android.view.LayoutInflater |
|
||||||
import android.view.View |
|
||||||
import android.widget.DatePicker |
|
||||||
import android.widget.TimePicker |
|
||||||
import kotlinx.android.synthetic.main.bottom_sheet_date.* |
|
||||||
import kotlinx.android.synthetic.main.fragment_bottom_sheet.view.* |
|
||||||
import timber.log.Timber |
|
||||||
import java.util.* |
|
||||||
|
|
||||||
|
|
||||||
class BottomSheetDateFragment : BottomSheetFragment(), DatePickerDialog.OnDateSetListener, TimePickerDialog.OnTimeSetListener { |
|
||||||
|
|
||||||
enum class DateEdition { |
|
||||||
START, |
|
||||||
END |
|
||||||
} |
|
||||||
|
|
||||||
private val calendar = Calendar.getInstance() |
|
||||||
private val calendarStart = Calendar.getInstance() |
|
||||||
private val calendarEnd = Calendar.getInstance() |
|
||||||
|
|
||||||
private var currentDateEdition = DateEdition.START |
|
||||||
|
|
||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
|
||||||
super.onViewCreated(view, savedInstanceState) |
|
||||||
initData() |
|
||||||
initUI() |
|
||||||
} |
|
||||||
|
|
||||||
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) { |
|
||||||
when(currentDateEdition) { |
|
||||||
DateEdition.START -> { |
|
||||||
calendarStart.set(Calendar.YEAR, year) |
|
||||||
calendarStart.set(Calendar.MONTH, month) |
|
||||||
calendarStart.set(Calendar.DAY_OF_MONTH, dayOfMonth) |
|
||||||
} |
|
||||||
DateEdition.END -> { |
|
||||||
calendarEnd.set(Calendar.YEAR, year) |
|
||||||
calendarEnd.set(Calendar.MONTH, month) |
|
||||||
calendarEnd.set(Calendar.DAY_OF_MONTH, dayOfMonth) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
showTimePicker() |
|
||||||
} |
|
||||||
|
|
||||||
override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) { |
|
||||||
|
|
||||||
when(currentDateEdition) { |
|
||||||
DateEdition.START -> { |
|
||||||
calendarStart.set(Calendar.HOUR_OF_DAY, hourOfDay) |
|
||||||
calendarStart.set(Calendar.MINUTE, minute) |
|
||||||
} |
|
||||||
DateEdition.END -> { |
|
||||||
calendarEnd.set(Calendar.HOUR_OF_DAY, hourOfDay) |
|
||||||
calendarEnd.set(Calendar.MINUTE, minute) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
Timber.d("Start date: ${calendarStart.time}") |
|
||||||
Timber.d("End date: ${calendarEnd.time}") |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Init data |
|
||||||
*/ |
|
||||||
private fun initData() { |
|
||||||
val data = getData() |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Init UI |
|
||||||
*/ |
|
||||||
private fun initUI() { |
|
||||||
|
|
||||||
LayoutInflater.from(requireContext()).inflate(net.pokeranalytics.android.R.layout.bottom_sheet_date, view?.bottomSheetContainer, true) |
|
||||||
|
|
||||||
setAddButtonVisible(false) |
|
||||||
|
|
||||||
startDate.setOnClickListener { |
|
||||||
currentDateEdition = DateEdition.START |
|
||||||
showDatePicker() |
|
||||||
} |
|
||||||
|
|
||||||
endDate.setOnClickListener { |
|
||||||
currentDateEdition = DateEdition.END |
|
||||||
showDatePicker() |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Show the date picker |
|
||||||
*/ |
|
||||||
private fun showDatePicker() { |
|
||||||
val year = calendar.get(Calendar.YEAR) |
|
||||||
val month = calendar.get(Calendar.MONTH) |
|
||||||
val day = calendar.get(Calendar.DAY_OF_MONTH) |
|
||||||
val datePickerDialog = DatePickerDialog(requireContext(), this, year, month, day) |
|
||||||
datePickerDialog.show() |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Show the time picker |
|
||||||
*/ |
|
||||||
private fun showTimePicker() { |
|
||||||
val hour = calendar.get(Calendar.YEAR) |
|
||||||
val minute = calendar.get(Calendar.MONTH) |
|
||||||
val timePickerDialog = TimePickerDialog(activity, this, hour, minute, DateFormat.is24HourFormat(activity)) |
|
||||||
timePickerDialog.show() |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
@ -0,0 +1,77 @@ |
|||||||
|
package net.pokeranalytics.android.ui.fragment.components |
||||||
|
|
||||||
|
import android.app.DatePickerDialog |
||||||
|
import android.app.TimePickerDialog |
||||||
|
import android.content.Context |
||||||
|
import android.text.format.DateFormat |
||||||
|
import android.widget.DatePicker |
||||||
|
import android.widget.TimePicker |
||||||
|
import net.pokeranalytics.android.ui.adapter.components.DynamicRowInterface |
||||||
|
import java.util.* |
||||||
|
|
||||||
|
class DateTimePickerManager : DatePickerDialog.OnDateSetListener, |
||||||
|
TimePickerDialog.OnTimeSetListener { |
||||||
|
|
||||||
|
private var context: Context? = null |
||||||
|
private val calendar = Calendar.getInstance() |
||||||
|
|
||||||
|
lateinit var row: DynamicRowInterface |
||||||
|
lateinit var bottomSheetDelegate: BottomSheetDelegate |
||||||
|
|
||||||
|
|
||||||
|
companion object { |
||||||
|
fun create( |
||||||
|
context: Context, |
||||||
|
row: DynamicRowInterface, |
||||||
|
bottomSheetDelegate: BottomSheetDelegate, |
||||||
|
date: Date? |
||||||
|
) : DateTimePickerManager { |
||||||
|
val dateTimePickerManager = DateTimePickerManager() |
||||||
|
dateTimePickerManager.context = context |
||||||
|
dateTimePickerManager.showDatePicker() |
||||||
|
dateTimePickerManager.row = row |
||||||
|
dateTimePickerManager.bottomSheetDelegate = bottomSheetDelegate |
||||||
|
return dateTimePickerManager |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) { |
||||||
|
calendar.set(Calendar.YEAR, year) |
||||||
|
calendar.set(Calendar.MONTH, month) |
||||||
|
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth) |
||||||
|
showTimePicker() |
||||||
|
} |
||||||
|
|
||||||
|
override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) { |
||||||
|
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay) |
||||||
|
calendar.set(Calendar.MINUTE, minute) |
||||||
|
bottomSheetDelegate.setValue(calendar.time, row) |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Show the date picker |
||||||
|
*/ |
||||||
|
private fun showDatePicker() { |
||||||
|
val year = calendar.get(Calendar.YEAR) |
||||||
|
val month = calendar.get(Calendar.MONTH) |
||||||
|
val day = calendar.get(Calendar.DAY_OF_MONTH) |
||||||
|
context?.let { |
||||||
|
val datePickerDialog = DatePickerDialog(it, this, year, month, day) |
||||||
|
datePickerDialog.show() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Show the time picker |
||||||
|
*/ |
||||||
|
private fun showTimePicker() { |
||||||
|
val hour = calendar.get(Calendar.YEAR) |
||||||
|
val minute = calendar.get(Calendar.MONTH) |
||||||
|
context?.let { |
||||||
|
val timePickerDialog = TimePickerDialog(context, this, hour, minute, DateFormat.is24HourFormat(context)) |
||||||
|
timePickerDialog.show() |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,21 @@ |
|||||||
|
package net.pokeranalytics.android.util |
||||||
|
|
||||||
|
import java.text.DateFormat |
||||||
|
import java.util.* |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Date |
||||||
|
|
||||||
|
fun Date.short(): String { |
||||||
|
return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(this) |
||||||
|
} |
||||||
|
|
||||||
|
fun Date.medium(): String { |
||||||
|
return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(this) |
||||||
|
} |
||||||
|
|
||||||
|
fun Date.full(): String { |
||||||
|
return DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(this) |
||||||
|
} |
||||||
@ -0,0 +1,58 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/container" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:background="?selectableItemBackground" |
||||||
|
android:padding="16dp"> |
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatTextView |
||||||
|
android:id="@+id/title" |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_marginStart="8dp" |
||||||
|
android:layout_marginTop="8dp" |
||||||
|
android:layout_marginEnd="8dp" |
||||||
|
android:layout_marginBottom="8dp" |
||||||
|
android:textSize="18sp" |
||||||
|
app:layout_constraintBottom_toBottomOf="parent" |
||||||
|
app:layout_constraintHorizontal_bias="0.0" |
||||||
|
app:layout_constraintStart_toStartOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
tools:text="Title" /> |
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatTextView |
||||||
|
android:id="@+id/value" |
||||||
|
android:layout_width="0dp" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_marginStart="16dp" |
||||||
|
android:layout_marginTop="8dp" |
||||||
|
android:layout_marginEnd="8dp" |
||||||
|
android:layout_marginBottom="8dp" |
||||||
|
android:ellipsize="end" |
||||||
|
android:gravity="end|center_vertical" |
||||||
|
android:maxLines="1" |
||||||
|
android:textSize="14sp" |
||||||
|
app:layout_constraintBottom_toBottomOf="parent" |
||||||
|
app:layout_constraintEnd_toStartOf="@+id/action" |
||||||
|
app:layout_constraintStart_toEndOf="@+id/title" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
tools:text="Value" /> |
||||||
|
|
||||||
|
<androidx.appcompat.widget.AppCompatImageView |
||||||
|
android:id="@+id/action" |
||||||
|
android:layout_width="32dp" |
||||||
|
android:layout_height="32dp" |
||||||
|
android:layout_marginTop="4dp" |
||||||
|
android:layout_marginBottom="4dp" |
||||||
|
android:background="?selectableItemBackgroundBorderless" |
||||||
|
android:padding="4dp" |
||||||
|
android:visibility="visible" |
||||||
|
app:layout_constraintBottom_toBottomOf="parent" |
||||||
|
app:layout_constraintEnd_toEndOf="parent" |
||||||
|
app:layout_constraintTop_toTopOf="parent" |
||||||
|
tools:src="@drawable/ic_close_white_24dp" /> |
||||||
|
|
||||||
|
</androidx.constraintlayout.widget.ConstraintLayout> |
||||||
Loading…
Reference in new issue