sugarcube 0.11.3 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,39 @@
1
1
  module SugarCube
2
2
  module Modal
3
3
  module_function
4
- def present_modal(view_ctlr, &block)
5
- UIApplication.sharedApplication.keyWindow.rootViewController.presentViewController(view_ctlr, animated:true, completion:block)
4
+ def present_modal(view_ctlr, target=nil, &block)
5
+ target ||= UIApplication.sharedApplication.keyWindow.rootViewController
6
+ target.presentViewController(view_ctlr, animated:true, completion:block)
6
7
  end
7
8
 
8
- def present_modal_in_nav(view_ctlr, &block)
9
+ def present_modal_in_nav(view_ctlr, target=nil, &block)
9
10
  ctlr = UINavigationController.alloc.initWithRootViewController(view_ctlr)
10
11
  ctlr.modalTransitionStyle = UIModalTransitionStyleCoverVertical
11
12
 
12
- SugarCube::Modal.present_modal(ctlr, &block)
13
+ SugarCube::Modal.present_modal(ctlr, target, &block)
13
14
  ctlr
14
15
  end
15
16
 
16
- def dismiss_modal(&block)
17
- UIApplication.sharedApplication.keyWindow.rootViewController.dismissViewControllerAnimated(true, completion:block)
17
+ def dismiss_modal(target=nil, &block)
18
+ target ||= UIApplication.sharedApplication.keyWindow.rootViewController
19
+ target.dismissViewControllerAnimated(true, completion:block)
18
20
  end
19
21
  end
20
22
  end
23
+
24
+
25
+ class UIViewController
26
+
27
+ def present_modal(view_ctlr, &block)
28
+ SugarCube::Modal.present_modal(view_ctlr, self, &block)
29
+ end
30
+
31
+ def present_modal_in_nav(view_ctlr, &block)
32
+ SugarCube::Modal.present_modal_in_nav(view_ctlr, self, &block)
33
+ end
34
+
35
+ def dismiss_modal(view_ctlr, &block)
36
+ SugarCube::Modal.dismiss_modal(view_ctlr, self, &block)
37
+ end
38
+
39
+ end
@@ -1,5 +1,7 @@
1
- class Array
1
+ class NSArray
2
2
 
3
+ # @param [Symbol] type A pointer type from the list at {http://www.rubymotion.com/developer-center/guides/runtime/ RubyMotion Pointers Reference#_pointers}
4
+ # @return [Pointer] A pointer to the array, of the specified type
3
5
  def to_pointer(type)
4
6
  ret = Pointer.new(type, self.length)
5
7
  self.each_index do |i|
@@ -8,6 +10,8 @@ class Array
8
10
  ret
9
11
  end
10
12
 
13
+ # Creates an NSIndexPath object using the items in `self` as the indices
14
+ # @return [NSIndexPath]
11
15
  def nsindexpath
12
16
  if self.length == 0
13
17
  raise "An index path must have at least one index"
@@ -24,6 +28,8 @@ class Array
24
28
  return path
25
29
  end
26
30
 
31
+ # Creates an NSIndexSet object using the items in `self` as the indices
32
+ # @return [NSIndexSet]
27
33
  def nsindexset
28
34
  if self.length == 0
29
35
  raise "An index set must have at least one index"
@@ -0,0 +1,22 @@
1
+ class NSData
2
+
3
+ # converts NSData into an NSString using any encoding, default is UTF8
4
+ # @return [NSString]
5
+ def nsstring(encoding=nil)
6
+ if encoding
7
+ NSString.stringWithCString(self.bytes, encoding:encoding)
8
+ else
9
+ NSString.stringWithUTF8String(self.bytes)
10
+ end
11
+ end
12
+
13
+ # @return [UIImage]
14
+ def uiimage(scale=nil)
15
+ if scale
16
+ UIImage.imageWithData(self, scale:scale)
17
+ else
18
+ UIImage.imageWithData(self)
19
+ end
20
+ end
21
+
22
+ end
@@ -1,4 +1,27 @@
1
1
  class NSDate
