using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
using System.Linq;
public class SearchDropBoxInit : MonoBehaviour
{
[SerializeField] TMP_Dropdown dr_car;
[SerializeField] TMP_Dropdown dr_company;
[SerializeField] TMP_Dropdown dr_product;
[SerializeField] SearchDropBoxOpen uic_car;
[SerializeField] SearchDropBoxOpen uic_company;
[SerializeField] SearchDropBoxOpen uic_product;
[SerializeField] TextMeshProUGUI txt_result_car;
[SerializeField] TextMeshProUGUI txt_result_company;
[SerializeField] TextMeshProUGUI txt_result_product;
[Header("Scrollview")]
[SerializeField] CompanyItemTableViewController m_CompanyItemTableViewController;
[Header("Search")]
[SerializeField] TMP_InputField m_TMP_InputField;
List<CompanyUiItem> originTable;
// Start is called before the first frame update
void Start()
{
StartCoroutine(InitData());
StartCoroutine(InitScrollView());
}
IEnumerator InitData()
{
yield return new WaitUntil(() => LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataCarsXml.data.Count != 0);
SetFunction_UI(dr_car, LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataCarsXml.data, uic_car, txt_result_car);
SetFunction_UI(dr_company, LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataCompanysXml.data, uic_company, txt_result_company);
SetFunction_UI(dr_product, LoadPartLinksWebGL.Instance.m_xmlLoadDatas.m_dataProductsXml.data, uic_product, txt_result_product);
}
IEnumerator InitScrollView()
{
yield return new WaitUntil(() => LoadPartLinksWebGL.Instance.m_AllProgramPath.links.Count != 0);
SetScrollview();
//m_TMP_InputField.onEndEdit.AddListener(ValueChanged);
m_TMP_InputField.onValueChanged.AddListener(ValueChanged);
}
void SetScrollview()
{
originTable = new List<CompanyUiItem>();
foreach (var x in LoadPartLinksWebGL.Instance.m_AllProgramPath.links)
{
CompanyUiItem temp = new CompanyUiItem();
///회사이름이 없으면 일반씬이므로 컨티뉴
if (x.Value.companyName == "")
continue;
temp.companyName = x.Value.companyName;
temp.companyType = x.Value.companyType;
temp.url = x.Value.url;
temp.products = x.Value.products;
temp.cars = x.Value.targetCar;
originTable.Add(temp);
}
m_CompanyItemTableViewController.UpdateData(originTable);
}
#region 검색
public List<CompanyUiItem> SearchTableData(string input)
{
if (input.Length == 0)
return originTable;
input = input.Replace(",", ""); ///따움표는 없는걸로 체크
Func<CompanyUiItem,bool> condition_car; //검색 조건
Func<CompanyUiItem, bool> condition_products; //검색 조건
Func<CompanyUiItem, bool> condition_companyName; //검색 조건
Func<CompanyUiItem, bool> condition_companyType; //검색 조건
//Func<CompanyUiItem, object> order; //정렬 기준
string search = input.ToLower(); //소문자로 변환
condition_car = item => item.cars.Where(x=> x.ToLower().Contains(search)).Any();
condition_products = item => item.products.Where(x => x.ToLower().Contains(search)).Any();
condition_companyName = item => item.companyName.ToLower().Contains(search);
condition_companyType = item => item.companyType.ToLower().Contains(search);
IEnumerable<CompanyUiItem> result_car;
IEnumerable<CompanyUiItem> result_products;
IEnumerable<CompanyUiItem> result_company;
IEnumerable<CompanyUiItem> result_type;
result_car = originTable.Where(condition_car);
result_products = originTable.Where(condition_products);
result_company = originTable.Where(condition_companyName);
result_type = originTable.Where(condition_companyType);
//result = originTable.Where(condition_car).Where(condition_companyName).Where(condition_companyType).Where(condition_products);
result_car = result_car.Select(x => new CompanyUiItem
{
companyName = x.companyName,
companyType = x.companyType,
cars = x.cars,
products = x.products,
url = x.url,
});
result_products = result_products.Select(x => new CompanyUiItem
{
companyName = x.companyName,
companyType = x.companyType,
cars = x.cars,
products = x.products,
url = x.url,
});
result_company = result_company.Select(x => new CompanyUiItem
{
companyName = x.companyName,
companyType = x.companyType,
cars = x.cars,
products = x.products,
url = x.url,
});
result_type = result_type.Select(x => new CompanyUiItem
{
companyName = x.companyName,
companyType = x.companyType,
cars = x.cars,
products = x.products,
url = x.url,
});
IEnumerable<CompanyUiItem> result =
new[] { result_car, result_products, result_company, result_type }.
Where(x=>x!=null).
SelectMany(x => x).
Distinct();
return result.ToList();
}
void ValueChanged(string text)
{
string[] texts = text.Split(",");
if (texts.Length == 0)
{
m_CompanyItemTableViewController.UpdateData(SearchTableData(text));
}
else
{
List<CompanyUiItem> result = new List<CompanyUiItem>();
foreach (var x in texts)
result.AddRange(SearchTableData(x));
m_CompanyItemTableViewController.UpdateData(result);
}
}
#endregion
#region 드롭다운
private void SetFunction_UI(TMP_Dropdown dropdown,List<string> data, SearchDropBoxOpen uic,TextMeshProUGUI result)
{
//Reset
ResetFunction_UI(dropdown, data);
// Set Default Value
result.text = dropdown.options[dropdown.value].text;
dropdown.onValueChanged.AddListener(delegate {
Function_Dropdown(dropdown, uic, result);
});
}
//private void Function_Button()
//{
// string op = dropdown.options[dropdown.value].text;
// result.text = op;
// result_img.sprite = dropdown.options[dropdown.value].image;
// Debug.LogError("Dropdown Result!\n" + op);
//}
private void Function_Dropdown(TMP_Dropdown select, SearchDropBoxOpen uic, TextMeshProUGUI result)
{
string op = select.options[select.value].text;
//message.text = op;
Debug.Log("Dropdown Change!\n" + op);
result.text = op;
uic.Hide();
}
private void ResetFunction_UI(TMP_Dropdown dropdown,List<string> data)
{
dropdown.onValueChanged.RemoveAllListeners();
dropdown.options.Clear();
for (int i = 0; i < data.Count; i++)
{
int idx = i;
TMP_Dropdown.OptionData newData = new TMP_Dropdown.OptionData();
newData.text = data[idx];
//newData.image = sprites[i];
dropdown.options.Add(newData);
}
dropdown.SetValueWithoutNotify(-1);
dropdown.SetValueWithoutNotify(0);
}
#endregion
}
댓글 없음:
댓글 쓰기