Back to Home

ESO Lua File v101043

ingame/mail/gamepad/mailsend_gamepad.lua

[◄ back to folders ]
1
2
3
4
5
6
7
8
9
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
-- Settings.
local MAX_SEND_ATTACHMENTS = MAIL_MAX_ATTACHED_ITEMS
local EMPTY_ATTACHMENT_ICON = "EsoUI/Art/Mail/Gamepad/gp_mailMenu_attachItem.dds"
local SEND_GOLD_ICON = "EsoUI/Art/Mail/Gamepad/gp_mailMenu_sendGold.dds"
local REQUEST_GOLD_ICON = "EsoUI/Art/Mail/Gamepad/gp_mailMenu_requestGold.dds"
local ATTACHING_GOLD = "attach"
local REQUESTING_GOLD = "request"
-- For a given inventory item, return the attachment slot index, or nil if the item is not attached.
local function GetItemAttachedIndex(bagId, slotIndex)
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedBagId, queuedSlotIndex = GetQueuedItemAttachmentInfo(i)
        if queuedBagId == bagId and queuedSlotIndex == slotIndex then
            return i
        end
    end
    return nil -- Item not attached.
end
-- Returns whether the specified inventory item is attached.
local function IsItemAttached(bagId, slotIndex)
    if GetItemAttachedIndex(bagId, slotIndex) then
        return true
    else
        return false
    end
end
-- Returns the next open attachment slot index, or nil if all attachment slots are full.
local function GetNextOpenAttachIndex()
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedFromBag = GetQueuedItemAttachmentInfo(i)
        if queuedFromBag == 0 then -- Slot is open.
            return i
        end
    end
    return nil -- No open slots.
end
-- Returns whether there is any item attached.
local function IsAnyItemAttached(bagId, slotIndex)
    for i = 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedFromBag = GetQueuedItemAttachmentInfo(i)
        if queuedFromBag ~= 0 then -- Slot is filled.
            return true
        end
    end
    return false
end
-- Removes the item attached in the specified slot, behaving as design requested.
local function RemoveQueuedAttachment(attachmentIndex)
    RemoveQueuedItemAttachment(attachmentIndex)
    for i = attachmentIndex + 1, MAIL_MAX_ATTACHED_ITEMS do
        local queuedBagId, queuedSlotIndex = GetQueuedItemAttachmentInfo(i)
        if queuedBagId ~= 0 then -- Slot is filled.
            RemoveQueuedItemAttachment(i)
            QueueItemAttachment(queuedBagId, queuedSlotIndex, i-1)
        end 
    end
end
-- Returns the proper platform-dependent text for the "To" field
local function GetDefaultAddresseeText()
    local addresseeText
    if IsConsoleUI() then
        addresseeText = zo_strformat(GetString(SI_GAMEPAD_MAIL_DEFAULT_ADDRESSEE), ZO_GetPlatformAccountLabel())
    else
        addresseeText = GetString(SI_REQUEST_NAME_DEFAULT_TEXT)
    end
    return addresseeText
end
-- The main class.
ZO_MailSend_Gamepad = ZO_InitializingObject:Subclass()
function ZO_MailSend_Gamepad:Initialize(control)
    self.control = control
    self.sendControl = self.control:GetNamedChild("Send")
end
function ZO_MailSend_Gamepad:OnShowing()
    local TRIGGER_CALLBACK = true
    self.inventoryList:RefreshList(TRIGGER_CALLBACK)
    self:EnterOutbox()
    ZO_MailSend_Shared.RestorePendingMail(self)
    if self.initialContact or self.initialSubject then
        self:ClearFields()
        self.mailView:Display(nil, nil, self.initialContact, self.initialSubject)
        self.initialContact = nil
        self.initialSubject = nil
    end
    if self.initialBodyInsertText then
        self.mailView.bodyEdit.edit:InsertText(self.initialBodyInsertText)
        self.initialBodyInsertText = nil
    end
end
function ZO_MailSend_Gamepad:OnHidden()
    self:Reset()
    MAIL_GAMEPAD:DeactivateTextSearch()
end
function ZO_MailSend_Gamepad:PerformDeferredInitialization()
    if self.initialized then return end
    self.initialized = true
