Skip to content

Commit

Permalink
environment: remove & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
marfavi committed Feb 7, 2024
1 parent 7ebc1e8 commit 88c14b2
Show file tree
Hide file tree
Showing 31 changed files with 476 additions and 505 deletions.
51 changes: 34 additions & 17 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import 'package:coffeecard/core/network/network_request_executor.dart';
import 'package:coffeecard/core/strings.dart';
import 'package:coffeecard/core/styles/theme.dart';
import 'package:coffeecard/core/widgets/pages/splash/splash_error_page.dart';
import 'package:coffeecard/core/widgets/pages/splash/splash_loading_page.dart';
import 'package:coffeecard/features/authentication.dart';
import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart';
import 'package:coffeecard/features/product.dart';
import 'package:coffeecard/features/redirection/redirection_router.dart';
import 'package:coffeecard/features/user/presentation/cubit/user_cubit.dart';
import 'package:coffeecard/generated/api/coffeecard_api_v2.swagger.dart';
import 'package:coffeecard/service_locator.dart';
import 'package:coffeecard/src/environment/environment.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand All @@ -20,25 +22,40 @@ class App extends StatelessWidget {
// Force screen orientation to portrait
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);

return MultiBlocProvider(
return MultiRepositoryProvider(
providers: [
BlocProvider.value(value: sl<AuthenticationCubit>()..appStarted()),
BlocProvider.value(value: sl<EnvironmentCubit>()..getConfig()),
BlocProvider(create: (_) => sl<UserCubit>()),
BlocProvider.value(value: sl<ProductCubit>()),
RepositoryProvider.value(value: sl<CoffeecardApiV2>()),
RepositoryProvider.value(value: sl<NetworkRequestExecutor>()),
RepositoryProvider(
create: (context) => EnvironmentRepository(
apiV2: context.read(),
executor: context.read(),
),
),
],
child: MainRedirectionRouter(
navigatorKey: _navigatorKey,
child: MaterialApp(
title: Strings.appTitle,
theme: analogTheme,
child: MultiBlocProvider(
providers: [
BlocProvider.value(value: sl<AuthenticationCubit>()..appStarted()),
BlocProvider(
create: (context) =>
EnvironmentCubit(repository: context.read())..loadEnvironment(),
),
BlocProvider(create: (_) => sl<UserCubit>()),
BlocProvider.value(value: sl<ProductCubit>()),
],
child: MainRedirectionRouter(
navigatorKey: _navigatorKey,
home: BlocBuilder<EnvironmentCubit, EnvironmentState>(
builder: (_, state) {
return (state is EnvironmentError)
? SplashErrorPage(errorMessage: state.message)
: const SplashLoadingPage();
},
child: MaterialApp(
title: Strings.appTitle,
theme: analogTheme,
navigatorKey: _navigatorKey,
home: BlocBuilder<EnvironmentCubit, EnvironmentState>(
builder: (_, state) => switch (state) {
EnvironmentLoadError(:final message) =>
SplashErrorPage(message),
_ => const SplashLoadingPage(),
},
),
),
),
),
Expand Down
5 changes: 5 additions & 0 deletions lib/core/errors/failures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ class ServerFailure extends NetworkFailure {
class ConnectionFailure extends NetworkFailure {
const ConnectionFailure() : super('connection refused');
}

/// An unknown failure that is not expected to occur.
class UnknownFailure extends Failure {
const UnknownFailure(super.reason);
}
1 change: 1 addition & 0 deletions lib/core/network/network_request_executor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ part 'network_request_executor_mapping.dart';

typedef _NetworkRequest<BodyType> = Future<Response<BodyType>> Function();
typedef _ExecutorResult<R> = Future<Either<Failure, R>>;
// FIXME: Attempt to change back to NetworkFailure for Left type?
typedef _ExecutorTaskEither<R> = TaskEither<Failure, R>;

class NetworkRequestExecutor {
Expand Down
71 changes: 33 additions & 38 deletions lib/core/widgets/components/scaffold.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import 'package:coffeecard/core/styles/app_colors.dart';
import 'package:coffeecard/core/styles/app_text_styles.dart';
import 'package:coffeecard/features/environment/domain/entities/environment.dart';
import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart';
import 'package:coffeecard/features/environment/presentation/widgets/environment_banner.dart';
import 'package:coffeecard/features/environment/presentation/widgets/environment_button.dart';
import 'package:coffeecard/features/environment.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class AppScaffold extends StatelessWidget {
final Widget? title;
final Widget body;
final bool applyPadding;
final Color backgroundColor;
final bool resizeToAvoidBottomInset;
final double? appBarHeight;

bool get hasTitle => title != null;

/// A Scaffold with a normal app bar.
///
/// If connected to test envrionment, an environment
/// warning button is shown below the title.
/// If connected to test environment, a [TestingEnvironmentBanner]
/// is shown below the title.
///
/// The body's background color is always `AppColor.background`.
AppScaffold.withTitle({
Expand All @@ -35,8 +23,8 @@ class AppScaffold extends StatelessWidget {

/// A Scaffold with an empty, 48 dp tall app bar.
///
/// If connected to test envrionment, an environment warning
/// button is shown where the title would normally show.
/// If connected to test environment, an [TestingEnvironmentButton]
/// is shown where the title would normally show.
///
/// The body's background color is, by default, the same as the app bar.
const AppScaffold.withoutTitle({
Expand All @@ -47,8 +35,20 @@ class AppScaffold extends StatelessWidget {
applyPadding = false,
appBarHeight = 48;

final Widget? title;
final Widget body;
final bool applyPadding;
final Color backgroundColor;
final bool resizeToAvoidBottomInset;
final double? appBarHeight;

bool get hasTitle => title != null;

Check warning on line 45 in lib/core/widgets/components/scaffold.dart

View check run for this annotation

Codecov / codecov/patch

lib/core/widgets/components/scaffold.dart#L45

Added line #L45 was not covered by tests

@override
Widget build(BuildContext context) {
final isTestEnvironment =
context.watch<EnvironmentCubit>().state.isTestingEnvironment;

Check warning on line 50 in lib/core/widgets/components/scaffold.dart

View check run for this annotation

Codecov / codecov/patch

lib/core/widgets/components/scaffold.dart#L50

Added line #L50 was not covered by tests

return Scaffold(
resizeToAvoidBottomInset: resizeToAvoidBottomInset,
// The background color is set to avoid a thin line
Expand All @@ -57,31 +57,26 @@ class AppScaffold extends StatelessWidget {
// in the child of the Expanded widget below.
backgroundColor: AppColors.primary,
appBar: AppBar(
title: hasTitle ? title : const EnvironmentButton(),
centerTitle: hasTitle ? null : true,
title: (!hasTitle && isTestEnvironment)

Check warning on line 60 in lib/core/widgets/components/scaffold.dart

View check run for this annotation

Codecov / codecov/patch

lib/core/widgets/components/scaffold.dart#L60

Added line #L60 was not covered by tests
? const TestingEnvironmentButton()
: title,
centerTitle: !hasTitle,

Check warning on line 63 in lib/core/widgets/components/scaffold.dart

View check run for this annotation

Codecov / codecov/patch

lib/core/widgets/components/scaffold.dart#L62-L63

Added lines #L62 - L63 were not covered by tests
toolbarHeight: appBarHeight,
backgroundColor: AppColors.primary,
iconTheme: const IconThemeData(color: AppColors.white),
),
body: BlocBuilder<EnvironmentCubit, EnvironmentState>(
builder: (_, state) {
final bool isTestEnvironment =
state is EnvironmentLoaded && state.env.isTest;

return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (hasTitle && isTestEnvironment) const EnvironmentBanner(),
Expanded(
child: Container(
padding: applyPadding ? const EdgeInsets.all(16) : null,
color: backgroundColor,
child: body,
),
),
],
);
},
body: Column(

Check warning on line 68 in lib/core/widgets/components/scaffold.dart

View check run for this annotation

Codecov / codecov/patch

lib/core/widgets/components/scaffold.dart#L68

Added line #L68 was not covered by tests
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (hasTitle && isTestEnvironment) const TestingEnvironmentBanner(),
Expanded(
child: Container(
padding: applyPadding ? const EdgeInsets.all(16) : null,
color: backgroundColor,
child: body,

Check warning on line 76 in lib/core/widgets/components/scaffold.dart

View check run for this annotation

Codecov / codecov/patch

lib/core/widgets/components/scaffold.dart#L70-L76

Added lines #L70 - L76 were not covered by tests
),
),
],
),
);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/core/widgets/pages/splash/splash_error_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import 'package:coffeecard/core/strings.dart';
import 'package:coffeecard/core/styles/app_text_styles.dart';
import 'package:coffeecard/core/widgets/components/loading_overlay.dart';
import 'package:coffeecard/core/widgets/components/rounded_button.dart';
import 'package:coffeecard/features/environment/presentation/cubit/environment_cubit.dart';
import 'package:coffeecard/features/environment.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:gap/gap.dart';

class SplashErrorPage extends StatefulWidget {
const SplashErrorPage({required this.errorMessage});
const SplashErrorPage(this.errorMessage);
final String errorMessage;

@override
Expand All @@ -23,7 +23,7 @@ class _SplashErrorPageState extends State<SplashErrorPage> {
// may not be obvious that a load is happening with no internet
await Future.wait<void>([
Future.delayed(const Duration(milliseconds: 200)),
context.read<EnvironmentCubit>().getConfig(),
context.read<EnvironmentCubit>().loadEnvironment(),
]);

if (mounted) {
Expand Down
1 change: 1 addition & 0 deletions lib/features/environment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'package:coffeecard/src/environment/environment.dart';

This file was deleted.

25 changes: 0 additions & 25 deletions lib/features/environment/domain/entities/environment.dart

This file was deleted.

14 changes: 0 additions & 14 deletions lib/features/environment/domain/usecases/get_environment_type.dart

This file was deleted.

22 changes: 0 additions & 22 deletions lib/features/environment/presentation/cubit/environment_cubit.dart

This file was deleted.

28 changes: 0 additions & 28 deletions lib/features/environment/presentation/cubit/environment_state.dart

This file was deleted.

Loading

0 comments on commit 88c14b2

Please sign in to comment.