-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
137 lines (107 loc) · 4.46 KB
/
main.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
import os
from kivy.uix.screenmanager import RiseInTransition, FallOutTransition, SlideTransition
from kivymd.app import MDApp
from kivymd.uix.list import OneLineIconListItem
from kivymd.uix.menu import MDDropdownMenu
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.metrics import dp
from kivy.utils import platform
if platform == 'android':
from android import mActivity
REST_ENDPOINT = 'https://restdemo.jennasys.com/api'
class AppMenu(MDDropdownMenu):
def __init__(self):
self.items = []
super().__init__(items=self.items, width_mult=3)
def add_item(self, **kwargs):
base_item = {"viewclass": "MenuItem",
"text": "",
"icon": "",
"height": dp(48),
"on_release": None,
}
base_item.update(kwargs)
self.items.append(base_item)
def remove_item(self, item_id):
self.items = [item for item in self.items if item['id'] != item_id]
def click(self, ref):
self.caller = ref
self.open()
class MenuItem(OneLineIconListItem):
icon = StringProperty()
class MainApp(MDApp):
use_kivy_settings = False
sm = None
menu = None
session_cookie = None
def build_config(self, config):
config.setdefaults('app', {'rest_endpoint': REST_ENDPOINT})
def build_settings(self, settings):
jsondata = """ [{"type": "title",
"title": "Kivy REST Demo Settings"},
{"type": "string",
"title": "REST Endpoint",
"desc": "URL for REST API endpoint",
"section": "app",
"key": "rest_endpoint"}] """
settings.add_json_panel('REST Demo', self.config, data=jsondata)
def open_settings(self, *largs):
self.menu.dismiss()
MDApp.open_settings(self, *largs)
def on_config_change(self, config, section, key, value):
if config is self.config:
token = (section, key)
if token == ('app', 'rest_endpoint'):
os.environ['REST_ENDPOINT'] = value
def build(self):
self.theme_cls.theme_style = "Light"
self.theme_cls.primary_palette = "Teal"
self.theme_cls.accent_palette = "Pink"
self.title = "Books"
self.icon = 'images/icon.png'
Window.bind(on_keyboard=self.keyboard_hook)
if platform in ['win', 'linux', 'macosx']:
Window.size = (400, 600)
os.environ['REST_ENDPOINT'] = self.config.get('app', 'rest_endpoint')
self.sm = self.root
self.menu = AppMenu()
self.menu.add_item(id="about", text="About", icon="information", on_release=self.sm.get_screen('about').open)
self.menu.add_item(id="settings", text="Settings", icon="cog", on_release=self.open_settings)
self.menu.add_item(id="refresh", text="Refresh", icon="reload", on_release=self.sm.get_screen('books').get_books)
self.menu.add_item(id="login", text="Login", icon="login", on_release=self.sm.get_screen('login').open)
self.sm.get_screen('books').get_books()
def on_start(self):
self.sm.get_screen('books').open()
def keyboard_hook(self, key, scancode, codepoint, modifier, *args):
if scancode == 27: # ESC
if self.sm.current == 'books':
if platform == 'android':
mActivity.finishAndRemoveTask()
return True
else:
return False
elif self.sm.current == 'edit':
self.sm.get_screen('edit').close()
return True
else:
self.sm.get_screen('books').open()
return True
def switch_screen(self, screen_name='books'):
self.menu.dismiss()
if screen_name == 'login':
self.sm.transition = RiseInTransition()
elif self.sm.current == 'login':
self.sm.transition = FallOutTransition()
elif self.sm.current == 'edit':
self.sm.transition = SlideTransition(direction='right')
elif screen_name == 'about':
self.sm.transition = SlideTransition(direction='right')
else:
self.sm.transition = SlideTransition(direction='left')
self.sm.current = screen_name
def is_auth(self):
return self.session_cookie is not None
if __name__ == '__main__':
app = MainApp()
app.run()