Skip to content

Commit

Permalink
add logging view
Browse files Browse the repository at this point in the history
  • Loading branch information
maxisme committed Apr 2, 2021
1 parent 84665d3 commit 6406291
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ app.*.symbols
# Obfuscation related
app.*.map.json
/test/failures/
/flog.db
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ linter:
always_specify_types: true
always_use_package_imports: true
avoid_classes_with_only_static_members: true
avoid_print: false
avoid_print: true
avoid_redundant_argument_values: true
avoid_relative_lib_imports: true
avoid_slow_async_io: true
Expand Down
3 changes: 1 addition & 2 deletions lib/local_notifications.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';

import 'notifications/notification.dart';
import 'package:notifi/notifications/notification.dart';

Future<FlutterLocalNotificationsPlugin> initPushNotifications() async {
final FlutterLocalNotificationsPlugin localNotifications =
Expand Down
3 changes: 1 addition & 2 deletions lib/notifications/db_provider.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import 'dart:async';

import 'package:notifi/notifications/notification.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';

// import 'package:sqlite3/sqlite3.dart';

import 'notification.dart';

class DBProvider {
DBProvider(this.dbPath);

Expand Down
2 changes: 1 addition & 1 deletion lib/notifications/notifis.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Notifications extends ChangeNotifier {
try {
id = await db.store(notification);
} catch (e) {
print('Problem storing notification in db: $e');
L.e('Problem storing notification in db: $e');
return -1;
}

Expand Down
40 changes: 6 additions & 34 deletions lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:notifi/notifications/notifications_table.dart';
import 'package:notifi/notifications/notifis.dart';
import 'package:notifi/pallete.dart';
import 'package:notifi/user.dart';
import 'package:notifi/utils.dart';
import 'package:provider/provider.dart';

class HomeScreen extends StatelessWidget {
Expand Down Expand Up @@ -93,7 +94,11 @@ class HomeScreen extends StatelessWidget {
Provider.of<Notifications>(context, listen: false).readAll();
} else if (index == 1) {
// DELETE ALL EVENT
_deleteAllNotificationsDialogue(context);
showAlert(context, 'Delete All',
'All notifications will be irretrievable', onOkPressed: () {
Provider.of<Notifications>(context, listen: false).deleteAll();
Navigator.pop(context);
});
}
},
// ignore: prefer_const_literals_to_create_immutables
Expand All @@ -116,39 +121,6 @@ class HomeScreen extends StatelessWidget {
));
}

Future<void> _deleteAllNotificationsDialogue(BuildContext context) async {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Delete All'),
content: const Text('All notifications will be irretrievable'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text(
'Cancel',
style: TextStyle(color: MyColour.grey),
),
),
TextButton(
onPressed: () {
Provider.of<Notifications>(context, listen: false)
.deleteAll();
Navigator.pop(context);
},
child: const Text(
'Ok',
style: TextStyle(color: MyColour.black),
)),
],
);
},
);
}

// var waitErr;
// setError(bool err) {
// this.waitErr = err;
Expand Down
26 changes: 26 additions & 0 deletions lib/screens/logs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:notifi/utils.dart';

class LogsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 80,
title: const Text('Logs'),
),
body: Container(
padding: const EdgeInsets.only(left: 10.0, right: 10.0),
child: FutureBuilder<ListView>(
future: L.logListView(),
// ignore: always_specify_types
builder: (BuildContext context, AsyncSnapshot f) {
if (f.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
return f.data;
}),
));
}
}
12 changes: 10 additions & 2 deletions lib/screens/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:http/http.dart' as http;
import 'package:launch_at_login/launch_at_login.dart';
import 'package:notifi/notifications/notifications_table.dart';
import 'package:notifi/pallete.dart';
import 'package:notifi/screens/logs.dart';
import 'package:notifi/user.dart';
import 'package:notifi/utils.dart';
import 'package:package_info/package_info.dart';
Expand Down Expand Up @@ -53,7 +54,7 @@ class SettingsScreenState extends State<SettingsScreen> {
if (value.statusCode == 200) {
_remoteVersion.value = value.body;
} else {
print('problem getting /version');
L.e('Problem getting /version from notifi.it');
}
});

