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

feat(SNP-1314): Down by Maintenance #119

Closed
wants to merge 6 commits into from
Closed
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
8 changes: 7 additions & 1 deletion lib/features/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_easy_dialogs/flutter_easy_dialogs.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_template/features/down_for_maintenance/presentation/down_for_maintenance_widget.dart';
import 'package:flutter_template/features/navigation/app_router.dart';
import 'package:flutter_template/features/snack_queue/presentation/snack_queue_provider.dart';
import 'package:flutter_template/features/theme_mode/presentation/widgets/theme_mode_builder.dart';
Expand Down Expand Up @@ -53,7 +54,7 @@ class _AppState extends State<App> {
final mediaQueryData = MediaQuery.of(builderContext);
final easyDialogsBuilder = FlutterEasyDialogs.builder();

return Nested(
final child = Nested(
children: const [
SnackQueueProvider(),
],
Expand All @@ -68,6 +69,11 @@ class _AppState extends State<App> {
),
),
);

// TODO(init): Uncomment it or delete if you don't need it.
return DownForMaintenanceWidget(child: child);

// return child;
},

/// Localization.
Expand Down
8 changes: 8 additions & 0 deletions lib/features/app/di/app_scope_register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ final class AppScopeRegister {
});
final logger = LogWriter(surfLogger);

// TODO(init-project): In order to use Down For Maintenance feature you have to:
// - create implementation of [ICheckServerStatusRepository] here
// - call [ICheckServerStatusRepository.configure] and await for it
// - call [ICheckServerStatusRepository.initImmediateCheck]
// - add it to AppScope
// - add DownForMaintenanceWidget in builder of MaterialApp in app.dart
// Otherwise just delete this.

Comment on lines +50 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worth putting information about this also in the documentation in the repository itself, and provide link to the documentation here

