| | 1 | | using MainScripts.DCL.Helpers.Utils; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using TMPro; |
| | 5 | | using UIComponents.Scripts.Components; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | | using Object = UnityEngine.Object; |
| | 9 | |
|
| | 10 | | namespace DCL.Quests |
| | 11 | | { |
| | 12 | | public class QuestOfferComponentView : BaseComponentView<QuestOfferComponentModel>, IQuestOfferComponentView |
| | 13 | | { |
| | 14 | | private const int MAX_REWARDS_COUNT = 3; |
| | 15 | |
|
| | 16 | | [SerializeField] internal GameObject rewardsSection; |
| | 17 | | [SerializeField] internal GameObject guestSection; |
| | 18 | | [SerializeField] internal TMP_Text questTitle; |
| | 19 | | [SerializeField] internal TMP_Text questDescription; |
| | 20 | | [SerializeField] internal Button acceptButton; |
| | 21 | | [SerializeField] internal Button cancelButton; |
| | 22 | | [SerializeField] internal Transform rewardsContainer; |
| | 23 | |
|
| | 24 | | [SerializeField] internal QuestRewardComponentView rewardPrefab; |
| | 25 | | public event Action<string> OnQuestAccepted; |
| | 26 | |
|
| | 27 | | private UnityObjectPool<QuestRewardComponentView> rewardsPool; |
| 7 | 28 | | private List<QuestRewardComponentView> usedRewards = new (); |
| | 29 | |
|
| | 30 | | public override void Awake() |
| | 31 | | { |
| 6 | 32 | | acceptButton.onClick.RemoveAllListeners(); |
| 6 | 33 | | acceptButton.onClick.AddListener(()=>OnQuestAccepted?.Invoke(model.questId)); |
| 6 | 34 | | rewardsPool = new UnityObjectPool<QuestRewardComponentView>(rewardPrefab, rewardsContainer); |
| 6 | 35 | | rewardsPool.Prewarm(MAX_REWARDS_COUNT); |
| 6 | 36 | | } |
| | 37 | |
|
| | 38 | | public override void Dispose() |
| | 39 | | { |
| 12 | 40 | | base.Dispose(); |
| | 41 | |
|
| 32 | 42 | | foreach (var pooledReward in usedRewards) |
| 4 | 43 | | rewardsPool.Release(pooledReward); |
| 12 | 44 | | rewardsPool.Clear(); |
| 12 | 45 | | } |
| | 46 | |
|
| | 47 | | public override void RefreshControl() |
| | 48 | | { |
| 0 | 49 | | SetQuestId(model.questId); |
| 0 | 50 | | SetQuestTitle(model.title); |
| 0 | 51 | | SetQuestDescription(model.description); |
| 0 | 52 | | SetRewards(model.rewardsList); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void SetIsGuest(bool isGuest) |
| | 56 | | { |
| 2 | 57 | | guestSection.SetActive(isGuest); |
| 2 | 58 | | } |
| | 59 | |
|
| | 60 | | public void SetQuestId(string questId) => |
| 0 | 61 | | model.questId = questId; |
| | 62 | |
|
| | 63 | | public void SetQuestTitle(string title) |
| | 64 | | { |
| 1 | 65 | | model.title = title; |
| 1 | 66 | | questTitle.text = title; |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | public void SetQuestDescription(string description) |
| | 70 | | { |
| 1 | 71 | | model.description = description; |
| 1 | 72 | | questDescription.text = description; |
| 1 | 73 | | } |
| | 74 | |
|
| | 75 | | public void SetRewards(List<QuestRewardComponentModel> rewardsList) |
| | 76 | | { |
| 2 | 77 | | model.rewardsList = rewardsList; |
| | 78 | |
|
| 16 | 79 | | for (var i = 0; i < rewardsContainer.childCount; i++) |
| 6 | 80 | | Destroy(rewardsContainer.GetChild(i)); |
| | 81 | |
|
| 2 | 82 | | if (rewardsList == null || rewardsList.Count == 0) |
| | 83 | | { |
| 1 | 84 | | rewardsSection.SetActive(false); |
| 1 | 85 | | return; |
| | 86 | | } |
| | 87 | |
|
| 1 | 88 | | rewardsSection.SetActive(true); |
| | 89 | |
|
| 2 | 90 | | foreach (var pooledReward in usedRewards) |
| 0 | 91 | | rewardsPool.Release(pooledReward); |
| | 92 | |
|
| 6 | 93 | | foreach (var rewardModel in rewardsList) |
| | 94 | | { |
| 2 | 95 | | QuestRewardComponentView pooledReward = rewardsPool.Get(); |
| 2 | 96 | | pooledReward.SetModel(rewardModel); |
| 2 | 97 | | usedRewards.Add(pooledReward); |
| | 98 | | } |
| 1 | 99 | | } |
| | 100 | | } |
| | 101 | | } |