| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.UI; |
| | 4 | |
|
| | 5 | | public class FavoriteButtonComponentView : BaseComponentView, IComponentModelConfig<FavoriteButtonComponentModel> |
| | 6 | | { |
| | 7 | | [Header("Prefab References")] |
| | 8 | | [SerializeField] private ButtonComponentView button; |
| | 9 | | [SerializeField] private Image buttonFill; |
| | 10 | | [SerializeField] private Color noFavoriteFillColor; |
| | 11 | | [SerializeField] private Color favoriteFillColor; |
| | 12 | |
|
| | 13 | | [Header("Configuration")] |
| | 14 | | [SerializeField] internal FavoriteButtonComponentModel model; |
| | 15 | |
|
| | 16 | | public event Action<string, bool> OnFavoriteChange; |
| | 17 | |
|
| | 18 | | public override void Awake() |
| | 19 | | { |
| 1106 | 20 | | base.Awake(); |
| | 21 | |
|
| 1106 | 22 | | button.onClick.RemoveAllListeners(); |
| 1106 | 23 | | button.onClick.AddListener(SetFavoriteFromButton); |
| 1106 | 24 | | } |
| | 25 | |
|
| | 26 | | public override void RefreshControl() |
| | 27 | | { |
| 59 | 28 | | if (model == null) |
| 0 | 29 | | return; |
| | 30 | |
|
| 59 | 31 | | SetFavorite(model.isFavorite); |
| 59 | 32 | | } |
| | 33 | |
|
| | 34 | | public bool IsFavorite() => |
| 1361 | 35 | | model?.isFavorite ?? false; |
| | 36 | |
|
| | 37 | | private void SetFavoriteFromButton() |
| | 38 | | { |
| 0 | 39 | | model.isFavorite = !model.isFavorite; |
| 0 | 40 | | OnFavoriteChange?.Invoke(model.placeUUID, model.isFavorite); |
| 0 | 41 | | RefreshControl(); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | private void SetFavorite(bool isFavorite) |
| | 45 | | { |
| 59 | 46 | | SetButtonVisuals(isFavorite); |
| 59 | 47 | | } |
| | 48 | |
|
| | 49 | | private void SetButtonVisuals(bool isFavorite) => |
| 59 | 50 | | buttonFill.color = isFavorite ? favoriteFillColor : noFavoriteFillColor; |
| | 51 | |
|
| | 52 | | public void SetInteractable(bool isInteractable) |
| | 53 | | { |
| 0 | 54 | | button.button.interactable = isInteractable; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void Configure(FavoriteButtonComponentModel newModel) |
| | 58 | | { |
| 59 | 59 | | model = newModel; |
| 59 | 60 | | RefreshControl(); |
| 59 | 61 | | } |
| | 62 | | } |