Skip to content

Commit

Permalink
Improved UI Screen Switching and MyApp change_screen, soft_restart added
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedmujtabaraza committed Mar 29, 2022
1 parent 26a0935 commit e719b27
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 40 deletions.
Binary file modified src/__pycache__/app.cpython-38.pyc
Binary file not shown.
17 changes: 9 additions & 8 deletions src/app.kv
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
size_hint: None, None
size: dp(48), dp(48)

# ScreenManager:
# MenuScreen:
# name: "menu_screen"
# id: menu_screen
# MeaningsScreen:
# name: "meanings_screen"
# id: meanings_screen
ScreenManager:
id: screen_manager
MenuScreen:
id: menu_screen
MeaningsScreen:
id: meanings_screen

# MDLabel:
# id: url_label
Expand All @@ -21,6 +20,7 @@
# pos_hint: {'center_x': 0.5, 'center_y': 0.92}

<MenuScreen>:
name: "menu_screen"
MDBoxLayout:
orientation: "vertical"
size: root.width, root.height
Expand Down Expand Up @@ -86,6 +86,7 @@


<MeaningsScreen>:
name: "meanings_screen"
MDBoxLayout:
spacing: "10dp"
pos_hint: {"top": 1}
Expand All @@ -96,7 +97,7 @@
MDToolbar:
title: 'Vocab to Anki'
anchor_title: 'center'
left_action_items: [["arrow-left", lambda x: app.get_running_app().restart()]]
left_action_items: [["arrow-left", lambda x: app.soft_restart()]]
elevation: 10
ScrollView:

Expand Down
108 changes: 76 additions & 32 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from kivymd.uix.list import OneLineListItem, TwoLineListItem
from kivymd.uix.dialog import MDDialog
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDFlatButton, MDRectangleFlatButton, MDIconButton, MDFloatingActionButton
from kivymd.uix.button import MDFlatButton, MDRaisedButton, MDRectangleFlatButton, MDIconButton, MDFloatingActionButton
from kivymd.uix.menu import MDDropdownMenu
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
Expand Down Expand Up @@ -87,8 +87,6 @@ def run_spider(spider, *args):


Window.size = (500, 400)
Builder.load_file("src/app.kv")
sm = ScreenManager()


class MeaningsPanelContent(MDBoxLayout):
Expand Down Expand Up @@ -169,6 +167,17 @@ def __init__(self, **kwargs):
# self.submit.bind(on_press=self.press)
# self.add_widget(self.submit)

# def change_screen(self):
# if self.manager.current == "meanings_screen":
# self.manager.transition.direction = 'right'
# self.manager.transition.duration = 0.5
# self.manager.current = "menu_screen"
# else:
# # sm.current = 'meanings_screen'
# self.manager.transition.direction = 'left'
# self.manager.transition.duration = 0.5
# self.manager.current = 'meanings_screen'

def open_dropdown(self, dict_dropdown=False):
if self.dropdown_menu is not None:
self.dropdown_menu.dismiss()
Expand Down Expand Up @@ -264,42 +273,42 @@ def close_dialog(self, obj):

def open_apkg(self, obj):
# todo: use kivymd android platform statement/variable
apkg_filename = 'output-' + self.timestamp + '.apkg'
print(apkg_filename)
print(platform.system())
if platform.system() == 'Darwin': # macOS
subprocess.call(('open', apkg_filename))
elif platform.system() == 'Windows': # Windows
os.startfile(apkg_filename)
else: # linux variants
subprocess.call(('xdg-open', apkg_filename))
print("Okay.")
# apkg_filename = 'output' + '.apkg'
# # print(apkg_filename)
# if platform.system() == 'Darwin': # macOS
# subprocess.call(('open', apkg_filename))
# elif platform.system() == 'Windows': # Windows
# os.startfile(apkg_filename)
# else: # linux variants
# subprocess.call(('xdg-open', apkg_filename))

def dialog_popup(self, title, text, close=False, open_=False):
if not self.dialog:
pass
else:
self.dialog.dismiss()
open_button = MDFlatButton(text="Open", on_release=self.open_apkg)
close_button = MDFlatButton(text="Close", on_release=self.close_dialog)
open_button = MDRaisedButton(text="OPEN", on_release=self.open_apkg)
close_button = MDFlatButton(text="CLOSE", on_release=self.close_dialog)
if close:
self.dialog = MDDialog(
title=title,
text=text,
size_hint=(0.7, 1),
# size_hint=(0.7, 1),
buttons=[close_button]
)
elif open_:
self.dialog = MDDialog(
title=title,
text=text,
size_hint=(0.7, 1),
# size_hint=(0.7, 1),
buttons=[close_button, open_button]
)
else:
self.dialog = MDDialog(
title=title,
text=text,
size_hint=(0.7, 1)
# size_hint=(0.7, 1)
)
self.dialog.open()

