Skip to content

Commit 1ba8c19

Browse files
committed
Add abstract base classes (ABCs) to pythonnative
1 parent 3c692ec commit 1ba8c19

6 files changed

Lines changed: 146 additions & 27 deletions

File tree

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from .view import View
21
from .button import Button
32
from .label import Label
43
from .linear_layout import LinearLayout
54
from .screen import Screen
65

7-
__all__ = ["View", "Button", "Label", "LinearLayout", "Screen"]
6+
__all__ = ["Button", "Label", "LinearLayout", "Screen"]

libs/pythonnative/pythonnative/button.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1+
from abc import ABC, abstractmethod
12
from .utils import IS_ANDROID
2-
from .view import View
3+
from .view import ViewBase
4+
5+
# ========================================
6+
# Base class
7+
# ========================================
8+
9+
10+
class ButtonBase(ABC):
11+
@abstractmethod
12+
def __init__(self) -> None:
13+
super().__init__()
14+
15+
@abstractmethod
16+
def set_title(self, title: str) -> None:
17+
pass
18+
19+
@abstractmethod
20+
def get_title(self) -> str:
21+
pass
22+
323

424
if IS_ANDROID:
25+
# ========================================
26+
# Android class
27+
# ========================================
28+
529
from java import jclass
630

7-
class Button(View):
31+
class Button(ButtonBase, ViewBase):
832
def __init__(self, context, title: str = "") -> None:
933
super().__init__()
1034
self.native_class = jclass("android.widget.Button")
@@ -18,9 +42,13 @@ def get_title(self) -> str:
1842
return self.native_instance.getText().toString()
1943

2044
else:
45+
# ========================================
46+
# iOS class
47+
# ========================================
48+
2149
from rubicon.objc import ObjCClass
2250

23-
class Button(View):
51+
class Button(ButtonBase, ViewBase):
2452
def __init__(self, title: str = "") -> None:
2553
super().__init__()
2654
self.native_class = ObjCClass("UIButton")

libs/pythonnative/pythonnative/label.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,34 @@
1+
from abc import ABC, abstractmethod
12
from .utils import IS_ANDROID
2-
from .view import View
3+
from .view import ViewBase
4+
5+
# ========================================
6+
# Base class
7+
# ========================================
8+
9+
10+
class LabelBase(ABC):
11+
@abstractmethod
12+
def __init__(self) -> None:
13+
super().__init__()
14+
15+
@abstractmethod
16+
def set_text(self, text: str) -> None:
17+
pass
18+
19+
@abstractmethod
20+
def get_text(self) -> str:
21+
pass
22+
323

424
if IS_ANDROID:
25+
# ========================================
26+
# Android class
27+
# ========================================
28+
529
from java import jclass
630

7-
class Label(View):
31+
class Label(LabelBase, ViewBase):
832
def __init__(self, context, text: str = "") -> None:
933
super().__init__()
1034
self.native_class = jclass("android.widget.TextView")
@@ -18,9 +42,13 @@ def get_text(self) -> str:
1842
return self.native_instance.getText().toString()
1943

2044
else:
45+
# ========================================
46+
# iOS class
47+
# ========================================
48+
2149
from rubicon.objc import ObjCClass
2250

23-
class Label(View):
51+
class Label(LabelBase, ViewBase):
2452
def __init__(self, text: str = "") -> None:
2553
super().__init__()
2654
self.native_class = ObjCClass("UILabel")

libs/pythonnative/pythonnative/linear_layout.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
1+
from abc import ABC, abstractmethod
12
from .utils import IS_ANDROID
2-
from .view import View
3+
from .view import ViewBase
4+
5+
# ========================================
6+
# Base class
7+
# ========================================
8+
9+
10+
class LinearLayoutBase(ABC):
11+
@abstractmethod
12+
def __init__(self) -> None:
13+
super().__init__()
14+
self.views = []
15+
16+
@abstractmethod
17+
def add_view(self, view) -> None:
18+
pass
19+
320

