@@ -11,6 +11,8 @@ import 'package:uuid/uuid.dart';
1111
1212import '../../../platform/platformUtils.dart' ;
1313
14+ enum PresetChangeDirection { previous, next }
15+
1416class PresetsStorage extends ChangeNotifier {
1517 static final PresetsStorage _storage = PresetsStorage ._();
1618 static const presetsFile = "presets.json" ;
@@ -150,6 +152,29 @@ class PresetsStorage extends ChangeNotifier {
150152 return null ;
151153 }
152154
155+ dynamic findAdjacentPreset (
156+ String uuid, bool acrossCategories, PresetChangeDirection direction) {
157+ int catsCount = presetsData.length;
158+ int catIndex = 0 ;
159+
160+ if (uuid.isEmpty) {
161+ return _getAdjacentPreset (0 , - 1 , direction, acrossCategories);
162+ }
163+ for (catIndex = 0 ; catIndex < catsCount; catIndex++ ) {
164+ int pIndex = 0 ;
165+ var cat = presetsData[catIndex];
166+ var presets = cat["presets" ];
167+ int pCount = presets.length;
168+ for (pIndex = 0 ; pIndex < pCount; pIndex++ ) {
169+ if (presets[pIndex]? ["uuid" ] == uuid) {
170+ return _getAdjacentPreset (
171+ catIndex, pIndex, direction, acrossCategories);
172+ }
173+ }
174+ }
175+ return _getAdjacentPreset (0 , - 1 , direction, acrossCategories);
176+ }
177+
153178 Map <String , dynamic >? findCategoryOfPreset (Map <String , dynamic > preset) {
154179 for (var cat in presetsData) {
155180 for (var pr in cat["presets" ]) {
@@ -410,6 +435,44 @@ class PresetsStorage extends ChangeNotifier {
410435 if (_name != null ) savePreset (presetData, _name, category);
411436 }
412437
438+ dynamic _getAdjacentPreset (int catIndex, int pIndex,
439+ PresetChangeDirection direction, bool acrossCategory) {
440+ if (presetsData.length <= catIndex) return null ;
441+ var catLength = presetsData[catIndex]["presets" ].length;
442+ if (direction == PresetChangeDirection .previous) {
443+ pIndex-- ;
444+ } else {
445+ pIndex++ ;
446+ }
447+
448+ if (pIndex < 0 ) {
449+ if (! acrossCategory) {
450+ pIndex = catLength - 1 ;
451+ } else {
452+ do {
453+ catIndex-- ;
454+ if (catIndex < 0 ) {
455+ catIndex = presetsData.length - 1 ;
456+ }
457+ } while (presetsData[catIndex]["presets" ].length == 0 );
458+ pIndex = presetsData[catIndex]["presets" ].length - 1 ;
459+ }
460+ } else if (pIndex >= catLength) {
461+ if (! acrossCategory) {
462+ pIndex = 0 ;
463+ } else {
464+ do {
465+ catIndex++ ;
466+ if (catIndex >= presetsData.length) {
467+ catIndex = 0 ;
468+ }
469+ } while (presetsData[catIndex]["presets" ].length == 0 );
470+ pIndex = 0 ;
471+ }
472+ }
473+ return presetsData[catIndex]["presets" ][pIndex];
474+ }
475+
413476 String ? _findFreeName (String name, String category) {
414477 for (int i = 1 ; i < 1000 ; i++ ) {
415478 RegExp regex = RegExp (r"\((\d+)\)$" );
0 commit comments