Skip to content

Commit

Permalink
feat: add flutterfire_remote_parameter_featcher (#11)
Browse files Browse the repository at this point in the history
* feat: add flutterfire_remote_parameter_fetcher

* update: bump firebase_remote_config
  • Loading branch information
naipaka authored Nov 4, 2023
1 parent 2ef1c9a commit fe61bdd
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/flutterfire_remote_parameter_fetcher/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions packages/flutterfire_remote_parameter_fetcher/.metadata
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: "ead455963c12b453cdb2358cad34969c76daf180"
channel: "stable"

project_type: package
3 changes: 3 additions & 0 deletions packages/flutterfire_remote_parameter_fetcher/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 0.1.0

* initial release.
39 changes: 39 additions & 0 deletions packages/flutterfire_remote_parameter_fetcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/guides/libraries/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-library-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/developing-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.

## Usage

TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include: package:altive_lints/altive_lints.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Remote Parameter Fetcher
library remote_parameter_fetcher;

export '../src/remote_parameter_fetcher.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import 'dart:convert';

import 'package:firebase_remote_config/firebase_remote_config.dart';

/// A class that wraps Remote Config.
/// Its role is to "fetch the configured parameters from remote and provide
/// them".
///
/// Exposes [fetchAndActivate] and configuration methods for Remote Config.
class RemoteParameterFetcher {
RemoteParameterFetcher({
FirebaseRemoteConfig? rc,
}) : _rc = rc ?? FirebaseRemoteConfig.instance;

final FirebaseRemoteConfig _rc;

/// Fetch and activate parameters from remote.
Future<bool> fetchAndActivate() async {
return _rc.fetchAndActivate();
}

/// Activate parameters fetched from remote.
Future<bool> activate() async {
return _rc.activate();
}

/// Configure settings related to parameter fetching.
Future<void> configure({
required Duration fetchTimeout,
required Duration minimumFetchInterval,
}) async {
await _rc.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: fetchTimeout,
minimumFetchInterval: minimumFetchInterval,
),
);
}

/// Set default values for when parameters cannot be fetched from remote.
Future<void> setDefaults(Map<String, Object?> defaultParameters) async {
for (final p in defaultParameters.values) {
assert(p is String || p is int || p is double || p is bool);
}
await _rc.setDefaults(defaultParameters);
}

/// Provide a Stream of updated parameter information.
Stream<RemoteConfigUpdate> get onConfigUpdated {
return _rc.onConfigUpdated;
}

String getString(String key) {
final value = _rc.getString(key);
return value;
}

int getInt(String key) {
final value = _rc.getInt(key);
return value;
}

double getDouble(String key) {
final value = _rc.getDouble(key);
return value;
}

bool getBool(String key) {
final value = _rc.getBool(key);
return value;
}

/// Returns a JSON string converted to a [Map] type.
Map<String, Object?> getJson(String key) {
final value = _rc.getString(key);
return json.decode(value) as Map<String, dynamic>;
}

/// Returns a List JSON string converted to a [List] type.
List<Map<String, Object?>> getListJson(String key) {
final value = _rc.getString(key);
final list = json.decode(value) as List<dynamic>;
return list.map((dynamic e) => e as Map<String, Object?>).toList();
}

/// Class Object from JSON.
T getData<T extends Object>({
required String key,
required T Function(Map<String, Object?>) fromJson,
}) {
final json = getJson(key);
return fromJson(json);
}
}
16 changes: 16 additions & 0 deletions packages/flutterfire_remote_parameter_fetcher/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: flutterfire_remote_parameter_fetcher
description: flutterfire_remote_parameter_fetcher with FlutterFire Remote Config.
publish_to: "none"
version: 0.1.0

environment:
sdk: ^3.0.0

dependencies:
firebase_remote_config: ^4.3.3
meta: ^1.9.1

dev_dependencies:
altive_lints: ^1.8.1
flutter_test:
sdk: flutter
24 changes: 24 additions & 0 deletions packages/flutterfire_remote_parameter_fetcher/test/data_class.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:meta/meta.dart';

@immutable
class DataClass {
const DataClass({required this.value});

factory DataClass.fromJson(Map<String, dynamic> json) =>
DataClass(value: json['value'] as String);

final String value;

@override
bool operator ==(Object other) =>
identical(this, other) ||
other is DataClass &&
runtimeType == other.runtimeType &&
value == other.value;

@override
int get hashCode => value.hashCode;

@override
String toString() => '_Data(value: $value)';
}
Loading

0 comments on commit fe61bdd

Please sign in to comment.