-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaterial_date_picker.py
More file actions
87 lines (67 loc) · 2.92 KB
/
material_date_picker.py
File metadata and controls
87 lines (67 loc) · 2.92 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
76
77
78
79
80
81
82
83
84
85
86
87
from abc import ABC, abstractmethod
from .utils import IS_ANDROID
from .view import ViewBase
# ========================================
# Base class
# ========================================
class MaterialDatePickerBase(ABC):
@abstractmethod
def __init__(self) -> None:
super().__init__()
@abstractmethod
def set_date(self, year: int, month: int, day: int) -> "MaterialDatePickerBase":
pass
@abstractmethod
def get_date(self) -> tuple:
pass
if IS_ANDROID:
# ========================================
# Android class
# https://developer.android.com/reference/com/google/android/material/datepicker/MaterialDatePicker
# ========================================
from java import jclass
class MaterialDatePicker(MaterialDatePickerBase, ViewBase):
def __init__(self, year: int = 0, month: int = 0, day: int = 0) -> None:
super().__init__()
self.native_class = jclass("com.google.android.material.datepicker.MaterialDatePicker")
self.builder = self.native_class.Builder.datePicker()
self.set_date(year, month, day)
self.native_instance = self.builder.build()
def set_date(self, year: int, month: int, day: int) -> "MaterialDatePicker":
# MaterialDatePicker uses milliseconds since epoch to set date
from java.util import Calendar
cal = Calendar.getInstance()
cal.set(year, month, day)
milliseconds = cal.getTimeInMillis()
self.builder.setSelection(milliseconds)
return self
def get_date(self) -> tuple:
# Convert selection (milliseconds since epoch) back to a date
from java.util import Calendar
cal = Calendar.getInstance()
cal.setTimeInMillis(self.native_instance.getSelection())
return (
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH),
)
else:
# ========================================
# iOS class
# https://developer.apple.com/documentation/uikit/uidatepicker
# ========================================
from datetime import datetime
from rubicon.objc import ObjCClass
class MaterialDatePicker(MaterialDatePickerBase, ViewBase):
def __init__(self, year: int = 0, month: int = 0, day: int = 0) -> None:
super().__init__()
self.native_class = ObjCClass("UIDatePicker")
self.native_instance = self.native_class.alloc().init()
self.set_date(year, month, day)
def set_date(self, year: int, month: int, day: int) -> "MaterialDatePicker":
date = datetime(year, month, day)
self.native_instance.setDate_(date)
return self
def get_date(self) -> tuple:
date = self.native_instance.date()
return date.year, date.month, date.day