diff --git a/packages/mobile/lib/main.dart b/packages/mobile/lib/main.dart index 1f338ed..e17358a 100644 --- a/packages/mobile/lib/main.dart +++ b/packages/mobile/lib/main.dart @@ -1,11 +1,15 @@ import 'dart:async'; import 'dart:convert'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:usage_tracker/usage_tracker.dart'; import 'package:geolocator/geolocator.dart'; import 'package:http/http.dart' as http; +const oneSecond = Duration(seconds: 1); +const baseUrl = "http://SERVER_URL:32768"; +const activityPostUrl = "$baseUrl/api/mobile-activity/"; void main() { runApp(const MyApp()); @@ -18,12 +22,11 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: 'Activity Tracker', theme: ThemeData( - primarySwatch: Colors.blue, ), - home: const MyHomePage(title: 'Flutter Demo Home Page'), + home: const MyHomePage(title: 'Activity Tracker'), ); } } @@ -37,119 +40,73 @@ class MyHomePage extends StatefulWidget { State createState() => _MyHomePageState(); } - - class _MyHomePageState extends State { - - List _infos = []; - String _position = ""; - String _output = ""; - - void getUsageStats() async { - Timer mytimer = Timer.periodic(Duration(seconds: 1), (timer) async { - - try { - - String position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high).toString(); - - - Duration oneSecond = const Duration(seconds: 1); - DateTime endDate = DateTime.now(); - DateTime startDate = endDate.subtract(oneSecond); - - List infos = - await UsageTracker.getAppUsage(startDate, endDate); - - String output = (infos == []) ? "No app" : infos[4].appName; - - setState(() { - _output = output; - _position = position; - final url = Uri.parse("http://10.0.0.6:32768/api/mobile-activity/"); - final response = http.post( - url, - headers: { - 'Content-Type': 'application/json; charset=UTF-8', - }, - body: jsonEncode({ - 'name': _output, - 'location': _position, - 'startTime': startDate.toString(), - 'endTime': endDate.toString() - }), - ); - }); - - } - - on AppUsageException catch (exception) { - // ignore: avoid_print - print(exception); + void startTimer() async { + Timer _ = Timer.periodic(oneSecond, (timer) async { + List infos = await getUsageStats(); + if (kDebugMode) { + print(infos); } - + pushUsageStats(infos); }); - - - - void getUsageStats() async { - Timer mytimer = Timer.periodic(Duration(seconds: 1), (timer) async { - - try { - - Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high); - - Duration oneSecond = const Duration(seconds: 1); - DateTime endDate = DateTime.now(); - DateTime startDate = endDate.subtract(oneSecond); + } - List infos = - await UsageTracker.getAppUsage(startDate, endDate); + void pushUsageStats(List infos) { + final url = Uri.parse(activityPostUrl); + for (var activityBlock in infos) { + http.post( + url, + headers: {'Content-Type': 'application/json'}, + body: jsonEncode({ + 'name': activityBlock.appName, + 'startTime': activityBlock.startDate.toString(), + 'endTime': activityBlock.endDate.toString() + }), + ); + } + } - setState(() { - _infos = infos; - }); + Future> getUsageStats() async { + try { + // String position = (await Geolocator.getCurrentPosition( + // desiredAccuracy: LocationAccuracy.high)) + // .toString(); - print(startDate); - print(endDate); - print(_infos); - print(position); - } + DateTime endDate = DateTime.now(); + DateTime startDate = endDate.subtract(oneSecond); - on AppUsageException catch (exception) { - // ignore: avoid_print - print(exception); - } + List infos = + await UsageTracker.getAppUsage(startDate, endDate); - }); + return infos; + } on AppUsageException catch (exception) { + // ignore: avoid_print + print(exception); + return []; + } } - - @override Widget build(BuildContext context) { - return Scaffold( appBar: AppBar( - title: Text(widget.title), ), body: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'Nothing here . . .', + children: const [ + Text( + 'Txt', ), ], ), ), floatingActionButton: FloatingActionButton( - onPressed: getUsageStats, - tooltip: 'Start Logging App Usage Data', + onPressed: startTimer, + tooltip: 'Start Timer', child: const Icon(Icons.add_alarm_sharp), ), - ); + ); } } diff --git a/packages/server/models/mobileActivity.model.js b/packages/server/models/mobileActivity.model.js index 730e587..f5e4f8a 100644 --- a/packages/server/models/mobileActivity.model.js +++ b/packages/server/models/mobileActivity.model.js @@ -2,13 +2,13 @@ import mongoose from 'mongoose'; const schema = mongoose.Schema; const MobileActivitySchema = new schema({ - _owner: { type: schema.Types.ObjectId, ref: 'User' }, - _device: { type: schema.Types.ObjectId, ref: 'Device' }, - name: { type: String, required: true }, - location: { type: String, required: true }, - // title: { type: String, required: true }, - startTime: { type: Date, required: true, index: true }, - endTime: { type: Date, required: true }, + _owner: { type: schema.Types.ObjectId, ref: 'User' }, + _device: { type: schema.Types.ObjectId, ref: 'Device' }, + name: { type: String, required: true }, + location: { type: String, required: false }, + // title: { type: String, required: true }, + startTime: { type: Date, required: true, index: true }, + endTime: { type: Date, required: true }, }); export default mongoose.model('mobileActivity', MobileActivitySchema);