@@ -3,6 +3,7 @@ import 'dart:developer';
33
44import 'package:firebase_crashlytics/firebase_crashlytics.dart' ;
55import 'package:firebase_remote_config/firebase_remote_config.dart' ;
6+ import 'package:flutter/foundation.dart' ;
67import 'package:flutter/services.dart' ;
78import 'package:freecodecamp/utils/upgrade_controller.dart' ;
89import 'package:upgrader/upgrader.dart' ;
@@ -11,13 +12,20 @@ class RemoteConfigService {
1112 static final RemoteConfigService _instance = RemoteConfigService ._internal ();
1213
1314 static final remoteConfig = FirebaseRemoteConfig .instance;
14- static const _superblockActivationKey = 'superblock_activation_overrides ' ;
15- static const _blockActivationKey = 'block_activation_overrides' ;
15+ static const _activationOverridesKey = 'activation_overrides ' ;
16+ final String Function ( String key) _getConfigString ;
1617
1718 factory RemoteConfigService () {
1819 return _instance;
1920 }
2021
22+ @visibleForTesting
23+ factory RemoteConfigService .withConfigReader (
24+ String Function (String key) getConfigString,
25+ ) {
26+ return RemoteConfigService ._internal (getConfigString: getConfigString);
27+ }
28+
2129 Future <void > init () async {
2230 try {
2331 await remoteConfig.setConfigSettings (
@@ -28,8 +36,7 @@ class RemoteConfigService {
2836 );
2937 await remoteConfig.setDefaults ({
3038 'min_app_version' : '7.3.0' ,
31- _superblockActivationKey: '{}' ,
32- _blockActivationKey: '{}' ,
39+ _activationOverridesKey: '{}' ,
3340 });
3441
3542 await remoteConfig.fetchAndActivate ();
@@ -59,71 +66,36 @@ class RemoteConfigService {
5966 }
6067 }
6168
62- RemoteConfigService ._internal ();
63-
64- bool isSuperBlockActive (
65- String dashedName, {
66- required bool fallbackValue,
67- }) {
68- final override = getSuperBlockActivationOverride (dashedName);
69- return override ?? fallbackValue;
70- }
71-
72- bool ? getSuperBlockActivationOverride (String dashedName) {
73- return _getOverrideValue (
74- configKey: _superblockActivationKey,
75- key: dashedName,
76- );
77- }
69+ RemoteConfigService ._internal ({
70+ String Function (String key)? getConfigString,
71+ }) : _getConfigString = getConfigString ?? remoteConfig.getString;
7872
79- bool isBlockActive ({
73+ bool isActive ({
8074 required String superBlockDashedName,
81- required String blockDashedName,
82- bool fallbackValue = true ,
75+ String ? blockDashedName,
76+ required bool fallbackValue,
8377 }) {
84- final override = getBlockActivationOverride (
78+ final override = getActivationOverride (
8579 superBlockDashedName: superBlockDashedName,
8680 blockDashedName: blockDashedName,
8781 );
8882 return override ?? fallbackValue;
8983 }
9084
91- bool ? getBlockActivationOverride ({
85+ bool ? getActivationOverride ({
9286 required String superBlockDashedName,
93- required String blockDashedName,
87+ String ? blockDashedName,
9488 }) {
95- final overrides = _getOverridesMap (_blockActivationKey);
96- if (overrides == null ) {
97- return null ;
98- }
99-
100- final superBlockScopedKey = '$superBlockDashedName /$blockDashedName ' ;
101- final directScopedOverride = overrides[superBlockScopedKey];
102- if (directScopedOverride is bool ) {
103- return directScopedOverride;
104- }
105-
106- final scopedOverrides = overrides[superBlockDashedName];
107- if (scopedOverrides is Map ) {
108- final nestedOverride = scopedOverrides[blockDashedName];
109- if (nestedOverride is bool ) {
110- return nestedOverride;
111- }
112- }
113-
114- final directOverride = overrides[blockDashedName];
115- if (directOverride is bool ) {
116- return directOverride;
117- }
118-
119- return null ;
89+ final overrideKey = blockDashedName == null
90+ ? superBlockDashedName
91+ : '$superBlockDashedName /$blockDashedName ' ;
92+ return _getOverrideValue (key: overrideKey);
12093 }
12194
12295 bool ? _getOverrideValue ({
123- required String configKey,
12496 required String key,
12597 }) {
126- final overrides = _getOverridesMap (configKey );
98+ final overrides = _getOverridesMap ();
12799 if (overrides == null ) {
128100 return null ;
129101 }
@@ -136,9 +108,9 @@ class RemoteConfigService {
136108 return null ;
137109 }
138110
139- Map <String , dynamic >? _getOverridesMap (String configKey ) {
111+ Map <String , dynamic >? _getOverridesMap () {
140112 try {
141- final String rawConfig = remoteConfig. getString (configKey );
113+ final String rawConfig = _getConfigString (_activationOverridesKey );
142114 if (rawConfig.trim ().isEmpty) {
143115 return null ;
144116 }
@@ -150,7 +122,7 @@ class RemoteConfigService {
150122 return decoded;
151123 } catch (exception) {
152124 log (
153- 'Invalid remote config for $configKey . '
125+ 'Invalid remote config for $_activationOverridesKey . '
154126 'Ignoring overrides: $exception ' ,
155127 );
156128 return null ;
0 commit comments