Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. using Dao;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Timers;
  6.  
  7. namespace Gloove.Util
  8. {
  9. public static class AutoComplete<T>
  10. where T : class
  11. {
  12. private class Dao : GenericDao<T>, IDisposable { }
  13.  
  14. private static bool Ativo = false;
  15. private static int Ciclos = 1;
  16. private static int Length;
  17.  
  18. private static Timer t;
  19. public static int Milisegundos { get; set; } = 500;
  20. public static int MaximoCiclos { get; set; } = 20;
  21. public static int MinLength { get; set; } = 3;
  22.  
  23. public static List<T> Resultado { get; private set; }
  24. private static Func<T, string> Funcao = null;
  25. private static string Filtro = null;
  26.  
  27. public static void Monitorar(Func<T, string> funcao, string filtro)
  28. {
  29. Funcao = funcao;
  30. Filtro = filtro;
  31.  
  32. if (!Ativo)
  33. {
  34. Resultado = null;
  35. Length = filtro.Length;
  36.  
  37. t = new Timer(Milisegundos);
  38. t.Elapsed += OnTimedEvent;
  39. t.AutoReset = true;
  40. t.Enabled = true;
  41.  
  42. Ativo = true;
  43.  
  44. Console.WriteLine("Monitor iniciado");
  45. }
  46. }
  47.  
  48. public static void Finalizar()
  49. {
  50. t.Stop();
  51. t.Dispose();
  52.  
  53. Ativo = false;
  54. Ciclos = 1;
  55. Length = 0;
  56. Filtro = null;
  57. Funcao = null;
  58.  
  59. Console.WriteLine("Monitor finalizado");
  60. }
  61.  
  62. private static void OnTimedEvent(Object source, ElapsedEventArgs e)
  63. {
  64. Console.WriteLine("Evento acionado {0:HH:mm:ss.fff}", e.SignalTime);
  65. Console.WriteLine("Ciclo: " + Ciclos);
  66. Console.WriteLine("length Anterior: " + Length);
  67. Console.WriteLine("length Filtro: " + Filtro.Length);
  68.  
  69. if (Length >= MinLength && Filtro.Length.Equals(Length))
  70. {
  71. using (GenericDao<T> dao = new Dao())
  72. {
  73. Resultado = dao.Set<T>()
  74. .Where(entity => Funcao(entity).ToLower().Contains(Filtro.ToLower()))
  75. .Take(20).ToList();
  76. }
  77.  
  78. Console.WriteLine("Filtrou");
  79. Finalizar();
  80. return;
  81. }
  82.  
  83. Length = Filtro.Length;
  84. Ciclos++;
  85.  
  86. if (Ciclos > MaximoCiclos)
  87. Finalizar();
  88. }
  89. }
  90. }
  91.  
  92. private void CbFornecedorTeste_PreviewKeyUp(object sender, KeyEventArgs e)
  93. {
  94. AutoComplete<Fornecedor>.Monitorar(f => f.Nome, cbFornecedorTeste.Text);
  95. }
  96.  
  97. private void CbFornecedorTeste_KeyUp(object sender, KeyEventArgs e)
  98. {
  99. listaFornecedores = AutoComplete<Fornecedor>.Resultado;
  100.  
  101. if (listaFornecedores != null && listaFornecedores.Count > 0)
  102. {
  103. foreach (Fornecedor f in listaFornecedores)
  104. {
  105. Console.WriteLine("ID " + f.Id);
  106. Console.WriteLine("NOME " + f.Nome);
  107. }
  108. }
  109. }
  110.  
  111. <ComboBox Grid.Column="1" x:Name="cbFornecedorTeste" VerticalAlignment="Center" Height="30" BorderThickness="0" md:TextFieldAssist.DecorationVisibility="Collapsed" Background="AliceBlue"
  112. FontSize="12" Foreground="{StaticResource CinzaMedioBrush}" DisplayMemberPath="Nome" SelectedValuePath="Id" IsEditable="True" IsTextSearchEnabled="False"
  113. PreviewKeyUp="CbFornecedorTeste_PreviewKeyUp" KeyUp="CbFornecedorTeste_KeyUp"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement