-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch.py
More file actions
68 lines (51 loc) · 1.91 KB
/
switch.py
File metadata and controls
68 lines (51 loc) · 1.91 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
from abc import ABC, abstractmethod
from .utils import IS_ANDROID, get_android_context
from .view import ViewBase
# ========================================
# Base class
# ========================================
class SwitchBase(ABC):
@abstractmethod
def __init__(self) -> None:
super().__init__()
@abstractmethod
def set_on(self, value: bool) -> "SwitchBase":
pass
@abstractmethod
def is_on(self) -> bool:
pass
if IS_ANDROID:
# ========================================
# Android class
# https://developer.android.com/reference/android/widget/Switch
# ========================================
from java import jclass
class Switch(SwitchBase, ViewBase):
def __init__(self, value: bool = False) -> None:
super().__init__()
self.native_class = jclass("android.widget.Switch")
context = get_android_context()
self.native_instance = self.native_class(context)
self.set_on(value)
def set_on(self, value: bool) -> "Switch":
self.native_instance.setChecked(value)
return self
def is_on(self) -> bool:
return self.native_instance.isChecked()
else:
# ========================================
# iOS class
# https://developer.apple.com/documentation/uikit/uiswitch
# ========================================
from rubicon.objc import ObjCClass
class Switch(SwitchBase, ViewBase):
def __init__(self, value: bool = False) -> None:
super().__init__()
self.native_class = ObjCClass("UISwitch")
self.native_instance = self.native_class.alloc().init()
self.set_on(value)
def set_on(self, value: bool) -> "Switch":
self.native_instance.setOn_animated_(value, False)
return self
def is_on(self) -> bool:
return self.native_instance.isOn()