diff --git a/nekoyume/Assets/_Scripts/State/States.cs b/nekoyume/Assets/_Scripts/State/States.cs index bf7e4236858..6a95627b546 100644 --- a/nekoyume/Assets/_Scripts/State/States.cs +++ b/nekoyume/Assets/_Scripts/State/States.cs @@ -686,6 +686,7 @@ await UniTask.Run(async () => await SetCombinationSlotStatesAsync(curAvatarState); await AddOrReplaceAvatarStateAsync(curAvatarState, CurrentAvatarKey); await SetPetStates(avatarState.address); + await Widget.Find().InitializePatrolReward(); }); } diff --git a/nekoyume/Assets/_Scripts/UI/Model/PatrolReward.cs b/nekoyume/Assets/_Scripts/UI/Model/PatrolReward.cs index a3757bc823c..16b25ccaf21 100644 --- a/nekoyume/Assets/_Scripts/UI/Model/PatrolReward.cs +++ b/nekoyume/Assets/_Scripts/UI/Model/PatrolReward.cs @@ -21,24 +21,6 @@ public class PatrolReward private const string PatrolRewardPushIdentifierKey = "PATROL_REWARD_PUSH_IDENTIFIER"; private bool _initialized; - public void Initialize() - { - if (_initialized) - { - return; - } - - _initialized = true; - PatrolTime = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(1)) - .CombineLatest(LastRewardTime, (_, lastReward) => - { - var timeSpan = DateTime.Now - lastReward; - return timeSpan > Interval ? Interval : timeSpan; - }) - .ToReactiveProperty(); - LastRewardTime.Subscribe(_ => SetPushNotification()); - } - public async Task InitializeInformation(string avatarAddress, string agentAddress, int level) { var serviceClient = Game.Game.instance.PatrolRewardServiceClient; @@ -104,6 +86,21 @@ ... on FungibleItemRewardModel {{ Interval = response.Policy.MinimumRequiredInterval; RewardModels.Value = response.Policy.Rewards; } + + if (_initialized) + { + return; + } + + _initialized = true; + PatrolTime = Observable.Timer(TimeSpan.Zero, TimeSpan.FromMinutes(1)) + .CombineLatest(LastRewardTime, (_, lastReward) => + { + var timeSpan = DateTime.Now - lastReward; + return timeSpan > Interval ? Interval : timeSpan; + }) + .ToReactiveProperty(); + LastRewardTime.Subscribe(_ => SetPushNotification()); } public async Task LoadAvatarInfo(string avatarAddress, string agentAddress) diff --git a/nekoyume/Assets/_Scripts/UI/Module/Lobby/PatrolRewardMenu.cs b/nekoyume/Assets/_Scripts/UI/Module/Lobby/PatrolRewardMenu.cs index ff1f891666e..fdc75f17db4 100644 --- a/nekoyume/Assets/_Scripts/UI/Module/Lobby/PatrolRewardMenu.cs +++ b/nekoyume/Assets/_Scripts/UI/Module/Lobby/PatrolRewardMenu.cs @@ -49,27 +49,14 @@ private void OnEnable() return; } - SetData(popup); - } - - private async void SetData(PatrolRewardPopup popup) - { - await popup.InitializePatrolReward(); - - var patrolReward = popup.PatrolReward; - patrolReward.PatrolTime - .Select(time => time < patrolReward.Interval) + popup.PatrolReward.PatrolTime + .Select(time => time < popup.PatrolReward.Interval) .Where(_ => !popup.Claiming.Value) .Subscribe(patrolling => SetCanClaim(patrolling, false)) .AddTo(_disposables); - popup.Claiming.Subscribe(value => - { - if (value) - { - SetCanClaim(false, true); - } - }).AddTo(_disposables); + popup.Claiming.Where(claiming => claiming) + .Subscribe(value => SetCanClaim(false, true)).AddTo(_disposables); } private void SetCanClaim(bool patrolling, bool claiming) diff --git a/nekoyume/Assets/_Scripts/UI/Widget/Popup/PatrolRewardPopup.cs b/nekoyume/Assets/_Scripts/UI/Widget/Popup/PatrolRewardPopup.cs index 2a8ab9fca29..fee91a6bdfa 100644 --- a/nekoyume/Assets/_Scripts/UI/Widget/Popup/PatrolRewardPopup.cs +++ b/nekoyume/Assets/_Scripts/UI/Widget/Popup/PatrolRewardPopup.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Libplanet.Crypto; using Nekoyume.Game.Controller; using Nekoyume.GraphQL; using Nekoyume.L10n; @@ -45,9 +44,7 @@ private class RewardData public readonly PatrolReward PatrolReward = new(); public readonly ReactiveProperty Claiming = new(false); private readonly Dictionary _rewards = new(); - private readonly List _disposables = new(); - private Address _currentAvatarAddress; private bool _initialized; public bool CanClaim => @@ -75,30 +72,20 @@ public override void Show(bool ignoreShowAnimation = false) ShowAsync(ignoreShowAnimation); } + // Called at CurrentAvatarState isNewlySelected public async Task InitializePatrolReward() { var avatarAddress = Game.Game.instance.States.CurrentAvatarState.address; - if (_currentAvatarAddress.Equals(avatarAddress)) - { - return; - } - var agentAddress = Game.Game.instance.States.AgentState.address; var level = Game.Game.instance.States.CurrentAvatarState.level; await PatrolReward.InitializeInformation(avatarAddress.ToHex(), agentAddress.ToHex(), level); - PatrolReward.Initialize(); + // for changed avatar Claiming.Value = false; - - _currentAvatarAddress = avatarAddress; } private async void ShowAsync(bool ignoreShowAnimation = false) { - await InitializePatrolReward(); - - SetIntervalText(PatrolReward.Interval); - if (!_initialized) { Init(); @@ -108,33 +95,32 @@ private async void ShowAsync(bool ignoreShowAnimation = false) if (PatrolReward.NextLevel <= level) { await PatrolReward.LoadPolicyInfo(level); - - SetIntervalText(PatrolReward.Interval); } + SetIntervalText(PatrolReward.Interval); OnChangeTime(PatrolReward.PatrolTime.Value); base.Show(ignoreShowAnimation); } + // it must be called after PatrolReward.InitializeInformation (called avatar selected) private void Init() { - _disposables.DisposeAllAndClear(); - PatrolReward.RewardModels .Subscribe(OnChangeRewardModels) - .AddTo(_disposables); + .AddTo(gameObject); PatrolReward.PatrolTime .Where(_ => gameObject.activeSelf) .Subscribe(OnChangeTime) - .AddTo(_disposables); + .AddTo(gameObject); receiveButton.OnSubmitSubject .Subscribe(_ => ClaimRewardAsync()) - .AddTo(_disposables); + .AddTo(gameObject); Claiming.Where(claiming => claiming) - .Subscribe(_ => receiveButton.Interactable = false); + .Subscribe(_ => receiveButton.Interactable = false) + .AddTo(gameObject); _initialized = true; }