SortHelper.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Gtk;
  2. using System;
  3. namespace Ryujinx.Ui.Helper
  4. {
  5. static class SortHelper
  6. {
  7. public static int TimePlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  8. {
  9. static string ReverseFormat(string time)
  10. {
  11. if (time == "Never")
  12. {
  13. return "00";
  14. }
  15. var numbers = time.Split(new char[] { 'd', 'h', 'm' });
  16. time = time.Replace(" ", "").Replace("d", ".").Replace("h", ":").Replace("m", "");
  17. if (numbers.Length == 2)
  18. {
  19. return $"00.00:{time}";
  20. }
  21. else if (numbers.Length == 3)
  22. {
  23. return $"00.{time}";
  24. }
  25. return time;
  26. }
  27. string aValue = ReverseFormat(model.GetValue(a, 5).ToString());
  28. string bValue = ReverseFormat(model.GetValue(b, 5).ToString());
  29. return TimeSpan.Compare(TimeSpan.Parse(aValue), TimeSpan.Parse(bValue));
  30. }
  31. public static int LastPlayedSort(ITreeModel model, TreeIter a, TreeIter b)
  32. {
  33. string aValue = model.GetValue(a, 6).ToString();
  34. string bValue = model.GetValue(b, 6).ToString();
  35. if (aValue == "Never")
  36. {
  37. aValue = DateTime.UnixEpoch.ToString();
  38. }
  39. if (bValue == "Never")
  40. {
  41. bValue = DateTime.UnixEpoch.ToString();
  42. }
  43. return DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue));
  44. }
  45. public static int FileSizeSort(ITreeModel model, TreeIter a, TreeIter b)
  46. {
  47. string aValue = model.GetValue(a, 8).ToString();
  48. string bValue = model.GetValue(b, 8).ToString();
  49. if (aValue[^3..] == "GiB")
  50. {
  51. aValue = (float.Parse(aValue[0..^3]) * 1024).ToString();
  52. }
  53. else
  54. {
  55. aValue = aValue[0..^3];
  56. }
  57. if (bValue[^3..] == "GiB")
  58. {
  59. bValue = (float.Parse(bValue[0..^3]) * 1024).ToString();
  60. }
  61. else
  62. {
  63. bValue = bValue[0..^3];
  64. }
  65. if (float.Parse(aValue) > float.Parse(bValue))
  66. {
  67. return -1;
  68. }
  69. else if (float.Parse(bValue) > float.Parse(aValue))
  70. {
  71. return 1;
  72. }
  73. else
  74. {
  75. return 0;
  76. }
  77. }
  78. }
  79. }