Expand All @@ -309,23 +318,33 @@ def checkbox_click(self, instance, value, tld):

def confirm_generation(self, section_id, meaning):
meaning_text = meaning[1] if type(meaning) is tuple else meaning
confirm_button = MDFlatButton(
text="Confirm", on_release=lambda x, y=section_id, z=meaning: self.generate_flashcard(x, y, z)
confirm_button = MDRaisedButton(
text="CONFIRM", on_release=lambda x, y=section_id, z=meaning: self.generate_flashcard(x, y, z)
)
close_button = MDFlatButton(text="Close", on_release=self.close_dialog)
close_button = MDFlatButton(text="CLOSE", on_release=self.close_dialog)
if self.dialog:
self.dialog.dismiss()
self.dialog = MDDialog(
title="Confirm generation",
text=f"Do you want to generate Anki flashcard for \"{meaning_text}\"?",
size_hint=(0.7, 1),
# size_hint=(0.7, 1),
buttons=[close_button, confirm_button]
)
self.dialog.open()

def generate_flashcard(self, btn, section_id, meaning):
print(section_id, meaning)
if type(meaning) is tuple:
pass
else:
pass
MDApp.get_running_app().soft_restart()
# run_spider(CambridgeSpider, gcurl, self.tld, self.timestamp)
print(section_id, meaning) # {'cid': 'cald4-1-1', 'word': 'run', 'gw': '(GO QUICKLY)', 'pos': 'verb'}
self.dialog_popup(
"Open Anki Package?",
f"Successfully generated flashcard. Do you want to open it in Anki?",
open_=True
)

def show_data(self):
# word_url = self.word_url.text
Expand Down Expand Up @@ -406,16 +425,10 @@ def show_data(self):
)
)
)
sm.switch_to(meanings_screen)
MDApp.get_running_app().change_screen()
else:
self.toast("Invalid URL. Please try again")
return False
# self.dialog_popup(
# "Open Anki Package?",
# f"Successfully generated flashcard. Do you want to open it in Anki?",
# open_=True
# )
# p1 = Process(reactor.run) # todo: what to do?????
# self.add_widget(MDLabel(
# text=f"Successfully generated flashcard for {word_url}..",
# halign='center', theme_text_color='Hint', font_style='OVERLINE'
Expand All @@ -432,6 +445,17 @@ def __init__(self, **kwargs):
super(MeaningsScreen, self).__init__(**kwargs)
# self.on_start()

# def change_screen(self):
# if self.manager.current == "menu_screen":
# self.manager.transition.direction = 'left'
# self.manager.transition.duration = 0.5 # 0.5 second
# self.manager.current = "meanings_screen"
# else:
# # sm.current = 'menu_screen'
# self.manager.transition.direction = 'right'
# self.manager.transition.duration = 0.5 # 0.5 second
# self.manager.current = 'menu_screen'

def on_start(self):
# for i in range(20):
# self.ids.meanings_container.add_widget(
Expand All @@ -454,13 +478,15 @@ def on_start(self):

class MyApp(MDApp):
def build(self):
# sm.add_widget(MenuScreen(name='menu_screen'))
# sm.add_widget(MeaningsScreen(name='meanings_screen'))
self.title = 'Vocab to Anki'
sm.add_widget(MenuScreen(name='menu_screen'))
sm.add_widget(MeaningsScreen(name='meanings_screen'))
self.theme_cls.primary_palette = "Blue"
self.theme_cls.theme_style = "Dark"
self.theme_cls.material_style = "M3"
return sm
screen = Builder.load_file("src/app.kv")
return screen
# return sm
# return MyLayout()

def restart(self):
Expand All @@ -470,6 +496,24 @@ def restart(self):
CONTAINER = {'url': '', 'dictionary': [], 'meanings': []}
return MyApp().run()

def change_screen(self):
if self.root.current == "menu_screen":
self.root.transition.direction = 'left'
self.root.transition.duration = 0.5 # 0.5 second
self.root.current = "meanings_screen"
else:
# sm.current = 'menu_screen'
self.root.transition.direction = 'right'
self.root.transition.duration = 0.5 # 0.5 second
self.root.current = 'menu_screen'

def soft_restart(self):
global CONTAINER
CONTAINER = {'url': '', 'dictionary': [], 'meanings': []}
self.root.transition.direction = 'right'
self.root.transition.duration = 0.5 # 0.5 second
self.root.current = 'menu_screen'


# Run the App
if __name__ == "__main__":
Expand Down

0 comments on commit e719b27

Please sign in to comment.