Skip to content

Commit a3ce7b8

Browse files
feat: add ability to listed for accessed values
1 parent e72f4ed commit a3ce7b8

File tree

6 files changed

+147
-39
lines changed

6 files changed

+147
-39
lines changed

configurator/lib/src/configuration.dart

+113-32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:configurator/configurator.dart';
2+
import 'package:configurator/src/models/config_access_log.dart';
23
import 'package:configurator/src/utils/change_notifier.dart';
34
import 'package:collection/collection.dart';
5+
import 'package:rxdart/rxdart.dart';
46

57
typedef VoidCallback = void Function();
68

@@ -82,11 +84,11 @@ class Configuration {
8284
}
8385

8486
Future<void> removeLastScopeWhere(
85-
PopScopePredicate predicate, {
86-
bool notify = true,
87-
}) async {
87+
PopScopePredicate predicate, {
88+
bool notify = true,
89+
}) async {
8890
var idx = _scopes.lastIndexWhere((scope) => predicate(scope));
89-
91+
9092
if (idx > -1) {
9193
_scopes.removeAt(idx);
9294
}
@@ -97,32 +99,69 @@ class Configuration {
9799

98100
notifyListeners();
99101
}
100-
102+
101103
bool flag(String id) {
102-
return _scopesSorted.reversed.firstWhereOrNull((s) {
103-
return s.flags.containsKey(id);
104-
})?.flags[id] == true;
104+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
105+
return s.flags.containsKey(id);
106+
});
107+
108+
final value = scope?.flags[id] == true;
109+
110+
if (scope != null) {
111+
publisher.sink.add(
112+
ConfigKeyLog(KeyType.flag, id, value),
113+
);
114+
}
115+
116+
return value;
105117
}
106118

107119
String color(String id) {
108-
return _scopesSorted.reversed.firstWhereOrNull((s) {
109-
return s.colors.containsKey(id);
110-
})?.colors[id] ??
111-
'';
120+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
121+
return s.colors.containsKey(id);
122+
});
123+
124+
final value = scope?.colors[id] ?? '';
125+
126+
if (scope != null) {
127+
publisher.sink.add(
128+
ConfigKeyLog(KeyType.color, id, value),
129+
);
130+
}
131+
132+
return value;
112133
}
113134

114135
String route(int id) {
115-
return _scopesSorted.reversed.firstWhereOrNull((s) {
116-
return s.routes.containsKey(id);
117-
})?.routes[id] ??
118-
'';
136+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
137+
return s.routes.containsKey(id);
138+
});
139+
140+
final value = scope?.routes[id] ?? '';
141+
142+
if (scope != null) {
143+
publisher.sink.add(
144+
ConfigKeyLog(KeyType.route, id, value),
145+
);
146+
}
147+
148+
return value;
119149
}
120150

121151
String image(String id) {
122-
return _scopesSorted.reversed.firstWhereOrNull((s) {
123-
return s.images.containsKey(id);
124-
})?.images[id] ??
125-
'';
152+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
153+
return s.images.containsKey(id);
154+
});
155+
156+
final value = scope?.images[id] ?? '';
157+
158+
if (scope != null) {
159+
publisher.sink.add(
160+
ConfigKeyLog(KeyType.image, id, value),
161+
);
162+
}
163+
164+
return value;
126165
}
127166

128167
List<String> imageList(String id) {
@@ -138,22 +177,51 @@ class Configuration {
138177
}
139178

140179
dynamic misc(String id) {
141-
return _scopesSorted.reversed.firstWhereOrNull((s) {
180+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
142181
return s.misc.containsKey(id);
143-
})?.misc[id];
182+
});
183+
184+
final value = scope?.misc[id];
185+
186+
if (scope != null) {
187+
publisher.sink.add(
188+
ConfigKeyLog(KeyType.misc, id, value),
189+
);
190+
}
191+
192+
return value;
144193
}
145194

146195
Map<String, dynamic> textStyle(String id) {
147-
return _scopesSorted.reversed.firstWhereOrNull((s) {
196+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
148197
return s.textStyles.containsKey(id);
149-
})?.textStyles[id];
198+
});
199+
200+
final value = scope?.textStyles[id];
201+
202+
if (scope != null) {
203+
publisher.sink.add(
204+
ConfigKeyLog(KeyType.textStyle, id, value),
205+
);
206+
}
207+
208+
return value;
150209
}
151210

