Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds unleash for feature flagging #11

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/core/config/unleash_config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:unleash/unleash.dart';
import 'package:unplash_sample/core/utils/string_constants.dart';

abstract class UnleashConfig {
bool get isDetailsPageEnabled;
}

class UnleashConfigImp extends UnleashConfig {
final Unleash unleash;

UnleashConfigImp(this.unleash);

@override
bool get isDetailsPageEnabled => unleash.isEnabled(isImageDetailsEnabled, defaultValue: false);
}
1 change: 1 addition & 0 deletions lib/core/utils/string_constants.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const String serverErrorMessage = "Server error, please try agian later!";
const String isImageDetailsEnabled = "isImageDetailsEnabled";
18 changes: 17 additions & 1 deletion lib/dependency_injection.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'package:dio/dio.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:get_it/get_it.dart';
import 'package:unleash/unleash.dart';
import 'package:unplash_sample/core/config/unleash_config.dart';
import 'package:unplash_sample/features/home/data/datasources/unsplash_remote_datasource.dart';
import 'package:unplash_sample/features/home/data/repositories/unsplash_repository_impl.dart';
import 'package:unplash_sample/features/home/domain/repositories/unsplash_repository.dart';
Expand All @@ -11,7 +14,7 @@ class DependencyInjection {

static GetIt get getIt => GetIt.instance;

static void initialize() {
static Future<void> initialize() async {
getIt.registerFactory(() => UnsplashImageBloc(getIt()));

getIt.registerLazySingleton(() => FetchImages(repository: getIt()));
Expand All @@ -24,6 +27,19 @@ class DependencyInjection {
() => UnsplashRemoteDataSourceImpl(getIt()),
);

final unleash = await Unleash.init(
UnleashSettings(
appName: "unplash_demo",
instanceId: "instanceId",
// TODO(AyushBherwani1998): Fix me
unleashApi: Uri.parse(""),
apiToken: dotenv.env["UNLEASH_API_KEY"] as String,
),
);

getIt.registerLazySingleton(() => unleash);
getIt.registerLazySingleton(() => UnleashConfigImp(getIt()));

getIt.registerLazySingleton(() => Dio());
}
}
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import 'package:unplash_sample/features/home/presentation/pages/home_page.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
DependencyInjection.initialize();
await dotenv.load(fileName: "./lib/core/.env");
await DependencyInjection.initialize();
runApp(const MyApp());
}

Expand Down
12 changes: 10 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ packages:
dependency: transitive
description:
name: http
sha256: "759d1a329847dd0f39226c688d3e06a6b8679668e350e2891a6474f8b4bb8525"
sha256: "5895291c13fa8a3bd82e76d5627f69e0d85ca6a30dcac95c4ea19a5d555879c2"
url: "https://pub.dev"
source: hosted
version: "1.1.0"
version: "0.13.6"
http_multi_server:
dependency: transitive
description:
Expand Down Expand Up @@ -645,6 +645,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
unleash:
dependency: "direct main"
description:
name: unleash
sha256: "59845a6846509205de91238fc227c965781eacd0f480fc998ed19a97a2ead170"
url: "https://pub.dev"
source: hosted
version: "0.4.1"
uuid:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies:
get_it: ^7.6.0
go_router: ^10.0.0
mocktail: ^0.3.0
unleash: ^0.4.1


# The following adds the Cupertino Icons font to your application.
Expand Down
39 changes: 39 additions & 0 deletions test/core/config/unleash_config_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:unleash/unleash.dart';
import 'package:unplash_sample/core/config/unleash_config.dart';
import 'package:unplash_sample/core/utils/string_constants.dart';

import '../mocks/unleash_mock.dart';

void main() {
late final UnleashConfig unleashConifg;
late final Unleash unleashMock;

group('UnleashConfig tests', () {
setUpAll(() {
unleashMock = UnleashMock();
unleashConifg = UnleashConfigImp(unleashMock);
});

test(
"returns true if the remote flag for isImageDetailsEnabled if true",
() {
when(() => unleashMock.isEnabled(isImageDetailsEnabled))
.thenReturn(true);

expect(unleashConifg.isDetailsPageEnabled, isTrue);
},
);

test(
"returns false if the remote flag for isImageDetailsEnabled if false",
() {
when(() => unleashMock.isEnabled(isImageDetailsEnabled))
.thenReturn(false);

expect(unleashConifg.isDetailsPageEnabled, isFalse);
},
);
});
}
4 changes: 4 additions & 0 deletions test/core/mocks/unleash_mock.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:mocktail/mocktail.dart';
import 'package:unleash/unleash.dart';

class UnleashMock extends Mock implements Unleash {}
Loading