return AppScope(
env: env,
appConfig: appConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/repository/i_check_server_status_repository.dart';
import 'package:rxdart/rxdart.dart';

/// Implementation of [ICheckServerStatusRepository] that uses Firebase Remote Config to check server status.
class FirebaseCheckServerStatusRepository implements ICheckServerStatusRepository {
@override
final BehaviorSubject<ServerCheckResult> serverStatus = BehaviorSubject.seeded(ServerCheckResult.processing);

@override
Future<void> configurate() async {
/// Here you can configurate your Firebase Remote Config interval of requests for example.
/// For example:
//
// await remoteConfig.setConfigSettings(RemoteConfigSettings(
// fetchTimeout: const Duration(minutes: 1),
// minimumFetchInterval: const Duration(hours: 1),
// ));
//
}

@override
Future<void> initImmediateCheck() async {
/// Here you can make a request to your Firebase Remote Config to check if it is available.
/// For example:
// try {
// await remoteConfig.fetchAndActivate();
// final isServerAvailable = remoteConfig.getBool('is_server_available');
// serverStatus.value = isServerAvailable ? ServerCheckResult.worksNormally : ServerCheckResult.notActive;
// } on Exception catch (_) {
// serverStatus.value = ServerCheckResult.errorOccurred;
// }
}
Comment on lines +11 to +33

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that if the method is not implemented, you should add throw NotImplementedException() to the template method body.


@override
void dispose() {
serverStatus.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:async';

import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/repository/i_check_server_status_repository.dart';
import 'package:rxdart/rxdart.dart';

/// Implementation of [ICheckServerStatusRepository] that mocks server status.
class MockCheckServerStatusRepository implements ICheckServerStatusRepository {
@override
final BehaviorSubject<ServerCheckResult> serverStatus = BehaviorSubject.seeded(ServerCheckResult.processing);

late final Timer _timer;

@override
Future<void> configurate() async {
_check().ignore();
_timer = Timer.periodic(const Duration(minutes: 5), (timer) {
_check();
});
}

@override
void initImmediateCheck() {
serverStatus.value = ServerCheckResult.processing;
_check();
}

Future<void> _check() async {
await Future.delayed(const Duration(seconds: 1));
serverStatus.value = ServerCheckResult.worksNormally;
}

@override
void dispose() {
_timer.cancel();
serverStatus.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:dio/dio.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/repository/i_check_server_status_repository.dart';
import 'package:rxdart/rxdart.dart';

/// Implementation of [ICheckServerStatusRepository] that uses REST API to check server status.
class RestCheckServerStatusRepository implements ICheckServerStatusRepository {
// ignore: unused_field
final Dio _dio;

/// @nodoc
RestCheckServerStatusRepository(this._dio);

@override
final BehaviorSubject<ServerCheckResult> serverStatus = BehaviorSubject.seeded(ServerCheckResult.processing);

@override
Future<void> configurate() async {
/// Here you can configurate your client to make requests every n minutes for example.
/// For example:
///
// _timer = Timer.periodic(Duration(minutes: 5), (timer) {
// initImmediateCheck();
// });
}

@override
void initImmediateCheck() {
/// Here you can make a request to your server to check if it is available.
}

@override
void dispose() {
// _timer.cancel();
serverStatus.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Server check result enum.
enum ServerCheckResult {
/// Server works normally.
worksNormally,

/// Server is not active.
notActive,

/// Error occurred during server check.
errorOccurred,

/// Server check is in progress.
processing,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';

/// Interface for checking server status.
abstract interface class ICheckServerStatusRepository {
/// Current state of server.
Stream<ServerCheckResult> get serverStatus;

/// Method that initiate check of current server status.
void initImmediateCheck();

/// Method that configurate service. For example, you can configurate interval of requests.
Future<void> configurate();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configure() would be a more appropriate name


/// Method that dispose service. For example, you can cancel timers.
void dispose();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';

/// Interface for checking server status.
abstract interface class ICheckServerStatusService {
/// Current state of server.
ValueListenable<ServerCheckResult> get serverStatus;

/// Method that initiate check of current server status.
void initImmediateCheck();

/// Method that configurate service. For example, you can configurate interval of requests.
Future<void> configurate();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configure() would be a more appropriate name


/// Method that dispose service. For example, you can cancel timers.
void dispose();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';

import 'package:elementary/elementary.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/repository/i_check_server_status_repository.dart';

/// Model with businness-logic for Down For Maintenance feature.
class DownForMaintenanceModel extends ElementaryModel {
final ICheckServerStatusRepository _checkServerStatusRepository;
final _serverCheckResult = ValueNotifier(ServerCheckResult.processing);

/// Current server status.
ValueListenable<ServerCheckResult> get serverCheckResult => _serverCheckResult;
late StreamSubscription<ServerCheckResult> _streamSubscription;

/// @nodoc
DownForMaintenanceModel(this._checkServerStatusRepository);

@override
void init() {
_streamSubscription = _checkServerStatusRepository.serverStatus.listen((event) {
_serverCheckResult.value = event;
});
super.init();
}

/// Initiates immediate check of server status.
void initImmediateCheck() {
_checkServerStatusRepository.initImmediateCheck();
}

@override
void dispose() {
_streamSubscription.cancel();
_serverCheckResult.dispose();
_checkServerStatusRepository.dispose();
super.dispose();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:elementary/elementary.dart';
import 'package:flutter/material.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';
import 'package:flutter_template/features/down_for_maintenance/presentation/down_for_maintenance_wm.dart';

/// Widget that shows child widget if server works normally or shows placeholder if server is down.
class DownForMaintenanceWidget extends ElementaryWidget<IDownForMaintenanceWM> {
/// Child widget that will be shown if server works normally. Usually it is whole application, but
/// you also can use it for some part of application (e.g. some feature).
final Widget child;

/// @nodoc
const DownForMaintenanceWidget({
required this.child,
super.key,
}) : super(defaultDownForMaintenanceWMFactory);

@override
Widget build(IDownForMaintenanceWM wm) {
return ValueListenableBuilder(
valueListenable: wm.currentResult,
builder: (_, status, __) => switch (status) {
ServerCheckResult.errorOccurred || ServerCheckResult.notActive => _PlaceholderWidget(
onRetryPressed: wm.onCheckCurrentStatusPressed,
),
ServerCheckResult.processing => const Center(child: CircularProgressIndicator()),
ServerCheckResult.worksNormally => child,
},
);
}
}

/// Placeholder widget that shows when server is not available.
/// Replace it with your own widget.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good place for // TODO(init-project)

class _PlaceholderWidget extends StatelessWidget {
final VoidCallback onRetryPressed;
const _PlaceholderWidget({required this.onRetryPressed});

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Server is not available now. Please try again later.'),
const SizedBox(height: 10),
ElevatedButton(
onPressed: onRetryPressed,
child: const Text('Retry'),
),
],
),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:elementary/elementary.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_template/features/down_for_maintenance/data/repository/mock_check_server_status_repository.dart';
import 'package:flutter_template/features/down_for_maintenance/domain/entities/server_check_result.dart';
import 'package:flutter_template/features/down_for_maintenance/presentation/down_for_maintenance_model.dart';
import 'package:flutter_template/features/down_for_maintenance/presentation/down_for_maintenance_widget.dart';

/// Factory that creates DownForMaintenanceWM.
DownForMaintenanceWM defaultDownForMaintenanceWMFactory(BuildContext context) {
return DownForMaintenanceWM(DownForMaintenanceModel(MockCheckServerStatusRepository()));
}

/// WidgetModel for DownForMaintenanceWidget.
class DownForMaintenanceWM extends WidgetModel<DownForMaintenanceWidget, DownForMaintenanceModel>
implements IDownForMaintenanceWM {
@override
ValueListenable<ServerCheckResult> get currentResult => model.serverCheckResult;

/// @nodoc
DownForMaintenanceWM(super._model);

@override
void onCheckCurrentStatusPressed() {
model.initImmediateCheck();
}
}

/// Interface for DownForMaintenanceWM.
abstract interface class IDownForMaintenanceWM implements IWidgetModel {
/// Current state of server.
ValueListenable<ServerCheckResult> get currentResult;

/// Callback that initiate check of current server status.
void onCheckCurrentStatusPressed();
}
Loading