rubymotion/BubbleWrap

View on GitHub
motion/sms/sms.rb

Summary

Maintainability
A
0 mins
Test Coverage
module BubbleWrap
  module SMS

    module_function

    # Base method to create your in-app mail
    # ---------------------------------------
    # EX
    # BW::SMS.compose (
    # {
    #   delegate: self, # optional, will use root view controller by default
    #   to: [ "1(234)567-8910" ],
    #   message: "This is my message. It isn't very long.",
    #   animated: false
    # }) {|result, error|
    #   result.sent?      # => boolean
    #   result.canceled?  # => boolean
    #   result.failed?    # => boolean
    #   error             # => NSError
    #   }

    def compose(options = {}, &callback)
      @delegate = options[:delegate] || App.window.rootViewController
      @callback = callback
      @callback.weak! if @callback && BubbleWrap.use_weak_callbacks?

      @message_controller = create_message_controller(options)
      @message_is_animated = options[:animated] == false ? false : true
      @delegate.presentModalViewController(@message_controller, animated: @message_is_animated)
    end

    def create_message_controller(options = {})
      message_controller = MFMessageComposeViewController.alloc.init
      message_controller.messageComposeDelegate = self
      message_controller.body = options[:message]
      message_controller.recipients = Array(options[:to])
      message_controller
    end

    def can_send_sms?
      !!MFMessageComposeViewController.canSendText
    end

    # Event when the MFMessageComposeViewController is closed
    # -------------------------------------------------------------
    # the callback is fired if it was present in the constructor

    def messageComposeViewController(controller, didFinishWithResult: result)
      @delegate.dismissModalViewControllerAnimated(@message_is_animated)
      @callback.call Result.new(result) if @callback
    end
  end
end