Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Dao;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Timers;
- namespace Gloove.Util
- {
- public static class AutoComplete<T>
- where T : class
- {
- private class Dao : GenericDao<T>, IDisposable { }
- private static bool Ativo = false;
- private static int Ciclos = 1;
- private static int Length;
- private static Timer t;
- public static int Milisegundos { get; set; } = 500;
- public static int MaximoCiclos { get; set; } = 20;
- public static int MinLength { get; set; } = 3;
- public static List<T> Resultado { get; private set; }
- private static Func<T, string> Funcao = null;
- private static string Filtro = null;
- public static void Monitorar(Func<T, string> funcao, string filtro)
- {
- Funcao = funcao;
- Filtro = filtro;
- if (!Ativo)
- {
- Resultado = null;
- Length = filtro.Length;
- t = new Timer(Milisegundos);
- t.Elapsed += OnTimedEvent;
- t.AutoReset = true;
- t.Enabled = true;
- Ativo = true;
- Console.WriteLine("Monitor iniciado");
- }
- }
- public static void Finalizar()
- {
- t.Stop();
- t.Dispose();
- Ativo = false;
- Ciclos = 1;
- Length = 0;
- Filtro = null;
- Funcao = null;
- Console.WriteLine("Monitor finalizado");
- }
- private static void OnTimedEvent(Object source, ElapsedEventArgs e)
- {
- Console.WriteLine("Evento acionado {0:HH:mm:ss.fff}", e.SignalTime);
- Console.WriteLine("Ciclo: " + Ciclos);
- Console.WriteLine("length Anterior: " + Length);
- Console.WriteLine("length Filtro: " + Filtro.Length);
- if (Length >= MinLength && Filtro.Length.Equals(Length))
- {
- using (GenericDao<T> dao = new Dao())
- {
- Resultado = dao.Set<T>()
- .Where(entity => Funcao(entity).ToLower().Contains(Filtro.ToLower()))
- .Take(20).ToList();
- }
- Console.WriteLine("Filtrou");
- Finalizar();
- return;
- }
- Length = Filtro.Length;
- Ciclos++;
- if (Ciclos > MaximoCiclos)
- Finalizar();
- }
- }
- }
- private void CbFornecedorTeste_PreviewKeyUp(object sender, KeyEventArgs e)
- {
- AutoComplete<Fornecedor>.Monitorar(f => f.Nome, cbFornecedorTeste.Text);
- }
- private void CbFornecedorTeste_KeyUp(object sender, KeyEventArgs e)
- {
- listaFornecedores = AutoComplete<Fornecedor>.Resultado;
- if (listaFornecedores != null && listaFornecedores.Count > 0)
- {
- foreach (Fornecedor f in listaFornecedores)
- {
- Console.WriteLine("ID " + f.Id);
- Console.WriteLine("NOME " + f.Nome);
- }
- }
- }
- <ComboBox Grid.Column="1" x:Name="cbFornecedorTeste" VerticalAlignment="Center" Height="30" BorderThickness="0" md:TextFieldAssist.DecorationVisibility="Collapsed" Background="AliceBlue"
- FontSize="12" Foreground="{StaticResource CinzaMedioBrush}" DisplayMemberPath="Nome" SelectedValuePath="Id" IsEditable="True" IsTextSearchEnabled="False"
- PreviewKeyUp="CbFornecedorTeste_PreviewKeyUp" KeyUp="CbFornecedorTeste_KeyUp"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement