Skip to content

Commit

Permalink
Merge pull request #91 from Myzel394/add-change-schedule
Browse files Browse the repository at this point in the history
Add change schedule
  • Loading branch information
Myzel394 committed Jul 29, 2023
2 parents 9f5a3b6 + 9500aaa commit 8bf162b
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 130 deletions.
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"deleteLabel": "Delete",
"dismissLabel": "Dismiss",
"editRelays": "Edit Relays",
"editTimers": "Edit Timers",
"uniLinksOpenError": "Link could not be opened",
"actionNotUndoable": "This action cannot be undone!",
"linkCopiedToClipboard": "Link copied to clipboard!",
Expand Down Expand Up @@ -285,6 +286,7 @@
"taskDetails_goToDetails": "Go to Details",
"taskDetails_lastKnownLocation": "Last known location",
"taskDetails_noLocations": "No locations saved yet",
"taskDetails_noTimers": "No timers set up",
"taskDetails_summary": "{count, plural, =1{One location saved} other{{count} locations saved}} ranging from {startDate} to {endDate}",
"@taskDetails_summary": {
"placeholders": {
Expand Down
69 changes: 65 additions & 4 deletions lib/screens/task_detail_screen_widgets/Details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:locus/widgets/TimerWidget.dart';
import 'package:provider/provider.dart';

import '../../widgets/PlatformListTile.dart';
import '../../widgets/TimerWidgetSheet.dart';

class Details extends StatefulWidget {
final List<LocationPointService> locations;
Expand All @@ -37,6 +38,7 @@ class Details extends StatefulWidget {

class _DetailsState extends State<Details> {
late final RelayController _relaysController;
final timersController = TimerController();

@override
void initState() {
Expand All @@ -45,14 +47,22 @@ class _DetailsState extends State<Details> {
_relaysController = RelayController(
relays: widget.task.relays,
);
timersController.addAll(widget.task.timers);
widget.task.addListener(updateUI);
}

@override
void dispose() {
_relaysController.dispose();
widget.task.removeListener(updateUI);

super.dispose();
}

void updateUI() {
setState(() {});
}

@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context);
Expand Down Expand Up @@ -431,10 +441,61 @@ class _DetailsState extends State<Details> {
),
DetailInformationBox(
title: l10n.detailsTimersLabel,
child: TimerWidget(
timers: widget.task.timers,
allowEdit: false,
physics: const NeverScrollableScrollPhysics(),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (timersController.timers.isEmpty)
Text(
l10n.taskDetails_noTimers,
textAlign: TextAlign.start,
style: getBodyTextTextStyle(context),
)
else
TimerWidget(
timers: timersController.timers,
allowEdit: false,
physics: const NeverScrollableScrollPhysics(),
),
Align(
alignment: Alignment.centerRight,
child: PlatformTextButton(
material: (_, __) => MaterialTextButtonData(
icon: Icon(context.platformIcons.edit),
),
child: Text(l10n.editTimers),
onPressed: () async {
final logService = context.read<LogService>();

final timers = await showPlatformModalSheet(
context: context,
material: MaterialModalSheetData(
backgroundColor: Colors.transparent,
isScrollControlled: true,
isDismissible: true,
),
builder: (_) => TimerWidgetSheet(
allowEmpty: true,
controller: timersController,
),
);

if (timers == null) {
return;
}

await widget.task.stopExecutionImmediately();
await widget.task.stopSchedule();
await widget.task
.update(timers: timersController.timers);
taskService.update(widget.task);
await taskService.save();
await widget.task.startSchedule();
await taskService.checkup(logService);
await widget.task.update();
},
),
),
],
),
),
Center(
Expand Down
19 changes: 17 additions & 2 deletions lib/services/task_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,18 @@ class TaskService extends ChangeNotifier {
final tasksToRemove = <Task>{};

for (final task in tasks) {
final isRunning = await task.isRunning();
final shouldRun = await task.shouldRunNow();

if ((!task.isInfinite() && task.nextEndDate() == null) ||
(task.deleteAfterRun &&
!task.isInfinite() &&
task.timers.isNotEmpty &&
!(await task.shouldRunNow()))) {
!shouldRun)) {
FlutterLogs.logInfo(LOG_TAG, "Task Service", "Removing task.");

tasksToRemove.add(task);
} else if (!(await task.shouldRunNow()) && await task.isRunning()) {
} else if (!shouldRun && isRunning) {
FlutterLogs.logInfo(LOG_TAG, "Task Service", "Stopping task.");
await task.stopExecutionImmediately();

Expand All @@ -548,6 +551,18 @@ class TaskService extends ChangeNotifier {
active: false,
),
);
} else if (shouldRun && !isRunning) {
FlutterLogs.logInfo(LOG_TAG, "Task Service", "Start task.");
await task.startExecutionImmediately();

await logService.addLog(
Log.taskStatusChanged(
initiator: LogInitiator.system,
taskId: task.id,
taskName: task.name,
active: true,
),
);
}
}

Expand Down
11 changes: 7 additions & 4 deletions lib/widgets/TimerWidget.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:collection/collection.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart' hide PlatformListTile;
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart'
hide PlatformListTile;
import 'package:locus/services/timers_service.dart';

import 'PlatformListTile.dart';
Expand All @@ -10,7 +11,8 @@ import 'WeekdaySelection.dart';
class TimerController extends ChangeNotifier {
final List<TaskRuntimeTimer> _timers = [];

UnmodifiableListView<TaskRuntimeTimer> get timers => UnmodifiableListView(_timers);
UnmodifiableListView<TaskRuntimeTimer> get timers =>
UnmodifiableListView(_timers);

void add(final TaskRuntimeTimer timer) {
// Merge the new timer if a timer for the same weekday already exists
Expand Down Expand Up @@ -135,7 +137,8 @@ class _TimerWidgetState extends State<TimerWidget> {
? PlatformIconButton(
icon: PlatformWidget(
material: (_, __) => const Icon(Icons.cancel),
cupertino: (_, __) => const Icon(CupertinoIcons.clear_thick_circled),
cupertino: (_, __) =>
const Icon(CupertinoIcons.clear_thick_circled),
),
onPressed: () {
_controller.removeAt(index);
Expand All @@ -155,7 +158,7 @@ class _TimerWidgetState extends State<TimerWidget> {
);

if (data != null) {
_controller.timers.add(
_controller.add(
WeekdayTimer(
day: data["weekday"] as int,
startTime: data["startTime"] as TimeOfDay,
Expand Down
Loading

0 comments on commit 8bf162b

Please sign in to comment.