-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
firebase_remote_config.dart
177 lines (149 loc) · 6.02 KB
/
firebase_remote_config.dart
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of firebase_remote_config;
/// The entry point for accessing Remote Config.
///
/// You can get an instance by calling [FirebaseRemoteConfig.instance]. Note
/// [FirebaseRemoteConfig.instance] is async.
// ignore: prefer_mixin
class FirebaseRemoteConfig extends FirebasePluginPlatform with ChangeNotifier {
FirebaseRemoteConfig._({required this.app})
: super(app.name, 'plugins.flutter.io/firebase_remote_config');
// Cached instances of [FirebaseRemoteConfig].
static final Map<String, FirebaseRemoteConfig>
_firebaseRemoteConfigInstances = {};
/// Returns the underlying delegate implementation.
///
/// If called and no [_delegatePackingProperty] exists, it will first be
/// created and assigned before returning the delegate.
late final _delegate = FirebaseRemoteConfigPlatform.instanceFor(
app: app,
pluginConstants: pluginConstants,
);
/// The [FirebaseApp] this instance was initialized with.
final FirebaseApp app;
/// Returns an instance using the default [FirebaseApp].
static FirebaseRemoteConfig get instance {
return FirebaseRemoteConfig.instanceFor(app: Firebase.app());
}
/// Returns an instance using the specified [FirebaseApp].
static FirebaseRemoteConfig instanceFor({required FirebaseApp app}) {
return _firebaseRemoteConfigInstances.putIfAbsent(app.name, () {
final instance = FirebaseRemoteConfig._(app: app);
FirebasePluginPlatform.verify(instance);
return instance;
});
}
/// Returns the [DateTime] of the last successful fetch.
///
/// If no successful fetch has been made a [DateTime] representing
/// the epoch (1970-01-01 UTC) is returned.
DateTime get lastFetchTime {
return _delegate.lastFetchTime;
}
/// Returns the status of the last fetch attempt.
RemoteConfigFetchStatus get lastFetchStatus {
return _delegate.lastFetchStatus;
}
/// Returns the [RemoteConfigSettings] of the current instance.
RemoteConfigSettings get settings {
return _delegate.settings;
}
/// Makes the last fetched config available to getters.
///
/// Returns a [bool] that is true if the config parameters
/// were activated. Returns a [bool] that is false if the
/// config parameters were already activated.
Future<bool> activate() async {
bool configChanged = await _delegate.activate();
notifyListeners();
return configChanged;
}
/// Ensures the last activated config are available to getters.
Future<void> ensureInitialized() {
return _delegate.ensureInitialized();
}
/// Fetches and caches configuration from the Remote Config service.
Future<void> fetch() {
return _delegate.fetch();
}
/// Performs a fetch and activate operation, as a convenience.
///
/// Returns [bool] in the same way that is done for [activate].
Future<bool> fetchAndActivate() async {
bool configChanged = await _delegate.fetchAndActivate();
notifyListeners();
return configChanged;
}
/// Returns a Map of all Remote Config parameters.
Map<String, RemoteConfigValue> getAll() {
return _delegate.getAll();
}
/// Gets the value for a given key as a bool.
bool getBool(String key) {
return _delegate.getBool(key);
}
/// Gets the value for a given key as an int.
int getInt(String key) {
return _delegate.getInt(key);
}
/// Gets the value for a given key as a double.
double getDouble(String key) {
return _delegate.getDouble(key);
}
/// Gets the value for a given key as a String.
String getString(String key) {
return _delegate.getString(key);
}
/// Gets the [RemoteConfigValue] for a given key.
RemoteConfigValue getValue(String key) {
return _delegate.getValue(key);
}
/// Sets the [RemoteConfigSettings] for the current instance.
Future<void> setConfigSettings(RemoteConfigSettings remoteConfigSettings) {
assert(!remoteConfigSettings.fetchTimeout.isNegative);
assert(!remoteConfigSettings.minimumFetchInterval.isNegative);
// To be consistent with iOS fetchTimeout is set to the default
// 1 minute (60 seconds) if an attempt is made to set it to zero seconds.
if (remoteConfigSettings.fetchTimeout.inSeconds == 0) {
remoteConfigSettings.fetchTimeout = const Duration(seconds: 60);
}
return _delegate.setConfigSettings(remoteConfigSettings);
}
/// Sets the default parameter values for the current instance.
/// Only booleans, strings and numbers are supported as values of the map
Future<void> setDefaults(Map<String, dynamic> defaultParameters) {
defaultParameters.forEach(_checkIsSupportedType);
return _delegate.setDefaults(defaultParameters);
}
void _checkIsSupportedType(String key, dynamic value) {
if (value is! bool && value is! num && value is! String) {
throw ArgumentError(
'Invalid value type "${value.runtimeType}" for key "$key". '
'Only booleans, numbers and strings are supported as config values. '
"If you're trying to pass a json object – convert it to string beforehand",
);
}
}
}
@Deprecated('Use FirebaseRemoteConfig instead.')
class RemoteConfig extends FirebaseRemoteConfig {
@Deprecated('Use FirebaseRemoteConfig instead.')
/// The [FirebaseApp] this instance was initialized with.
RemoteConfig._({required FirebaseApp app}) : super._(app: app);
// Cached instances of [RemoteConfig].
static final Map<String, RemoteConfig> _firebaseRemoteConfigInstances = {};
/// Returns an instance using the default [FirebaseApp].
@Deprecated('Use FirebaseRemoteConfig.instance instead.')
static RemoteConfig get instance {
return RemoteConfig.instanceFor(app: Firebase.app());
}
/// Returns an instance using the specified [FirebaseApp].
@Deprecated('Use FirebaseRemoteConfig.instanceFor instead.')
static RemoteConfig instanceFor({required FirebaseApp app}) {
return _firebaseRemoteConfigInstances.putIfAbsent(app.name, () {
return RemoteConfig._(app: app);
});
}
}