forked from beeware/toga
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateinput.py
More file actions
75 lines (54 loc) · 2.15 KB
/
dateinput.py
File metadata and controls
75 lines (54 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import weakref
from datetime import date, datetime, time
from android import R
from android.app import DatePickerDialog
from java import dynamic_proxy
from toga_android.widgets.base import ContainedWidget
from .base import suppress_reference_error
from .internal.pickers import PickerBase
def py_date(native_date):
return date.fromtimestamp(native_date / 1000)
def native_date(py_date):
return int(datetime.combine(py_date, time.min).timestamp() * 1000)
class DatePickerListener(dynamic_proxy(DatePickerDialog.OnDateSetListener)):
def __init__(self, impl):
super().__init__()
self.impl = weakref.proxy(impl)
def onDateSet(self, view, year, month_0, day):
with suppress_reference_error():
# It should be impossible for the dialog to return an out-of-range value in
# normal use, but it can happen in the testbed, so go via the interface to
# clip the value.
self.impl.interface.value = date(year, month_0 + 1, day)
class DateInput(PickerBase, ContainedWidget):
@classmethod
def _get_icon(cls):
return R.drawable.ic_menu_my_calendar
def create(self):
super().create()
self.native.setText("2000-01-01") # Dummy initial value
def get_value(self):
return date.fromisoformat(str(self.native.getText()))
def set_value(self, value):
self.native.setText(value.isoformat())
self._dialog.updateDate(value.year, value.month - 1, value.day)
self.interface.on_change()
def get_min_date(self):
return py_date(self._picker.getMinDate())
def set_min_date(self, value):
self._picker.setMinDate(native_date(value))
def get_max_date(self):
return py_date(self._picker.getMaxDate())
def set_max_date(self, value):
self._picker.setMaxDate(native_date(value))
def _create_dialog(self):
return DatePickerDialog(
self._native_activity,
DatePickerListener(self),
2000, # year (dummy initial value)
0, # month (0 = January)
1, # day
)
@property
def _picker(self):
return self._dialog.getDatePicker()