Example #1
0
class NYMainMenu(HistorySubModelPresenter, GiftEventHubWatcher):
    _GIFT_EVENT_ID = GiftEventID.NY_HOLIDAYS
    __celebrityController = dependency.descriptor(ICelebritySceneController)
    __settingsCore = dependency.descriptor(ISettingsCore)
    __topMenuLogger = NyWidgetTopMenuLogger()
    __flowLogger = NyTopMenuFlowLogger()

    def __init__(self, viewModel, parentView, *args, **kwargs):
        super(NYMainMenu, self).__init__(viewModel, parentView, *args,
                                         **kwargs)
        self.__tabsController = NewYearMainTabsController()
        self.__soundsManager = None
        self.__currentView = None
        return

    @property
    def viewModel(self):
        return self.getViewModel()

    def createToolTipContent(self, event, contentID):
        tooltips = R.views.lobby.new_year.tooltips
        if event.contentID == tooltips.NyMenuGiftTooltip():
            return NyMenuGiftTooltip()
        if contentID == tooltips.NyMenuShardsTooltip():
            return NyMenuShardsTooltip()
        return NyMenuCollectionsTooltip(
        ) if contentID == tooltips.NyMenuCollectionsTooltip() else super(
            NYMainMenu, self).createToolTipContent(event, contentID)

    def initialize(self, *args, **kwargs):
        super(NYMainMenu, self).initialize(*args, **kwargs)
        self.catchGiftEventHub()
        soundConfig = {
            NewYearSoundConfigKeys.ENTRANCE_EVENT:
            self.__getEntranceSoundEvent,
            NewYearSoundConfigKeys.EXIT_EVENT: self.__getExitSoundEvent,
            NewYearSoundConfigKeys.STATE_VALUE: self.__getSoundStateValue
        }
        self.__soundsManager = NewYearSoundsManager(soundConfig)
        self.viewModel.setIsExtendedAnim(not isMemoryRiskySystem())

    def finalize(self):
        self.__soundsManager.onExitView()
        self.__soundsManager.clear()
        self.__soundsManager = None
        self.__currentView = None
        self.releaseGiftEventHub()
        super(NYMainMenu, self).finalize()
        return

    def _getEvents(self):
        return ((self.viewModel.onInfoBtnClick, self.__onInfoBtnClick),
                (self.viewModel.onSwitchContent, self.__onMenuItemSelected),
                (self._nyController.onDataUpdated, self.__onDataUpdated),
                (self.__celebrityController.onQuestsUpdated,
                 self.__onDataUpdated), (AccountSettings.onSettingsChanging,
                                         self.__onAccountDataUpdated),
                (self.__settingsCore.onSettingsChanged,
                 self.__onSettingsChanged))

    def _getListeners(self):
        return ((events.NewYearEvent.ON_SWITCH_VIEW, self.__onSwitchViewEvent,
                 EVENT_BUS_SCOPE.LOBBY), )

    def _getCallbacks(self):
        return (('tokens', self.__onTokensChanged), )

    def _onGiftHubUpdate(self, reason, _=None):
        if reason == HubUpdateReason.SETTINGS:
            self.__recreateMenu()
        elif reason == HubUpdateReason.STAMPER_UPDATE:
            self.__updateMenu()

    def __onInfoBtnClick(self):
        self.__flowLogger.logInfoClick(
            source=self.__currentView,
            albumTab=self._tabCache.getCurrentYear(),
            rewardTab=self._tabCache.getRewardsTab(),
            selectedObject=NewYearNavigation.getCurrentObject())
        NewYearNavigation.showInfoView(showDefaultTabForced=True)

    def __onMenuItemSelected(self, args):
        menuName = args['view']
        viewAlias = _NAVIGATION_ALIAS_VIEWS[menuName]
        self._navigationHistory.clear()
        self.__topMenuLogger.logClick(menuName)
        self.__flowLogger.logTabSelect(
            source=self.__currentView,
            view=menuName,
            albumTab=self._tabCache.getCurrentYear(),
            rewardTab=self._tabCache.getRewardsTab(),
            selectedObject=NewYearNavigation.getCurrentObject(),
            previousObject=NewYearNavigation.getPreviousObject())
        with self.viewModel.transaction() as tx:
            tx.setStartIndexMenu(self.__tabsController.tabOrderKey(menuName))
        self.__soundsManager.playEvent(NewYearSoundEvents.TAB_BAR_CLICK)
        self._goToByViewAlias(viewAlias, saveHistory=False)

    def __onSwitchViewEvent(self, event):
        self.__onSwitchView(event.ctx)

    def __onSwitchView(self, ctx):
        menuName = ctx.menuName
        if menuName != self.__currentView:
            self.__soundsManager.onExitView()
            self.__currentView = menuName
            self.__soundsManager.onEnterView()
        if menuName != NyWidgetTopMenu.INFO and self.__tabsController.getCurrentTabName(
        ) != menuName:
            self.__tabsController.selectTab(menuName)
        self.__updateMenu()

    def __onDataUpdated(self, *_):
        self.__updateMenu()

    def __onAccountDataUpdated(self, key, _):
        if key in (NY_CELEBRITY_QUESTS_VISITED_MASK,
                   NY_OLD_COLLECTIONS_BY_YEAR_VISITED,
                   NY_OLD_REWARDS_BY_YEAR_VISITED):
            self.__updateMenu()

    def __onSettingsChanged(self, diff):
        if NewYearStorageKeys.CELEBRITY_CHALLENGE_VISITED in diff:
            self.__updateMenu()

    def __onTokensChanged(self, tokens):
        if any((token.startswith(CelebrityQuestTokenParts.PREFIX)
                for token in tokens)):
            self.__updateMenu()

    def __recreateMenu(self):
        with self.viewModel.transaction() as model:
            tabIdx = self.__getTabIdx()
            model.setStartIndexMenu(tabIdx)
            self.__tabsController.setSelectedTabIdx(tabIdx)
            self.__tabsController.createTabModels(model.getItemsMenu())

    def __updateMenu(self):
        with self.viewModel.transaction() as model:
            self.__tabsController.updateTabModels(model.getItemsMenu())
            model.setStartIndexMenu(self.__getTabIdx())

    def __getEntranceSoundEvent(self):
        return _SOUNDS_MAP.get(NewYearSoundConfigKeys.ENTRANCE_EVENT,
                               {}).get(self.__currentView)

    def __getExitSoundEvent(self):
        return _SOUNDS_MAP.get(NewYearSoundConfigKeys.EXIT_EVENT,
                               {}).get(self.__currentView)

    def __getSoundStateValue(self):
        return _SOUNDS_MAP.get(NewYearSoundConfigKeys.STATE_VALUE,
                               {}).get(self.__currentView)

    def __getTabIdx(self):
        currentView = self.__currentView
        if currentView == NyWidgetTopMenu.INFO:
            return -1
        return 0 if currentView not in self.__tabsController.getEnabledTabsArray(
        ) else self.__tabsController.tabOrderKey(currentView)
