Extracted GroupHandler.lua to GroupHelper.lua

torsten.philipp [03-29-18 - 21:37]
Extracted GroupHandler.lua to GroupHelper.lua
Filename
TaosGroupTools/TaosGroupTools.lua
TaosGroupTools/logic/GroupHandler.lua
TaosGroupTools/ui/CompactSwimlaneList.lua
TaosGroupTools/ui/GroupUltimateSelector.lua
TaosGroupTools/ui/SimpleList.lua
TaosGroupTools/ui/SwimlaneList.lua
TaosGroupTools/util/GroupHelper.lua
diff --git a/TaosGroupTools/TaosGroupTools.lua b/TaosGroupTools/TaosGroupTools.lua
index 8d2acac..864ea86 100644
--- a/TaosGroupTools/TaosGroupTools.lua
+++ b/TaosGroupTools/TaosGroupTools.lua
@@ -8,8 +8,6 @@
 	Global variables
 ]]--
 -- Callbacks
-TGU_GROUP_CHANGED = "TGU-GroupChanged"
-TGU_UNIT_GROUPED_CHANGED = "TGU-UnitGroupedChanged"
 TGU_MAP_PING_CHANGED = "TGU-MapPingChanged"
 TGU_PLAYER_DATA_CHANGED = "TGU-PlayerDataChanged"
 TGU_STYLE_CHANGED = "TGU-StyleChanged"
@@ -63,7 +61,6 @@ function TaosGroupUltimate:initialize()
     TGU_Communicator.Initialize(logger, ISMOCKED)

     -- Initialize logic
-    TGU_GroupHandler.Initialize(logger, ISMOCKED)
     TGU_MapPingHandler.Initialize(logger, ISMOCKED)
     TGU_UltimateGroupHandler.Initialize(logger)
     TGU_CommandsHandler.Initialize(logger)
