/
main.py
285 lines (198 loc) · 7.9 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from kivymd.app import MDApp
from kivymd.toast import toast
from kivymd.uix.menu import MDDropdownMenu
from kivy.properties import ObjectProperty, StringProperty
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.screen import MDScreen
from kivymd.theming import ThemableBehavior
from kivymd.uix.behaviors import RectangularElevationBehavior
from kivymd.uix.card import MDCardSwipe
from kivy.lang import Builder
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelThreeLine
from kivymd.uix.bottomsheet import MDGridBottomSheet
from kivy.clock import Clock
from kivy.utils import platform
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from custompy.customdialogs import CustomDialogs
from custompy.custompickers import CustomPickers
from custompy.customtaptargetviews import CustomTapTargetViews
class StarterScreen(MDScreen):
pass
class MDTextFieldsScreen(MDScreen):
pass
class MDButtonsScreen(MDScreen):
pass
class MDToastScreen(MDScreen):
pass
class MDDialogsScreen(MDScreen):
pass
class MDChipsScreen(MDScreen):
pass
class MDPickersScreen(MDScreen):
pass
class MDSwitchesScreen(MDScreen):
pass
class MDMenuScreen(MDScreen):
pass
class MDCardScreen(MDScreen):
pass
class MDCardSwipeScreen(MDScreen):
pass
class MDExpansionPanelScreen(MDScreen):
pass
class MDBottomNavigationScreen(MDScreen):
pass
class MDBottomSheetScreen(MDScreen):
pass
class MDProgressBarScreen(MDScreen):
pass
class MDSliderScreen(MDScreen):
pass
class MDSpinnerScreen(MDScreen):
pass
class MDSnackbarScreen(MDScreen):
pass
class MDTabsScreen(MDScreen):
pass
class MDTooltipScreen(MDScreen):
pass
class MDTapTargetViewScreen(MDScreen):
pass
class CustomTab(MDTabsBase, MDFloatLayout):
"""Class implementing content for a tab"""
# Inheriting from MDTabsBase for the tab itself
# Inheriting from MDFloatLayout because that is the layout you will see when you click on a tab
class SwipeToDeleteItem(MDCardSwipe):
"""Card with `swipe-to-delete` behavior."""
text = StringProperty()
class DrawerContent(MDBoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class CustomToolbar(
ThemableBehavior, RectangularElevationBehavior, MDBoxLayout,
):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.md_bg_color = self.theme_cls.primary_color
class ExpansionPanelContent(MDBoxLayout):
pass
class MainDemoApp(MDApp):
def build(self):
self.theme_cls.primary_palette = 'Blue'
self.theme_cls.accent_palette = 'Green'
return Builder.load_file('main.kv')
def on_bottom_navigation(self):
# Without this, the bottom bar is positioned weird
self.root.ids.MD_bottom_navigation_screen.ids.bottom_navigation.on_resize()
def on_start(self):
self.custom_dialogs = CustomDialogs()
self.custom_pickers = CustomPickers()
self.custom_tap_target_views = CustomTapTargetViews(app=self)
# Set up the menus for the menu screen
menu_items = [{"icon": "git", "text": f"Item {i}"} for i in range(5)]
self.menu_1 = MDDropdownMenu(
caller=self.root.ids.MD_menu_screen.ids.button_1, items=menu_items, width_mult=4, position='center')
self.menu_2 = MDDropdownMenu(
caller=self.root.ids.MD_menu_screen.ids.custom_toolbar.ids.button_2, items=menu_items, width_mult=4)
# Set up the swipe cards for card screen
for i in range(10):
self.root.ids.MD_card_swipe_screen.ids.MD_card_swipe_demo.add_widget(SwipeToDeleteItem(text=f'Item {i}'))
self.root.ids.MD_expansion_panel_screen.ids.box.add_widget(
MDExpansionPanel(
icon=r'kivymd/images/kivymd_logo.png',
content=ExpansionPanelContent(),
panel_cls=MDExpansionPanelThreeLine(
text="Text",
secondary_text="Secondary text",
tertiary_text="Tertiary text",
)
)
)
# Set up the tabs for tab screen
tabs = ['Home', 'Search', 'Friends', 'History', 'Account', 'Settings']
for tab in tabs:
self.root.ids.MD_tabs_screen.ids.tabs_holder.add_widget(CustomTab(text=tab))
# Update the current toolbar
current_screen = self.root.ids.screen_manager.current
toolbar = self.root.ids[current_screen].ids.toolbar
toolbar.left_action_items = [
['menu', lambda x: self.root.ids.nav_drawer.set_state('toggle')]]
# If on ios, do this to display the ad banner
if platform == 'ios':
from pyobjus import autoclass
self.banner_ad = autoclass('adSwitch').alloc().init()
def on_tab_switch(self, tabs_holder_instance, tab_instance, tab_label_instance, tab_text):
# tab_instance stands for not only the tab, but also what you see when you click the tab of course,
# that is the inheritence of the floatlayout.
# Set the buttons text in the tab screen to the text of the tab itself
tab_instance.ids.button.text = tab_text
def show_banner(self):
# Show ads
self.banner_ad.show_ads()
def hide_banner(self):
# Hide ads
self.banner_ad.hide_ads()
def change_screen(self, screen_name, text):
screen_manager = self.root.ids.screen_manager
screen_manager.current = screen_name
if screen_name != 'MD_menu_screen':
self.update_toolbar(screen_name, text)
def update_toolbar(self, screen_name, text):
# Update the current active toolbar
toolbar = self.root.ids[screen_name].ids.toolbar
toolbar.left_action_items = [
['menu', lambda x: self.root.ids.nav_drawer.set_state('toggle')]]
toolbar.title = text
def toast(self, instance, value):
toast(value)
print('Maybe hide ad banner')
def remove_item(self, instance):
self.root.ids.MD_card_swipe_screen.ids.MD_card_swipe_demo.remove_widget(instance)
def show_share_bottom_sheet(self):
share_sheet = MDGridBottomSheet(radius_from='top_left')
share_sheet.bind(on_dismiss=lambda *args: print('Hide banner ad'),
on_open=lambda *args: print('Show banner ad'))
data = {
"Facebook": "facebook-box",
"YouTube": "youtube",
"Twitter": "twitter-box",
"iCloud": "cloud-upload",
"WhatsApp": "whatsapp",
}
for item in data.items():
share_sheet.add_item(
text=item[0],
callback=lambda x, y=item[0]: self.toast(x, y),
icon_src=item[1]
)
share_sheet.open()
def animate_progressbar(self):
def set_progress_back(*args):
# Sets bar back to 0
self.root.ids.MD_progress_bar_screen.ids.progressbar.value = 0
def update(*args):
# Update bar by one 150th until 100
pb = self.root.ids.MD_progress_bar_screen.ids.progressbar.value
if pb >= 100:
Clock.schedule_once(set_progress_back, 1)
return False
self.root.ids.MD_progress_bar_screen.ids.progressbar.value += 100/150
set_progress_back()
try:
self.progress.cancel()
except AttributeError:
pass
self.progress = Clock.schedule_interval(update, 0)
def show_spinner(self):
def spinner_done(interval):
self.root.ids.MD_spinner_screen.ids.spinner.active = False
toast('Done!')
self.root.ids.MD_spinner_screen.ids.spinner.active = True
try:
self.spinner.cancel()
except AttributeError:
pass
self.spinner = Clock.schedule_once(spinner_done, 3)
if __name__ == '__main__':
MainDemoApp().run()