Skip to content

Commit

Permalink
feat: implement web support for Firebase Remote Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Salakar committed Sep 16, 2021
1 parent ac62214 commit 070e5e2
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ dependency_overrides:
path: ../../../firebase_core/firebase_core_web
firebase_remote_config_platform_interface:
path: ../../firebase_remote_config_platform_interface
firebase_remote_configweb:
path: ../../firebase_remote_configweb

dev_dependencies:
drive: 0.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ dependencies:
firebase_core: ^1.6.0
firebase_core_platform_interface: ^4.0.1
firebase_remote_config_platform_interface: ^0.3.0+5
firebase_remote_config_web: ^0.1.0
flutter:
sdk: flutter

Expand All @@ -35,3 +36,5 @@ flutter:
pluginClass: FLTFirebaseRemoteConfigPlugin
macos:
pluginClass: FLTFirebaseRemoteConfigPlugin
web:
default_package: firebase_remote_config_web
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 659dc8129d4edb9166e9a0d600439d135740933f
channel: beta

project_type: plugin
27 changes: 27 additions & 0 deletions packages/firebase_remote_config/firebase_remote_config_web/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2017-2020 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# firebase_remote_config_web

The web implementation of `firebase_remote_config`.

## Getting Started

To get started with Firebase Remote Config on Web,
please [see the documentation](https://firebase.flutter.dev/docs/remote-config/overview)
available at [https://firebase.flutter.dev](https://firebase.flutter.dev)

Once installed, Firebase Remote Config needs to be configured for Web Installation.
Please [see the documentation](https://firebase.flutter.dev/docs/remote-config/overview#3-web-only-add-the-sdk) on Web
Installation.

To learn more about Firebase Remote Config, please visit
the [Firebase website](https://firebase.google.com/products/remote-config).
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2021 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.

import 'package:firebase_remote_config_platform_interface/firebase_remote_config_platform_interface.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:firebase_core_web/firebase_core_web_interop.dart'
as core_interop;
import 'interop/firebase_remote_config.dart' as remote_config_interop;

/// Web implementation of [FirebaseRemoteConfigPlatform].
class FirebaseRemoteConfigWeb extends FirebaseRemoteConfigPlatform {
/// The entry point for the [FirebaseRemoteConfigWeb] class.
FirebaseRemoteConfigWeb({FirebaseApp? app})
: _webRemoteConfig = remote_config_interop.getRemoteConfigInstance(
core_interop.app(app?.name)),
super(appInstance: app);

/// Stub initializer to allow the [registerWith] to create an instance without
/// registering the web delegates or listeners.
FirebaseRemoteConfigWeb._()
: _webRemoteConfig = null,
super(appInstance: null);

/// Instance of functions from the web plugin
final remote_config_interop.RemoteConfig? _webRemoteConfig;

/// Create the default instance of the [FirebaseRemoteConfigPlatform] as a [FirebaseRemoteConfigWeb]
static void registerWith(Registrar registrar) {
FirebaseRemoteConfigPlatform.instance = FirebaseRemoteConfigWeb.instance;
}

/// Returns an instance of [FirebaseRemoteConfigWeb].
static FirebaseRemoteConfigWeb get instance {
return FirebaseRemoteConfigWeb._();
}

@override
FirebaseRemoteConfigPlatform delegateFor({FirebaseApp? app}) {
return FirebaseRemoteConfigWeb(app: app);
}


/// 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.
@override
DateTime get lastFetchTime {
return _webRemoteConfig!.fetchTime;
}

/// Returns the status of the last fetch attempt.
@override
RemoteConfigFetchStatus get lastFetchStatus {
switch (_webRemoteConfig!.lastFetchStatus) {
case remote_config_interop.RemoteConfigFetchStatus.failure:
return RemoteConfigFetchStatus.failure;
case remote_config_interop.RemoteConfigFetchStatus.success:
return RemoteConfigFetchStatus.success;
case remote_config_interop.RemoteConfigFetchStatus.notFetchedYet:
return RemoteConfigFetchStatus.noFetchYet;
case remote_config_interop.RemoteConfigFetchStatus.throttle:
return RemoteConfigFetchStatus.throttle;
}
}

/// Returns the [RemoteConfigSettings] of the current instance.
@override
RemoteConfigSettings get settings {
return RemoteConfigSettings(
fetchTimeout: _webRemoteConfig!.settings.fetchTimeoutMillis,
minimumFetchInterval: _webRemoteConfig!.settings.minimumFetchInterval,);
}

/// 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.
@override
Future<bool> activate() {
return _webRemoteConfig!.activate();
}

/// Ensures the last activated config are available to getters.
@override
Future<void> ensureInitialized() {
return _webRemoteConfig!.ensureInitialized();
}

/// Fetches and caches configuration from the Remote Config service.
@override
Future<void> fetch() {
return _webRemoteConfig!.fetch();
}

/// Performs a fetch and activate operation, as a convenience.
///
/// Returns [bool] in the same way that is done for [activate].
@override
Future<bool> fetchAndActivate() {
return _webRemoteConfig!.fetchAndActivate();
}

/// Returns a Map of all Remote Config parameters.
@override
Map<String, RemoteConfigValue> getAll() {
// TODO implement
throw UnimplementedError('getAll() is not implemented');
}

/// Gets the value for a given key as a bool.
@override
bool getBool(String key) {
return _webRemoteConfig!.getBoolean(key);
}

/// Gets the value for a given key as an int.
@override
int getInt(String key) {
return _webRemoteConfig!.getNumber(key).toInt();
}

/// Gets the value for a given key as a double.
@override
double getDouble(String key) {
return _webRemoteConfig!.getNumber(key).toDouble();
}

/// Gets the value for a given key as a String.
@override
String getString(String key) {
return _webRemoteConfig!.getString(key);
}

/// Gets the [RemoteConfigValue] for a given key.
@override
RemoteConfigValue getValue(String key) {
// TODO implement
throw UnimplementedError('getValue() is not implemented');
}

/// Sets the [RemoteConfigSettings] for the current instance.
@override
Future<void> setConfigSettings(RemoteConfigSettings remoteConfigSettings) {
_webRemoteConfig!.settings.minimumFetchInterval = remoteConfigSettings.minimumFetchInterval;
_webRemoteConfig!.settings.fetchTimeoutMillis = remoteConfigSettings.fetchTimeout;
return Future<void>.value();
}

/// Sets the default parameter values for the current instance.
@override
Future<void> setDefaults(Map<String, dynamic> defaultParameters) {
_webRemoteConfig!.defaultConfig = defaultParameters;
return Future<void>.value();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2021, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: public_member_api_docs

@JS('firebase')
library firebase_interop.firebase;

import 'package:firebase_core_web/firebase_core_web_interop.dart';

import 'package:js/js.dart';
import 'firebase_remote_config_interop.dart';

@JS()
external RemoteConfigJsImpl remoteConfig([AppJsImpl app]);
Loading

0 comments on commit 070e5e2

Please sign in to comment.