Skip to content

Commit

Permalink
fix(#17) : update app usage logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyHers committed Jun 20, 2023
1 parent af67a6a commit 4627a5e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/services/app_usage_local_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import 'dart:async';
import 'dart:io' show Platform;

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/services.dart';
import 'package:parental_control/app/helpers/parsing_extension.dart';

Expand Down Expand Up @@ -60,10 +61,10 @@ class AppUsageInfo {
Duration get usage => _usage;

/// The start of the interval
DateTime get startDate => _startDate;
Timestamp get startDate => _startDate;

/// The end of the interval
DateTime get endDate => _endDate;
Timestamp get endDate => _endDate;

@override
String toString() {
Expand Down
49 changes: 49 additions & 0 deletions lib/services/app_usage_service.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import 'package:flutter/cupertino.dart';
import 'package:parental_control/app/helpers/parsing_extension.dart';
import 'package:parental_control/common_widgets/show_logger.dart';
import 'package:parental_control/models/child_model/child_model.dart';
import 'package:parental_control/services/app_usage_local_service.dart';
import 'package:parental_control/services/database.dart';

abstract class AppService {
Future<void> getAppUsageService();
Future<Duration?> getChildrenAppUsageAverage(Database database);
Future<Duration?> getChildAppUsagePerDay(BuildContext context);
}

class AppUsageService implements AppService {
List<AppUsageInfo> _info = <AppUsageInfo>[];
Duration _averageDuration = Duration(minutes: 1);

List<AppUsageInfo> get info => _info;
Duration get averageDuration => _averageDuration;

@override
Future<void> getAppUsageService() async {
Expand All @@ -22,4 +30,45 @@ class AppUsageService implements AppService {
JHLogger.$.e(exception);
}
}

@override
Future<Duration?> getChildrenAppUsageAverage(Database database) async {
var _children = <ChildModel>[];
var _durations = <Duration>[];
try {
var response = await database.childrenStream().first;
if (response.isNotEmpty) {
_children = response.toList();

for (var listUsage in _children) {
for (var usage in listUsage.appsUsageModel) {
_durations.add(usage.usage);
}
}
var averageDuration = getMedian(_durations);
JHLogger.$.e(averageDuration);
_averageDuration = averageDuration;
return averageDuration;
}
} on AppUsageException catch (exception) {
JHLogger.$.e(exception);
}

return null;
}

@override
Future<Duration?> getChildAppUsagePerDay(BuildContext context) async {
try {
var endDate = DateTime.now();
var startDate = endDate.subtract(Duration(hours: 1));
var infoList =
await AppUsage.getAppUsage(startDate, endDate, useMock: false);
_info = infoList;
} on AppUsageException catch (exception) {
JHLogger.$.e(exception);
}

return null;
}
}

0 comments on commit 4627a5e

Please sign in to comment.