Skip to content

Commit

Permalink
Merge pull request #143 from icerockdev/#90-datepicker-android
Browse files Browse the repository at this point in the history
datepicker screen action
  • Loading branch information
Alex009 authored Feb 25, 2020
2 parents a540b8a + e51d2b5 commit 3b00eb5
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 0 deletions.
5 changes: 5 additions & 0 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ object Deps {
common = "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:${Versions.Libs.MultiPlatform.coroutines}",
ios = "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:${Versions.Libs.MultiPlatform.coroutines}"
)
val klock = MultiPlatformLibrary(
android = "com.soywiz.korlibs.klock:klock-android:${Versions.Libs.MultiPlatform.klockVersion}",
common = "com.soywiz.korlibs.klock:klock:${Versions.Libs.MultiPlatform.klockVersion}",
ios = "com.soywiz.korlibs.klock:klock:${Versions.Libs.MultiPlatform.klockVersion}"
)
}

object Jvm {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object Versions {
const val mokoMedia = "0.2.0"
const val mokoGraphics = "0.2.0"
const val mokoParcelize = "0.2.0"
const val klockVersion = "1.8.4"
}
}
}
1 change: 1 addition & 0 deletions widgets/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
mppLibrary(Deps.Libs.MultiPlatform.mokoMedia)
mppLibrary(Deps.Libs.MultiPlatform.mokoGraphics)
mppLibrary(Deps.Libs.MultiPlatform.mokoParcelize)
mppLibrary(Deps.Libs.MultiPlatform.klock)

androidLibrary(Deps.Libs.Android.appCompat)
androidLibrary(Deps.Libs.Android.fragment)
Expand Down
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)
}
}
}
}
}
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
)
Loading

0 comments on commit 3b00eb5

Please sign in to comment.