This is the wrapper to the extend picker wheel.
----------------------------------------------------------------------------------------- -- -- program: DateTimePicker.lua -- project: -- purpose: -- author : -- date : -- ----------------------------------------------------------------------------------------- local composer = require("composer") local scene = composer.newScene() local widget = require('widget') local dateAndTime = require("framework.datePickerWheel") local loaded local widgets = {} local displayText, datePicker --widget.setTheme("widget\_theme\_ios") -- JRB may not need local device = require('framework.device') local function new(params) -- if DateTimePicker ~=nil then -- remove() logDebugMessage({'DateTimePicker new',params}) --print("JRB here1 " ..params.time) local currYear = tonumber(os.date("%Y")) local currMonth = tonumber(os.date("%m")) local currDay = tonumber(os.date("%d")) local currHr = tonumber(os.date("%H")) local currMin = tonumber(os.date("%M")) local self={ -- JRB need to reduce.. container = display.newGroup(), time = params.time, done = params.done, isForm = params.isForm or false, startY = params.startY or 10, thisObject = params.thisObject } local sceneGroup = self.view if self.time ~= nil and self.time ~= "" then print("What time is it roger? " ..self.time) if tonumber(self.time:sub(1,4)) \> 0 then currYear = self.time:sub(1,4) end if tonumber(self.time:sub(6,7)) \> 0 then currMonth = self.time:sub(6,7) end if tonumber(self.time:sub(9,10)) \> 0 then currDay = self.time:sub(9,10) end if tonumber(self.time:sub(12,13)) \> 0 then currHr = self.time:sub(12,13) end if tonumber(self.time:sub(15,16)) \> 0 then currMin = self.time:sub(15,16) end end local function setDateTime(PassValue,pastTimeDisplay) logDebugMessage({'DateTimePicker done'}) if self.done then self.done(PassValue,pastTimeDisplay) end return true end -- Create the widget. datePicker = widget.newDatePickerWheel(currYear, currMonth, currDay, currHr, currMin, {callBack=setDateTime}) widgets[#widgets+1] = datePicker -- Position it however you like. datePicker.anchorChildren = true datePicker.anchorX = 0.5 datePicker.anchorY = 0 datePicker.x = display.contentCenterX datePicker.y = self.startY self.container:insert(datePicker) local function disableTouch() return true end local function view(fadingTime) if not fadingTime then fadingTime=0 end transition.fadeIn(self.container,{time=fadingTime}) end local function hide(fadingTime) if not fadingTime then fadingTime=0 end transition.fadeOut(self.container,{time=fadingTime}) end local function moveTo(y) self.container.y=y end local function remove() print("I would like to remove my self") local value , text = datePicker.getDT() self.container:removeSelf() self.container=nil return value , text end local function getTime() return self.time,text end local function cancel(event) logDebugMessage({'TimePicker cancel'}) remove() return true end local function onAccept() local values = datePicker:getSelectedDate() print ( "values: " ..values) end local function render() logDebugMessage({'TimePicker render',self.time}) end render() showMem('TimePicker','finished rendering') return { view = view, hide = hide, moveTo = moveTo, remove = remove, setTime = setTime, getTime = getTime } end return {new=new} -- end of TimePicker.lua
I have not cleaned up some of the un-needed code yet as I was still debugging
Here is the extend Wheel.
local widget = require("widget") local lastDateTime = "" local lastFormattedDT = "" local td = {} local function getLimitedDays(thisDate) -- returns a limited range of dates more reasonable for time entry local p = {} local d = {} thisDate = thisDate or os.date("\*t") local startTime = os.time(thisDate) startTime = startTime - (60\*60\*24\*30) -- sec \* min \* hrs \* days local displayDate local actualDate local dispTime = startTime for i = 1,60 do displayDate = os.date("%a", dispTime) .." " ..os.date("%b", dispTime) .." "..os.date("%d", dispTime) -- JRB may have issue with lenght of month and day! actualDate = os.date("%Y", dispTime) .."-" ..os.date("%m", dispTime) .."-" ..os.date("%d", dispTime) --print(displayDate .." " ..actualDate) d[#d+1] = actualDate p[#p+1] = displayDate dispTime = dispTime + (60\*60\*24) -- sec \* min \* hrs end td = d return p, d end local function newWheel(year, month, day, hour, mins) print ("NewWheel: " .." year: " ..year .." month: " ..month .." day: " ..day .." hour: " ..hour .." mins: " ..mins ) local useDate = { year=year, month=month, day=day, hour=hour, min=mins, sec=0 } local printDate, dValue = getLimitedDays(useDate) local idxHr = tonumber(hour) or 1 local idxAMPM = 1 local idxMins = tonumber(mins) or 1 if idxHr \< 1 then idxHr = 12 end if idxHr \> 23 then idxHr = 12 end if idxHr \> 12 then idxHr = idxHr - 12 idxAMPM = 2 end if idxMins \> 59 then idxMins = 60 end if idxMins \< 1 then idxMins = 1 end local columnData = { { -- Day align = "right", width = 140, startIndex = 31, labels = printDate, dateValue = dValue }, { -- Hours align = "center", width = 50, startIndex = idxHr, labels = {"1","2","3","4","5","6","7","8","9","10","11","12"} }, { -- Mins align = "center", width = 50, startIndex = idxMins, labels = {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","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","55","56","57","58","59","00"} } , { -- Am/Pm align = "center", width = 40, startIndex = idxAMPM, labels = {"AM","PM"} } } return widget.newPickerWheel{columns = columnData, font=native.systemFont} end function widget.newDatePickerWheel(year, month, day, hour, mins, params) year = year or tonumber(os.date("%Y")) month = month or tonumber(os.date("%m")) day = day or tonumber(os.date("%d")) hour = hour or tonumber(os.date("%H")) mins = mins or tonumber(os.date("%M")) local cb={ callBack = params.callBack } local w = display.newGroup() w.wheel = newWheel(year, month, day, hour, mins) w:insert(w.wheel) function w:getValues() return self.wheel:getValues() end function w:getSelectedDate() local values = self:getValues() local formatedDateTime = "" local militaryHour = 0 local tz = os.date('%z') -- CORONA BUG: Sometimes the values can be nil. -- This happens when one of the tables stopped scrolling but hasn't "snapped" to a selected index. -- Prompt the user to fix the bad column. if not values[1] then native.showAlert(\_M.appName, "Please make sure a month is selected.", {"OK"}) elseif not values[2] then native.showAlert(\_M.appName, "Please make sure a day is selected.", {"OK"}) elseif not values[3] then native.showAlert(\_M.appName, "Please make sure a year is selected.", {"OK"}) --else --local valid = validateDate(values[1].index, values[2].value, values[3].value) end -- CORONA BUG: Sometimes the values can be nil. -- This happens when one of the tables stopped scrolling but hasn't "snapped" to a selected index. if not values[1] or not values[2] or not values[3] or not values[4] then return end militaryHour = tonumber(values[2].value) if values[4].value == "PM" then militaryHour = militaryHour + 12 end if militaryHour == 12 then militaryHour = 0 end if militaryHour == 24 then militaryHour = 12 end if militaryHour \< 10 then formatedDateTime = td[values[1].index] .."T0"..militaryHour ..":"..values[3].value ..":00.000" ..string.sub(tz,1,3)..':'..string.sub(tz,4,5) else formatedDateTime = td[values[1].index] .."T"..militaryHour ..":"..values[3].value ..":00.000" ..string.sub(tz,1,3)..':'..string.sub(tz,4,5) end --print ( "Function: formatedDateTime: " ..formatedDateTime .." | " ..td[values[1].index]) return formatedDateTime end function w:monitor() local isoDateTime = w:getSelectedDate() local formatedDateTime = "" if isoDateTime ~= lastDateTime then if isoDateTime ~=nill and isoDateTime ~="" then formatedDateTime = getFormattedDateTime(isoDateTime) --print ( "isoDateTime monitor : " ..isoDateTime) --print ( "formatedDateTime monitor : " ..formatedDateTime) lastDateTime = isoDateTime lastFormattedDT = formatedDateTime cb.callBack(isoDateTime,formatedDateTime) else lastDateTime = "" lastFormattedDT = "" end end end function w:finalize(event) timer.cancel(self.timer) self.timer = nil self.wheel:removeSelf() self.wheel = nil end function w:getDT() return lastDateTime , lastFormattedDT end w:addEventListener("finalize") -- Monitor for changes roughly 30 times per second. w.timer = timer.performWithDelay(33, function() w:monitor() end, -1) return w end
The execution flow is as follows. I have a lua screen. From there I insert a new field into the layout…
fields.startTimeStamp = fieldView.new({parent=sv,label='Start Time',action='datetimepick',text=displayStartDT,value=data.startTimeStamp,finally=validateMandatory,startY=240})
fields comes from
fieldView = require('framework.FieldView')
the FieldView class is really large so here is the starting call…
-- JRB elseif self.action=='datetimepick' then print("JRB in action") if self.dateTimePicker == nil then if self.uiControlCallBack ~= nil then self.uiControlCallBack("disable",self.label) end self.dateTimePicker=dateTimePicker.new({time=self.value,isForm=self.isForm,done=didPickDateTime,startY=self.startY,thisObject=self}) else self.value , self.text = self.dateTimePicker.remove() didPickDateTime(self.value,self.text) self.dateTimePicker = nil if self.uiControlCallBack ~= nil then self.uiControlCallBack("enable",self.label) end end elseif self.action=='url' then