diff --git a/TaosGroupTools/logic/GroupHandler.lua b/TaosGroupTools/logic/GroupHandler.lua
deleted file mode 100644
index 14092b9..0000000
--- a/TaosGroupTools/logic/GroupHandler.lua
+++ /dev/null
@@ -1,95 +0,0 @@
---[[
-	Addon: Taos Group Ultimate
-	Author: TProg Taonnor
-	Created by @Taonnor
-]]--
-
---[[
-	Local variables
-]]--
-local LOG_ACTIVE = false
-local _logger = nil
-
---[[
-	Table TGU_GroupHandler
-]]--
-TGU_GroupHandler = {}
-TGU_GroupHandler.__index = TGU_GroupHandler
-
---[[
-	Table Members
-]]--
-TGU_GroupHandler.Name = "TGU-GroupHandler"
-TGU_GroupHandler.IsMocked = false
-TGU_GroupHandler.IsGrouped = false
-
---[[
-	Called when group member joined group
-]]--
-function TGU_GroupHandler.OnGroupMemberJoined()
-	if (LOG_ACTIVE) then _logger:logTrace("TGU_GroupHandler.OnGroupMemberJoined") end
-
-    CALLBACK_MANAGER:FireCallbacks(TGU_GROUP_CHANGED)
-
-    TGU_GroupHandler.OnGroupUpdate()
-end
-
---[[
-	Called when group member left group
-]]--
-function TGU_GroupHandler.OnGroupMemberLeft()
-	if (LOG_ACTIVE) then _logger:logTrace("TGU_GroupHandler.OnGroupMemberLeft") end
-
-    CALLBACK_MANAGER:FireCallbacks(TGU_GROUP_CHANGED)
-
-    TGU_GroupHandler.OnGroupUpdate()
-end
-
---[[
-	Called when groupUnitTags updated
-]]--
-function TGU_GroupHandler.OnGroupUpdate()
-	if (LOG_ACTIVE) then _logger:logTrace("TGU_GroupHandler.OnGroupUpdate") end
-
-    local isGrouped = IsUnitGrouped("player")
-
-    if (TGU_GroupHandler.IsMocked) then
-        isGrouped = true
-    end
-
-    if (isGrouped ~= TGU_GroupHandler.IsGrouped) then
-        TGU_GroupHandler.IsGrouped = isGrouped
-        CALLBACK_MANAGER:FireCallbacks(TGU_UNIT_GROUPED_CHANGED)
-    end
-end
-
---[[
-	Called on ???
-]]--
-function TGU_GroupHandler.OnUnitFrameUpdate()
-	if (LOG_ACTIVE) then _logger:logTrace("TGU_GroupHandler.OnUnitFrameUpdate") end
-
-    CALLBACK_MANAGER:FireCallbacks(TGU_GROUP_CHANGED)
-
-    TGU_GroupHandler.OnGroupUpdate()
-end
-
---[[
-	Initialize initializes TGU_GroupHandler
-]]--
-function TGU_GroupHandler.Initialize(logger, isMocked)
-    if (LOG_ACTIVE) then logger:logTrace("TGU_GroupHandler.Initialize") end
-
-    _logger = logger
-
-    TGU_GroupHandler.IsMocked = isMocked
-
-    -- Initial call
-	TGU_GroupHandler:OnGroupUpdate()
-
-	-- Register events
-	EVENT_MANAGER:RegisterForEvent(TGU_GroupHandler.Name, EVENT_GROUP_MEMBER_JOINED, TGU_GroupHandler.OnGroupMemberJoined)
-	EVENT_MANAGER:RegisterForEvent(TGU_GroupHandler.Name, EVENT_GROUP_MEMBER_LEFT, TGU_GroupHandler.OnGroupMemberLeft)
-	EVENT_MANAGER:RegisterForEvent(TGU_GroupHandler.Name, EVENT_GROUP_UPDATE, TGU_GroupHandler.OnGroupUpdate)
-	EVENT_MANAGER:RegisterForEvent(TGU_GroupHandler.Name, EVENT_UNIT_FRAME_UPDATE, TGU_GroupHandler.OnUnitFrameUpdate)
-end
\ No newline at end of file
diff --git a/TaosGroupTools/ui/CompactSwimlaneList.lua b/TaosGroupTools/ui/CompactSwimlaneList.lua
index 5d7b793..562499c 100644
--- a/TaosGroupTools/ui/CompactSwimlaneList.lua
+++ b/TaosGroupTools/ui/CompactSwimlaneList.lua
@@ -305,7 +305,7 @@ function TGU_CompactSwimlaneList.SetControlHidden(isHidden)
         _logger:logDebug("isHidden", isHidden)
     end

-    if (TGU_GroupHandler.IsGrouped) then
+    if (GetIsUnitGrouped()) then
         _control:SetHidden(isHidden)
     else
         _control:SetHidden(true)
@@ -329,7 +329,7 @@ function TGU_CompactSwimlaneList.SetControlActive()
         -- Start timeout timer
 	    EVENT_MANAGER:UnregisterForUpdate(TGU_CompactSwimlaneList.Name)

-        CALLBACK_MANAGER:UnregisterCallback(TGU_GROUP_CHANGED, TGU_CompactSwimlaneList.RefreshList)
+        CALLBACK_MANAGER:UnregisterCallback(TAO_GROUP_CHANGED, TGU_CompactSwimlaneList.RefreshList)
         CALLBACK_MANAGER:UnregisterCallback(TGU_PLAYER_DATA_CHANGED, TGU_CompactSwimlaneList.UpdatePlayer)
         CALLBACK_MANAGER:UnregisterCallback(TGU_MOVABLE_CHANGED, TGU_CompactSwimlaneList.SetControlMovable)
         CALLBACK_MANAGER:UnregisterCallback(TGU_SWIMLANE_ULTIMATE_GROUP_ID_CHANGED, TGU_CompactSwimlaneList.SetSwimlaneUltimate)
@@ -344,7 +344,7 @@ function TGU_CompactSwimlaneList.SetControlActive()
         -- Start timeout timer
 	    EVENT_MANAGER:RegisterForUpdate(TGU_CompactSwimlaneList.Name, REFRESHRATE, TGU_CompactSwimlaneList.RefreshList)

