Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): Refactor mobile app #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 48 additions & 91 deletions packages/mobile/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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'),
);
}
}
Expand All @@ -37,119 +40,73 @@ class MyHomePage extends StatefulWidget {
State<MyHomePage> createState() => _MyHomePageState();
}



class _MyHomePageState extends State<MyHomePage> {

List<AppUsageInfo> _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<AppUsageInfo> 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<AppUsageInfo> 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<AppUsageInfo> infos =
await UsageTracker.getAppUsage(startDate, endDate);
void pushUsageStats(List<AppUsageInfo> 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<List<AppUsageInfo>> 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<AppUsageInfo> 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: <Widget>[
const Text(
'Nothing here . . .',
children: const <Widget>[
Text(
'Txt',
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: getUsageStats,
tooltip: 'Start Logging App Usage Data',
onPressed: startTimer,
tooltip: 'Start Timer',
child: const Icon(Icons.add_alarm_sharp),
),
);
);
}
}
14 changes: 7 additions & 7 deletions packages/server/models/mobileActivity.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);