Example #2
0
class NYSidebar(HistorySubModelPresenter):
    __slots__ = ('__tabsController', '__controllers', '__currentTab', '__currentViewName', '__soundsManager')
    __celebrityController = dependency.descriptor(ICelebritySceneController)
    __newYearController = dependency.descriptor(INewYearController)
    __settingsCore = dependency.descriptor(ISettingsCore)
    __flowUILogger = NySideBarFlowLogger()

    def __init__(self, viewModel, parentView, *args, **kwargs):
        super(NYSidebar, self).__init__(viewModel, parentView)
        self.__tabsController = None
        self.__controllers = {}
        self.__currentTab = None
        self.__currentViewName = None
        self.__soundsManager = None
        return

    @property
    def viewModel(self):
        return self.getViewModel()

    def initialize(self, ctx=None, *args, **kwargs):
        super(NYSidebar, self).initialize(*args, **kwargs)
        self.viewModel.onSideBarBtnClick += self.__onSideBarBtnClick
        self._nyController.onDataUpdated += self.__onDataUpdated
        self.__celebrityController.onQuestsUpdated += self.__onQuestUpdated
        AccountSettings.onSettingsChanging += self.__onAccountDataUpdated
        self.__settingsCore.onSettingsChanged += self.__onSettingsChanged
        soundConfig = {NewYearSoundConfigKeys.ENTRANCE_EVENT: self.__getEntranceSoundEvent,
         NewYearSoundConfigKeys.EXIT_EVENT: self.__getExitSoundEvent,
         NewYearSoundConfigKeys.STATE_VALUE: self.__getSoundStateValue}
        self.__soundsManager = NewYearSoundsManager(soundConfig)
        self.__controllers = {NyWidgetTopMenu.GLADE: GladeTabsController(),
         NyWidgetTopMenu.COLLECTIONS: AlbumsTabsController(),
         NyWidgetTopMenu.REWARDS: RewardsTabsController()}
        g_eventBus.addListener(events.NewYearEvent.ON_SWITCH_VIEW, self.__onSwitchViewEvent, scope=EVENT_BUS_SCOPE.LOBBY)
        g_eventBus.addListener(events.NewYearEvent.SELECT_SIDEBAR_TAB_OUTSIDE, self.__onSelectTabOutside, scope=EVENT_BUS_SCOPE.LOBBY)
        if ctx is not None:
            self.__onSwitchView(ctx)
        return

    def finalize(self):
        self._nyController.onDataUpdated -= self.__onDataUpdated
        self.__celebrityController.onQuestsUpdated -= self.__onQuestUpdated
        self.viewModel.onSideBarBtnClick -= self.__onSideBarBtnClick
        AccountSettings.onSettingsChanging -= self.__onAccountDataUpdated
        self.__settingsCore.onSettingsChanged -= self.__onSettingsChanged
        g_eventBus.removeListener(events.NewYearEvent.ON_SWITCH_VIEW, self.__onSwitchViewEvent, scope=EVENT_BUS_SCOPE.LOBBY)
        g_eventBus.removeListener(events.NewYearEvent.SELECT_SIDEBAR_TAB_OUTSIDE, self.__onSelectTabOutside, scope=EVENT_BUS_SCOPE.LOBBY)
        self.__tabsController = None
        self.__controllers.clear()
        self.__soundsManager.onExitView()
        self.__soundsManager.clear()
        self.__soundsManager = None
        self.__currentViewName = None
        super(NYSidebar, self).finalize()
        return

    def __onSwitchViewEvent(self, event):
        ctx = event.ctx
        self.__onSwitchView(ctx)

    def __onSwitchView(self, ctx):
        menuName = ctx.menuName
        selectedTab = None
        self.viewModel.setViewName(menuName)
        self.__tabsController = self.__controllers.get(menuName)
        if self.__tabsController is not None:
            self.__updateTabs(ctx.tabName, menuName)
            selectedTab = self.__tabsController.getSelectedName(self.viewModel.getItemsTabBar())
        else:
            self.__clearTabs()
        if self.__currentViewName != menuName or self.__currentTab != selectedTab:
            self.__soundsManager.onExitView()
            self.__currentViewName = menuName
            self.__currentTab = selectedTab
            self.__soundsManager.onEnterView()
        return

    def __onSelectTabOutside(self, event):
        menuName = event.ctx['menuName']
        tabName = event.ctx['tabName']
        if self.__currentViewName != menuName or self.__tabsController is None or self.__currentTab == tabName:
            return
        else:
            needToInvokeSound = self.__currentViewName != NyWidgetTopMenu.COLLECTIONS or self.__newYearController.isMaxAtmosphereLevel()
            if needToInvokeSound:
                self.__soundsManager.onExitView()
            self.__currentTab = tabName
            self.__tabsController.selectTab(tabName)
            self.viewModel.setStartIndex(self.__tabsController.getSelectedTabIdx())
            if needToInvokeSound:
                self.__soundsManager.onEnterView()
            return

    def __updateTabs(self, tabName, menuName):
        if tabName is not None:
            if self.__tabsController.getCurrentTabName() != tabName:
                self.__tabsController.selectTab(tabName)
        if self._tabCache.getIntroScreenState(menuName) == NewYearTabCache.OPENED_INTRO_STATE:
            self.__clearTabs()
        else:
            self.__fillTabs()
        return

    def __fillTabs(self):
        with self.viewModel.transaction() as model:
            tabsArray = model.getItemsTabBar()
            self.__tabsController.createTabModels(tabsArray)
            tabIdx = self.__tabsController.getSelectedTabIdx()
            model.setStartIndex(tabIdx)

    def __validateTabs(self):
        if self._tabCache.getIntroScreenState(self.__currentViewName) == NewYearTabCache.OPENED_INTRO_STATE:
            return
        self.__tabsController.updateTabModels(self.viewModel.getItemsTabBar())

    def __clearTabs(self):
        with self.viewModel.transaction() as model:
            tabsArray = model.getItemsTabBar()
            tabsArray.clear()
            tabsArray.invalidate()
            model.setStartIndex(0)

    def __onDataUpdated(self, keys):
        if self.__tabsController is None:
            return
        else:
            checkKeys = {SyncDataKeys.INVENTORY_TOYS,
             SyncDataKeys.SLOTS,
             SyncDataKeys.TOY_COLLECTION,
             SyncDataKeys.TOY_FRAGMENTS}
            if set(keys) & checkKeys:
                self.__validateTabs()
            return

    def __onQuestUpdated(self):
        if self.__tabsController is None:
            return
        else:
            self.__validateTabs()
            return

    def __onAccountDataUpdated(self, key, value):
        if self.__tabsController is None:
            return
        else:
            if key in (NY_OLD_COLLECTIONS_BY_YEAR_VISITED, NY_OLD_REWARDS_BY_YEAR_VISITED):
                self.__validateTabs()
            return

    def __onSettingsChanged(self, diff):
        if self.__tabsController is None:
            return
        else:
            if NewYearStorageKeys.GLADE_INTRO_VISITED in diff:
                self.__updateTabs(self.__currentTab, self.__currentViewName)
            return

    def __onSideBarBtnClick(self, args):
        tabName = args['tabName']
        self.__soundsManager.playEvent(NewYearSoundEvents.SIDE_BAR_CLICK)
        self.__flowUILogger.logTabSelect(view=self.__currentViewName, currentTab=self.__tabsController.getCurrentTabName(), targetTab=tabName)
        self.__selectTab(tabName)

    def __selectTab(self, tabName):
        needToInvokeSound = self.__currentViewName != NyWidgetTopMenu.COLLECTIONS or self.__newYearController.isMaxAtmosphereLevel()
        if needToInvokeSound:
            self.__soundsManager.onExitView()
        self.__currentTab = tabName
        self.__tabsController.selectTab(tabName)
        with self.viewModel.transaction() as model:
            model.setStartIndex(self.__tabsController.getSelectedTabIdx())
        g_eventBus.handleEvent(events.NewYearEvent(events.NewYearEvent.ON_SIDEBAR_SELECTED, ctx=NYTabCtx(tabName=tabName, menuName=self.viewModel.getViewName())), scope=EVENT_BUS_SCOPE.LOBBY)
        if needToInvokeSound:
            self.__soundsManager.onEnterView()

    def __getEntranceSoundEvent(self):
        return self.__getSoundEvent(NewYearSoundConfigKeys.ENTRANCE_EVENT)

    def __getExitSoundEvent(self):
        return self.__getSoundEvent(NewYearSoundConfigKeys.EXIT_EVENT)

    def __getSoundEvent(self, eventType):
        if self.__currentViewName == NyWidgetTopMenu.GLADE:
            return _GLADE_SOUNDS_MAP.get(eventType, {}).get(self.__currentTab)
        else:
            if self.__currentViewName == NyWidgetTopMenu.COLLECTIONS and (self.__currentTab == Collections.NewYear22 or self.__newYearController.isMaxAtmosphereLevel()):
                return _COLLECTIONS_SOUNDS_MAP.get(eventType, {}).get(self.__currentTab)
            return None

    def __getSoundStateValue(self):
        if self.__currentViewName == NyWidgetTopMenu.GLADE:
            selectedTabName = self.__tabsController.getCurrentTabName()
            return _GLADE_SOUNDS_MAP.get(NewYearSoundConfigKeys.STATE_VALUE, {}).get(selectedTabName)
        else:
            return None
