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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use std::fmt;

use relay_protocol::{Annotated, Empty, FromValue, IntoValue};

use crate::protocol::{Contexts, DeviceContext};

#[derive(Clone, Copy, Debug, FromValue, IntoValue, Empty, PartialEq)]
pub struct DeviceClass(pub u64);

const GIB: u64 = 1024 * 1024 * 1024;

impl DeviceClass {
    pub const LOW: Self = Self(1);
    pub const MEDIUM: Self = Self(2);
    pub const HIGH: Self = Self(3);

    pub fn from_contexts(contexts: &Contexts) -> Option<DeviceClass> {
        let device = contexts.get::<DeviceContext>()?;
        let family = device.family.value()?;

        if family == "iPhone" || family == "iOS" || family == "iOS-Device" {
            model_to_class(device.model.as_str()?)
        } else if let (Some(&freq), Some(&proc), Some(&mem)) = (
            device.processor_frequency.value(),
            device.processor_count.value(),
            device.memory_size.value(),
        ) {
            if freq < 2000 || proc < 8 || mem < 4 * GIB {
                Some(DeviceClass::LOW)
            } else if freq < 2500 || mem < 6 * GIB {
                Some(DeviceClass::MEDIUM)
            } else {
                Some(DeviceClass::HIGH)
            }
        } else {
            None
        }
    }
}