end
function ZO_MailSend_Gamepad:InitializeControls()
    -- Sending Mail
    self.loadingBox = self.control:GetNamedChild("Loading")
    self.loadingLabel = self.loadingBox:GetNamedChild("ContainerText")
    -- Mail View
    self.mailView = self.sendControl:GetNamedChild("RightPane"):GetNamedChild("Container"):GetNamedChild("MailView")
    local IS_OUTBOX = true
    self.mailView:Initialize(GetString(SI_GAMEPAD_MAIL_SEND_TO), EMPTY_ATTACHMENT_ICON, IS_OUTBOX, ZO_MAIL_COD_MONEY_OPTIONS_GAMEPAD, ZO_MAIL_ATTACHED_MONEY_OPTIONS_GAMEPAD, MAX_SEND_ATTACHMENTS)
    self.mailView:Clear()
    self.mailView.subjectEdit.edit:SetMaxInputChars(MAIL_MAX_SUBJECT_CHARACTERS)
    self.mailView.bodyEdit.edit:SetMaxInputChars(MAIL_MAX_BODY_CHARACTERS)
    -- Gold Slider
    self.goldSliderControl = self.sendControl:GetNamedChild("GoldSliderBox")
    self.goldSlider = ZO_CurrencySelector_Gamepad:New(self.goldSliderControl:GetNamedChild("Selector"))
    self.goldSlider:SetCurrencyType(CURT_MONEY)
    self.goldSlider:SetClampValues(true)
    self.goldSlider:RegisterCallback("OnValueChanged", function() MAIL_GAMEPAD:RefreshKeybind() end)
end
function ZO_MailSend_Gamepad:InitializeFragment()
    GAMEPAD_MAIL_SEND_FRAGMENT = ZO_FadeSceneFragment:New(ZO_Mail_Gamepad_TopLevelSend)
    GAMEPAD_MAIL_SEND_FRAGMENT:RegisterCallback("StateChange", function(oldState, newState)
        if newState == SCENE_FRAGMENT_SHOWING then
            self:OnShowing()
        elseif newState == SCENE_SHOWN then
            if self.pendingMailChanged then
                ZO_Dialogs_ShowGamepadDialog("MAIL_ATTACHMENTS_CHANGED")
                self.pendingMailChanged = nil
            end
        elseif newState == SCENE_FRAGMENT_HIDDEN then
            ZO_MailSend_Shared.SavePendingMail()
            self:OnHidden()
        end
    end)
end
function ZO_MailSend_Gamepad:ConnectShownEvents()
    self.control:RegisterForEvent(EVENT_MAIL_COD_CHANGED, function() self:UpdateMoneyAttachment() end)
    self.control:RegisterForEvent(EVENT_MAIL_ATTACHED_MONEY_CHANGED, function() self:UpdateMoneyAttachment() end)
    self.control:RegisterForEvent(EVENT_MONEY_UPDATE, function() self:UpdatePostageMoney() end)
    local function HandleInventoryChanged()
        self:PopulateMainList()
    end
    self.control:RegisterForEvent(EVENT_INVENTORY_FULL_UPDATE, HandleInventoryChanged)
    self.control:RegisterForEvent(EVENT_INVENTORY_SINGLE_SLOT_UPDATE, HandleInventoryChanged)
end
function ZO_MailSend_Gamepad:DisconnectShownEvent()
    self.control:UnregisterForEvent(EVENT_MAIL_COD_CHANGED)
    self.control:UnregisterForEvent(EVENT_MAIL_ATTACHED_MONEY_CHANGED)
    self.control:UnregisterForEvent(EVENT_MONEY_UPDATE)
    self.control:UnregisterForEvent(EVENT_INVENTORY_FULL_UPDATE)
    self.control:UnregisterForEvent(EVENT_INVENTORY_SINGLE_SLOT_UPDATE)
end
function ZO_MailSend_Gamepad:InitializeEvents()
    self.control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_ADDED, function(_, attachSlot) self:OnMailAttachmentAdded(attachSlot) end)
    self.control:RegisterForEvent(EVENT_MAIL_ATTACHMENT_REMOVED, function(_, attachSlot) self:OnMailAttachmentRemoved(attachSlot) end)
    self.control:RegisterForEvent(EVENT_MAIL_SEND_SUCCESS, function() self:OnMailSendSuccess() end)
    self.control:RegisterForEvent(EVENT_MAIL_SEND_FAILED, function(_, ...) self:OnMailSendFailure(...) end)
end
--Global API
function ZO_MailSend_Gamepad:ComposeMailTo(address, subject)
    self.initialContact = address
    self.initialSubject = subject
    local PUSH_SCENE = true
    MAIL_GAMEPAD:ShowTab(ZO_MAIL_TAB_INDEX.SEND, PUSH_SCENE)
end
function ZO_MailSend_Gamepad:InsertBodyText(text)
    self.initialBodyInsertText = text
    local PUSH_SCENE = true
    MAIL_GAMEPAD:ShowTab(ZO_MAIL_TAB_INDEX.SEND, PUSH_SCENE)
