Skip to content

Instantly share code, notes, and snippets.

@ferrarafer
Last active October 29, 2019 21:04
Show Gist options
  • Save ferrarafer/6d9ab2127c3912e3b97051e7ba919d78 to your computer and use it in GitHub Desktop.
Save ferrarafer/6d9ab2127c3912e3b97051e7ba919d78 to your computer and use it in GitHub Desktop.
Recovery App from Background
import 'package:project/core/constants/route_paths.dart' as routes;
import 'package:project/core/services/navigation_service.dart';
import 'package:project/core/services/stoppable_service.dart';
import 'package:project/core/utils/localization_keys.dart';
import 'package:project/locator.dart';
class RecoveryService extends StoppableService {
final _navigationService = locator<NavigationService>();
Duration appInBackground;
@override
void resume() {
appInBackground = DateTime.now().difference(pausedAt);
if (appInBackground.inMinutes > 5) {
_navigationService.navigateTo(
routes.LoadingRoute, arguments: LocalizationKeys.loading
);
}
super.resume();
}
@override
void pause() {
super.pause();
}
}
import 'package:flutter/material.dart';
import 'package:project/core/viewmodels/recovery_view_model.dart';
import 'package:project/ui/views/base_view.dart';
import 'package:project/ui/widgets/custom_stack.dart';
import 'package:project/ui/widgets/loading_card.dart';
class RecoveryView extends StatelessWidget {
final String message;
const RecoveryView({
Key key,
this.message
}) : super(key: key);
@override
Widget build(BuildContext context) {
return BaseView<RecoveryViewModel>(
onModelReady: (vm) => vm.runTasks(),
builder: (context, vm, child) {
return CustomStack(
child: LoadingCard(
message: message
),
);
}
);
}
}
import 'dart:async';
import 'package:project/core/services/authentication_service.dart';
import 'package:project/core/services/posts_service.dart';
import 'package:project/core/services/events_service.dart';
import 'package:project/core/services/navigation_service.dart';
import 'package:project/core/viewmodels/base_model.dart';
import 'package:project/locator.dart';
class RecoveryViewModel extends BaseModel {
final _authService = locator<AuthenticationService>();
final _postsService = locator<PostsService>();
final _eventsService = locator<EventsService>();
final _navigationService = locator<NavigationService>();
String _taskInformation;
String get taskInformation => _taskInformation;
Future<void> runTasks() async {
await _authService.refreshSession();
await Future.wait([
_postsService.fetchAll(),
_eventsService.fetchAll()
]);
_navigationService.goBack();
}
}
import 'package:flutter/material.dart';
/// Abstract class that defines a stoppable service
/// - Stoppable service must be able to deactive
/// - Stoppable service must be able to pause
/// - Stoppable service must be able to resume
/// - Stoppable service must be able to suspend
abstract class StoppableService {
bool _serviceStoped = false;
bool get serviceStopped => _serviceStoped;
DateTime _pausedAt;
DateTime get pausedAt => _pausedAt;
@mustCallSuper
void pause() {
_serviceStoped = true;
_pausedAt = new DateTime.now();
}
@mustCallSuper
void inactive() {
_serviceStoped = true;
}
@mustCallSuper
void resume() {
_serviceStoped = false;
}
@mustCallSuper
void suspending() {
_serviceStoped = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment