-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
datepicker screen action
- Loading branch information
Showing
6 changed files
with
433 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
widgets/src/androidMain/kotlin/dev/icerock/moko/widgets/screen/DatePickerDialogHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
/* | ||
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.widgets.screen | ||
|
||
import android.app.DatePickerDialog | ||
import android.app.Dialog | ||
import android.os.Bundle | ||
import android.os.Parcelable | ||
import androidx.fragment.app.DialogFragment | ||
import androidx.fragment.app.FragmentManager | ||
import com.soywiz.klock.DateTime | ||
import dev.icerock.moko.graphics.Color | ||
import dev.icerock.moko.parcelize.Parcelize | ||
import java.util.* | ||
import kotlin.properties.ReadOnlyProperty | ||
|
||
actual fun Screen<*>.showDatePickerDialog( | ||
dialogId: Int, | ||
handler: DatePickerDialogHandler, | ||
factory: DatePickerDialogBuilder.() -> Unit | ||
) { | ||
val alert = DatePickerDialogBuilder( | ||
dialogId, | ||
this.childFragmentManager, | ||
handler | ||
) | ||
factory(alert) | ||
alert.show() | ||
} | ||
|
||
actual class DatePickerDialogHandler | ||
|
||
actual fun Screen<*>.registerDatePickerDialogHandler( | ||
positive: ((dialogId: Int, date: DateTime) -> Unit)?, | ||
negative: ((dialogId: Int) -> Unit)? | ||
): ReadOnlyProperty<Screen<*>, DatePickerDialogHandler> = | ||
registerAttachFragmentHook(DatePickerDialogHandler()) { fragment -> | ||
if (fragment !is DatePickerDialogFragment) return@registerAttachFragmentHook | ||
|
||
fragment.listener = object : | ||
DatePickerDialogFragment.Listener { | ||
override fun onPositivePressed(dialogId: Int, date: DateTime) { | ||
positive?.invoke(dialogId, date) | ||
} | ||
|
||
override fun onNegativePressed(dialogId: Int) { | ||
negative?.invoke(dialogId) | ||
} | ||
} | ||
} | ||
|
||
actual class DatePickerDialogBuilder( | ||
private val dialogId: Int, | ||
private val fragmentManager: FragmentManager, | ||
private val handler: DatePickerDialogHandler | ||
) { | ||
private var startDate: DateTime? = null | ||
private var endDate: DateTime? = null | ||
private var selectedDate: DateTime? = null | ||
|
||
actual fun accentColor(color: Color) { | ||
//android color from style | ||
} | ||
|
||
actual fun startDate(date: DateTime) { | ||
startDate = date | ||
} | ||
|
||
actual fun endDate(date: DateTime) { | ||
endDate = date | ||
} | ||
|
||
actual fun selectedDate(date: DateTime) { | ||
selectedDate = date | ||
} | ||
|
||
internal fun show() { | ||
val alertDialogFragment = | ||
DatePickerDialogFragment.instantiate( | ||
arg = DatePickerDialogFragment.Args( | ||
dialogId = dialogId, | ||
startDate = startDate?.unixMillisLong, | ||
endDate = endDate?.unixMillisLong, | ||
selectedDate = selectedDate?.unixMillisLong | ||
) | ||
) | ||
alertDialogFragment.show(fragmentManager, null) | ||
} | ||
} | ||
|
||
class DatePickerDialogFragment : DialogFragment() { | ||
var listener: Listener? = null | ||
|
||
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
val argument = arguments?.getParcelable<Args>( | ||
ARG_KEY | ||
) | ||
requireNotNull(argument) { "can't be opened without argument" } | ||
|
||
val dialogId = argument.dialogId | ||
val selectedDate = Calendar.getInstance() | ||
argument.selectedDate?.let { | ||
selectedDate.timeInMillis = it | ||
} | ||
val dialog = DatePickerDialog( | ||
context, { _ | ||
, year | ||
, month | ||
, day | ||
-> | ||
val date = Calendar.getInstance() | ||
date.set(year, month, day) | ||
listener?.onPositivePressed(dialogId, DateTime(date.timeInMillis)) | ||
}, | ||
selectedDate.get(Calendar.YEAR), | ||
selectedDate.get(Calendar.MONTH), | ||
selectedDate.get(Calendar.DAY_OF_MONTH) | ||
) | ||
argument.endDate?.let { dialog.datePicker.maxDate = it } | ||
argument.startDate?.let { dialog.datePicker.minDate = it } | ||
dialog.setOnCancelListener { listener?.onNegativePressed(dialogId) } | ||
return dialog | ||
} | ||
|
||
interface Listener { | ||
fun onPositivePressed(dialogId: Int, date: DateTime) | ||
fun onNegativePressed(dialogId: Int) | ||
} | ||
|
||
@Parcelize | ||
data class Args( | ||
val dialogId: Int, | ||
val startDate: Long?, | ||
val endDate: Long?, | ||
val selectedDate: Long? | ||
) : Parcelable | ||
|
||
companion object { | ||
private const val ARG_KEY = "arg_bundle" | ||
|
||
fun instantiate(arg: Args): DatePickerDialogFragment { | ||
return DatePickerDialogFragment() | ||
.apply { | ||
arguments = Bundle().apply { | ||
putParcelable(ARG_KEY, arg) | ||
} | ||
} | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
widgets/src/commonMain/kotlin/dev/icerock/moko/widgets/screen/ShowDatePickerExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2020 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package dev.icerock.moko.widgets.screen | ||
|
||
import com.soywiz.klock.DateTime | ||
import dev.icerock.moko.graphics.Color | ||
import kotlin.properties.ReadOnlyProperty | ||
|
||
expect class DatePickerDialogHandler | ||
|
||
expect fun Screen<*>.registerDatePickerDialogHandler( | ||
positive: ((dialogId: Int, date: DateTime) -> Unit)?, | ||
negative: ((dialogId: Int) -> Unit)? = null | ||
): ReadOnlyProperty<Screen<*>, DatePickerDialogHandler> | ||
|
||
expect class DatePickerDialogBuilder { | ||
fun accentColor(color: Color) | ||
fun startDate(date: DateTime) | ||
fun endDate(date: DateTime) | ||
fun selectedDate(date: DateTime) | ||
} | ||
|
||
expect fun Screen<*>.showDatePickerDialog( | ||
dialogId: Int, | ||
handler: DatePickerDialogHandler, | ||
factory: DatePickerDialogBuilder.() -> Unit | ||
) |
Oops, something went wrong.