-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
a2ad474
d071643
e714361
a356313
9962560
2a7a2e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that if the method is not implemented, you should add |
||
|
||
@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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds like a good place for |
||
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(); | ||
} |
There was a problem hiding this comment.
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