Skip to content

Commit

Permalink
refactor contributor; fix auth
Browse files Browse the repository at this point in the history
refactor contributor feature
fix authentication store
remove upgrader library
  • Loading branch information
marfavi committed Feb 6, 2024
1 parent b581ae6 commit 485ebfc
Show file tree
Hide file tree
Showing 23 changed files with 290 additions and 576 deletions.
3 changes: 0 additions & 3 deletions lib/core/store/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ part 'crate.dart';
///
/// Powered by Hive and FlutterSecureStorage to provide both encrypted and
/// unencrypted storage.
///
/// Store is a singleton and should first be initialized with [init] before
/// being accessed through [instance].
class Store {
const Store({
required FlutterSecureStorage secureStorage,
Expand Down
22 changes: 0 additions & 22 deletions lib/core/widgets/upgrade_alert.dart

This file was deleted.

59 changes: 28 additions & 31 deletions lib/features/login/presentation/pages/login_page_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:coffeecard/core/styles/app_text_styles.dart';
import 'package:coffeecard/core/widgets/components/helpers/responsive.dart';
import 'package:coffeecard/core/widgets/components/scaffold.dart';
import 'package:coffeecard/core/widgets/images/analog_logo.dart';
import 'package:coffeecard/core/widgets/upgrade_alert.dart';
import 'package:coffeecard/features/login/presentation/widgets/login_input_hint.dart';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
Expand All @@ -28,37 +27,35 @@ class LoginPageBase extends StatelessWidget {

@override
Widget build(BuildContext context) {
return UpgradeAlert(
child: AppScaffold.withoutTitle(
resizeToAvoidBottomInset: resizeToAvoidBottomInset,
body: Column(
children: [
Expanded(
child: Column(
children: [
Gap(deviceIsSmall(context) ? 12 : 64),
const AnalogLogo(),
const Gap(16),
Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: AppTextStyle.pageTitle,
),
const Gap(16),
inputWidget,
const Gap(16),
LoginInputHint(defaultHint: defaultHint, error: error),
const Gap(12),
...ctaChildren,
const Gap(12),
],
),
return AppScaffold.withoutTitle(
resizeToAvoidBottomInset: resizeToAvoidBottomInset,
body: Column(
children: [
Expanded(
child: Column(
children: [
Gap(deviceIsSmall(context) ? 12 : 64),
const AnalogLogo(),
const Gap(16),
Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: AppTextStyle.pageTitle,
),
const Gap(16),
inputWidget,
const Gap(16),
LoginInputHint(defaultHint: defaultHint, error: error),
const Gap(12),
...ctaChildren,
const Gap(12),
],
),
if (bottomWidget != null) bottomWidget!,
],
),
),
if (bottomWidget != null) bottomWidget!,
],
),
);
}
Expand Down
37 changes: 17 additions & 20 deletions lib/features/ticket/presentation/pages/tickets_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:coffeecard/core/strings.dart';
import 'package:coffeecard/core/widgets/components/barista_perks_section.dart';
import 'package:coffeecard/core/widgets/components/scaffold.dart';
import 'package:coffeecard/core/widgets/upgrade_alert.dart';
import 'package:coffeecard/features/product.dart';
import 'package:coffeecard/features/ticket/presentation/widgets/shop_section.dart';
import 'package:coffeecard/features/ticket/presentation/widgets/tickets_section.dart';
Expand All @@ -25,26 +24,24 @@ class TicketsPage extends StatelessWidget {
final user = (context.read<UserCubit>().state as UserLoaded).user;
final perksAvailable = context.read<PurchasableProducts>().perks.isNotEmpty;

return UpgradeAlert(
child: AppScaffold.withTitle(
title: Strings.ticketsPageTitle,
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: ListView(
controller: scrollController,
shrinkWrap: true,
padding: const EdgeInsets.all(16.0),
children: [
const TicketSection(),
if (perksAvailable) BaristaPerksSection(userRole: user.role),
const ShopSection(),
],
),
return AppScaffold.withTitle(
title: Strings.ticketsPageTitle,
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: ListView(
controller: scrollController,
shrinkWrap: true,
padding: const EdgeInsets.all(16.0),
children: [
const TicketSection(),
if (perksAvailable) BaristaPerksSection(userRole: user.role),
const ShopSection(),
],
),
],
),
),
],
),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/main_development.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Future<void> main() async {
await Hive.initFlutter();

await Firebase.initializeApp(options: DefaultFirebaseOptions.development);
configureServices();
await configureServices();
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;

runApp(App());
Expand Down
2 changes: 1 addition & 1 deletion lib/main_production.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Future<void> main() async {
await Hive.initFlutter();

await Firebase.initializeApp(options: DefaultFirebaseOptions.production);
configureServices();
await configureServices();
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;

runApp(App());
Expand Down
139 changes: 73 additions & 66 deletions lib/service_locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:coffeecard/core/network/network_request_executor.dart';
import 'package:coffeecard/core/store/store.dart';
import 'package:coffeecard/env/env.dart';
import 'package:coffeecard/features/authentication.dart';
import 'package:coffeecard/features/contributor.dart';
import 'package:coffeecard/features/environment/data/datasources/environment_remote_data_source.dart';
import 'package:coffeecard/features/environment/domain/usecases/get_environment_type.dart';
import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart';
Expand Down Expand Up @@ -60,25 +59,84 @@ import 'package:get_it/get_it.dart';
import 'package:hive_flutter/hive_flutter.dart' hide Box;
import 'package:logger/logger.dart';

// FIXME: Get rid of this file.
final GetIt sl = GetIt.instance;

void configureServices() {
ignoreValue(sl.registerSingleton(Logger()));
Future<void> configureServices() async {
sl.registerSingleton(Logger());
sl.registerSingleton(const FlutterSecureStorage());
sl.registerSingleton(Store(secureStorage: sl(), hive: Hive));

final authRepoCrate =
await sl.get<Store>().openEncryptedCrate<String>('auth').run();
sl.registerLazySingleton<AuthenticationRepository>(
() => AuthenticationRepository(crate: authRepoCrate, logger: sl()),
);
sl.registerLazySingleton(() => AuthenticationCubit(sl()));

sl.registerFactory(() => OpeningHoursCubit(fetchOpeningHours: sl()));
sl.registerFactory(() => GetOpeningHours(repository: sl()));
sl.registerLazySingleton(() => OpeningHoursLocalDataSource());
sl.registerFactory<OpeningHoursRepository>(
() => OpeningHoursRepositoryImpl(dataSource: sl(), dateService: sl()),
);

sl.registerFactory(() => OccupationCubit(getOccupations: sl()));
sl.registerFactory(() => GetOccupations(dataSource: sl()));
sl.registerLazySingleton(
() => OccupationRemoteDataSource(api: sl(), executor: sl()),
);

sl.registerFactory(
() => UserCubit(
getUser: sl(),
requestAccountDeletion: sl(),
updateUserDetails: sl(),
),
);
sl.registerFactory(
() => LoadTickets(ticketRemoteDataSource: sl(), productRepository: sl()),
);
final consumeTicketCrate =
await sl<Store>().openCrate<int>('lastUsedMenuItemByProductId').run();
sl.registerFactory(
() => ConsumeTicket(
ticketRemoteDataSource: sl(),
crate: consumeTicketCrate,
),
);
sl.registerLazySingleton(
() => TicketRemoteDataSource(api: sl(), executor: sl()),
);
sl.registerFactory(() => GetUser(dataSource: sl()));
sl.registerFactory(() => RequestAccountDeletion(dataSource: sl()));
sl.registerFactory(() => UpdateUserDetails(dataSource: sl()));
sl.registerLazySingleton(
() => UserRemoteDataSource(apiV2: sl(), executor: sl()),
);

initReceipt();
initTicket();
initPayment();
initLeaderboard();
initEnvironment();
initProduct();
initVoucher();
initLogin();
initRegister();

initFeatures();
initExternal();

// Reactivation authenticator (uninitalized), http client and interceptors
initHttp();

// provide the account repository to the reactivation authenticator
sl<RetryAuthenticator>().initialize(sl<AccountRemoteDataSource>());

await sl.allReady();
}

void initExternal() {
ignoreValue(sl.registerSingleton(const FlutterSecureStorage()));
ignoreValue(sl.registerSingleton(Store(secureStorage: sl(), hive: Hive)));

ignoreValue(sl.registerFactory(() => DateService()));
ignoreValue(sl.registerFactory(() => ScreenBrightness()));
ignoreValue(sl.registerLazySingleton(() => ExternalUrlLauncher()));
Expand All @@ -97,14 +155,11 @@ void initExternal() {
);
}

void initFeatures() {
initAuthentication();
initOpeningHours();
Future<void> initFeatures() async {
initOccupation();
initUser();
await initUser();
initReceipt();
initTicket();
initContributor();
initPayment();
initLeaderboard();
initEnvironment();
Expand All @@ -114,46 +169,6 @@ void initFeatures() {
initRegister();
}

void initAuthentication() {
// bloc
sl.registerLazySingleton(
() => AuthenticationCubit(sl()),
);

// repository
sl.registerLazySingletonAsync<AuthenticationRepository>(
() async => AuthenticationRepository(
crate: await sl<Store>()
.openEncryptedCrate<AuthenticationInfo>('auth')
.run(),
logger: sl(),
),
);
}

void initOpeningHours() {
// bloc
sl.registerFactory(
() => OpeningHoursCubit(fetchOpeningHours: sl()),
);

// use case
sl.registerFactory(() => GetOpeningHours(repository: sl()));

// data source
sl.registerLazySingleton<OpeningHoursLocalDataSource>(
() => OpeningHoursLocalDataSource(),
);

// repository
sl.registerFactory<OpeningHoursRepository>(
() => OpeningHoursRepositoryImpl(
dataSource: sl(),
dateService: sl(),
),
);
}

void initTicket() {
// bloc
sl.registerFactory(
Expand All @@ -179,7 +194,7 @@ void initOccupation() {
);
}

void initUser() {
Future<void> initUser() async {
// bloc
sl.registerFactory(
() => UserCubit(
Expand All @@ -196,10 +211,13 @@ void initUser() {
productRepository: sl(),
),
);

final crate =
await sl<Store>().openCrate<int>('lastUsedMenuItemByProductId').run();
sl.registerFactory(
() async => ConsumeTicket(
() => ConsumeTicket(
ticketRemoteDataSource: sl(),
crate: await sl<Store>().openCrate<int>('auth').run(),
crate: crate,
),
);

Expand Down Expand Up @@ -244,17 +262,6 @@ void initReceipt() {
);
}

void initContributor() {
// bloc
sl.registerFactory(() => ContributorCubit(fetchContributors: sl()));

// use case
sl.registerFactory(() => FetchContributors(dataSource: sl()));

// data source
sl.registerLazySingleton(() => ContributorLocalDataSource());
}

void initPayment() {
// bloc

Expand Down
Loading

0 comments on commit 485ebfc

Please sign in to comment.