2
+
3
+ def self.new
4
+ self.date
5
+ end
6
+
7
+ def string_with_style(style)
8
+ date_formatter = NSDateFormatter.new
9
+ if style.is_a? Symbol
10
+ style = style.nsdatesyle
11
+ end
12
+ date_formatter.setDateStyle(style)
13
+ date_formatter.stringFromDate(self)
14
+ end
15
+
16
+
17
+ def string_with_format(format)
18
+ format_template = NSDateFormatter.dateFormatFromTemplate(format, options:0,
19
+ locale:NSLocale.currentLocale)
20
+ date_formatter = NSDateFormatter.new
21
+ date_formatter.setDateFormat(format_template)
22
+ date_formatter.stringFromDate(self)
23
+ end
24
+
2
25
  def timezone
3
26
  return _calendar_components(NSTimeZoneCalendarUnit).timeZone
4
27
  end
@@ -0,0 +1,16 @@
1
+ class NSError
2
+
3
+ def self.new(message='Error', options={})
4
+ domain = options[:domain] || 'Error'
5
+ code = options[:code] || 0
6
+ info = {NSLocalizedDescriptionKey => message}
7
+ if options[:userInfo]
8
+ info.merge! options[:userInfo]
9
+ end
10
+ NSError.alloc.initWithDomain( domain,
11
+ code: code,
12
+ userInfo: info
13
+ )
14
+ end
15
+
16
+ end
@@ -1,40 +1,32 @@
1
1
  class NSString
2
2
 
3
+ # @return [NSURL]
3
4
  def nsurl
4
5
  @url ||= NSURL.alloc.initWithString(self)
5
6
  end
6
7
 
7
- def escape_url
8
- CFURLCreateStringByAddingPercentEscapes(
9
- nil,
10
- self,
11
- nil,
12
- "!*'();:@&=+$,/?%#[]",
13
- CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)
14
- )
15
- end
16
-
17
- def unescape_url
18
- CFURLCreateStringByReplacingPercentEscapes(
19
- nil,
20
- self,
21
- ""
22
- )
8
+ # @return [NSData] NSData representation encoded using UTF8, or a specified
9
+ # encoding
10
+ def nsdata(encoding=NSUTF8StringEncoding)
11
+ dataUsingEncoding(encoding)
23
12
  end
24
13
 
14
+ # @return [UIImage]
25
15
  def uiimage
26
16
  UIImage.imageNamed(self)
27
17
  end
28
18
 
19
+ # @return [UIImageView]
29
20
  def uiimageview
30
- (self.uiimage and self.uiimage.uiimageview or UIImageView.alloc.initWithImage(nil))
21
+ self.uiimage ? self.uiimage.uiimageview : UIImageView.alloc.initWithImage(nil)
31
22
  end
32
23
 
24
+ # @return [UIFont]
33
25
  def uifont(size=UIFont.systemFontSize)
34
- @uifont ||= {}
35
- @uifont[size] ||= UIFont.fontWithName(self, size:size)
26
+ UIFont.fontWithName(self, size:size)
36
27
  end
37
28
 
29
+ # @return [UIColor]
38
30
  def uicolor(alpha=nil)
39
31
  if self[0,1] == '#'
40
32
  if self.length == 4
@@ -46,6 +38,24 @@ class NSString
46
38
  self.uiimage.uicolor(alpha)
47
39
  end
48
40
 
41
+ def escape_url
42
+ CFURLCreateStringByAddingPercentEscapes(
43
+ nil,
44
+ self,
45
+ nil,
46
+ "!*'();:@&=+$,/?%#[]",
47
+ CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)
48
+ )
49
+ end
50
+
51
+ def unescape_url
52
+ CFURLCreateStringByReplacingPercentEscapes(
53
+ nil,
54
+ self,
55
+ ""
56
+ )
57
+ end
58
+
49
59
  # This can be called as `"Hello".localized` or `"Hello"._`. The `str._`