Example #3
0
class SubModelPresenter(object):
    __slots__ = ('__viewModel', '__isLoaded', '__parentView',
                 '__soundsManager')

    def __init__(self, viewModel, parentView, soundConfig=None):
        super(SubModelPresenter, self).__init__()
        self.__parentView = parentView
        self.__viewModel = viewModel
        self.__isLoaded = False
        self.__soundsManager = NewYearSoundsManager(
            {} if soundConfig is None else soundConfig)
        return

    @property
    def isLoaded(self):
        return self.__isLoaded

    @property
    def parentView(self):
        return self.__parentView

    def getViewModel(self):
        return self.__viewModel

    def getParentWindow(self):
        return self.parentView.getParentWindow()

    def initialize(self, *args, **kwargs):
        self.__soundsManager.onEnterView()
        self.__subscribe()
        self.__isLoaded = True

    def finalize(self):
        self.__isLoaded = False
        self.__unsubscribe()
        self.__soundsManager.onExitView()

    def clear(self):
        self.__viewModel = None
        self.__parentView = None
        self.__soundsManager.clear()
        return

    def createToolTip(self, event):
        return None

    def createToolTipContent(self, event, ctID):
        return None

    def createPopOverContent(self, event):
        return None

    def _getCallbacks(self):
        pass

    def _getListeners(self):
        pass

    def _getEvents(self):
        pass

    def __subscribe(self):
        g_clientUpdateManager.addCallbacks(dict(self._getCallbacks()))
        for eventBusArgs in self._getListeners():
            g_eventBus.addListener(*eventBusArgs)

        for event, handler in self._getEvents():
            event += handler

    def __unsubscribe(self):
        for event, handler in reversed(self._getEvents()):
            event -= handler

        for eventBusArgs in reversed(self._getListeners()):
            g_eventBus.removeListener(*eventBusArgs[:3])

        g_clientUpdateManager.removeObjectCallbacks(self)