-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
373 lines (271 loc) · 10.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from utils import *
mobile_screen_sim('samsung')
from kivy.properties import NumericProperty, StringProperty, \
ObjectProperty, OptionProperty, ColorProperty
from kivy.uix.screenmanager import ShaderTransition, TransitionBase, AnimationTransition
from kivy.uix.behaviors import ButtonBehavior
from kivy.animation import Animation
from kivy.core.window import Window
from kivy.modules import inspector
from kivy.uix.image import Image
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.metrics import dp
from kivymd.uix.behaviors import FakeRectangularElevationBehavior, \
CircularRippleBehavior, RectangularRippleBehavior
from kivymd.uix.list import OneLineAvatarListItem, ImageLeftWidget, \
OneLineListItem
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.button import BaseFlatIconButton
from kivymd.uix.behaviors import TouchBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.icon_definitions import md_icons
from kivymd.theming import ThemableBehavior
from kivymd.uix.carousel import MDCarousel
from kivymd.uix.label import MDLabel
from kivymd.app import MDApp
import kivymd_extensions.akivymd.uix
from configparser import ConfigParser
from ast import literal_eval
from pathlib import Path
import sys
import os
if getattr(sys, "frozen", False):
os.environ["BillBook"] = sys._MEIPASS
else:
os.environ["BillBook"] = str(Path(__file__).parent)
app = MDApp.get_running_app
BILLBOOK_DIR = os.environ["BillBook"]
PRODUCTS_DIR = os.path.join(BILLBOOK_DIR,'assets','images','products')
AVATAR_DIR = os.path.join(BILLBOOK_DIR,'assets','images','avatars')
ICON_DIR = os.path.join(BILLBOOK_DIR,'assets','images','icons')
SHADERS_DIR = os.path.join(BILLBOOK_DIR,'assets','shaders')
BG_DIR = os.path.join(BILLBOOK_DIR,'assets','images','bg')
config = ConfigParser()
config.read(f"{BILLBOOK_DIR}/main.cfg")
# CONSTANTS
FIRST = config.getboolean("APP", "first_time")
AREAS = literal_eval(config.get("STORAGE", "areas"))
PRODUCTS = literal_eval(config.get("STORAGE", "products"))
# ToolBar
class Toolbar(ThemableBehavior, FakeRectangularElevationBehavior,
MDFloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.md_bg_color = (0, 0, 0, 0)
def set_search_mode(self, mode=False):
if mode:
app().kv.ids.screens.transition = SingleSlideTransition()
app().kv.ids.screens.transition.Direction = 'up'
app().kv.ids.screens.current = 'search_result_screen'
size = 0
self.ids.search_bar.opacity = 1
self.ids.search_bar.size_hint_y = 0.6
self.ids.search_bar.disabled = False
self.ids.search_bar.focus = True
self.ids.mag.disabled = True
self.ids.app_icon.disabled = True
Animation(size=(size, size), opacity=0,
d=0.1).start(self.ids.mag)
Animation(size=(size, size), opacity=0,
d=0.1).start(self.ids.title)
Animation(size=(size, size), opacity=0,
d=0.1).start(self.ids.app_icon)
Animation(opacity=1,
d=0.1).start(self.ids.search_bar)
else:
app().kv.ids.screens.transition = SingleSlideTransition()
app().kv.ids.screens.transition.Direction = 'down'
app().kv.ids.screens.current = 'product_carousel_screen'
size = dp(42)
self.ids.search_bar.opacity = 0
self.ids.search_bar.size_hint_y = 0.1
self.ids.search_bar.disabled = True
self.ids.search_bar.focus = False
self.ids.mag.opacity = 1
self.ids.app_icon.disabled = False
self.ids.mag.disabled = False
Animation(size=(size, size), opacity=1,
d=0.1).start(self.ids.mag)
Animation(size=(size, size), opacity=1,
d=0.1).start(self.ids.title)
Animation(size=(size, size), opacity=1,
d=0.1).start(self.ids.app_icon)
Animation(opacity=0,
d=0.1).start(self.ids.search_bar)
def set_search_list(self, text="", search=False):
text = text.lower()
def add_search_item(name_icon,callback):
app().kv.ids.search_results_container.ids.rv.data.append(
{
"viewclass": "OneLineAvatarSearchListItem",
"source": f"{AVATAR_DIR}/{callback[0].upper()}.png",
"text": name_icon,
"on_release":lambda:print(callback)
}
)
app().kv.ids.search_results_container.ids.rv.data = []
for name_icon in md_icons.keys():
if search:
if text in name_icon:
callback_name = name_icon
before_text = before(name_icon,text)
after_text = after(name_icon, text)
name_icon = before_text + f'[b]{text}[/b]' + after_text
add_search_item(name_icon,callback_name)
else:
add_search_item(name_icon)
# AppBar
class Appbar(ThemableBehavior, FakeRectangularElevationBehavior,
MDFloatLayout):
left_icon = StringProperty()
left_text = StringProperty()
right_icon = StringProperty()
right_text = StringProperty()
right_callback = ObjectProperty(lambda x: None)
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.md_bg_color = (1, 1, 1, 1)
def on_right_callback(self, instance, value):
self.icon_right_widget.bind(on_release=value)
# Iamge Icon Button
class CustomImageIconButton(CircularRippleBehavior, ButtonBehavior,
MDLabel):
user_font_size = NumericProperty()
icon = StringProperty('android')
source = StringProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
# Icon Flat Button
class CustomIconFlatButton(BaseFlatIconButton):
user_font_size = NumericProperty()
icon = StringProperty()
class OneLineAvatarSearchListItem(OneLineAvatarListItem):
source = StringProperty()
class CustomOneLineAvatarListItem(OneLineAvatarListItem, TouchBehavior):
def on_long_touch(self, *args):
print("<on_long_touch> event")
# Carousel Items
class CustomCarouselItem(MDFloatLayout):
source = StringProperty()
product = StringProperty()
product_pic_dict = product_pic_dict(PRODUCTS_DIR)
def product_carousel(self):
for product_image_name in os.listdir(PRODUCTS_DIR):
path_to_product_image = os.path.join(PRODUCTS_DIR,
product_image_name)
app().kv.ids.custom_carousel.ids.car.add_widget(CustomCarouselItem(source=path_to_product_image,
product=self.product_pic_dict[product_image_name]))
app().kv.ids.custom_carousel.ids.car.children[0].children[0].ids.lab.bind(pos=app().update)
# Carousel
class CustomCarousel(MDFloatLayout, ButtonBehavior,
RectangularRippleBehavior, Image):
def product_carousel_autoplay(self):
self.product_carousel_event = \
Clock.schedule_interval(self.product_carousel_callback, 5)
# Carousel AutoPlay
def product_carousel_callback(self, dt):
app().kv.ids.custom_carousel.ids.car.load_next()
# Chart
class CustomChart(MDFloatLayout, ButtonBehavior,
RectangularRippleBehavior, Image):
items = [{'Credit': 50, 'Due': 50}]
custom_colors = [{'Credit': '#2ebd6f', 'Due': '#ce4f47'}]
def chart(self):
app().kv.ids.custom_chart.ids.pie.bind(size=app().update)
class set_list:
def set_area_list(self):
for area in sorted(AREAS):
source = os.path.join(AVATAR_DIR,f'{area[0]}.png')
avatar = ImageLeftWidget(source=source)
area_container_item = CustomOneLineAvatarListItem(text=f"{area}",on_release=lambda area:print(dir(area)))
area_container_item.add_widget(avatar)
app().kv.ids.area_container.add_widget(area_container_item)
def set_shop_list(self):
for shop in SHOPS:
source = os.path.join(AVATAR_DIR,f'{shop[0]}.png')
avatar = ImageLeftWidget(source=source)
shop_container_item = CustomOneLineAvatarListItem(text=f"{shop}",on_release=lambda area:print(shop.text))
shop_container_item.add_widget(avatar)
app().kv.ids.shop_container.add_widget(shop_container_item)
app().kv.ids.shop_container.ids.rv.data.append(
{
"viewclass": "OneLineAvatarSearchListItem",
"source": f"{AVATAR_DIR}/T.png",
"text": name_icon,
"on_release":lambda:print(callback)
}
)
class CustomAddButton(MDBoxLayout, ButtonBehavior, RectangularRippleBehavior, Image):
""" Custom Add Button """
# Transitions
class PageCurlTransition(ShaderTransition):
Direction = OptionProperty("bt", options=["bt", "tb"])
page_curl = open(f'{SHADERS_DIR}/page_curl.frag','r').read()
fs = StringProperty(page_curl)
clearcolor = ColorProperty([0, 0, 0, 0])
def add_screen(self, screen):
super().add_screen(screen)
self.render_ctx["resolution"] = list(map(float, screen.size))
aspect_ratio = screen.size[0]/screen.size[1]
self.render_ctx["aspect"] = 1.0 * (aspect_ratio > 1.0) + 2.0 * (1.0 >= aspect_ratio)
self.render_ctx["direction"] = 1.0 if self.Direction == "bt" else 2.0
class SingleSlideTransition(ShaderTransition):
Direction = OptionProperty("up", options=["up", "down"])
slide_over = open(f'{SHADERS_DIR}/slide_over.frag','r').read()
fs = StringProperty(slide_over)
clearcolor = ColorProperty([0, 0, 0, 0])
def add_screen(self, screen):
super().add_screen(screen)
self.render_ctx["resolution"] = list(map(float, screen.size))
self.render_ctx["direction"] = 1.0 if self.Direction == "down" else 2.0
# App
class BillBookApp(MDApp):
chart_size = NumericProperty()
chart_pos = NumericProperty()
pad = NumericProperty()
Carousel = ObjectProperty()
AutoPlay = ObjectProperty()
Chart = ObjectProperty()
Search = ObjectProperty()
Area = ObjectProperty()
def configure(self):
if FIRST:
for area in AREAS:
create_avatar(avatar_name=area,AVATAR_DIR=AVATAR_DIR)
config['APP']['first_time'] = "False"
with open(f"{BILLBOOK_DIR}/main.cfg",'w') as config_file:
config.write(config_file)
def update(self, *args):
self.pad = \
self.kv.ids.custom_carousel.ids.car.children[0].children[0].ids.lab.pos[0]
self.chart_size = \
self.kv.ids.custom_chart.ids.chart_box.size[1] - 4 \
* self.kv.ids.custom_chart.ids.label.font_size
def build(self):
self.kv = Builder.load_file(f'{BILLBOOK_DIR}/main.kv')
inspector.create_inspector(Window, self.kv)
return self.kv
def on_start(self):
self.configure()
self.Carousel = CustomCarouselItem()
self.Carousel.product_carousel()
self.AutoPlay = CustomCarousel()
self.AutoPlay.product_carousel_autoplay()
self.Chart = CustomChart()
self.Chart.chart()
self.Search = Toolbar()
self.Search.set_search_list(text="",search=True)
self.set = set_list()
self.set.set_area_list()
def on_pause(self):
return True
def on_resume(self):
return True
def on_stop(self):
return True
def switch_screen(self,screen,transition):
self.kv.ids.screens.transition = PageCurlTransition(duration=1, Direction=transition)
self.kv.ids.screens.current = screen
if __name__ == '__main__':
BillBookApp().run()