-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatePickerController.py
237 lines (199 loc) · 8.78 KB
/
datePickerController.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
"""
note: Storyboard 実装なし
"""
import ctypes
from pyrubicon.objc.api import ObjCClass
from pyrubicon.objc.api import objc_method, objc_property
from pyrubicon.objc.runtime import send_super, objc_id, SEL
from rbedge.enumerations import (
UIDatePickerMode,
UIControlContentHorizontalAlignment,
UIControlContentVerticalAlignment,
UILayoutConstraintAxis,
UIUserInterfaceSizeClass,
UIDatePickerStyle,
NSDateFormatterStyle,
NSCalendarUnit,
UIControlEvents,
NSTextAlignment,
NSLineBreakMode,
)
from rbedge.functions import NSStringFromClass
from rbedge import pdbr
from pyLocalizedString import localizedString
UIViewController = ObjCClass('UIViewController')
NSLayoutConstraint = ObjCClass('NSLayoutConstraint')
UIColor = ObjCClass('UIColor')
UIDatePicker = ObjCClass('UIDatePicker')
NSDate = ObjCClass('NSDate')
NSDateFormatter = ObjCClass('NSDateFormatter')
NSDateComponents = ObjCClass('NSDateComponents')
NSCalendar = ObjCClass('NSCalendar')
UILabel = ObjCClass('UILabel')
UIFont = ObjCClass('UIFont')
class DatePickerController(UIViewController):
dateFormatter: NSDateFormatter = objc_property()
datePicker: UIDatePicker = objc_property()
dateLabel: UILabel = objc_property()
@objc_method
def dealloc(self):
# xxx: 呼ばない-> `send_super(__class__, self, 'dealloc')`
print(f'\t- {NSStringFromClass(__class__)}: dealloc')
# MARK: - View Life Cycle
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad')
self.navigationItem.title = localizedString('DatePickerTitle') if (
title := self.navigationItem.title) is None else title
self.view.backgroundColor = UIColor.systemBackgroundColor()
# A date formatter to format the `date` property of `datePicker`.
# xxx: 関数化すると落ちる(かも?な)ので、ここに展開
dateFormatter = NSDateFormatter.new()
dateFormatter.setDateStyle_(NSDateFormatterStyle.medium)
dateFormatter.setTimeStyle_(NSDateFormatterStyle.short)
datePicker = UIDatePicker.new()
# todo: Default
datePicker.setDatePickerMode_(UIDatePickerMode.dateAndTime)
datePicker.setMinuteInterval_(1)
datePicker.setContentHorizontalAlignment_(
UIControlContentHorizontalAlignment.center)
datePicker.setContentVerticalAlignment_(
UIControlContentVerticalAlignment.center)
dateLabel = UILabel.new()
dateLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.horizontal)
dateLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.vertical)
dateLabel.text = 'Label'
dateLabel.textAlignment = NSTextAlignment.center
dateLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail
dateLabel.font = UIFont.systemFontOfSize_(17.0)
dateLabel.textColor = UIColor.secondaryLabelColor()
if True: # wip: `available(iOS 15, *)`
# In case the label's content is too large to fit inside the label (causing truncation),
# use this to reveal the label's full text drawn as a tool tip.
dateLabel.showsExpansionTextWhenTruncated = True
# --- Layout
safeAreaLayoutGuide = self.view.safeAreaLayoutGuide
layoutMarginsGuide = self.view.layoutMarginsGuide
self.view.addSubview_(datePicker)
datePicker.translatesAutoresizingMaskIntoConstraints = False
# xxx: `datePicker.centerYAnchor` が画面全体を取ってる模様で中心ではないが、表記の通りに実装
NSLayoutConstraint.activateConstraints_([
datePicker.centerYAnchor.constraintEqualToAnchor_(
self.view.centerYAnchor),
datePicker.centerXAnchor.constraintEqualToAnchor_(
safeAreaLayoutGuide.centerXAnchor),
])
self.view.addSubview_(dateLabel)
dateLabel.translatesAutoresizingMaskIntoConstraints = False
NSLayoutConstraint.activateConstraints_([
dateLabel.topAnchor.constraintEqualToAnchor_constant_(
datePicker.bottomAnchor, 19.0),
dateLabel.leadingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.leadingAnchor),
dateLabel.trailingAnchor.constraintEqualToAnchor_(
layoutMarginsGuide.trailingAnchor),
])
self.dateFormatter = dateFormatter
self.datePicker = datePicker
self.dateLabel = dateLabel
self.configureDatePicker()
@objc_method
def viewWillAppear_(self, animated: bool):
send_super(__class__,
self,
'viewWillAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewWillAppear_')
@objc_method
def viewDidAppear_(self, animated: bool):
send_super(__class__,
self,
'viewDidAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewDidAppear_')
@objc_method
def viewWillDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewWillDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
# print(f'\t{NSStringFromClass(__class__)}: viewWillDisappear_')
@objc_method
def viewDidDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewDidDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
print(f'\t{NSStringFromClass(__class__)}: viewDidDisappear_')
@objc_method
def didReceiveMemoryWarning(self):
send_super(__class__, self, 'didReceiveMemoryWarning')
print(f'{__class__}: didReceiveMemoryWarning')
# MARK: - Configuration
@objc_method
def configureDatePicker(self):
self.datePicker.datePickerMode = UIDatePickerMode.dateAndTime
# Set min/max date for the date picker. As an example we will limit the date between now and 7 days from now.
# 日付ピッカーの最小/最大日付を設定します。例として、日付を現在から 7 日後までに制限します。
now = NSDate.now() # todo: `new()` 、`alloc().init()` と同義
self.datePicker.minimumDate = now
# Decide the best date picker style based on the trait collection's vertical size.
# 特性コレクションの垂直サイズに基づいて、最適な日付ピッカー スタイルを決定します。
self.datePicker.preferredDatePickerStyle = UIDatePickerStyle.compact if self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClass.compact else UIDatePickerStyle.inline
# xxx: `dateComponents` 使わない、、、?
dateComponents = NSDateComponents.new()
dateComponents.day = 7
sevenDaysFromNow = NSCalendar.currentCalendar.dateByAddingUnit(
NSCalendarUnit.day, value=7, toDate=now, options=0)
self.datePicker.maximumDate = sevenDaysFromNow
self.datePicker.minuteInterval = 2
self.datePicker.addTarget(self,
action=SEL('updateDatePickerLabel'),
forControlEvents=UIControlEvents.valueChanged)
self.updateDatePickerLabel()
@objc_method # override
def traitCollectionDidChange_(self, previousTraitCollection):
# xxx: iOS 17 からはDeprecated
# note: [iOS 17 からの画面サイズ変化への対応方法](https://zenn.dev/matsuei/articles/a9143244622d01)
# ref: [traitCollectionDidChange: | Apple Developer Documentation](https://developer.apple.com/documentation/uikit/uitraitenvironment/traitcollectiondidchange(_:)?language=objc)
# ref: [registerForTraitChanges:withHandler: | Apple Developer Documentation](https://developer.apple.com/documentation/uikit/uitraitchangeobservable-7qoet/registerfortraitchanges:withhandler:?language=objc)
send_super(__class__,
self,
'traitCollectionDidChange:',
previousTraitCollection,
argtypes=[
objc_id,
])
# Adjust the date picker style due to the trait collection's vertical size.
self.datePicker.preferredDatePickerStyle = UIDatePickerStyle.compact if self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClass.compact else UIDatePickerStyle.inline
# MARK: - Actions
@objc_method
def updateDatePickerLabel(self):
# todo: `print` でも呼び出すので、変数化
_date_text = self.dateFormatter.stringFromDate_(self.datePicker.date)
self.dateLabel.text = _date_text
print(f'Chosen date: {_date_text}')
if __name__ == '__main__':
from rbedge.app import App
from rbedge.enumerations import UIModalPresentationStyle
main_vc = DatePickerController.new()
_title = NSStringFromClass(DatePickerController)
main_vc.navigationItem.title = _title
#presentation_style = UIModalPresentationStyle.fullScreen
presentation_style = UIModalPresentationStyle.pageSheet
app = App(main_vc, presentation_style)
app.present()