impl fmt::Display for DeviceClass {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

fn model_to_class(model: &str) -> Option<DeviceClass> {
    match model {
        // iPhones
        "iPhone1,1" => Some(DeviceClass::LOW),
        "iPhone1,2" => Some(DeviceClass::LOW),
        "iPhone2,1" => Some(DeviceClass::LOW),
        "iPhone3,1" => Some(DeviceClass::LOW),
        "iPhone3,2" => Some(DeviceClass::LOW),
        "iPhone3,3" => Some(DeviceClass::LOW),
        "iPhone4,1" => Some(DeviceClass::LOW),
        "iPhone5,1" => Some(DeviceClass::LOW),
        "iPhone5,2" => Some(DeviceClass::LOW),
        "iPhone5,3" => Some(DeviceClass::LOW),
        "iPhone5,4" => Some(DeviceClass::LOW),
        "iPhone6,1" => Some(DeviceClass::LOW),
        "iPhone6,2" => Some(DeviceClass::LOW),
        "iPhone7,1" => Some(DeviceClass::LOW),
        "iPhone7,2" => Some(DeviceClass::LOW),
        "iPhone8,1" => Some(DeviceClass::LOW),
        "iPhone8,2" => Some(DeviceClass::LOW),
        "iPhone8,4" => Some(DeviceClass::LOW),
        "iPhone9,1" => Some(DeviceClass::MEDIUM),
        "iPhone9,3" => Some(DeviceClass::MEDIUM),
        "iPhone9,2" => Some(DeviceClass::MEDIUM),
        "iPhone9,4" => Some(DeviceClass::MEDIUM),
        "iPhone10,1" => Some(DeviceClass::MEDIUM),
        "iPhone10,4" => Some(DeviceClass::MEDIUM),
        "iPhone10,2" => Some(DeviceClass::MEDIUM),
        "iPhone10,5" => Some(DeviceClass::MEDIUM),
        "iPhone10,3" => Some(DeviceClass::MEDIUM),
        "iPhone10,6" => Some(DeviceClass::MEDIUM),
        "iPhone11,8" => Some(DeviceClass::MEDIUM),
        "iPhone11,2" => Some(DeviceClass::MEDIUM),
        "iPhone11,4" => Some(DeviceClass::MEDIUM),
        "iPhone11,6" => Some(DeviceClass::MEDIUM),
        "iPhone12,1" => Some(DeviceClass::MEDIUM),
        "iPhone12,3" => Some(DeviceClass::MEDIUM),
        "iPhone12,5" => Some(DeviceClass::MEDIUM),
        "iPhone12,8" => Some(DeviceClass::MEDIUM),
        "iPhone13,1" => Some(DeviceClass::HIGH),
        "iPhone13,2" => Some(DeviceClass::HIGH),
        "iPhone13,3" => Some(DeviceClass::HIGH),
        "iPhone13,4" => Some(DeviceClass::HIGH),
        "iPhone14,4" => Some(DeviceClass::HIGH),
        "iPhone14,5" => Some(DeviceClass::HIGH),
        "iPhone14,2" => Some(DeviceClass::HIGH),
        "iPhone14,3" => Some(DeviceClass::HIGH),
        "iPhone14,6" => Some(DeviceClass::HIGH),
        "iPhone14,7" => Some(DeviceClass::HIGH),
        "iPhone14,8" => Some(DeviceClass::HIGH),
        "iPhone15,2" => Some(DeviceClass::HIGH),
        "iPhone15,3" => Some(DeviceClass::HIGH),

        // iPads
        "iPad1,1" => Some(DeviceClass::LOW),
        "iPad1,2" => Some(DeviceClass::LOW),
        "iPad2,1" => Some(DeviceClass::LOW),
        "iPad2,2" => Some(DeviceClass::LOW),
        "iPad2,3" => Some(DeviceClass::LOW),
        "iPad2,4" => Some(DeviceClass::LOW),
        "iPad3,1" => Some(DeviceClass::LOW),
        "iPad3,2" => Some(DeviceClass::LOW),
        "iPad3,3" => Some(DeviceClass::LOW),
        "iPad2,5" => Some(DeviceClass::LOW),
        "iPad2,6" => Some(DeviceClass::LOW),
        "iPad2,7" => Some(DeviceClass::LOW),
        "iPad3,4" => Some(DeviceClass::LOW),
        "iPad3,5" => Some(DeviceClass::LOW),
        "iPad3,6" => Some(DeviceClass::LOW),
        "iPad4,1" => Some(DeviceClass::LOW),
        "iPad4,2" => Some(DeviceClass::LOW),
        "iPad4,3" => Some(DeviceClass::LOW),
        "iPad4,4" => Some(DeviceClass::LOW),
        "iPad4,5" => Some(DeviceClass::LOW),
        "iPad4,6" => Some(DeviceClass::LOW),
        "iPad4,7" => Some(DeviceClass::LOW),
        "iPad4,8" => Some(DeviceClass::LOW),
        "iPad4,9" => Some(DeviceClass::LOW),
        "iPad5,1" => Some(DeviceClass::LOW),
        "iPad5,2" => Some(DeviceClass::LOW),
        "iPad5,3" => Some(DeviceClass::LOW),
        "iPad5,4" => Some(DeviceClass::LOW),
        "iPad6,3" => Some(DeviceClass::MEDIUM),
        "iPad6,4" => Some(DeviceClass::MEDIUM),
        "iPad6,7" => Some(DeviceClass::MEDIUM),
        "iPad6,8" => Some(DeviceClass::MEDIUM),
        "iPad6,11" => Some(DeviceClass::LOW),
        "iPad6,12" => Some(DeviceClass::LOW),
        "iPad7,2" => Some(DeviceClass::MEDIUM),
        "iPad7,3" => Some(DeviceClass::MEDIUM),
        "iPad7,4" => Some(DeviceClass::MEDIUM),
        "iPad7,5" => Some(DeviceClass::MEDIUM),
        "iPad7,6" => Some(DeviceClass::MEDIUM),
        "iPad7,1" => Some(DeviceClass::MEDIUM),
        "iPad7,11" => Some(DeviceClass::MEDIUM),
        "iPad7,12" => Some(DeviceClass::MEDIUM),
        "iPad8,1" => Some(DeviceClass::MEDIUM),
        "iPad8,2" => Some(DeviceClass::MEDIUM),
        "iPad8,3" => Some(DeviceClass::MEDIUM),
        "iPad8,4" => Some(DeviceClass::MEDIUM),
        "iPad8,5" => Some(DeviceClass::MEDIUM),
        "iPad8,6" => Some(DeviceClass::MEDIUM),
        "iPad8,7" => Some(DeviceClass::MEDIUM),
        "iPad8,8" => Some(DeviceClass::MEDIUM),
        "iPad8,9" => Some(DeviceClass::MEDIUM),
        "iPad8,10" => Some(DeviceClass::MEDIUM),
        "iPad8,11" => Some(DeviceClass::MEDIUM),
        "iPad8,12" => Some(DeviceClass::MEDIUM),
        "iPad11,1" => Some(DeviceClass::MEDIUM),
        "iPad11,2" => Some(DeviceClass::MEDIUM),
        "iPad11,3" => Some(DeviceClass::MEDIUM),
        "iPad11,4" => Some(DeviceClass::MEDIUM),
        "iPad11,6" => Some(DeviceClass::MEDIUM),
        "iPad11,7" => Some(DeviceClass::MEDIUM),
        "iPad12,1" => Some(DeviceClass::MEDIUM),
        "iPad12,2" => Some(DeviceClass::MEDIUM),
        "iPad14,1" => Some(DeviceClass::HIGH),
        "iPad14,2" => Some(DeviceClass::HIGH),
        "iPad13,1" => Some(DeviceClass::HIGH),
        "iPad13,2" => Some(DeviceClass::HIGH),
        "iPad13,4" => Some(DeviceClass::HIGH),
        "iPad13,5" => Some(DeviceClass::HIGH),
        "iPad13,6" => Some(DeviceClass::HIGH),
        "iPad13,7" => Some(DeviceClass::HIGH),
        "iPad13,8" => Some(DeviceClass::HIGH),
        "iPad13,9" => Some(DeviceClass::HIGH),
        "iPad13,10" => Some(DeviceClass::HIGH),
        "iPad13,11" => Some(DeviceClass::HIGH),
        "iPad13,16" => Some(DeviceClass::HIGH),
        "iPad13,17" => Some(DeviceClass::HIGH),
        "iPad13,18" => Some(DeviceClass::HIGH),
        "iPad13,19" => Some(DeviceClass::HIGH),
        "iPad14,3" => Some(DeviceClass::HIGH),
        "iPad14,4" => Some(DeviceClass::HIGH),
        "iPad14,5" => Some(DeviceClass::HIGH),
        "iPad14,6" => Some(DeviceClass::HIGH),

        _ => None,
    }
}