-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicker_view.py
More file actions
69 lines (51 loc) · 2.02 KB
/
picker_view.py
File metadata and controls
69 lines (51 loc) · 2.02 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
from abc import ABC, abstractmethod
from .utils import IS_ANDROID
from .view import ViewBase
# ========================================
# Base class
# ========================================
class PickerViewBase(ABC):
@abstractmethod
def __init__(self) -> None:
super().__init__()
@abstractmethod
def set_selected(self, index: int) -> "PickerViewBase":
pass
@abstractmethod
def get_selected(self) -> int:
pass
if IS_ANDROID:
# ========================================
# Android class
# https://developer.android.com/reference/android/widget/Spinner
# ========================================
from typing import Any
from java import jclass
class PickerView(PickerViewBase, ViewBase):
def __init__(self, context: Any, index: int = 0) -> None:
super().__init__()
self.native_class = jclass("android.widget.Spinner")
self.native_instance = self.native_class(context)
self.set_selected(index)
def set_selected(self, index: int) -> "PickerView":
self.native_instance.setSelection(index)
return self
def get_selected(self) -> int:
return self.native_instance.getSelectedItemPosition()
else:
# ========================================
# iOS class
# https://developer.apple.com/documentation/uikit/uipickerview
# ========================================
from rubicon.objc import ObjCClass
class PickerView(PickerViewBase, ViewBase):
def __init__(self, index: int = 0) -> None:
super().__init__()
self.native_class = ObjCClass("UIPickerView")
self.native_instance = self.native_class.alloc().init()
self.set_selected(index)
def set_selected(self, index: int) -> "PickerView":
self.native_instance.selectRow_inComponent_animated_(index, 0, False)
return self
def get_selected(self) -> int:
return self.native_instance.selectedRowInComponent_(0)