Skip to content

Commit

Permalink
Merge pull request #7 from JadeKim042386/release
Browse files Browse the repository at this point in the history
fix: stop timeCard when switching from background to foreground
#4
  • Loading branch information
JadeKim042386 authored May 30, 2023
2 parents 75cf460 + 0421cac commit 027814c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/blocs/time_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TimerBloc extends Bloc<TimerEvent, TimerState> {
on<TimerTicked>(_onTicked);
}

static const List<int> times = [1, 15, 20, 25, 30, 35];
static const List<int> times = [15, 20, 25, 30, 35];
final Ticker _ticker;
final SharedPreferences prefs;
final int initDuration;
Expand Down
31 changes: 16 additions & 15 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
import 'dart:ui';

Expand Down Expand Up @@ -57,6 +56,7 @@ Future<void> onService(args) async {
final service = FlutterBackgroundService();
final servicePort = ReceivePort();
var isServiceRunning = await service.isRunning();
StreamSubscription<int>? tickerSubscription;
const ticker = Ticker();

Future<void> disposeService(String message) async {
Expand All @@ -66,30 +66,31 @@ Future<void> 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]});
} else {
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<void> initializeService() async {
Expand Down
41 changes: 27 additions & 14 deletions lib/screens/home_screen.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -22,7 +23,7 @@ class HomeScreen extends StatefulWidget {
State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
static const settingTypeString = ['ROUND', 'TIME', 'COUNT'];
FixedExtentScrollController listWheelController =
FixedExtentScrollController();
Expand All @@ -33,10 +34,12 @@ class _HomeScreenState extends State<HomeScreen> {
int round = 0;
int time = 0;
int index = 0;
AppLifecycleState lifecycleState = AppLifecycleState.inactive;

@override
void dispose() {
_bannerAd?.dispose();
WidgetsBinding.instance.addObserver(this);
super.dispose();
}

Expand All @@ -59,6 +62,13 @@ class _HomeScreenState extends State<HomeScreen> {
},
),
).load();
WidgetsBinding.instance.addObserver(this);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
lifecycleState = state;
super.didChangeAppLifecycleState(state);
}

Future controllerInit() async {
Expand Down Expand Up @@ -86,22 +96,24 @@ class _HomeScreenState extends State<HomeScreen> {
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<bool> onWillPop() {
Future<bool> onWillPop() async {
DateTime now = DateTime.now();
if (currentBackPressTime == null ||
now.difference(currentBackPressTime!) > const Duration(seconds: 2)) {
Expand All @@ -111,6 +123,7 @@ class _HomeScreenState extends State<HomeScreen> {
final isolatePort = context.read<TimerBloc>().isolatePort;
if (isolatePort != null) {
isolatePort.send('exit');
await SystemNavigator.pop();
}
return Future.value(true);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 027814c

Please sign in to comment.