Module: RMXViewControllerPresentation::FactoryMethods

Included in:
RMXViewControllerPresentation
Defined in:
lib/motion/RMXViewControllerPresentation.rb

Instance Method Summary collapse

Instance Method Details

#dismiss(opts) ⇒ Object

remove the controller from the display heirarchy, taking into account how it is currently presented. avoid nesting animations and corrupting the UI by using whenOrIfViewState and executing on the main thread async to ensure it is not yanked out of the UI during existing animations



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
# File 'lib/motion/RMXViewControllerPresentation.rb', line 25

def dismiss(opts)
  unless [ :view_controller, :animated, :completion ].all? { |x| opts.key?(x) }
    raise "Missing RMXViewControllerPresentation.dismiss opts: #{opts.inspect}"
  end
  animated = opts[:animated]
  block = opts[:completion]
  view_controller = opts[:view_controller]
  navigationController = view_controller.navigationController


  if view_controller.presentingViewController
    Dispatch::Queue.main.async do
      view_controller.dismissViewControllerAnimated(animated, completion:block)
    end
  elsif navigationController
    if index = navigationController.viewControllers.index(view_controller)
      before_index = index - 1
      before_index = 0 if index < 0
      pop_to_controller = navigationController.viewControllers[before_index]
      if pop_to_controller && pop_to_controller != navigationController.viewControllers.last
        # p "pop_to_controller", pop_to_controller
        # p "navigationController.popToViewController(pop_to_controller, animated:animated)"
        Dispatch::Queue.main.async do
          RMX.new(navigationController).once(:done_animating, &block) if block
          navigationController.popToViewController(pop_to_controller, animated:animated)
        end
      end
    end
  end
end

#present(opts) ⇒ Object

presentViewController should always be called on the next runloop to avoid quirks. this just wraps that behavior



12
13
14
15
16
17
18
19
# File 'lib/motion/RMXViewControllerPresentation.rb', line 12

def present(opts)
  unless [ :origin, :view_controller, :animated, :completion ].all? { |x| opts.key?(x) }
    raise "Missing RMXViewControllerPresentation.present opts: #{opts.inspect}"
  end
  Dispatch::Queue.main.async do
    opts[:origin].presentViewController(opts[:view_controller], animated:opts[:animated], completion:opts[:completion])
  end
end