Expand Down Expand Up @@ -135,6 +136,13 @@ class SettingsScreenState extends State<SettingsScreen> {
SettingOption('About...', onTapCallback: () {
launch('https://notifi.it');
}),
SettingOption('Logs...', onTapCallback: () {
Navigator.push(
context,
MaterialPageRoute<StatelessWidget>(
builder: (BuildContext context) => LogsScreen()),
);
}),
if (Platform.isMacOS)
SettingOption(
'Quit notifi',
Expand Down Expand Up @@ -237,7 +245,7 @@ class SettingsScreenState extends State<SettingsScreen> {
.requestNewUser();
if (!gotUser) {
// TODO show error
print('Unable to fetch new user!');
L.i('Unable to fetch new user!');
}
},
child: const Text(
Expand Down
56 changes: 19 additions & 37 deletions lib/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import 'package:dio/dio.dart' as d;
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart' as dot_env;
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:notifi/local_notifications.dart';
import 'package:notifi/notifications/notification.dart';
import 'package:notifi/notifications/notifis.dart';
import 'package:notifi/utils.dart';
import 'package:path_provider/path_provider.dart';
import 'package:uuid/uuid.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/status.dart' as status;

import 'notifications/notification.dart';

final UserStore storage = UserStore();

const int requestNewUserCode = 551;
Expand Down Expand Up @@ -72,11 +69,11 @@ class User with ChangeNotifier {
if (alreadyHadCredentials && gotUser) {
// TODO return message to user to tell them
// that there credentials have been replaced
print('replaced your credentials...');
L.e('Replaced your credentials...');
}
} else {
setError(hasErr: true);
print('attempting to create user again...');
L.w('Attempting to create user again...');
await Future<dynamic>.delayed(const Duration(seconds: 5));
}
}
Expand All @@ -93,7 +90,7 @@ class User with ChangeNotifier {
}
final bool gotUser = await _newUserReq(data);
if (gotUser && _ws != null) {
print('reconnecting to ws...');
L.i('Reconnecting to ws...');
_ws.sink.close(status.normalClosure, 'new code!');
}
notifyListeners();
Expand All @@ -105,7 +102,7 @@ class User with ChangeNotifier {
////////
Future<void> _initWSS() async {
if (_ws != null) {
print('closing...');
L.i('Closing already open WS...');
_ws.sink.close();
_ws = null;
}
Expand All @@ -121,7 +118,7 @@ class User with ChangeNotifier {
'Version': await getVersionFromPubSpec(),
};

print('connecting...');
L.i('Connecting to WS...');
setError(hasErr: false);
final IOWebSocketChannel ws = IOWebSocketChannel.connect(env['WS_ENDPOINT'],
headers: headers, pingInterval: const Duration(seconds: 3));
Expand All @@ -136,9 +133,9 @@ class User with ChangeNotifier {
// ignore: always_specify_types
}, onError: (e) async {
wsError = true;
print('WS error: $e');
L.w('Problem with WS: $e');
}, onDone: () async {
print('ws connection closed');
L.i('WS connection closed.');
if (wsError) {
setError(hasErr: true);
await Future<dynamic>.delayed(const Duration(seconds: 5));
Expand All @@ -150,7 +147,7 @@ class User with ChangeNotifier {
}

Future<bool> _newUserReq(Map<String, dynamic> data) async {
print('creating new user...');
L.i('Creating new credentials...');
final d.Dio dio = d.Dio();
Response<dynamic> response;
try {
Expand All @@ -160,12 +157,12 @@ class User with ChangeNotifier {
'Sec-Key': env['SERVER_KEY'],
}, contentType: d.Headers.formUrlEncodedContentType));
} catch (e) {
print('Problem fetching user code: $e');
L.e('Problem fetching user code: $e');
return false;
}

if (response.statusCode != HttpStatus.ok) {
print('Problem fetching new code from server: $response');
L.e('Problem fetching new code from server: $response');
return false;
}

Expand All @@ -175,7 +172,7 @@ class User with ChangeNotifier {
credentialsMap =
json.decode(response.data as String) as Map<String, dynamic>;
} catch (e) {
print('Problem decoding new code from server: $e - ${response.data}');
L.e('Problem decoding new code from server: $e - ${response.data}');
return false;
}

Expand All @@ -196,7 +193,7 @@ class User with ChangeNotifier {
try {
notifications = json.decode(msg as String) as List<dynamic>;
} catch (e) {
print('ignoring un-parsable incoming message from server: $msg: $e');
L.e('Ignoring un-parsable incoming message from server: $msg: $e');
return <String>[];
}

Expand All @@ -207,7 +204,7 @@ class User with ChangeNotifier {
try {
jsonMessage = Map<String, dynamic>.from(notifications[i]);
} catch (e) {
print('ignoring un-parsable ws message: $msg: $e');
L.e('Ignoring un-parsable WS message: $msg: $e');
return <String>[];
}

Expand Down Expand Up @@ -239,7 +236,7 @@ class User with ChangeNotifier {
bool err;

void setError({bool hasErr}) {
// wait for 1 second to make sure still error to prevent
// wait for 1 second to make sure hasErr hasn't changed to prevent
// stuttering.
err = hasErr;
Future<dynamic>.delayed(const Duration(seconds: 1), () {
Expand All @@ -263,10 +260,8 @@ class UserStore {
String userJsonString;
try {
userJsonString = await storage.read(key: key);
} on MissingPluginException catch (_) {
// read json from file
final File file = await _getLinuxFile();
userJsonString = await file.readAsString();
} catch (e) {
L.f(e);
}

try {
Expand All @@ -277,7 +272,7 @@ class UserStore {
user.credentials = userJson['credentials'];
user.flutterToken = userJson['flutterToken'];
} catch (error) {
print(error);
L.f(error);
return false;
}
return true;
Expand All @@ -291,21 +286,8 @@ class UserStore {
});
try {
await storage.write(key: key, value: jsonData);
} on MissingPluginException catch (e) {
print('unable to store in keychain - storing in file $e');
// write to file instead of keychain
final File file = await _getLinuxFile();
file.writeAsString(jsonData);
} catch (e) {
print('unable to store in keychain $e');
L.f(e);
}
}

Future<File> _getLinuxFile() async {
final String dir = (await getApplicationDocumentsDirectory()).path;
final String savePath = '${'$dir/.notifi/'}$linuxFilePath';
final File file = File(savePath);
file.create(recursive: true);
return file;
}
}
Loading

0 comments on commit 6406291

Please sign in to comment.