App Life Circle

App Life Circle

controller/

  • Application.lua

    app is a bookX which is a viewInstance of Application, and it is display.newGroup too

    function M.new(Props)
      local app = display.newGroup()
      app.props = Props
  • AllicationContext.lua

  • ApplicationMediator.lua

  • ApplicationUI.lua

  • index.lua

    function lib.bootstrap(Props)
        local app = App.new{
            appName     = Props.name,
            editing     = Props.editing,
            systemDir   = system.ResourceDirectory,
            imgDir      = "App/"..Props.name.."/assets/images/",
            spriteDir   = "App/"..Props.name.."/assets/sprites/",
            thumbDir    = "App/"..Props.name.."/assets/thumbnails/",
            audioDir    = "App/"..Props.name.."/assets/audios/",
            videoDir    = "App/"..Props.name.."/assets/videos/",
            particleDir = "App/"..Props.name.."/assets/particles/",
            trans       = {},
            gt          = {},
            timerStash  = {},
            allAudios   = {kAutoPlay = 5},
            kBidi       = false,
            goPage      = Props.goPage, -- sceneIndex,
            scenes       = require("App."..Props.name..".index"),
            kAutoPlay   = 0,
            lang        = "en",
            position    = Props.position,
            --stage       = display.getCurrentStage(),
            randomAction = {},
            randomAnim   = {},
            DocumentsDir = system.DocumentsDirectory,
            common       = Props.common
        }
        App.gtween      = require("extlib.gtween")
        App.btween      = require("extlib.btween")
        App.Gesture     = require("extlib.dmc_gesture")
        App.MultiTouch  = require("extlib.dmc_multitouch")
        App.syncSound   = require("extlib.syncSound")
        App.currentName = Props.name
        app:init()
        common = Props.common or common
    --
    end
  • mediator.lua

  • scene.lua

  • sceneHandler.lua

Boostrap

  1. Initiating an app

    graph RL
    
    index:bootstrap --> Application.new
    
    Application.new --> app.init
    Application.new--> Context.new
    
    Context.new --> context:init
    
    context:init -- book/index.lua scens  --> controller.mediator:new
    
    app.init -. dispatch .-> robotlegs(context:onRobotlegsViewCreated)
    
    Context.new --o robotlegs
    • new medaitros from scenes in book/index.lua

    • when mediators are regsitered, let’s show one of the view

    function context:init(scens, props)
       for i=1, #scenes do
         local scene = require(appDir.."components."..scenes[i]..".index")
         scene:setProps(props)
         scene.app = app
         ---
         local model = scene.model
         model.pageNum = i
         --
         local mediatorName = appDir.."mediators."..model.page.."Mediator"
         package.loaded[mediatorName] = require( 'controller.mediator' ).new(appDir, scenes[i])
         self:mapMediator(appDir.."components."..model.page..".index", mediatorName)
         --
         local commands = scene:getCommands()
         for k, eventName in pairs(commands) do
           self:mapCommand(model.page.."."..eventName, appDir.."commands."..model.page.."."..eventName)
         end
         --
         self.Router["components."..scenes[i]..".index"] = scene
       end
    end
    graph LR
    
    app.init -- dispatch onRobotlegsViewCreated -->context
    
    context --> createMediator
    createMediator-->ApplicationMedaitor:onRegister
  2. onRegister calls showView which is a wrapper for composer.gotoScene

    graph LR
    
    ApplicationMedaitor:onRegister --> viewInstance:showView
  3. A scene is App/bookX/pageX/index.lua loaded by showView, each scene has been loaded by require in context:init() to self.Router

    graph LR
    
    App/book/pageX/index.lua --o scene.new

changeThisMug

Runtime:addEventListener("changeThisMug", function(event)
  local app = App.get()
  print (event.appName, event.goPage, app.props.appName, app.currentViewName)
  if event.appName == app.props.appName and event.goPage == app.props.goPage then
    print("not changeThisMug")
  else
    composer.gotoScene("components.bookstore.view.page_cutscene")
    composer.removeHidden(false)
    resetPacakges()
    lib.bootstrap({name=event.appName, goPage=event.goPage, editing = event.editing, position = {x=0, y=0}, common=common}) -- scenes.index
  end
end)

controller.scene controller.sceneCollection

scene.lua

local UI = require("controller.ApplicationUI")

M.new = function(sceneName, model)
 local parent = sceneName:match("(.-)[^%.]+$")
 local scene = composer.newScene(sceneName)
 scene.UI = UI.create(scene, model)
 scene.model = model

 function scene:create(event)
   self.view:insert(self.UI.sceneGroup)
   self.UI:create(event.params)
 end

return scene
end

so let’s return a scene which loads multiple UI()

sceneCollection.lua

function scene:init()
   self.col_num = 4
   self.row_num = nil
   self.width = 480 / self.col_num
   self.height = 320/self.col_num
   self.x = self.width/2
   self.y = self.height/2
end

function scene:create(event)
  local count = 0
  local row_max = math.ceil(#app.context.Router / self.col_num)
  for i=1, row_max do
    for k=1, self.col_num do
      count = count +1
      if count < #app.context.Router then
        local group = app.context.Router[count].sceneGroup
        group.x = self.x + (k-1)*self.width
        group.y = self.y + (i-1)*self.height
      end
    end
  end
end