Added final adjusment for demo
This commit is contained in:
@@ -1,16 +1,34 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UIElements;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
class FurnitureFetcher : MonoBehaviour
|
||||
{
|
||||
public VisualTreeAsset itemTemplate;
|
||||
public UIDocument uiDocument;
|
||||
|
||||
public static string API_URL = "http://100.123.31.103:5102/api/v1/FurnitureVariant/GetByIds";
|
||||
public static string API_URL = "https://localhost:7223/api/v1/FurnitureVariant/GetAll";
|
||||
|
||||
private static readonly Dictionary<string, string> CurrencySymbols = new()
|
||||
{
|
||||
{ "USD", "$" },
|
||||
{ "EUR", "€" },
|
||||
{ "GBP", "£" },
|
||||
{ "JPY", "¥" },
|
||||
{ "MXN", "$" },
|
||||
{ "CAD", "$" },
|
||||
{ "BRL", "R$" },
|
||||
{ "ARS", "$" },
|
||||
{ "CLP", "$" },
|
||||
{ "COP", "$" },
|
||||
{ "CNY", "¥" },
|
||||
{ "INR", "₹" }
|
||||
};
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -19,38 +37,84 @@ class FurnitureFetcher : MonoBehaviour
|
||||
|
||||
IEnumerator GetFurnitureData()
|
||||
{
|
||||
Debug.Log($"API URL: {API_URL}");
|
||||
List<string> variantIds = new()
|
||||
{
|
||||
"ab6fd51f-81f0-490d-b713-8d4f3de61a58",
|
||||
"1f3b348f-b3ee-4985-b76f-7f6d61f2c835"
|
||||
};
|
||||
|
||||
FurnitureIdsRequest data = new() { ids = variantIds };
|
||||
string json = JsonUtility.ToJson(data);
|
||||
|
||||
using UnityWebRequest request = new(API_URL, "POST");
|
||||
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
|
||||
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
using UnityWebRequest request = UnityWebRequest.Get(API_URL);
|
||||
request.downloadHandler = new DownloadHandlerBuffer();
|
||||
request.SetRequestHeader("Content-Type", "application/json");
|
||||
|
||||
yield return request.SendWebRequest();
|
||||
|
||||
string rawResponse = request.downloadHandler.text;
|
||||
//Debug.Log("📦 RAW RESPONSE:\n" + rawResponse);
|
||||
|
||||
if (request.result == UnityWebRequest.Result.Success)
|
||||
{
|
||||
string wrappedJson = "{\"items\":" + request.downloadHandler.text + "}";
|
||||
FurnitureVariantList list = JsonUtility.FromJson<FurnitureVariantList>(wrappedJson);
|
||||
string wrappedJson = "{\"items\":" + rawResponse + "}";
|
||||
FurnitureVariantList list;
|
||||
|
||||
try
|
||||
{
|
||||
list = JsonConvert.DeserializeObject<FurnitureVariantList>(wrappedJson);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Debug.LogError("❌ Error al deserializar JSON: " + ex.Message);
|
||||
yield break;
|
||||
}
|
||||
|
||||
var root = uiDocument.rootVisualElement;
|
||||
var scrollView = root.Q<ScrollView>("furniture-list");
|
||||
|
||||
scrollView.Clear();
|
||||
|
||||
foreach (var variant in list.items)
|
||||
{
|
||||
VisualElement item = itemTemplate.CloneTree();
|
||||
item.Q<Label>("nameLabel").text = $"Name: {variant.name}";
|
||||
item.Q<Label>("colorLabel").text = $"Color: {variant.color}";
|
||||
item.Q<Label>("lineLabel").text = $"Line: {variant.line}";
|
||||
|
||||
string currencySymbol = CurrencySymbols.TryGetValue(variant.currency, out string symbol)
|
||||
? symbol
|
||||
: variant.currency;
|
||||
|
||||
string formattedPrice = $"{currencySymbol}{variant.price.ToString("N2", CultureInfo.InvariantCulture)} {variant.currency}";
|
||||
item.Q<Label>("priceLabel").text = $"Price: {formattedPrice}";
|
||||
|
||||
item.Q<Label>("stockLabel").text = $"Stock: {variant.stock}";
|
||||
|
||||
var attributesContainer = item.Q<VisualElement>("attributesContainer");
|
||||
if (variant.attributes != null)
|
||||
{
|
||||
string attributesText = "Attributes:\n";
|
||||
foreach (var kvp in variant.attributes)
|
||||
{
|
||||
attributesText += $"{kvp.Key}: {kvp.Value}\n";
|
||||
}
|
||||
//Debug.Log(attributesText);
|
||||
//Debug.Log($"--- ATRIBUTOS PARA: {variant.name} ---");
|
||||
//Debug.Log($"Total keys: {variant.attributes.Count}");
|
||||
|
||||
int index = 0;
|
||||
foreach (var kvp in variant.attributes)
|
||||
{
|
||||
//Debug.Log($"[{index}] {kvp.Key}: {kvp.Value}");
|
||||
|
||||
string formattedKey = FormatKey(kvp.Key);
|
||||
Label attrLabel = new Label($"{formattedKey}: {kvp.Value}");
|
||||
attrLabel.AddToClassList("furniture-attribute"); // estilo opcional
|
||||
attrLabel.style.whiteSpace = WhiteSpace.Normal;
|
||||
attributesContainer.Add(attrLabel);
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
if (index == 0)
|
||||
Debug.Log("⚠️ El diccionario está vacío aunque no es null.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"❌ 'attributes' es NULL para {variant.name}");
|
||||
}
|
||||
|
||||
scrollView.Add(item);
|
||||
}
|
||||
}
|
||||
@@ -59,4 +123,16 @@ class FurnitureFetcher : MonoBehaviour
|
||||
Debug.LogError("Error: " + request.error);
|
||||
}
|
||||
}
|
||||
|
||||
private string FormatKey(string rawKey)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(rawKey))
|
||||
return "";
|
||||
|
||||
// Inserta espacio antes de mayúsculas
|
||||
string spaced = System.Text.RegularExpressions.Regex.Replace(rawKey, "([a-z])([A-Z])", "$1 $2");
|
||||
|
||||
// Capitaliza primera letra
|
||||
return char.ToUpper(spaced[0]) + spaced.Substring(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user