aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx/Ui/Helper/SortHelper.cs
blob: 4def89323d31529940c4b9eb484018dd77e76ea6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using Gtk;
using System;

namespace Ryujinx.Ui.Helper
{
    static class SortHelper
    {
        public static int TimePlayedSort(ITreeModel model, TreeIter a, TreeIter b)
        {
            string aValue = model.GetValue(a, 5).ToString();
            string bValue = model.GetValue(b, 5).ToString();
            float aFloat;
            float bFloat;

            if (aValue.Length > 7 && aValue[^7..] == "minutes")
            {
                aValue = aValue.Replace("minutes", "");
                aFloat = (float.Parse(aValue) * 60);
            }
            else if (aValue.Length > 5 && aValue[^5..] == "hours")
            {
                aValue = aValue.Replace("hours", "");
                aFloat = (float.Parse(aValue) * 3600);
            }
            else if (aValue.Length > 4 && aValue[^4..] == "days")
            {
                aValue = aValue.Replace("days", "");
                aFloat = (float.Parse(aValue) * 86400);
            }
            else
            {
                aValue = aValue.Replace("seconds", "");
                aFloat = float.Parse(aValue);
            }

            if (bValue.Length > 7 && bValue[^7..] == "minutes")
            {
                bValue = bValue.Replace("minutes", "");
                bFloat = (float.Parse(bValue) * 60);
            }
            else if (bValue.Length > 5 && bValue[^5..] == "hours")
            {
                bValue = bValue.Replace("hours", "");
                bFloat = (float.Parse(bValue) * 3600);
            }
            else if (bValue.Length > 4 && bValue[^4..] == "days")
            {
                bValue =  bValue.Replace("days", "");
                bFloat = (float.Parse(bValue) * 86400);
            }
            else
            {
                bValue = bValue[0..^8];
                bFloat = float.Parse(bValue);
            }

            if (aFloat > bFloat)
            {
                return -1;
            }
            else if (bFloat > aFloat)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public static int LastPlayedSort(ITreeModel model, TreeIter a, TreeIter b)
        {
            string aValue = model.GetValue(a, 6).ToString();
            string bValue = model.GetValue(b, 6).ToString();

            if (aValue == "Never")
            {
                aValue = DateTime.UnixEpoch.ToString();
            }

            if (bValue == "Never")
            {
                bValue = DateTime.UnixEpoch.ToString();
            }

            return DateTime.Compare(DateTime.Parse(bValue), DateTime.Parse(aValue));
        }

        public static int FileSizeSort(ITreeModel model, TreeIter a, TreeIter b)
        {
            string aValue = model.GetValue(a, 8).ToString();
            string bValue = model.GetValue(b, 8).ToString();

            if (aValue[^3..] == "GiB")
            {
                aValue = (float.Parse(aValue[0..^3]) * 1024).ToString();
            }
            else
            {
                aValue = aValue[0..^3];
            }

            if (bValue[^3..] == "GiB")
            {
                bValue = (float.Parse(bValue[0..^3]) * 1024).ToString();
            }
            else
            {
                bValue = bValue[0..^3];
            }

            if (float.Parse(aValue) > float.Parse(bValue))
            {
                return -1;
            }
            else if (float.Parse(bValue) > float.Parse(aValue))
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}