Module: Briar::Picker::DateManipulation

Defined in:
lib/briar/picker/date_picker_manipulation.rb

Instance Method Summary collapse

Instance Method Details

#args_for_change_date_on_picker(options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/briar/picker/date_picker_manipulation.rb', line 68

def args_for_change_date_on_picker(options)
  args = []
  if options.has_key?(:notify_targets)
    args << options[:notify_targets] ? 1 : 0
  else
    args << 1
  end

  if options.has_key?(:animate)
    args << options[:animate] ? 1 : 0
  else
    args << 1
  end
  args
end

#brief_date_strings_are_equivalent(a, b) ⇒ Object



270
271
272
273
274
# File 'lib/briar/picker/date_picker_manipulation.rb', line 270

def brief_date_strings_are_equivalent (a, b)
  a_iso_str = Date.parse(a).strftime(BRIAR_PICKER_ISO8601_BRIEF_DATE_FMT)
  b_iso_str = Date.parse(b).strftime(BRIAR_PICKER_ISO8601_BRIEF_DATE_FMT)
  a_iso_str.eql? b_iso_str
end

#brief_date_time_strings_are_equivalent(a, b) ⇒ Object



277
278
279
280
281
# File 'lib/briar/picker/date_picker_manipulation.rb', line 277

def brief_date_time_strings_are_equivalent (a, b)
  a_iso_str = Date.parse(a).strftime(BRIAR_PICKER_ISO8601_BRIEF_DATE_TIME_FMT)
  b_iso_str = Date.parse(b).strftime(BRIAR_PICKER_ISO8601_BRIEF_DATE_TIME_FMT)
  a_iso_str.eql? b_iso_str
end

#change_date_on_picker_with_date(date, options = {:animate => true, :picker_id => nil, :notify_targets => true}) ⇒ Object



215
216
217
218
219
220
221
# File 'lib/briar/picker/date_picker_manipulation.rb', line 215

def change_date_on_picker_with_date (date, options = {:animate => true,
                                                      :picker_id => nil,
                                                      :notify_targets => true})
  picker_id = picker_id_from_options options
  target_dt = target_date_for_change_date_on_picker_with_date date, picker_id
  change_picker_date_time target_dt, options
end

#change_date_on_picker_with_date_str(date_str, options = {:animate => true, :picker_id => nil, :notify_targets => true}) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/briar/picker/date_picker_manipulation.rb', line 206

def change_date_on_picker_with_date_str (date_str, options = {:animate => true,
                                                              :picker_id => nil,
                                                              :notify_targets => true})
  picker_id = picker_id_from_options options
  target_dt = target_date_for_change_date_on_picker_with_date_str date_str, picker_id
  change_picker_date_time target_dt, options
end

#change_picker_date_time(target_dt, options = {:convert_time_to_utc => false, :animate => true, :picker_id => nil, :notify_targets => true}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/briar/picker/date_picker_manipulation.rb', line 140

def change_picker_date_time (target_dt, options = {:convert_time_to_utc => false,
                                                   :animate => true,
                                                   :picker_id => nil,
                                                   :notify_targets => true})


  picker_id = picker_id_from_options options
  unless picker_is_in_time_mode picker_id or picker_is_in_date_and_time_mode picker_id
    screenshot_and_raise 'picker is not in date time or time mode'
  end


  should_see_date_picker picker_id


  convert = convert_to_utc_from_options options


  ensure_can_change_picker_to_date target_dt, picker_id


  target_str = convert_date_time_to_objc_fmt target_dt, convert
  fmt_str = date_format_for_target(:objc, convert ? :zoned : :default)


  args = args_for_change_date_on_picker options


  query_str = query_string_for_picker picker_id


  views_touched = map(query_str, :changeDatePickerDate, target_str, fmt_str, *args)

  msg = "could not change date on picker to '#{target_dt}' using query '#{query_str}' with options '#{options}'"
  views_touched = assert_map_results(views_touched, msg)

  set_briar_date_picker_variables target_dt, options

  step_pause
  views_touched
end

#change_time_on_picker_with_time(time, options = {:convert_time_to_utc => false, :animate => true, :picker_id => nil, :notify_targets => true}) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/briar/picker/date_picker_manipulation.rb', line 196

def change_time_on_picker_with_time (time, options = {:convert_time_to_utc => false,
                                                      :animate => true,
                                                      :picker_id => nil,
                                                      :notify_targets => true})
  convert = convert_to_utc_from_options options
  picker_id = picker_id_from_options options
  target_dt = target_date_for_change_time_on_picker_with_time time, convert, picker_id
  change_picker_date_time target_dt, options
end

#change_time_on_picker_with_time_str(time_str, options = {:convert_time_to_utc => false, :animate => true, :picker_id => nil, :notify_targets => true}) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/briar/picker/date_picker_manipulation.rb', line 182

def change_time_on_picker_with_time_str (time_str, options = {:convert_time_to_utc => false,
                                                              :animate => true,
                                                              :picker_id => nil,
                                                              :notify_targets => true})

  convert = convert_to_utc_from_options options
  picker_id = picker_id_from_options options

  target_dt = target_date_for_change_time_on_picker_with_time_str time_str, convert, picker_id

  change_picker_date_time target_dt, options
end

#convert_date_time_to_objc_fmt(date_time, convert = false) ⇒ Object



63
64
65
66
# File 'lib/briar/picker/date_picker_manipulation.rb', line 63

def convert_date_time_to_objc_fmt (date_time, convert=false)
  format = date_format_for_target(:ruby, convert ? :zoned : :default)
  date_time.strftime(format).squeeze(' ').strip
end