end
function ZO_MailSend_Gamepad:IsMailValid()
    local to = self.mailView:GetAddress()
    if (not to) or (to == "") then
        return false
    end
    local subject = self.mailView:GetSubject()
    local hasSubject = subject and (subject ~= "")
    local body = self.mailView:GetBody()
    local hasBody = body and (body ~= "")
    return hasSubject or hasBody or (GetQueuedMoneyAttachment() > 0) or IsAnyItemAttached()
end
function ZO_MailSend_Gamepad:InitializeKeybindDescriptors()
    -- Main list.
    self.mainKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Back
        KEYBIND_STRIP:GetDefaultGamepadBackButtonDescriptor(),
        -- Select
        {
            name = GetString(SI_GAMEPAD_SELECT_OPTION),
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                local targetData = self.mainList:GetTargetData()
                if targetData and targetData.actionFunction then
                    targetData:actionFunction()
                end
            end,
            enabled = function()
                local isEnabled = true
                local targetData = self.mainList:GetTargetData()
                local validEntry = self:IsMailValid()
                if targetData and targetData.text == GetString(SI_MAIL_SEND_SEND) then
                    isEnabled = validEntry
                end
                for i = 1, self.mainList:GetNumItems() do
                    local itemData = self.mainList:GetDataForDataIndex(i)
                    if itemData.text and itemData.text == GetString(SI_MAIL_SEND_SEND) then
                        itemData.disabled = not validEntry
                        break
                    end
                end
                return isEnabled
            end,
        },
        {
            keybind = "UI_SHORTCUT_SECONDARY",
            name = function()
                local targetData = self.mainList:GetTargetData()
                return targetData.secondaryCallbackName
            end,
            callback = function()
                local targetData = self.mainList:GetTargetData()
                targetData:secondaryCallback()
            end,
            visible = function()
                local targetData = self.mainList:GetTargetData()
                return targetData and targetData.secondaryCallback ~= nil
            end,
        },
        -- Clear
        {
            name = GetString(SI_GAMEPAD_MAIL_SEND_CLEAR),
            keybind = "UI_SHORTCUT_TERTIARY",
            callback = function() ZO_Dialogs_ShowGamepadDialog("CONFIRM_CLEAR_MAIL_COMPOSE", { callback = function() self:ClearFields() end }) end
        },
    }
    ZO_Gamepad_AddListTriggerKeybindDescriptors(self.mainKeybindDescriptor, self.mainList)
    -- Slider Edit
    self.sliderKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Accept
        {
            name = GetString(SI_GAMEPAD_MAIL_SEND_ACCEPT_MONEY),
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                if self.goldMode == ATTACHING_GOLD then
                    QueueCOD(0)
                    QueueMoneyAttachment(self.goldSlider:GetValue())
                elseif self.goldMode == REQUESTING_GOLD then
                    QueueMoneyAttachment(0)
                    QueueCOD(self.goldSlider:GetValue())
                end
                PlaySound(SOUNDS.ITEM_MONEY_CHANGED)
                self:UpdatePostageMoney()
                self:EnterOutbox()
                local targetControl = self.mainList:GetTargetControl()
                targetControl:SetHidden(false)
            end,
            visible = function() return self.goldSlider:GetValue() <= self.goldSlider:GetMaxValue() end,
        },
        -- Cancel
        KEYBIND_STRIP:GenerateGamepadBackButtonDescriptor(function()
            self.mainList:WhenInactiveSetTargetControlHidden(true)
            self:UpdateMoneyAttachment()
            self:EnterOutbox()
        end),
    }
    -- Contacts List
    self.contactsKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Accept
        {
            name = GetString(SI_GAMEPAD_MAIL_SEND_ACCEPT_MONEY),
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                self:EnterOutbox()
                local selectedItem = self.contactsList:GetTargetData()
                if selectedItem.actionFunction then
                    selectedItem.actionFunction(selectedItem)
                end
                            
            end,
        },
        -- Cancel
        KEYBIND_STRIP:GenerateGamepadBackButtonDescriptor(function()
            self:UpdateMoneyAttachment()
            self:EnterOutbox()
        end),
    }
    ZO_Gamepad_AddListTriggerKeybindDescriptors(self.contactsKeybindDescriptor, self.contactsList)
    -- Inventory
    self.inventoryKeybindDescriptor =
    {
        alignment = KEYBIND_STRIP_ALIGN_LEFT,
        -- Add to Mail/Remove from Mail
        {
            name = function()
                local selectedItem = self.inventoryList:GetTargetData()
                if IsItemAttached(selectedItem.bagId, selectedItem.slotIndex) then
                    return GetString(SI_GAMEPAD_MAIL_SEND_DETACH_ITEM)
                else
                    return GetString(SI_GAMEPAD_MAIL_SEND_ATTACH_ITEM)
                end
            end,
            keybind = "UI_SHORTCUT_PRIMARY",
            callback = function()
                local selectedItem = self.inventoryList:GetTargetData()
                local bagId = selectedItem.bagId
                local slotIndex = selectedItem.slotIndex
                local attachedSlotIndex = GetItemAttachedIndex(bagId, slotIndex)
                if attachedSlotIndex then -- Item is attached, detach it.
                    RemoveQueuedAttachment(attachedSlotIndex)
                    local soundCategory = GetItemSoundCategory(bagId, slotIndex)
                    PlayItemSound(soundCategory, ITEM_SOUND_ACTION_UNEQUIP)
                else -- Item is not attached, attach it.
                    attachedSlotIndex = GetNextOpenAttachIndex()
                    if attachedSlotIndex then
                        QueueItemAttachment(bagId, slotIndex, attachedSlotIndex)
                        local soundCategory = GetItemSoundCategory(bagId, slotIndex)
                        PlayItemSound(soundCategory, ITEM_SOUND_ACTION_EQUIP)
                    end
                end
                SCREEN_NARRATION_MANAGER:QueueParametricListEntry(self.inventoryList.list)
            end,
            visible = function()
                local selectedItem = self.inventoryList:GetTargetData()
                if not selectedItem then
                    return false
                end
                local bagId = selectedItem.bagId
                local slotIndex = selectedItem.slotIndex
                if IsItemAttached(bagId, slotIndex) then
                    return true -- Can always remove an attached item.
                end
                local attachedSlotIndex = GetNextOpenAttachIndex()
                if not attachedSlotIndex then
                    return false
                end
                local canAttach = CanQueueItemAttachment(bagId, slotIndex, attachedSlotIndex)
                return canAttach
            end,
        },
        -- Back
        KEYBIND_STRIP:GenerateGamepadBackButtonDescriptor(function()
            self:OnInventoryListBackButtonClicked()
        end),
    }
