From 71b3e717cf5eae29f32a13212a92e538746fb0de Mon Sep 17 00:00:00 2001 From: chrox Date: Fri, 3 Jan 2014 22:09:55 +0800 Subject: [PATCH] refactoring onConfigChoose --- frontend/ui/widget/configdialog.lua | 69 +++++++++++++---------------- frontend/ui/widget/toggleswitch.lua | 19 +++----- 2 files changed, 38 insertions(+), 50 deletions(-) diff --git a/frontend/ui/widget/configdialog.lua b/frontend/ui/widget/configdialog.lua index 445de54f6..06268a14e 100644 --- a/frontend/ui/widget/configdialog.lua +++ b/frontend/ui/widget/configdialog.lua @@ -96,26 +96,8 @@ function OptionTextItem:onTapSelect() item[1].color = 0 end self[1].color = 15 - local option_value = nil - local option_arg = nil - if self.values then - self.values = self.values or {} - option_value = self.values[self.current_item] - self.config:onConfigChoice(self.name, option_value) - end - if self.event then - self.args = self.args or {} - option_arg = self.args[self.current_item] - self.config:onConfigEvent(self.event, option_arg) - end - if self.events then - for i=0,#self.events do - self.events[i].args = self.events[i].args or {} - option_arg = self.events[i].args[self.current_item] - self.config:onConfigEvent(self.events[i].event, option_arg) - end - end - UIManager.repaint_all = true + self.config:onConfigChoose(self.values, self.name, self.event, self.args, self.events, self.current_item) + UIManager:setDirty(self.config, "partial") return true end @@ -148,16 +130,8 @@ function OptionIconItem:onTapSelect() end --self[1][1].invert = true self[1].color = 15 - local option_value = nil - local option_arg = nil - if type(self.values) == "table" then - option_value = self.values[self.current_item] - self.config:onConfigChoice(self.name, option_value, self.event) - elseif type(self.args) == "table" then - option_arg = self.args[self.current_item] - self.config:onConfigChoice(self.name, option_arg, self.event) - end - UIManager.repaint_all = true + self.config:onConfigChoose(self.values, self.name, self.event, self.args, self.events, self.current_item) + UIManager:setDirty(self.config, "partial") return true end @@ -498,21 +472,40 @@ end function ConfigDialog:onConfigChoice(option_name, option_value) --DEBUG("config option value", option_name, option_value) - UIManager:scheduleIn(0.2, function() - self.configurable[option_name] = option_value - self.ui:handleEvent(Event:new("StartActivityIndicator")) - UIManager:setDirty(self, "partial") - end) + self.configurable[option_name] = option_value + self.ui:handleEvent(Event:new("StartActivityIndicator")) return true end function ConfigDialog:onConfigEvent(option_event, option_arg) --DEBUG("config option event", option_event, option_arg) - UIManager:scheduleIn(0.5, function() - self.ui:handleEvent(Event:new(option_event, option_arg)) + self.ui:handleEvent(Event:new(option_event, option_arg)) + return true +end + +function ConfigDialog:onConfigEvents(option_events, arg_index) + --DEBUG("config option events", option_events, arg_index) + for i=1, #option_events do + option_events[i].args = option_events[i].args or {} + self.ui:handleEvent(Event:new(option_events[i].event, option_events[i].args[arg_index])) + end + return true +end + +function ConfigDialog:onConfigChoose(values, name, event, args, events, position) + UIManager:scheduleIn(0.05, function() + if values then + self:onConfigChoice(name, values[position]) + end + if event then + args = args or {} + self:onConfigEvent(event, args[position]) + end + if events then + self:onConfigEvents(events, position) + end UIManager.repaint_all = true end) - return true end function ConfigDialog:closeDialog() diff --git a/frontend/ui/widget/toggleswitch.lua b/frontend/ui/widget/toggleswitch.lua index 99dee3cf3..a01763e4a 100644 --- a/frontend/ui/widget/toggleswitch.lua +++ b/frontend/ui/widget/toggleswitch.lua @@ -115,26 +115,21 @@ function ToggleSwitch:onTapSelect(arg, gev) ) --DEBUG("toggle position:", position) self:togglePosition(position) - local option_value = nil - local option_arg = nil + --[[ if self.values then self.values = self.values or {} - option_value = self.values[self.position] - self.config:onConfigChoice(self.name, option_value) + self.config:onConfigChoice(self.name, self.values[self.position]) end if self.event then self.args = self.args or {} - option_arg = self.args[self.position] - self.config:onConfigEvent(self.event, option_arg) + self.config:onConfigEvent(self.event, self.args[self.position]) end if self.events then - for i=1,#self.events do - self.events[i].args = self.events[i].args or {} - option_arg = self.events[i].args[self.position] - self.config:onConfigEvent(self.events[i].event, option_arg) - end + self.config:onConfigEvents(self.events, self.position) end - UIManager.repaint_all = true + --]] + self.config:onConfigChoose(self.values, self.name, self.event, self.args, self.events, self.position) + UIManager:setDirty(self.config, "partial") return true end