Skip to content

Commit

Permalink
First Release Candidate
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinX8 committed Jan 18, 2023
1 parent 60ca4be commit 9483029
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 174 deletions.
21 changes: 19 additions & 2 deletions lib/logger/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import 'dart:isolate';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:drift/isolate.dart';
import 'package:flutter/cupertino.dart';
import 'package:negate/sentiment_db.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../sentiment_analysis.dart';

Expand All @@ -27,9 +29,10 @@ class SentenceLogger {
static late DriftIsolate _iso;
static late TfParams _tfp;
static const int _updateFreq = 1; //update db every 5 minutes
final RegExp blacklist = RegExp(r".*system.*|.*keyboard.*|.*input.*|.*honeyboard.*|.*swiftkey.*");
RegExp blacklist = RegExp(r".*system.*|.*keyboard.*|.*input.*|.*honeyboard.*|.*swiftkey.*");
String _lastUsedApp = "";
bool _dbUpdated = false;
double? _multiplier;

factory SentenceLogger() {
return _instance;
Expand Down Expand Up @@ -62,6 +65,14 @@ class SentenceLogger {
IsolateStartRequest(sendDriftIsolate: rPort.sendPort, targetPath: request.targetPath),
);

var prefs = request.prefs;
if (prefs.getBool('average_sentiment')!) {
_multiplier = prefs.getDouble('multiplier_sentiment')!;
}
if (prefs.getString('blacklist') != null) {
blacklist = RegExp(prefs.getString('blacklist')!);
}

_iso = await rPort.first as DriftIsolate;
_tfp = request.tfp;
var sdb = SentimentDB.connect(await _iso.connect());
Expand Down Expand Up @@ -100,7 +111,13 @@ class SentenceLogger {
var appsInPeriod = _appMap.entries.where((element) => element.value.lastTimeUsed.difference(now).inMinutes <= 10);
for (var app in appsInPeriod) {
if (app.key == name) continue;
app.value.avgScore = ((app.value.avgScore * app.value.numCalled) + score) / (++app.value.numCalled);
if (_multiplier == null) {
app.value.avgScore =
((app.value.avgScore * app.value.numCalled) + score) /
(++app.value.numCalled);
} else {
app.value.avgScore = (app.value.avgScore * _multiplier!) + (score * (1 - _multiplier!));
}
}

if (_appMap.containsKey(name)) {
Expand Down
12 changes: 12 additions & 0 deletions lib/logger/logger_factory.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:io' show Platform;

import 'package:negate/logger/logger.dart';

import '../sentiment_db.dart';
import 'android_logger.dart';
import 'win_logger.dart';
Expand All @@ -16,4 +18,14 @@ class LoggerFactory {
throw UnsupportedError("This platform is unsupported!");
}
}

static SentenceLogger getLogger() {
if (Platform.isWindows) {
return WinLogger();
} else if (Platform.isAndroid) {
return AndroidLogger();
} else {
throw UnsupportedError("This platform is unsupported!");
}
}
}
49 changes: 24 additions & 25 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'package:negate/sentiment_db.dart';

import 'package:drift/isolate.dart';
import 'package:flutter/material.dart' hide MenuItem;
import 'package:negate/ui/daily_dashboard.dart';
import 'package:negate/ui/daily_breakdown.dart';
import 'package:negate/ui/settings.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand All @@ -42,13 +42,25 @@ Future<void> main() async {
await analyser.init();
var tfp = TfParams(analyser.sInterpreter.address, analyser.dictionary);

//WidgetsFlutterBinding.ensureInitialized();
var prefs = await SharedPreferences.getInstance();
if (prefs.getBool('dynamic_theme') == null) {
prefs.setBool('dynamic_theme', true);
getIt.registerSingleton<bool>(true);
} else {
getIt.registerSingleton<bool>(prefs.getBool('dynamic_theme')!);
}
prefs.getBool('average_sentiment') == null ? prefs.setBool('average_sentiment', true) : null;
prefs.getDouble('multiplier_sentiment') == null ? prefs.setDouble('multiplier_sentiment', 0.75) : null;
prefs.getString('blacklist') == null ? prefs.setString('blacklist', LoggerFactory.getLogger().blacklist.pattern) : null;

if (Platform.isAndroid || Platform.isIOS) {
LoggerFactory.startLoggerFactory(
TfliteRequest(rPort.sendPort, dbString, tfp));
TfliteRequest(rPort.sendPort, dbString, tfp, prefs));
} else {
await loggerUI.initSystemTray();
await Isolate.spawn(LoggerFactory.startLoggerFactory,
TfliteRequest(rPort.sendPort, dbString, tfp));
TfliteRequest(rPort.sendPort, dbString, tfp, prefs));
}