end
function ZO_MailSend_Gamepad:OnInventoryListBackButtonClicked()
    self:EnterOutbox()
end
local function UpdatePlayerGold(control)
    ZO_CurrencyControl_SetSimpleCurrency(control, CURT_MONEY, GetCurrencyAmount(CURT_MONEY, CURRENCY_LOCATION_CHARACTER), ZO_MAIL_HEADER_MONEY_OPTIONS_GAMEPAD)
    return true
end
local function UpdatePostage(control)
    ZO_CurrencyControl_SetSimpleCurrency(control, CURT_MONEY, GetQueuedMailPostage(), ZO_MAIL_HEADER_MONEY_OPTIONS_GAMEPAD)
    return true
end
local function GetPostageNarration()
    return ZO_Currency_FormatGamepad(CURT_MONEY, GetQueuedMailPostage(), ZO_CURRENCY_FORMAT_AMOUNT_NAME)
end
function ZO_MailSend_Gamepad:InitializeHeader()
    self.mainHeaderData =
    {
        data1HeaderText = GetString(SI_GAMEPAD_MAIL_INBOX_PLAYER_GOLD),
        data1Text = UpdatePlayerGold,
        data2HeaderText = GetString(SI_GAMEPAD_MAIL_SEND_POSTAGE_LABEL),
        data2Text = UpdatePostage,
        tabBarEntries = MAIL_GAMEPAD.tabBarEntries,
    }
    self.setFieldHeaderData =
    {
        data1HeaderText = GetString(SI_GAMEPAD_MAIL_INBOX_PLAYER_GOLD),
        data1Text = UpdatePlayerGold,
        data2HeaderText = GetString(SI_GAMEPAD_MAIL_SEND_POSTAGE_LABEL),
        data2Text = UpdatePostage,
    }
end
local function InventorySetupFunction(entryData)
    entryData.isMailAttached = IsItemAttached(entryData.bagId, entryData.slotIndex)
    entryData:SetIgnoreTraitInformation(true)
end
local function ItemFilterFunction(entryData)
    local bagId = entryData.bagId
    local slotIndex = entryData.slotIndex
    return (CanQueueItemAttachment(bagId, slotIndex) or IsItemAttached(bagId, slotIndex)) and not entryData.isPlayerLocked
end
local SETUP_LOCALLY = true
function ZO_MailSend_Gamepad:InitializeInventoryList()
    local function OnRefreshList(list)
        if list:GetNumItems() == 0 then
            MAIL_GAMEPAD:RequestEnterHeader()
        else
            MAIL_GAMEPAD:RequestLeaveHeader()
        end
    end
    self.inventoryList = MAIL_GAMEPAD:AddList("Inventory", SETUP_LOCALLY, ZO_GamepadInventoryList, BAG_BACKPACK, SLOT_TYPE_ITEM, function(...) self:InventorySelectionChanged(...) end, InventorySetupFunction)
    self.inventoryList:SetSearchContext("mailTextSearch")
    self.inventoryList:SetNoItemText(GetString(SI_GAMEPAD_INVENTORY_EMPTY))
    MAIL_GAMEPAD:SetOnBackButtonCallback(function() self:OnInventoryListBackButtonClicked() end)
    MAIL_GAMEPAD:SetTextSearchEntryHidden(true)
    self.inventoryListControl = self.inventoryList:GetControl()