50
60
  # syntax is meant to be reminiscent of gettext-style `_(str)`.
51
61
  def localized(value=nil, table=nil)
@@ -11,6 +11,12 @@ class NSString
11
11
  NSFileManager.defaultManager.fileExistsAtPath(self.document)
12
12
  end
13
13
 
14
+ def remove!
15
+ ptr = Pointer.new(:id)
16
+ NSFileManager.defaultManager.removeItemAtPath(self.document, error:ptr)
17
+ ptr[0]
18
+ end
19
+
14
20
  def resource
15
21
  @@sugarcube_resources ||= NSBundle.mainBundle.resourcePath
16
22
  return self if self.hasPrefix(@@sugarcube_resources)
@@ -8,9 +8,42 @@ class NSUserDefaults
8
8
 
9
9
  # Sets the value for a given key and save it right away.
10
10
  def []=(key, val)
11
- self.standardUserDefaults.setObject(val, forKey: key.to_s)
11
+ self.standardUserDefaults.setObject(val.to_nsuserdefaults, forKey: key.to_s)
12
12
  self.standardUserDefaults.synchronize
13
13
  end
14
+
15
+ def remove(key)
16
+ self.standardUserDefaults.removeObjectForKey(key)
17
+ self.standardUserDefaults.synchronize
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+
24
+ class Object
25
+ def to_nsuserdefaults
26
+ self
14
27
  end
28
+ end
29
+
15
30
 
31
+ class NilClass
32
+ def to_nsuserdefaults
33
+ false
34
+ end
35
+ end
36
+
37
+
38
+ class NSArray
39
+ def to_nsuserdefaults
40
+ self.map { |val| val.to_nsuserdefaults }
41
+ end
42
+ end
43
+
44
+
45
+ class NSDictionary
46
+ def to_nsuserdefaults
47
+ self.each_with_object({}) { |(key, val), h| h[key] = val.to_nsuserdefaults }
48
+ end
16
49
  end
@@ -25,6 +25,7 @@ You can extend the defaults by adding entries:
25
25
  class Symbol
26
26
  class << self
27
27
  attr_accessor :devices
28
+ attr_accessor :device_orientations
28
29
  attr_accessor :orientations
29
30
  attr_accessor :returnkeys
30
31
  attr_accessor :statusbar_styles
@@ -38,25 +39,32 @@ class Symbol
38
39
  attr_accessor :baselineadjustments
39
40
  attr_accessor :system_fonts
40
41
  attr_accessor :font_sizes
42
+ attr_accessor :date_styles
41
43
 
42
44
  attr_accessor :buttontypes
43
- attr_accessor :bordertypes
44
- attr_accessor :controlstates
45
- attr_accessor :controlevents
45
+ attr_accessor :border_types
46
+ attr_accessor :control_states
47
+ attr_accessor :control_events
46
48
  attr_accessor :activityindicator_styles
47
49
  attr_accessor :segmented_styles
48
50
  attr_accessor :datepicker_modes
51
+ attr_accessor :content_modes
49
52
 
50
53
  attr_accessor :tableview_styles
51
54
  attr_accessor :tableview_rowanimation
52
55
  attr_accessor :tableview_cellstyles
53
56
  attr_accessor :tableview_cellaccessorytype
54
57
  attr_accessor :tableview_cellselectionstyle
58
+ attr_accessor :tableview_cellseparatorstyle
55
59
 
56
60
  attr_accessor :image_sourcetypes
57
61
  attr_accessor :image_capturemode
58
62
  attr_accessor :image_cameradevice
59
63
  attr_accessor :image_quality
64
+
65
+ attr_accessor :ca_timingfunctions
66
+
67
+ attr_accessor :gesture_recognizer_states
60
68
  end
61
69
 
62
70
  @devices = {
@@ -64,6 +72,16 @@ class Symbol
64
72
  ipad: UIUserInterfaceIdiomPad,
65
73
  }
66
74
 
75
+ @device_orientations = {
76
+ unknown: UIDeviceOrientationUnknown,
77
+ portrait: UIDeviceOrientationPortrait,
78
+ upside_down: UIDeviceOrientationPortraitUpsideDown,
79
+ left: UIDeviceOrientationLandscapeLeft,
80
+ right: UIDeviceOrientationLandscapeRight,
81
+ face_up: UIDeviceOrientationFaceUp,
82
+ face_down: UIDeviceOrientationFaceDown
83
+ }
84
+
67
85
  @textalignments = {
68
86
  left: UITextAlignmentLeft,
69
87
  right: UITextAlignmentRight,
@@ -105,7 +123,7 @@ class Symbol
105
123
  contact_add: UIButtonTypeContactAdd,
106
124
  }
107
125
 
108
- @bordertypes = {
126
+ @border_types = {
109
127
  none: UITextBorderStyleNone,
110
128
  line: UITextBorderStyleLine,
111
129
  bezel: UITextBorderStyleBezel,
@@ -113,7 +131,7 @@ class Symbol
113
131
  rounded_rect: UITextBorderStyleRoundedRect,
114
132
  }
115
133
 
116
- @controlstates = {
134
+ @control_states = {
117
135
  normal: UIControlStateNormal,
118
136
  highlighted: UIControlStateHighlighted,
119
137
  disabled: UIControlStateDisabled,
@@ -121,16 +139,18 @@ class Symbol
121
139
  application: UIControlStateApplication,
122
140
  }
123
141
 
124
- @controlevents = {
142
+ @control_events = {
125
143
  touch: UIControlEventTouchUpInside,
126
144
  touch_up: UIControlEventTouchUpInside,
127
145
  touch_down: UIControlEventTouchDown,
128
- change: UIControlEventValueChanged|UIControlEventEditingChanged,
146
+ touch_start: UIControlEventTouchDown | UIControlEventTouchDragEnter,
147
+ touch_stop: UIControlEventTouchUpInside | UIControlEventTouchCancel | UIControlEventTouchDragExit,
148
+ change: UIControlEventValueChanged | UIControlEventEditingChanged,
129
149
  begin: UIControlEventEditingDidBegin,
130
150
  end: UIControlEventEditingDidEnd,
131
151
  # I'm leaving this for backwards compatibility. please use 'change' or
132
152
  # 'editing_did_change':
133
- changed: UIControlEventValueChanged|UIControlEventEditingChanged,
153
+ changed: UIControlEventValueChanged | UIControlEventEditingChanged,
134
154
 
135
155
  touch_down_repeat: UIControlEventTouchDownRepeat,
136
156
  touch_drag_inside: UIControlEventTouchDragInside,
@@ -171,6 +191,15 @@ class Symbol
171
191
  system: :systemFontSize,
172
192
  }
173
193
 
194
+ @date_styles = {
195
+ no: NSDateFormatterNoStyle,
196
+ none: NSDateFormatterNoStyle,
197
+ short: NSDateFormatterShortStyle,
198
+ medium: NSDateFormatterMediumStyle,
199
+ long: NSDateFormatterLongStyle,
200
+ full: NSDateFormatterFullStyle,
201
+ }
202
+
174
203
  @returnkeys = {
175
204
  default: UIReturnKeyDefault,
176
205
  return: UIReturnKeyDefault,
@@ -207,6 +236,28 @@ class Symbol
207
236
  countdowntimer: UIDatePickerModeCountDownTimer
208
237
  }
209
238
 
239
+ @content_modes = {
240
+ scale: UIViewContentModeScaleToFill,
241
+ scale_to_fill: UIViewContentModeScaleToFill,
242
+ scaletofill: UIViewContentModeScaleToFill,
243
+ fit: UIViewContentModeScaleAspectFit,
244
+ scaleaspectfit: UIViewContentModeScaleAspectFit,
245
+ scale_aspect_fit: UIViewContentModeScaleAspectFit,
246
+ fill: UIViewContentModeScaleAspectFill,
247
+ scaleaspectfill: UIViewContentModeScaleAspectFill,
248
+ scale_aspect_fill: UIViewContentModeScaleAspectFill,
249
+ redraw: UIViewContentModeRedraw,
250
+ center: UIViewContentModeCenter,
251
+ top: UIViewContentModeTop,
252
+ bottom: UIViewContentModeBottom,
253
+ left: UIViewContentModeLeft,
254
+ right: UIViewContentModeRight,
255
+ topleft: UIViewContentModeTopLeft,
256
+ topright: UIViewContentModeTopRight,
257
+ bottomleft: UIViewContentModeBottomLeft,
258
+ bottomright: UIViewContentModeBottomRight,
259
+ }
260
+
210
261
  @tableview_styles = {
211
262
  plain: UITableViewStylePlain,
212
263
  grouped: UITableViewStyleGrouped,
@@ -245,6 +296,14 @@ class Symbol
245
296
  gray: UITableViewCellSelectionStyleGray,
246
297
  }
247
298
 
299
+ @tableview_cellseparatorstyle = {
300
+ none: UITableViewCellSeparatorStyleNone,
301
+ singleline: UITableViewCellSeparatorStyleSingleLine,
302
+ single: UITableViewCellSeparatorStyleSingleLine,
303
+ singlelineetched: UITableViewCellSeparatorStyleSingleLineEtched,
304
+ etched: UITableViewCellSeparatorStyleSingleLineEtched,
305
+ }
306
+
248
307
  @statusbar_styles = {
249
308
  default: UIStatusBarStyleDefault,
250
309
  black: UIStatusBarStyleBlackOpaque,
@@ -344,6 +403,28 @@ class Symbol
344
403
  i540: UIImagePickerControllerQualityTypeIFrame960x540,
345
404
  }
346
405
 
406
+ @ca_timingfunctions = {
407
+ linear: KCAMediaTimingFunctionLinear,
408
+ easein: KCAMediaTimingFunctionEaseIn,
409
+ ease_in: KCAMediaTimingFunctionEaseIn,
410
+ easeout: KCAMediaTimingFunctionEaseOut,
411
+ ease_out: KCAMediaTimingFunctionEaseOut,
412
+ easeinout: KCAMediaTimingFunctionEaseInEaseOut,
413
+ ease_in_out: KCAMediaTimingFunctionEaseInEaseOut,
414
+ ease_in_ease_out: KCAMediaTimingFunctionEaseInEaseOut,
415
+ default: KCAMediaTimingFunctionDefault,
416
+ }
417
+
418
+ @gesture_recognizer_states = {
419
+ possible: UIGestureRecognizerStatePossible,
420
+ began: UIGestureRecognizerStateBegan,
421
+ changed: UIGestureRecognizerStateChanged,
422
+ ended: UIGestureRecognizerStateEnded,
423
+ cancelled: UIGestureRecognizerStateCancelled,
424
+ failed: UIGestureRecognizerStateFailed,
425
+ recognized: UIGestureRecognizerStateRecognized,
426
+ }
427
+
347
428
  private
348
429
  def look_in(here)
349
430
  return here[self] if here.has_key? self
@@ -355,6 +436,10 @@ class Symbol
355
436
  look_in(Symbol.devices)
356
437
  end
357
438
 
439
+ def uideviceorientation
440
+ look_in(Symbol.device_orientations)
441
+ end
442
+
358
443
  def uitextalignment
359
444
  look_in(Symbol.textalignments)
360
445
  end
@@ -371,26 +456,28 @@ class Symbol
371
456
  alias uibaseline uibaselineadjustment
372
457
 
373
458
 
374
- def uiorientation
459
+ def uiinterfaceorientation
375
460
  look_in(Symbol.orientations)
376
461
  end
462
+ alias uiorientation uiinterfaceorientation
377
463
 
378
464
  def uibuttontype
379
465
  look_in(Symbol.buttontypes)
380
466
  end
381
467
 
382
468
  def uibordertype
383
- look_in(Symbol.bordertypes)
469
+ look_in(Symbol.border_types)
384
470
  end
471
+ alias uiborderstyle uibordertype
385
472
 
386
473
  def uicontrolstate
387
- look_in(Symbol.controlstates)
474
+ look_in(Symbol.control_states)
388
475
  end
389
476
  alias uistate uicontrolstate
390
477
 
391
478
 
392
479
  def uicontrolevent
393
- look_in(Symbol.controlevents)
480
+ look_in(Symbol.control_events)
394
481
  end
395
482
 
396
483
  def uireturnkey
@@ -413,35 +500,39 @@ class Symbol
413
500
  look_in(Symbol.datepicker_modes)
414
501
  end
415
502
 
503
+ def uicontentmode
504
+ look_in(Symbol.content_modes)
505
+ end
506
+ alias uiviewcontentmode uicontentmode
507
+
416
508
  def uitablestyle
417
509
  look_in(Symbol.tableview_styles)
418
510
  end
419
511
  alias uitableviewstyle uitablestyle
420
512
 
421
-
422
513
  def uitablerowanimation
423
514
  look_in(Symbol.tableview_rowanimation)
424
515
  end
425
516
  alias uitableviewrowanimation uitablerowanimation
426
517
 
427
-
428
518
  def uitablecellstyle
429
519
  look_in(Symbol.tableview_cellstyles)
430
520
  end
431
521
  alias uitableviewcellstyle uitablecellstyle
432
522
 
433
-
434
523
  def uitablecellaccessory
435
524
  look_in(Symbol.tableview_cellaccessorytype)
436
525
  end
437
526
  alias uitableviewcellaccessorytype uitablecellaccessory
438
527
 
439
-
440
528
  def uitablecellselectionstyle
441
529
  look_in(Symbol.tableview_cellselectionstyle)
442
530
  end
443
531
  alias uitableviewcellselectionstyle uitablecellselectionstyle
444
532
 
533
+ def uitablecellseparatorstyle
534
+ look_in(Symbol.tableview_cellseparatorstyle)
535
+ end
445
536
 
446
537
  def uistatusbarstyle
447
538
  look_in(Symbol.statusbar_styles)
@@ -459,10 +550,12 @@ class Symbol
459
550
  look_in(Symbol.keyboardtypes)
460
551
  end
461
552
 
462
- def uiautoresize
553
+ def uiautoresizemask
463
554
  look_in(Symbol.autoresizemasks)
464
555
  end
465
556
  alias uiviewautoresizing uiautoresize
557
+ alias uiautoresizingmask uiautoresizemask
558
+ alias uiautoresize uiautoresizemask
466
559
 
467
560
  def uiimagesource
468
561
  look_in(Symbol.image_sourcetypes)
@@ -485,6 +578,16 @@ class Symbol
485
578
  end
486
579
  alias uiimagequalitytype uiimagequality
487
580
 
581
+ def catimingfunction
582
+ look_in(Symbol.ca_timingfunctions)
583
+ end
584
+ alias catiming catimingfunction
585
+
586
+ def uigesturerecognizerstate
587
+ look_in(Symbol.gesture_recognizer_states)
588
+ end
589
+ alias uigesturestate uigesturerecognizerstate
590
+
488
591
  def uifont(size=UIFont.systemFontSize)
489
592
  # system fonts
490
593
  if Symbol.system_fonts.has_key? self
@@ -508,4 +611,9 @@ class Symbol
508
611
  return size.to_f
509
612
  end
510
613
 
614
+ def nsdatestyle
615
+ look_in(Symbol.date_styles)
616
+ end
617
+ alias nsdateformatterstyle nsdatestyle
618
+
511
619
  end