diff --git a/lib/blocs/time_bloc.dart b/lib/blocs/time_bloc.dart index fc9d28d..5ddd035 100644 --- a/lib/blocs/time_bloc.dart +++ b/lib/blocs/time_bloc.dart @@ -25,7 +25,7 @@ class TimerBloc extends Bloc { on(_onTicked); } - static const List times = [1, 15, 20, 25, 30, 35]; + static const List times = [15, 20, 25, 30, 35]; final Ticker _ticker; final SharedPreferences prefs; final int initDuration; diff --git a/lib/main.dart b/lib/main.dart index c56168d..8061844 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:io'; import 'dart:isolate'; import 'dart:ui'; @@ -57,6 +56,7 @@ Future onService(args) async { final service = FlutterBackgroundService(); final servicePort = ReceivePort(); var isServiceRunning = await service.isRunning(); + StreamSubscription? tickerSubscription; const ticker = Ticker(); Future disposeService(String message) async { @@ -66,23 +66,10 @@ Future onService(args) async { } } - servicePort.listen((message) async { - if (message == 'stopService') { - await disposeService(message); - } else if (message is List) { - ticker.tick(ticks: message[0]).listen((duration) => service.invoke( - 'sendTime', {'currentTime': duration, 'isBreak': message[1]})); - } else if (message == 'exit') { - await disposeService('stopService'); - exit(0); - } - }); - args[2].send(servicePort.sendPort); - if (!isServiceRunning) { await service.startService(); } - ticker.tick(ticks: args[0]).listen((duration) async { + tickerSubscription = ticker.tick(ticks: args[0]).listen((duration) async { var isServiceRunning = await service.isRunning(); if (isServiceRunning) { service.invoke('sendTime', {'currentTime': duration, 'isBreak': args[1]}); @@ -90,6 +77,20 @@ Future onService(args) async { args[2].send('pause'); } }); + + servicePort.listen((message) async { + if (message == 'stopService') { + await disposeService(message); + } else if (message is List) { + tickerSubscription = ticker.tick(ticks: message[0]).listen((duration) => + service.invoke( + 'sendTime', {'currentTime': duration, 'isBreak': message[1]})); + } else if (message == 'exit') { + await disposeService('stopService'); + tickerSubscription!.cancel(); + } + }); + args[2].send(servicePort.sendPort); } Future initializeService() async { diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart index 3c86044..5676206 100644 --- a/lib/screens/home_screen.dart +++ b/lib/screens/home_screen.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:google_mobile_ads/google_mobile_ads.dart'; import 'package:pomotimer/ad_helper.dart'; @@ -22,7 +23,7 @@ class HomeScreen extends StatefulWidget { State createState() => _HomeScreenState(); } -class _HomeScreenState extends State { +class _HomeScreenState extends State with WidgetsBindingObserver { static const settingTypeString = ['ROUND', 'TIME', 'COUNT']; FixedExtentScrollController listWheelController = FixedExtentScrollController(); @@ -33,10 +34,12 @@ class _HomeScreenState extends State { int round = 0; int time = 0; int index = 0; + AppLifecycleState lifecycleState = AppLifecycleState.inactive; @override void dispose() { _bannerAd?.dispose(); + WidgetsBinding.instance.addObserver(this); super.dispose(); } @@ -59,6 +62,13 @@ class _HomeScreenState extends State { }, ), ).load(); + WidgetsBinding.instance.addObserver(this); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + lifecycleState = state; + super.didChangeAppLifecycleState(state); } Future controllerInit() async { @@ -86,22 +96,24 @@ class _HomeScreenState extends State { btTextController.text = ((getFromRepo('breakTime') - 3) ~/ 60).toString(); void showAlertScreen(String text) async { - if (mounted) { - Navigator.of(context).push( - MaterialPageRoute( - builder: (context) { - return AlertScreen( - text: text, - ); - }, - ), - ); + if (lifecycleState == AppLifecycleState.inactive) { + if (mounted) { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) { + return AlertScreen( + text: text, + ); + }, + ), + ); + } + await Future.delayed(const Duration(seconds: 3)); + if (mounted) Navigator.of(context).pop(); } - await Future.delayed(const Duration(seconds: 3)); - if (mounted) Navigator.of(context).pop(); } - Future onWillPop() { + Future onWillPop() async { DateTime now = DateTime.now(); if (currentBackPressTime == null || now.difference(currentBackPressTime!) > const Duration(seconds: 2)) { @@ -111,6 +123,7 @@ class _HomeScreenState extends State { final isolatePort = context.read().isolatePort; if (isolatePort != null) { isolatePort.send('exit'); + await SystemNavigator.pop(); } return Future.value(true); } diff --git a/pubspec.yaml b/pubspec.yaml index a855b3b..a1a2ff0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,7 +17,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.19.0+19 +version: 1.20.0+20 environment: sdk: '>=2.19.0 <3.0.0'