end
function ZO_MailSend_Gamepad:AddMainListEntry(text, header, icon, callback, secondaryCallbackName, secondaryCallback, narrationText)
    local newEntry = ZO_GamepadEntryData:New(text, icon)
    newEntry.actionFunction = callback
    newEntry.secondaryCallbackName = secondaryCallbackName
    newEntry.secondaryCallback = secondaryCallback
    newEntry.narrationText = narrationText
    local template
    if header then
        newEntry:SetHeader(header)
        template = "ZO_GamepadMenuEntryTemplateWithHeader"
    else
        template = "ZO_GamepadMenuEntryTemplate"
    end
    newEntry:SetIconTintOnSelection(true)
    self.mainList:AddEntry(template, newEntry)
end
function ZO_MailSend_Gamepad:PopulateMainList()
    local function RefreshKeybind()
        MAIL_GAMEPAD:RefreshKeybind()
    end
    self.mainList:Clear()
    self.onUserListDialogIdSelectedForMailTo = function(hasResult, displayName, consoleId)
        local editControl = self.mailView.addressEdit.edit
        if hasResult then
            editControl:SetText(displayName)
        end
    end
    local NO_HEADER = nil
    local NO_ICON = nil
    local NO_SECONDARY_CALLBACK_NAME = nil
    local NO_SECONDARY_CALLBACK = nil
    do
        local userListCallback = function()
            local INCLUDE_ONLINE_FRIENDS = true
            local INCLUDE_OFFLINE_FRIENDS = true
            PLAYER_CONSOLE_INFO_REQUEST_MANAGER:RequestIdFromUserListDialog(self.onUserListDialogIdSelectedForMailTo, GetString(SI_GAMEPAD_CONSOLE_SELECT_FOR_MAIL), INCLUDE_ONLINE_FRIENDS, INCLUDE_OFFLINE_FRIENDS)
        end
        local editBoxCallback = function()
            self.mailView.addressEdit.edit:TakeFocus()
        end
        local mailSendNarrationText = function(entryData, entryControl)
            return ZO_FormatEditBoxNarrationText(self.mailView.addressEdit.edit, entryData.text)
        end
        if ZO_IsPlaystationPlatform() then
            self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_TO), NO_HEADER, NO_ICON, userListCallback, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailSendNarrationText)
        elseif GetUIPlatform() == UI_PLATFORM_XBOX then
            if GetNumberConsoleFriends() > 0 then
                self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_TO), NO_HEADER, NO_ICON, editBoxCallback, GetString(SI_GAMEPAD_CONSOLE_CHOOSE_FRIEND), userListCallback, mailSendNarrationText)
            else
                self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_TO), NO_HEADER, NO_ICON, editBoxCallback, GetString(SI_GAMEPAD_CONSOLE_CHOOSE_FRIEND), NO_SECONDARY_CALLBACK, mailSendNarrationText)
            end
        else
            self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_TO), NO_HEADER, NO_ICON, editBoxCallback, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailSendNarrationText)
        end
    end
    local mailSubjectNarrationText = function(entryData, entryControl)
        return ZO_FormatEditBoxNarrationText(self.mailView.subjectEdit.edit, entryData.text)
    end
    local mailBodyNarrationText = function(entryData, entryControl)
        return ZO_FormatEditBoxNarrationText(self.mailView.bodyEdit.edit, entryData.text)
    end
    local mailGoldNarrationText = function(entryData, entryControl)
        local narrations = {}
        local queuedCOD = GetQueuedCOD()
        local queuedMoney = GetQueuedMoneyAttachment()
        local moneyHeader
        local moneyText
        if queuedMoney > 0 then
            moneyHeader = GetString(SI_MAIL_READ_SENT_GOLD_LABEL)
            moneyText = ZO_Currency_FormatGamepad(CURT_MONEY, queuedMoney, ZO_CURRENCY_FORMAT_AMOUNT_NAME)
        elseif queuedCOD > 0 then
            moneyHeader = GetString(SI_MAIL_READ_COD_LABEL)
            moneyText = ZO_Currency_FormatGamepad(CURT_MONEY, queuedCOD, ZO_CURRENCY_FORMAT_AMOUNT_NAME)
        else
            moneyHeader = GetString(SI_MAIL_READ_SENT_GOLD_LABEL)
            moneyText = GetString(SI_GAMEPAD_MAIL_INBOX_NO_ATTACHED_GOLD)
        end
        ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(entryData.text))
        ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(moneyHeader))
        ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(moneyText))
        return narrations
    end
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SUBJECT_LABEL), NO_HEADER, NO_ICON, function() self.mailView.subjectEdit.edit:TakeFocus() end, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailSubjectNarrationText)
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_BODY_LABEL), NO_HEADER, NO_ICON, function() self.mailView.bodyEdit.edit:TakeFocus() end, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailBodyNarrationText)
    self:AddMainListEntry(GetString(SI_MAIL_SEND_ATTACH_MONEY), GetString(SI_GAMEPAD_MAIL_SEND_GOLD_HEADER), SEND_GOLD_ICON, function() self:ShowSliderControl(ATTACHING_GOLD, GetQueuedMoneyAttachment(), GetCurrencyAmount(CURT_MONEY, CURRENCY_LOCATION_CHARACTER)) end, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailGoldNarrationText)
    self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_COD), NO_HEADER, REQUEST_GOLD_ICON, function() self:ShowSliderControl(REQUESTING_GOLD, GetQueuedCOD(), MAX_PLAYER_CURRENCY) end, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailGoldNarrationText)
    self.mailView.subjectEdit.edit:SetHandler("OnFocusLost", function(editBox)
        RefreshKeybind()
        ZO_GamepadEditBox_FocusLost(editBox)
        --Re-narrate the edit box when we are done typing in it
        SCREEN_NARRATION_MANAGER:QueueParametricListEntry(MAIL_GAMEPAD:GetCurrentList())
    end)
    self.mailView.addressEdit.edit:SetHandler("OnFocusLost", function(editBox)
        RefreshKeybind()
        ZO_GamepadEditBox_FocusLost(editBox)
        --Re-narrate the edit box when we are done typing in it
        SCREEN_NARRATION_MANAGER:QueueParametricListEntry(MAIL_GAMEPAD:GetCurrentList())
    end)
    self.mailView.bodyEdit.edit:SetHandler("OnFocusLost", function(editBox)
        RefreshKeybind()
        ZO_GamepadEditBox_FocusLost(editBox)
        --Re-narrate the edit box when we are done typing in it
        SCREEN_NARRATION_MANAGER:QueueParametricListEntry(MAIL_GAMEPAD:GetCurrentList())
    end)
    if not self.inventoryList:IsEmpty() then
        local mailAttachedItemsNarrationText = function(entryData, entryControl)
            local narrations = {}
            ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(entryData.text))
            local totalAttachments = 0
            for i = 1, MAIL_MAX_ATTACHED_ITEMS do
                local queuedFromBag, slotIndex, icon, stack = GetQueuedItemAttachmentInfo(i)
                if queuedFromBag ~= 0 then -- Slot is filled.
                    totalAttachments = totalAttachments + stack
                else
                    break
                end
            end
            ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(GetString(SI_MAIL_ATTACHMENTS_HEADER)))
            if totalAttachments > 0 then
                ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(totalAttachments))
            else
                ZO_AppendNarration(narrations, SCREEN_NARRATION_MANAGER:CreateNarratableObject(GetString(SI_GAMEPAD_MAIL_INBOX_NO_ATTACHMENTS)))
            end
            return narrations
        end
        self:AddMainListEntry(GetString(SI_GAMEPAD_MAIL_SEND_ATTACH), GetString(SI_GAMEPAD_MAIL_SEND_ITEMS_HEADER), EMPTY_ATTACHMENT_ICON, function() self:EnterInventoryList() end, NO_SECONDARY_CALLBACK_NAME, NO_SECONDARY_CALLBACK, mailAttachedItemsNarrationText)
    end
    local function AttemptSendMail()
        local to = self.mailView:GetAddress()
        local subject = self.mailView:GetSubject()
        local body = self.mailView:GetBody()
        if IsConsoleUI() then
            ZO_ConsoleAttemptCommunicateOrError(function(success)
                if success then
                    SendMail(to, subject, body)
                    self:EnterSending()
                end
            end, to, ZO_PLAYER_CONSOLE_INFO_REQUEST_BLOCK, ZO_CONSOLE_CAN_COMMUNICATE_ERROR_DIALOG, ZO_ID_REQUEST_TYPE_DISPLAY_NAME, to)
        else
            SendMail(to, subject, body)
            self:EnterSending()
        end
    end
    self:AddMainListEntry(GetString(SI_MAIL_SEND_SEND), GetString(SI_MAIL_SEND_SEND), ZO_GAMEPAD_SUBMIT_ENTRY_ICON, AttemptSendMail)
    self.mainList:Commit()
end
function ZO_MailSend_Gamepad:InitializeMainList()
    self.mainList = MAIL_GAMEPAD:GetMainList()
    self.mainList:SetOnSelectedDataChangedCallback(function(...) self:OnListMovement(...) end)
end
function ZO_MailSend_Gamepad:OnListMovement(list, isMoving)
    self.mailView.addressEdit.edit:LoseFocus()
    self.mailView.subjectEdit.edit:LoseFocus()
    self.mailView.bodyEdit.edit:LoseFocus()
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.mainKeybindDescriptor)
end
function ZO_MailSend_Gamepad:HighlightActiveTextField()
    local textFieldToControl =
    {
        [GetString(SI_GAMEPAD_MAIL_SEND_TO)] = self.mailView.addressEdit,
        [GetString(SI_GAMEPAD_MAIL_SUBJECT_LABEL)] = self.mailView.subjectEdit,
        [GetString(SI_GAMEPAD_MAIL_BODY_LABEL)] = self.mailView.bodyEdit
    }
    for _, control in pairs(textFieldToControl) do
        control.highlight:SetHidden(true)
    end
    local textField = self.mainList:GetTargetData().text
    local currentEditControl = textFieldToControl[textField]
    if currentEditControl then
        currentEditControl.highlight:SetHidden(false)
    end
    KEYBIND_STRIP:UpdateKeybindButtonGroup(self.mainKeybindDescriptor)