var iPort = await rPort.first as SendPort;
Expand Down Expand Up @@ -114,7 +126,9 @@ class ThemedHourlyUI extends StatelessWidget {
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
ColorScheme light;
ColorScheme dark;
if (lightDynamic != null && darkDynamic != null) {
bool enabled = getIt<bool>.call();
bool dynamicCheck = lightDynamic != null && darkDynamic != null && enabled;
if (dynamicCheck) {
light = lightDynamic;
dark = darkDynamic;
} else {
Expand All @@ -134,11 +148,11 @@ class ThemedHourlyUI extends StatelessWidget {
title: 'Negate Mental Health Tracker',
theme: ThemeData(
colorScheme: light,
scaffoldBackgroundColor: light.background,
scaffoldBackgroundColor: dynamicCheck ? light.background : null,
useMaterial3: true),
darkTheme: ThemeData(
colorScheme: dark,
scaffoldBackgroundColor: dark.background,
scaffoldBackgroundColor: dynamicCheck ? dark.background : null,
useMaterial3: true),
themeMode: ThemeMode.system,
home: home);
Expand Down Expand Up @@ -171,7 +185,7 @@ class HourlyDashboard extends ConsumerWidget {
});
return Scaffold(
appBar: AppBar(
title: const Text('Hourly Dashboard'),
title: const Text('Dashboard'),
actions: [
IconButton(
onPressed: () {
Expand All @@ -194,7 +208,7 @@ class HourlyDashboard extends ConsumerWidget {
MaterialPageRoute(
builder: (context) {
return StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
return DailyDashboard.dashboard(context, sdb, ref, setState);
return DailyBreakdown.dashboard(context, sdb, ref, setState);
});
}));
},
Expand Down Expand Up @@ -302,7 +316,7 @@ class HourlyDashboard extends ConsumerWidget {
persistentFooterButtons: [
TextButton(
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.primary,
foregroundColor: Theme.of(context).colorScheme.secondary,
padding: const EdgeInsets.all(16.0),
textStyle: const TextStyle(fontSize: 20),
),
Expand Down Expand Up @@ -399,29 +413,14 @@ class HourlyDashboard extends ConsumerWidget {
BarChartRodData(
toY: (res[i] * 100).roundToDouble(),
width: 10,
color: getBarColour(res[i]),
color: CommonUI.getBarColour(res[i]),
borderRadius: const BorderRadius.all(Radius.zero))
],
showingTooltipIndicators: show));
}
return bars;
}

Color getBarColour(double val) {
int percent = (val * 100).round();
if (percent >= 75) {
return Colors.green[900]!;
} else if (percent >= 65) {
return Colors.green;
} else if (percent >= 45) {
return Colors.greenAccent;
} else if (percent >= 35) {
return Colors.yellow;
} else {
return Colors.red;
}
}

void handleMenu(String value) async {
switch (value) {
case 'Export':
Expand Down
52 changes: 45 additions & 7 deletions lib/sentiment_db.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:collection';
import 'dart:convert';
import 'dart:developer';
import 'dart:isolate';

import 'package:drift/drift.dart';
Expand Down Expand Up @@ -65,9 +64,13 @@ class SentimentDB extends _$SentimentDB {
return jsonEncode(res);
}

Future<List<List<MapEntry<String, List<double>>>>> getRecommendations(DateTime after) async {
Future<List<MapEntry<String, List<double>>>> _getSentimentsByName(DateTime after, [DateTime? before]) async {
var query = select(sentimentLogs)..where((tbl) =>
tbl.hour.isBiggerOrEqualValue(after));
if (before != null) {
query = select(sentimentLogs)..where((tbl) =>
tbl.hour.isBetweenValues(after, before));
}
Map<String, List<double>> weeklyAverage = <String, List<double>>{};
Map<String, int> weeklyCount = <String, int>{};
var res = await query.get();
Expand All @@ -77,19 +80,53 @@ class SentimentDB extends _$SentimentDB {
weeklyCount[log.name] = weeklyCount[log.name]! + 1;
weeklyAverage[log.name]![1] = weeklyAverage[log.name]![1] + log.timeUsed;
} else {
weeklyAverage.putIfAbsent(log.name, () => [log.avgScore, log.timeUsed.toDouble()]);
weeklyAverage.putIfAbsent(log.name, () => [log.avgScore, log.timeUsed.toDouble(), 0]);
weeklyCount.putIfAbsent(log.name, () => 1);
}
}
var sorted = weeklyAverage.entries.toList();
return weeklyAverage.entries.toList();
}

Future<List<List<MapEntry<String, List<double>>>>> getRecommendations(DateTime after) async {
var sorted = await _getSentimentsByName(after);
//Ignore apps used for less than 10 minutes
sorted.removeWhere((element) => element.value[1] < 10);
sorted.sort((a, b) => a.value[0].compareTo(b.value[0]));
var negative = sorted.sublist(0,5);
var positive = sorted.reversed.toList().sublist(0, 5);
var negative = sorted;
var positive = sorted.reversed.toList();
if (sorted.length > 5) {
negative = sorted.sublist(0,5);
positive = sorted.reversed.toList().sublist(0, 5);
}
return [negative, positive];
}

Future<List<MapEntry<String, List<double>>>> getDailyBreakdown(DateTime date) async {
var selectedDate = date.alignDateTime(const Duration(days: 1));
var sentiments = await _getSentimentsByName(selectedDate, selectedDate.add(const Duration(days: 1)));
sentiments.sort((b, a) => a.value[1].compareTo(b.value[1]));
var sub = sentiments;
double totalTime = 0;
double subTime = 0;
int counter = 0;

for (var sentiment in sentiments) {
totalTime += sentiment.value[1];
if (counter == 7) {
subTime = totalTime;
}
counter++;
}
for (var sentiment in sub) {
sentiment.value[2] = sentiment.value[1] / totalTime;
}
if (sentiments.length > 8) {
sub = sentiments.sublist(0,8);
sub.add(MapEntry('Other', [-1, (totalTime - subTime), (totalTime - subTime)/totalTime]));
}
return sub;
}

Future<List<double>> getAvgHourlySentiment(DateTime date) async {
var query = (select(sentimentLogs)..where((tbl) => tbl.hour.year.equals(date.year)
& tbl.hour.month.equals(date.month)
Expand Down Expand Up @@ -203,8 +240,9 @@ class TfParams {

class TfliteRequest extends IsolateStartRequest {
final TfParams tfp;
final SharedPreferences prefs;

TfliteRequest(SendPort sendDriftIsolate,String targetPath, this.tfp) : super(sendDriftIsolate: sendDriftIsolate, targetPath: targetPath);
TfliteRequest(SendPort sendDriftIsolate,String targetPath, this.tfp, this.prefs) : super(sendDriftIsolate: sendDriftIsolate, targetPath: targetPath);
}

class AddSentimentRequest {
Expand Down
Loading

0 comments on commit 9483029

Please sign in to comment.