-        CALLBACK_MANAGER:RegisterCallback(TGU_GROUP_CHANGED, TGU_CompactSwimlaneList.RefreshList)
+        CALLBACK_MANAGER:RegisterCallback(TAO_GROUP_CHANGED, TGU_CompactSwimlaneList.RefreshList)
         CALLBACK_MANAGER:RegisterCallback(TGU_PLAYER_DATA_CHANGED, TGU_CompactSwimlaneList.UpdatePlayer)
         CALLBACK_MANAGER:RegisterCallback(TGU_MOVABLE_CHANGED, TGU_CompactSwimlaneList.SetControlMovable)
         CALLBACK_MANAGER:RegisterCallback(TGU_SWIMLANE_ULTIMATE_GROUP_ID_CHANGED, TGU_CompactSwimlaneList.SetSwimlaneUltimate)
@@ -496,5 +496,5 @@ function TGU_CompactSwimlaneList.Initialize(logger, isMocked)

     CALLBACK_MANAGER:RegisterCallback(TGU_STYLE_CHANGED, TGU_CompactSwimlaneList.SetControlActive)
     CALLBACK_MANAGER:RegisterCallback(TGU_IS_ZONE_CHANGED, TGU_CompactSwimlaneList.SetControlActive)
-    CALLBACK_MANAGER:RegisterCallback(TGU_UNIT_GROUPED_CHANGED, TGU_CompactSwimlaneList.SetControlActive)
+    CALLBACK_MANAGER:RegisterCallback(TAO_UNIT_GROUPED_CHANGED, TGU_CompactSwimlaneList.SetControlActive)
 end
\ No newline at end of file
diff --git a/TaosGroupTools/ui/GroupUltimateSelector.lua b/TaosGroupTools/ui/GroupUltimateSelector.lua
index 65c870a..bb648bf 100644
--- a/TaosGroupTools/ui/GroupUltimateSelector.lua
+++ b/TaosGroupTools/ui/GroupUltimateSelector.lua
@@ -134,7 +134,7 @@ function TGU_GroupUltimateSelector.SetControlHidden(isHidden)
         _logger:logDebug("isHidden", isHidden)
     end

-    if (TGU_GroupHandler.IsGrouped) then
+    if (GetIsUnitGrouped()) then
         _control:SetHidden(isHidden)
     else
         _control:SetHidden(true)
@@ -183,5 +183,5 @@ function TGU_GroupUltimateSelector.Initialize(logger)
     TGU_GroupUltimateSelector.SetUltimateIcon(staticUltimateID)

     CALLBACK_MANAGER:RegisterCallback(TGU_IS_ZONE_CHANGED, TGU_GroupUltimateSelector.SetControlActive)
-    CALLBACK_MANAGER:RegisterCallback(TGU_UNIT_GROUPED_CHANGED, TGU_GroupUltimateSelector.SetControlActive)
+    CALLBACK_MANAGER:RegisterCallback(TAO_UNIT_GROUPED_CHANGED, TGU_GroupUltimateSelector.SetControlActive)
 end
\ No newline at end of file
diff --git a/TaosGroupTools/ui/SimpleList.lua b/TaosGroupTools/ui/SimpleList.lua
index e6e00d4..d91ac0b 100644
--- a/TaosGroupTools/ui/SimpleList.lua
+++ b/TaosGroupTools/ui/SimpleList.lua
@@ -236,7 +236,7 @@ function TGU_SimpleList.SetControlHidden(isHidden)
         _logger:logDebug("isHidden", isHidden)
     end

-    if (TGU_GroupHandler.IsGrouped) then
+    if (GetIsUnitGrouped()) then
         _control:SetHidden(isHidden)
     else
         _control:SetHidden(true)
@@ -260,7 +260,7 @@ function TGU_SimpleList.SetControlActive()
 		-- Stop timeout timer
 	    EVENT_MANAGER:UnregisterForUpdate(TGU_SimpleList.Name)

-        CALLBACK_MANAGER:UnregisterCallback(TGU_GROUP_CHANGED, TGU_SimpleList.RefreshList)
+        CALLBACK_MANAGER:UnregisterCallback(TAO_GROUP_CHANGED, TGU_SimpleList.RefreshList)
         CALLBACK_MANAGER:UnregisterCallback(TGU_PLAYER_DATA_CHANGED, TGU_SimpleList.UpdatePlayer)
         CALLBACK_MANAGER:UnregisterCallback(TGU_MOVABLE_CHANGED, TGU_SimpleList.SetControlMovable)
         CALLBACK_MANAGER:UnregisterCallback(TAO_HUD_HIDDEN_STATE_CHANGED, TGU_SimpleList.SetControlHidden)