end
function ZO_MailSend_Gamepad:InitializeContactsList()
    self.contactsList = MAIL_GAMEPAD:AddList("Contacts")
    self.contactsListControl = self.contactsList:GetControl()
end
function ZO_MailSend_Gamepad:ClearFields()
    self:Clear()
    self:EnterOutbox()
end
function ZO_MailSend_Gamepad:Reset()
    if (not self.inventoryListControl:IsHidden()) or (not self.goldSliderControl:IsHidden()) or (not self.contactsListControl:IsHidden()) then
        PlaySound(SOUNDS.GAMEPAD_MENU_BACK)
    end
    self.goldSliderControl:SetHidden(true)
    self.mainList:WhenInactiveSetTargetControlHidden(false)
    GAMEPAD_TOOLTIPS:ClearTooltip(GAMEPAD_LEFT_TOOLTIP)
    self.loadingBox:SetHidden(true)
    self.goldSlider:Deactivate()
    self.goldMode = nil
    self.inSendMode = false
    MAIL_GAMEPAD:RequestLeaveHeader()
end
function ZO_MailSend_Gamepad:SwitchToSendTab()
    MAIL_GAMEPAD:SwitchToHeader(self.mainHeaderData, ZO_MAIL_TAB_INDEX.SEND)
end
function ZO_MailSend_Gamepad:EnterSending()
    self.inSendMode = true
    self:Reset()
    MAIL_GAMEPAD:SwitchToKeybind(nil) -- Remove keybinds as they are invaild when sending.
    self.loadingLabel:SetText(GetString(SI_GAMEPAD_MAIL_SEND_SENDING))
    self.loadingBox:SetHidden(false)
    MAIL_GAMEPAD:DeactivateTextSearch()
    MAIL_GAMEPAD:SetCurrentList(nil)
    self:EnableMailEditboxes(false)
end
function ZO_MailSend_Gamepad:EnterOutbox()
    self:Reset()
    if self.inSendMode then
        self:EnterSending()
    else
        self:SwitchToSendTab()
        MAIL_GAMEPAD:DeactivateTextSearch()
        MAIL_GAMEPAD:SetCurrentList(self.mainList)
        MAIL_GAMEPAD:SwitchToKeybind(self.mainKeybindDescriptor)
        self:EnableMailEditboxes(true)
    end
end
function ZO_MailSend_Gamepad:EnableMailEditboxes(enabled)
    self.mailView.addressEdit.edit:SetMouseEnabled(enabled)
    self.mailView.subjectEdit.edit:SetMouseEnabled(enabled)
    self.mailView.bodyEdit.edit:SetMouseEnabled(enabled)
end
function ZO_MailSend_Gamepad:AddContact(text, header, callback)
    local newEntry = ZO_GamepadEntryData:New(text)
    newEntry.actionFunction = callback
    local template
    if header then
        newEntry:SetHeader(header)
        template = "ZO_GamepadMenuEntryTemplateWithHeader"
    else
        template = "ZO_GamepadMenuEntryTemplate"
    end
    self.contactsList:AddEntry(template, newEntry)