152211
double size(String id) {
153-
return _scopesSorted.reversed.firstWhereOrNull((s) {
154-
return s.sizes.containsKey(id);
155-
})?.sizes[id] ??
156-
14.0;
212+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
213+
return s.sizes.containsKey(id);
214+
});
215+
216+
final value = scope?.sizes[id] ?? 14.0;
217+
218+
if (scope != null) {
219+
publisher.sink.add(
220+
ConfigKeyLog(KeyType.size, id, value),
221+
);
222+
}
223+
224+
return value;
157225
}
158226

159227
double padding(String id) {
@@ -171,10 +239,19 @@ class Configuration {
171239
}
172240

173241
Map<String, Map<String, String>> currentTranslations(String key) {
174-
return _scopesSorted.reversed.firstWhereOrNull((s) {
175-
return s.translations.isNotEmpty && s.translations.containsKey(key);
176-
})?.translations ??
177-
{};
242+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
243+
return s.translations.isNotEmpty && s.translations.containsKey(key);
244+
});
245+
246+
final value = scope?.translations ?? {};
247+
248+
if (scope != null) {
249+
publisher.sink.add(
250+
ConfigKeyLog(KeyType.string, key, value),
251+
);
252+
}
253+
254+
return value;
178255
}
179256

180257
Map<String, dynamic> get themeMap {
@@ -216,6 +293,10 @@ class Configuration {
216293
/// Not part of public API
217294
Stream<Configuration> watch() => changeNotifier.watch();
218295

296+
final publisher = PublishSubject<ConfigKeyLog>();
297+
298+
Stream<ConfigKeyLog> get accessStream => publisher.stream;
299+
219300
void notifyListeners() {
220301
changeNotifier.notify(this);
221302
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
enum KeyType {
3+
flag,
4+
string,
5+
route,
6+
image,
7+
color,
8+
size,
9+
misc,
10+
textStyle,
11+
}
12+
13+
class ConfigKeyLog<K, V> {
14+
final KeyType type;
15+
final K key;
16+
final V value;
17+
18+
ConfigKeyLog(this.type, this.key, this.value);
19+
}

configurator/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: configurator
22
description: A new Flutter package project.
3-
version: 1.0.8
3+
version: 1.0.9
44
homepage:
55

66
environment:
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_android-2.2.4/","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_windows-2.2.1/","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2024-06-16 00:15:17.530993","version":"3.19.0"}
1+
{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"path_provider_foundation","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"android":[{"name":"path_provider_android","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_android-2.2.4/","native_build":true,"dependencies":[]}],"macos":[{"name":"path_provider_foundation","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_foundation-2.4.0/","shared_darwin_source":true,"native_build":true,"dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_linux-2.2.1/","native_build":false,"dependencies":[]}],"windows":[{"name":"path_provider_windows","path":"/Users/cgiuliani/.pub-cache/hosted/pub.dev/path_provider_windows-2.2.1/","native_build":false,"dependencies":[]}],"web":[]},"dependencyGraph":[{"name":"path_provider","dependencies":["path_provider_android","path_provider_foundation","path_provider_linux","path_provider_windows"]},{"name":"path_provider_android","dependencies":[]},{"name":"path_provider_foundation","dependencies":[]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_windows","dependencies":[]}],"date_created":"2024-06-27 19:31:43.647582","version":"3.19.0"}

configurator_flutter/lib/src/extensions/config_theme.dart

+11-3
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,19 @@ extension ThemeF on Configuration {
2424
}
2525

2626
Color colorValue( String id ) {
27-
String? colorValue = _scopesSorted.reversed.firstWhereOrNull( ( s ) {
27+
final ConfigScope? scope = _scopesSorted.reversed.firstWhereOrNull((s) {
2828
return s.colors.containsKey( id );
29-
})?.colors[ id ];
29+
});
3030

31-
return UIColor( colorValue ?? const Color( 0xFF000000 ) );
31+
final value = scope?.colors[ id ];
32+
33+
if (scope != null) {
34+
publisher.sink.add(
35+
ConfigKeyLog(KeyType.flag, id, value),
36+
);
37+
}
38+
39+
return UIColor( value ?? const Color( 0xFF000000 ) );
3240
}
3341

3442
}

configurator_flutter/pubspec.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: configurator_flutter
22
description: A new Flutter package project.
3-
version: 1.0.8
3+
version: 1.0.9
44
homepage:
55

66
environment:
@@ -31,7 +31,7 @@ dependencies:
3131
git:
3232
url: [email protected]:camrongiuliani/configurator.git
3333
path: configurator
34-
ref: 1.0.8
34+
ref: 1.0.9
3535

3636
dev_dependencies:
3737
flutter_test:

0 commit comments

Comments
 (0)