@@ -271,7 +271,7 @@ function TGU_SimpleList.SetControlActive()
 		-- Start timeout timer
 	    EVENT_MANAGER:RegisterForUpdate(TGU_SimpleList.Name, REFRESHRATE, TGU_SimpleList.RefreshList)

-        CALLBACK_MANAGER:RegisterCallback(TGU_GROUP_CHANGED, TGU_SimpleList.RefreshList)
+        CALLBACK_MANAGER:RegisterCallback(TAO_GROUP_CHANGED, TGU_SimpleList.RefreshList)
         CALLBACK_MANAGER:RegisterCallback(TGU_PLAYER_DATA_CHANGED, TGU_SimpleList.UpdatePlayer)
         CALLBACK_MANAGER:RegisterCallback(TGU_MOVABLE_CHANGED, TGU_SimpleList.SetControlMovable)
         CALLBACK_MANAGER:RegisterCallback(TAO_HUD_HIDDEN_STATE_CHANGED, TGU_SimpleList.SetControlHidden)
@@ -317,5 +317,5 @@ function TGU_SimpleList.Initialize(logger, isMocked)

     CALLBACK_MANAGER:RegisterCallback(TGU_STYLE_CHANGED, TGU_SimpleList.SetControlActive)
     CALLBACK_MANAGER:RegisterCallback(TGU_IS_ZONE_CHANGED, TGU_SimpleList.SetControlActive)
-    CALLBACK_MANAGER:RegisterCallback(TGU_UNIT_GROUPED_CHANGED, TGU_SimpleList.SetControlActive)
+    CALLBACK_MANAGER:RegisterCallback(TAO_UNIT_GROUPED_CHANGED, TGU_SimpleList.SetControlActive)
 end
\ No newline at end of file
diff --git a/TaosGroupTools/ui/SwimlaneList.lua b/TaosGroupTools/ui/SwimlaneList.lua
index cf3eabf..ee959d6 100644
--- a/TaosGroupTools/ui/SwimlaneList.lua
+++ b/TaosGroupTools/ui/SwimlaneList.lua
@@ -304,7 +304,7 @@ function TGU_SwimlaneList.SetControlHidden(isHidden)
         _logger:logDebug("isHidden", isHidden)
     end

-    if (TGU_GroupHandler.IsGrouped) then
+    if (GetIsUnitGrouped()) then
         _control:SetHidden(isHidden)
     else
         _control:SetHidden(true)
@@ -328,7 +328,7 @@ function TGU_SwimlaneList.SetControlActive()
         -- Start timeout timer
 	    EVENT_MANAGER:UnregisterForUpdate(TGU_SwimlaneList.Name)

-        CALLBACK_MANAGER:UnregisterCallback(TGU_GROUP_CHANGED, TGU_SwimlaneList.RefreshList)
+        CALLBACK_MANAGER:UnregisterCallback(TAO_GROUP_CHANGED, TGU_SwimlaneList.RefreshList)
         CALLBACK_MANAGER:UnregisterCallback(TGU_PLAYER_DATA_CHANGED, TGU_SwimlaneList.UpdatePlayer)
         CALLBACK_MANAGER:UnregisterCallback(TGU_MOVABLE_CHANGED, TGU_SwimlaneList.SetControlMovable)
         CALLBACK_MANAGER:UnregisterCallback(TGU_SWIMLANE_ULTIMATE_GROUP_ID_CHANGED, TGU_SwimlaneList.SetSwimlaneUltimate)
@@ -340,7 +340,7 @@ function TGU_SwimlaneList.SetControlActive()
         -- Start timeout timer
 	    EVENT_MANAGER:RegisterForUpdate(TGU_SwimlaneList.Name, REFRESHRATE, TGU_SwimlaneList.RefreshList)