end
local RECIPIENT_HEADER_TEXT = GetString(SI_GAMEPAD_MAIL_SEND_RECIPIENT)
function ZO_MailSend_Gamepad:EnterContactsList()
    self.contactsList:Clear()
    -- Text edit
    self:AddContact(GetString(SI_GAMEPAD_MAIL_SEND_ENTER_NAME), nil, function() self.mailView.addressEdit.edit:TakeFocus() end)
    self.contactsList:Commit()
    self.setFieldHeaderData.titleText = RECIPIENT_HEADER_TEXT
    MAIL_GAMEPAD:SwitchToHeader(self.setFieldHeaderData)
    MAIL_GAMEPAD:SwitchToKeybind(self.contactsKeybindDescriptor)
    MAIL_GAMEPAD:DeactivateTextSearch()
    MAIL_GAMEPAD:SetCurrentList(self.contactsList)
    self:EnableMailEditboxes(true)
    PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
end
function ZO_MailSend_Gamepad:ShowSliderControl(mode, value, maxValue)
    MAIL_GAMEPAD:DeactivateCurrentList()
    self.goldSlider:SetMaxValue(maxValue)
    self.goldSlider:SetValue(value)
    MAIL_GAMEPAD:SwitchToKeybind(self.sliderKeybindDescriptor)
    self.goldSlider:Activate()
    self.goldMode = mode
    self.goldSliderControl:SetHidden(false)
    PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
end
local ATTACHMENT_HEADER_TEXT = GetString(SI_GAMEPAD_MAIL_SEND_ATTACH)
function ZO_MailSend_Gamepad:EnterInventoryList()
    self:Reset()
    self.setFieldHeaderData.titleText = ATTACHMENT_HEADER_TEXT
    MAIL_GAMEPAD:SwitchToHeader(self.setFieldHeaderData)
    MAIL_GAMEPAD:SwitchToKeybind(self.inventoryKeybindDescriptor)
    MAIL_GAMEPAD:SetCurrentList(self.inventoryList)
    MAIL_GAMEPAD:ActivateTextSearch()
    self:EnableMailEditboxes(true)
    if self.inventoryList and self.inventoryList:GetNumItems() == 0 then
        MAIL_GAMEPAD:RequestEnterHeader()
    end
    PlaySound(SOUNDS.GAMEPAD_MENU_FORWARD)
end
function ZO_MailSend_Gamepad:InventorySelectionChanged(list, selectedData)
    if MAIL_GAMEPAD:GetCurrentList() == self.inventoryList then
        GAMEPAD_TOOLTIPS:ClearLines(GAMEPAD_LEFT_TOOLTIP)
        if selectedData then
            GAMEPAD_TOOLTIPS:LayoutBagItem(GAMEPAD_LEFT_TOOLTIP, selectedData.bagId, selectedData.slotIndex)
        end
        MAIL_GAMEPAD:RefreshKeybind()
    end
end
function ZO_MailSend_Gamepad:Clear()
    ClearQueuedMail()
    self.mailView:Clear()
end
function ZO_MailSend_Gamepad:UpdateMoneyAttachment()
    local queuedCOD = GetQueuedCOD()
    local queuedMoney = GetQueuedMoneyAttachment()
    self.mailView:Display(queuedCOD, queuedMoney)
end
function ZO_MailSend_Gamepad:IsAttachingItems()
    return not self.sendControl:IsHidden()
end
function ZO_MailSend_Gamepad:UpdatePostageMoney()
    MAIL_GAMEPAD:RefreshHeader()
end
function ZO_MailSend_Gamepad:OnMailAttachmentAdded(attachSlot)
    local _, _, icon, stack = GetQueuedItemAttachmentInfo(attachSlot)
    self.mailView:SetAttachment(attachSlot, stack, icon)
    MAIL_GAMEPAD:RefreshKeybind()
    self.inventoryList:RefreshList()
end
function ZO_MailSend_Gamepad:OnMailAttachmentRemoved(attachSlot)
    self.mailView:ClearAttachment(attachSlot)
    MAIL_GAMEPAD:RefreshKeybind()
    self.inventoryList:RefreshList()
end
function ZO_MailSend_Gamepad:OnMailSendSuccess()
    PlaySound(SOUNDS.MAIL_SENT)
    self.inSendMode = false
    self:Clear()
    if not self.sendControl:IsHidden() then
        self:EnterOutbox()
    end
end
function ZO_MailSend_Gamepad:OnMailSendFailure(failureReason)
    self.inSendMode = false
    if not self.sendControl:IsHidden() then
        self:EnterOutbox()
    end
end
-- XML Handlers
    control.addressEdit.edit:SetDefaultText(GetDefaultAddresseeText())
    control.subjectEdit.edit:SetDefaultText(GetString(SI_MAIL_SUBJECT_DEFAULT_TEXT))
end