main.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. '''
  2. Goblinoid: Experience all of MediaGoblin on an Android Device
  3. Copyright (C) 2015 Dylan Jeffers
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. '''
  15. from __future__ import print_function
  16. from os.path import join
  17. from kivy.app import App
  18. from kivy.core.window import Window
  19. from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
  20. from kivy.utils import platform
  21. from pypump.exception import PyPumpException
  22. from goblinoid.services.login_service import user_logged_on, load_user
  23. from goblinoid.services.pump_service import PumpService
  24. from goblinoid.root.goblinscreenmanager import GoblinScreenManager
  25. __version__ = '0.1.1'
  26. class MediaGoblinApp(App):
  27. def build(self, **kwargs):
  28. self.pump_service = PumpService()
  29. self.transition = SlideTransition(duration=.35)
  30. self.sm = GoblinScreenManager(transition=self.transition)
  31. self.user_dir = join(self.user_data_dir, 'MediaGoblin')
  32. # NOTE: this is a horrible hotfix to avoid the common
  33. # PyPumpException. This is next on the priority list to fix
  34. if user_logged_on(self.user_dir):
  35. try:
  36. pump = load_user(self.user_dir)
  37. self.pump_service.pump = pump
  38. self.open_view('action', False)
  39. except PyPumpException, e:
  40. print('Error: {0}, trying once more...'.format(e))
  41. # Note: eventually add a toast to alert user of issue
  42. try:
  43. pump = load_user(self.user_dir)
  44. self.pump_service.pump = pump
  45. self.open_view('action', False)
  46. except PyPumpException, e:
  47. print('Error: {0}, transitioning to login'.format(e))
  48. self.open_view('login', True)
  49. else:
  50. self.open_view('login', False)
  51. self.bind(on_start=self.post_build_init)
  52. return self.sm
  53. def post_build_init(self, ev):
  54. if platform == 'android':
  55. Window.softinput_mode = 'below_target'
  56. win = self._app_window
  57. win.bind(on_keyboard=self._key_handler)
  58. def _key_handler(self, *args):
  59. key = args[1]
  60. if key in (1000, 27):
  61. self.sm.current_screen.dispatch("on_back_pressed")
  62. return True
  63. def update_pump_service(self, pump):
  64. self.pump_service.update_pump(pump)
  65. def open_view(self, name, previous):
  66. print('got to open_view: {0}, {1}'.format(name, previous))
  67. if self.sm.has_screen(name):
  68. self.sm.remove_widget(self.sm.get_screen(name))
  69. # grabs screen from dictionary of screens
  70. screen = self.pump_service.screen_dict[name](name=name)
  71. self.sm.add_widget(screen)
  72. if previous:
  73. self.transition.direction = 'right'
  74. else:
  75. self.transition.direction = 'left'
  76. self.sm.current = screen.name
  77. def get_pump_service(self):
  78. print('printing the pump model: {0}'.format(self.pump_service))
  79. return self.pump_service
  80. # handlers for when user exits from app
  81. def on_pause(self):
  82. return True
  83. def on_resume(self):
  84. pass
  85. if __name__ == '__main__':
  86. MediaGoblinApp().run()