-        CALLBACK_MANAGER:RegisterCallback(TGU_GROUP_CHANGED, TGU_SwimlaneList.RefreshList)
+        CALLBACK_MANAGER:RegisterCallback(TAO_GROUP_CHANGED, TGU_SwimlaneList.RefreshList)
         CALLBACK_MANAGER:RegisterCallback(TGU_PLAYER_DATA_CHANGED, TGU_SwimlaneList.UpdatePlayer)
         CALLBACK_MANAGER:RegisterCallback(TGU_MOVABLE_CHANGED, TGU_SwimlaneList.SetControlMovable)
         CALLBACK_MANAGER:RegisterCallback(TGU_SWIMLANE_ULTIMATE_GROUP_ID_CHANGED, TGU_SwimlaneList.SetSwimlaneUltimate)
@@ -497,5 +497,5 @@ function TGU_SwimlaneList.Initialize(logger, isMocked)

     CALLBACK_MANAGER:RegisterCallback(TGU_STYLE_CHANGED, TGU_SwimlaneList.SetControlActive)
     CALLBACK_MANAGER:RegisterCallback(TGU_IS_ZONE_CHANGED, TGU_SwimlaneList.SetControlActive)
-    CALLBACK_MANAGER:RegisterCallback(TGU_UNIT_GROUPED_CHANGED, TGU_SwimlaneList.SetControlActive)
+    CALLBACK_MANAGER:RegisterCallback(TAO_UNIT_GROUPED_CHANGED, TGU_SwimlaneList.SetControlActive)
 end
\ No newline at end of file
diff --git a/TaosGroupTools/util/GroupHelper.lua b/TaosGroupTools/util/GroupHelper.lua
new file mode 100644
index 0000000..ed9a962
--- /dev/null
+++ b/TaosGroupTools/util/GroupHelper.lua
@@ -0,0 +1,66 @@
+--[[
+	Addon: util
+	Author: TProg Taonnor
+	Created by @Taonnor
+]]--
+
+--[[
+	Class definition (Static class)
+]]--
+-- A table in hole lua workspace must be unique
+-- The group helper is global util table, used in several of my addons
+-- The table is created as "static" class without constructor and static helper methods
+if (TaosGroupHelper == nil) then
+	TaosGroupHelper = {}
+	TaosGroupHelper.__index = TaosGroupHelper
+
+    -- Global Callback Variables
+    TAO_GROUP_CHANGED = "TAO-GroupChanged"
+    TAO_UNIT_GROUPED_CHANGED = "TAO-UnitGroupedChanged"
+
+	-- local members
+    local name = "TaosGroupHelper"
+    local isUnitGrouped = IsUnitGrouped("player") -- Initial state
+
+    --[[
+        GetIsUnitGrouped Gets the current UnitGrouped state
+    ]]--
+    function GetIsUnitGrouped()
+        return isUnitGrouped
+    end
+
+    --[[
+	    Called when group member joined group
+    ]]--
+    local function OnGroupMemberJoined()
+        CALLBACK_MANAGER:FireCallbacks(TAO_GROUP_CHANGED)
+        OnGroupUpdate()
+    end
+
+    --[[
+	    Called when group member left group
+    ]]--
+    local function OnGroupMemberLeft()
+        CALLBACK_MANAGER:FireCallbacks(TAO_GROUP_CHANGED)
+        OnGroupUpdate()
+    end
+
+    --[[
+	    Called when group updated
+    ]]--
+    local function OnGroupUpdate()
+        local isGrouped = IsUnitGrouped("player")
+
+        if (isGrouped ~= isUnitGrouped) then
+            isUnitGrouped = isGrouped
+            CALLBACK_MANAGER:FireCallbacks(TAO_UNIT_GROUPED_CHANGED, isGrouped)
+        end
+    end
+
+    --[[
+        Register events
+    ]]--
+	EVENT_MANAGER:RegisterForEvent(name, EVENT_GROUP_MEMBER_JOINED, OnGroupMemberJoined)
+	EVENT_MANAGER:RegisterForEvent(name, EVENT_GROUP_MEMBER_LEFT, OnGroupMemberLeft)
+
+end
\ No newline at end of file