#date_format_for_target(target, zoned) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/briar/picker/date_picker_manipulation.rb', line 55

def date_format_for_target (target, zoned)
  res = BRIAR_DATE_CONVERSION_FORMATS[target][zoned]
  if res.nil?
    screenshot_and_raise "could not find format for target '#{target}' and zone '#{zoned}'"
  end
  res
end

#ensure_can_change_picker_to_date(target_dt, picker_id = nil) ⇒ Object

pickers that use utc (reminders, alerts, etc. do no usually have min/max dates



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/briar/picker/date_picker_manipulation.rb', line 123

def ensure_can_change_picker_to_date(target_dt, picker_id=nil)
  max_date = maximum_date_time_from_picker picker_id
  if max_date and target_dt > max_date
    puts "\ntarget: '#{target_dt}'"
    puts "   max: '#{max_date}'"
    screenshot_and_raise "cannot change time to '#{target_dt}' because the picker has a maximum date of '#{max_date}'"
  end

  min_date = minimum_date_time_from_picker picker_id
  if min_date and target_dt < min_date
    p "target: '#{target_dt}'"
    p "   min: '#{min_date}'"
    screenshot_and_raise "cannot change time to #{target_dt} because the picker has a minimum date of '#{min_date}'"
  end
end

#set_briar_date_picker_variables(target_dt, options = {}) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/briar/picker/date_picker_manipulation.rb', line 224

def set_briar_date_picker_variables(target_dt, options={})
  default_options = {:picker_id => nil,
                     :convert_time_to_utc => false,
                     :assert_mode => false}
  options = default_options.merge(options)

  options[:assert_picker] = false
  ruby_picker_time = ruby_time_from_picker options

  options[:time_on_picker] = ruby_picker_time


  picker_mode = picker_mode(options[:picker_id])

  if picker_mode == UIDatePickerModeDateAndTime or
        picker_mode == UIDatePickerModeTime
    @date_picker_time_12h = picker_time_str :h12, options
    @date_picker_time_24h = picker_time_str :h24, options
    @date_picker_time_hash = picker_time_strs_hash options
    @date_picker_time_arr = picker_time_strs_arr options
    unless time_strings_are_equivalent @date_picker_time_12h, @date_picker_time_24h
      screenshot_and_raise "ERROR: changing the picker resulted in two different times: '#{@date_picker_time_hash}'"
    end
  end

  @date_picker_brief_date_12h = picker_brief_date_str :brief_12h, options
  @date_picker_brief_date_24h = picker_brief_date_str :brief_24h, options
  @date_picker_brief_date_hash = picker_brief_date_strs_hash options
  @date_picker_brief_date_arr = picker_brief_date_strs_arr options

  unless brief_date_strings_are_equivalent @date_picker_brief_date_12h, @date_picker_brief_date_24h
    screenshot_and_raise "ERROR: changing the picker resulted in two different dates: '#{@date_picker_brief_date_hash}'"
  end

  @date_picker_date_time = target_dt
end

#target_date_for_change_date_on_picker_with_date(target_date, picker_id = nil) ⇒ Object



114
115
116
117
118
# File 'lib/briar/picker/date_picker_manipulation.rb', line 114

def target_date_for_change_date_on_picker_with_date(target_date, picker_id=nil)
  current_time = ruby_time_from_picker picker_id
  DateTime.new(target_date.year, target_date.mon, target_date.day,
               current_time.hour, current_time.min, 0, current_time.offset)
end

#target_date_for_change_date_on_picker_with_date_str(date_str, picker_id = nil) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/briar/picker/date_picker_manipulation.rb', line 106

def target_date_for_change_date_on_picker_with_date_str(date_str, picker_id=nil)
  if date_str.nil? || date_str.length == 0
    screenshot_and_raise "date str '#{date_str}' must be non-nil and non-empty"
  end
  date = Date.parse(date_str)
  target_date_for_change_date_on_picker_with_date date, picker_id
end

#target_date_for_change_time_on_picker_with_time(target_time, convert, picker_id = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/briar/picker/date_picker_manipulation.rb', line 92

def target_date_for_change_time_on_picker_with_time(target_time, convert, picker_id=nil)
  current_time = ruby_time_from_picker({:picker_id => picker_id})
  if RUBY_VERSION.start_with?('1.8')
    tz_offset = (convert) ? 0 : Rational(((target_time.utc_offset/3600.0)/24.0).to_i)
  else
    tz_offset = (convert) ? 0 : Rational((target_time.utc_offset/3600.0)/24.0)
  end

  DateTime.new(current_time.year, current_time.mon, current_time.day,
               target_time.hour, target_time.min,
               0, tz_offset)
end

#target_date_for_change_time_on_picker_with_time_str(time_str, convert, picker_id = nil) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/briar/picker/date_picker_manipulation.rb', line 84

def target_date_for_change_time_on_picker_with_time_str(time_str, convert, picker_id=nil)
  if time_str.nil? || time_str.length == 0
    screenshot_and_raise "time str '#{time_str}' must be non-nil and non-empty"
  end
  time = Time.parse(time_str)
  target_date_for_change_time_on_picker_with_time time, convert, picker_id
end

#time_strings_are_equivalent(a, b) ⇒ Object

utility



263
264
265
266
267
# File 'lib/briar/picker/date_picker_manipulation.rb', line 263

def time_strings_are_equivalent (a, b)
  a_iso_str = Time.parse(a).strftime(BRIAR_PICKER_ISO8601_TIME_FMT)
  b_iso_str = Time.parse(b).strftime(BRIAR_PICKER_ISO8601_TIME_FMT)
  a_iso_str.eql? b_iso_str
end