forked from fossasia/magic-epaper-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_provider.dart
More file actions
145 lines (128 loc) · 4.1 KB
/
display_provider.dart
File metadata and controls
145 lines (128 loc) · 4.1 KB
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
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:magic_epaper_app/model/display_model.dart';
import 'package:magic_epaper_app/util/epd/gdey037z03.dart';
import 'package:magic_epaper_app/util/epd/gdey037z03bw.dart';
class DisplayProvider extends ChangeNotifier {
// Filter options
String _activeFilter = 'All Displays';
int _selectedDisplayIndex = -1; // -1 means no selection
// Getters
String get activeFilter => _activeFilter;
int get selectedDisplayIndex => _selectedDisplayIndex;
bool get hasSelection => _selectedDisplayIndex != -1;
DisplayModel? get selectedDisplay =>
hasSelection ? filteredDisplays[_selectedDisplayIndex] : null;
// All filter options
final List<String> filterOptions = [
'All Displays',
'Color',
'Black & White',
'HD',
];
// List of all available displays
List<DisplayModel> allDisplays = [];
// Constructor - Initialize by loading from JSON
DisplayProvider() {
loadDisplaysFromJson();
}
// Load displays from JSON file
Future<void> loadDisplaysFromJson() async {
try {
// Load the JSON file from the assets
final String jsonString =
await rootBundle.loadString('assets/displays.json');
final Map<String, dynamic> jsonData = json.decode(jsonString);
// Clear the existing displays
allDisplays = [];
// Parse the JSON data
for (var displayData in jsonData['displays']) {
// Convert colors from strings to Color objects
List<Color> colors = [];
for (var colorName in displayData['colors']) {
switch (colorName) {
case 'black':
colors.add(Colors.black);
break;
case 'white':
colors.add(Colors.white);
break;
case 'red':
colors.add(Colors.red);
break;
case 'yellow':
colors.add(Colors.yellow);
break;
}
}
// Determine which EPD to use based on the class name
var epd = _getEpdForClassName(displayData['epdClass']);
// Create a DisplayModel from the JSON data
final display = DisplayModel(
id: displayData['id'],
name: displayData['name'],
size: displayData['size'],
ModelName: displayData['model'],
width: displayData['resolution'][0],
height: displayData['resolution'][1],
colors: colors,
driver: displayData['driver'],
imagePath: displayData['imagePath'],
epd: epd,
);
allDisplays.add(display);
}
// Notify listeners that the data has changed
notifyListeners();
} catch (e) {
print('Error loading displays from JSON: $e');
}
}
// Helper method to get the appropriate EPD based on class name
dynamic _getEpdForClassName(String className) {
switch (className) {
case 'Gdey037z03':
return Gdey037z03();
case 'Gdey037z03BW':
return Gdey037z03BW();
default:
return Gdey037z03(); // Default fallback
}
}
// Get filtered displays based on the active filter
List<DisplayModel> get filteredDisplays {
switch (_activeFilter) {
case 'HD':
return allDisplays.where((display) => display.isHd).toList();
case 'Color':
return allDisplays.where((display) => display.isColor).toList();
case 'Black & White':
return allDisplays.where((display) => !display.isColor).toList();
case 'All Displays':
default:
return allDisplays;
}
}
// Set the active filter
void setFilter(String filter) {
if (_activeFilter != filter) {
_activeFilter = filter;
// Reset selection when filter changes
_selectedDisplayIndex = -1;
notifyListeners();
}
}
// Select a display
void selectDisplay(int index) {
if (index >= 0 && index < filteredDisplays.length) {
_selectedDisplayIndex = index;
notifyListeners();
}
}
// Clear selection
void clearSelection() {
_selectedDisplayIndex = -1;
notifyListeners();
}
}