421
if IS_ANDROID:
22+
# ========================================
23+
# Android class
24+
# ========================================
25+
526
from java import jclass
627

7-
class LinearLayout(View):
28+
class LinearLayout(LinearLayoutBase, ViewBase):
829
def __init__(self, context) -> None:
930
super().__init__()
1031
self.native_class = jclass("android.widget.LinearLayout")
1132
self.native_instance = self.native_class(context)
1233
self.native_instance.setOrientation(self.native_class.VERTICAL)
13-
self.views = []
1434

1535
def add_view(self, view):
1636
self.views.append(view)
1737
self.native_instance.addView(view.native_instance)
1838

1939
else:
40+
# ========================================
41+
# iOS class
42+
# ========================================
43+
2044
from rubicon.objc import ObjCClass
2145

22-
class LinearLayout(View):
46+
class LinearLayout(LinearLayoutBase, ViewBase):
2347
def __init__(self) -> None:
2448
super().__init__()
2549
self.native_class = ObjCClass("UIStackView")
2650
self.native_instance = self.native_class.alloc().initWithFrame_(
2751
((0, 0), (0, 0))
2852
)
2953
self.native_instance.setAxis_(0) # Set axis to vertical
30-
self.views = []
3154

3255
def add_view(self, view):
3356
self.views.append(view)

libs/pythonnative/pythonnative/screen.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1+
from abc import ABC, abstractmethod
12
from .utils import IS_ANDROID
2-
from .view import View
3+
from .view import ViewBase
4+
5+
# ========================================
6+
# Base class
7+
# ========================================
8+
9+
10+
class ScreenBase(ABC):
11+
@abstractmethod
12+
def __init__(self) -> None:
13+
super().__init__()
14+
self.layout = None
15+
16+
@abstractmethod
17+
def add_view(self, view) -> None:
18+
pass
19+
20+
@abstractmethod
21+
def set_layout(self, layout) -> None:
22+
pass
23+
24+
@abstractmethod
25+
def show(self) -> None:
26+
pass
27+
328

429
if IS_ANDROID:
30+
# ========================================
31+
# Android class
32+
# ========================================
33+
534
from java import jclass
635

7-
class Screen(View):
36+
class Screen(ScreenBase, ViewBase):
837
def __init__(self):
938
super().__init__()
1039
self.native_class = jclass("android.app.Activity")
1140
self.native_instance = self.native_class()
12-
self.layout = None
1341

1442
def add_view(self, view):
1543
if self.layout is None:
@@ -25,14 +53,17 @@ def show(self):
2553
pass
2654

2755
else:
56+
# ========================================
57+
# iOS class
58+
# ========================================
59+
2860
from rubicon.objc import ObjCClass
2961

30-
class Screen(View):
62+
class Screen(ScreenBase, ViewBase):
3163
def __init__(self):
3264
super().__init__()
3365
self.native_class = ObjCClass("UIViewController")
3466
self.native_instance = self.native_class.alloc().init()
35-
self.layout = None
3667

3768
def add_view(self, view):
3869
if self.layout is None:
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
class View:
1+
from abc import ABC, abstractmethod
2+
3+
# ========================================
4+
# Base class
5+
# ========================================
6+
7+
8+
class ViewBase(ABC):
29
def __init__(self) -> None:
310
self.native_instance = None
411
self.native_class = None
512

6-
def add_view(self, view):
7-
raise NotImplementedError("This method should be implemented in a subclass.")
8-
9-
def set_layout(self, layout):
10-
raise NotImplementedError("This method should be implemented in a subclass.")
11-
12-
def show(self):
13-
raise NotImplementedError("This method should be implemented in a subclass.")
13+
# @abstractmethod
14+
# def add_view(self, view):
15+
# pass
16+
#
17+
# @abstractmethod
18+
# def set_layout(self, layout):
19+
# pass
20+
#
21+
# @abstractmethod
22+
# def show(self):
23+
# pass

0 commit comments

Comments
 (0)