From 2a7265256d5ec744fd66bda12b8870f61a9b6fa4 Mon Sep 17 00:00:00 2001 From: unknown <55nknown@pm.me> Date: Sun, 19 Sep 2021 17:56:38 +0200 Subject: [PATCH] analytics client --- filcnaplo/lib/api/client.dart | 15 ++++++++++----- filcnaplo/lib/app.dart | 2 +- filcnaplo/lib/database/init.dart | 4 +++- filcnaplo/lib/models/settings.dart | 12 +++++++++++- filcnaplo_kreta_api | 2 +- 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/filcnaplo/lib/api/client.dart b/filcnaplo/lib/api/client.dart index 4a51afa..203ca91 100644 --- a/filcnaplo/lib/api/client.dart +++ b/filcnaplo/lib/api/client.dart @@ -3,6 +3,7 @@ import 'dart:convert'; import 'package:filcnaplo/models/config.dart'; import 'package:filcnaplo/models/news.dart'; import 'package:filcnaplo/models/release.dart'; +import 'package:filcnaplo/models/settings.dart'; import 'package:filcnaplo/models/supporter.dart'; import 'package:filcnaplo_kreta_api/models/school.dart'; import 'package:http/http.dart' as http; @@ -41,10 +42,10 @@ class FilcAPI { } } - // TODO: implement `x-filc-id` header - static Future getConfig() async { + static Future getConfig(SettingsProvider settings) async { Map headers = { - "x-filc-id": "", + "x-filc-id": settings.xFilcId, + "user-agent": settings.config.userAgent, }; try { @@ -52,9 +53,11 @@ class FilcAPI { if (res.statusCode == 200) { return Config.fromJson(jsonDecode(res.body)); - } else { - throw "HTTP ${res.statusCode}: ${res.body}"; + } else if (res.statusCode == 429) { + res = await http.get(Uri.parse(CONFIG)); + if (res.statusCode == 200) return Config.fromJson(jsonDecode(res.body)); } + throw "HTTP ${res.statusCode}: ${res.body}"; } catch (error) { print("ERROR: FilcAPI.getConfig: $error"); } @@ -115,4 +118,6 @@ class FilcAPI { return Future.value(null); } + + // TODO: static Future sendReport(ErrorReport report) async {} } diff --git a/filcnaplo/lib/app.dart b/filcnaplo/lib/app.dart index 9fe579e..de1e499 100644 --- a/filcnaplo/lib/app.dart +++ b/filcnaplo/lib/app.dart @@ -46,7 +46,7 @@ class App extends StatelessWidget { setSystemChrome(context); WidgetsBinding.instance?.addPostFrameCallback((_) { - FilcAPI.getConfig().then((Config? config) { + FilcAPI.getConfig(settings).then((Config? config) { settings.update(context, database: database, config: config ?? Config.fromJson({})); }); }); diff --git a/filcnaplo/lib/database/init.dart b/filcnaplo/lib/database/init.dart index ea6a653..fdf5533 100644 --- a/filcnaplo/lib/database/init.dart +++ b/filcnaplo/lib/database/init.dart @@ -9,7 +9,8 @@ Future initDB() async { var settingsDB = await createSettingsTable(db); // Create table Users - await db.execute("CREATE TABLE IF NOT EXISTS users (id TEXT NOT NULL, name TEXT, username TEXT, password TEXT, institute_code TEXT, student TEXT)"); + await db.execute( + "CREATE TABLE IF NOT EXISTS users (id TEXT NOT NULL, name TEXT, username TEXT, password TEXT, institute_code TEXT, student TEXT, role INTEGER)"); await db.execute("CREATE TABLE IF NOT EXISTS user_data (" "id TEXT NOT NULL, grades TEXT, timetable TEXT, exams TEXT, homework TEXT, messages TEXT, notes TEXT, events TEXT, absences TEXT)"); @@ -30,6 +31,7 @@ Future createSettingsTable(Database db) async { "grade_color1": int, "grade_color2": int, "grade_color3": int, "grade_color4": int, "grade_color5": int, // grade colors "vibration_strength": int, "ab_weeks": int, "swap_ab_weeks": int, "notifications": int, "notifications_bitfield": int, "notification_poll_interval": int, // notifications + "x_filc_id": String, }); // Create table Settings diff --git a/filcnaplo/lib/models/settings.dart b/filcnaplo/lib/models/settings.dart index 98918c5..cd6f0a5 100644 --- a/filcnaplo/lib/models/settings.dart +++ b/filcnaplo/lib/models/settings.dart @@ -6,6 +6,7 @@ import 'package:filcnaplo/theme.dart'; import 'package:flutter/material.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'package:provider/provider.dart'; +import 'package:uuid/uuid.dart'; enum Pages { home, grades, timetable, messages, absences } enum UpdateChannel { stable, beta, dev } @@ -47,6 +48,7 @@ class SettingsProvider extends ChangeNotifier { bool _swapABweeks; UpdateChannel _updateChannel; Config _config; + String _xFilcId; SettingsProvider({ required String language, @@ -66,6 +68,7 @@ class SettingsProvider extends ChangeNotifier { required bool swapABweeks, required UpdateChannel updateChannel, required Config config, + required String xFilcId, }) : _language = language, _startPage = startPage, _rounding = rounding, @@ -82,7 +85,8 @@ class SettingsProvider extends ChangeNotifier { _ABweeks = ABweeks, _swapABweeks = swapABweeks, _updateChannel = updateChannel, - _config = config { + _config = config, + _xFilcId = xFilcId { PackageInfo.fromPlatform().then((PackageInfo packageInfo) { _packageInfo = packageInfo; }); @@ -113,6 +117,7 @@ class SettingsProvider extends ChangeNotifier { swapABweeks: map["swap_ab_weeks"] == 1 ? true : false, updateChannel: UpdateChannel.values[map["update_channel"]], config: Config.fromJson(jsonDecode(map["config"] ?? "{}")), + xFilcId: map["x_filc_id"], ); } @@ -139,6 +144,7 @@ class SettingsProvider extends ChangeNotifier { "swap_ab_weeks": _swapABweeks ? 1 : 0, "notification_poll_interval": _notificationPollInterval, "config": jsonEncode(config.json), + "x_filc_id": _xFilcId, }; } @@ -167,6 +173,7 @@ class SettingsProvider extends ChangeNotifier { swapABweeks: false, updateChannel: UpdateChannel.stable, config: Config.fromJson({}), + xFilcId: Uuid().v4(), ); } @@ -189,6 +196,7 @@ class SettingsProvider extends ChangeNotifier { UpdateChannel get updateChannel => _updateChannel; PackageInfo? get packageInfo => _packageInfo; Config get config => _config; + String get xFilcId => _xFilcId; Future update( BuildContext context, { @@ -210,6 +218,7 @@ class SettingsProvider extends ChangeNotifier { bool? swapABweeks, UpdateChannel? updateChannel, Config? config, + String? xFilcId, }) async { if (language != null && language != _language) _language = language; if (startPage != null && startPage != _startPage) _startPage = startPage; @@ -229,6 +238,7 @@ class SettingsProvider extends ChangeNotifier { if (swapABweeks != null && swapABweeks != _swapABweeks) _swapABweeks = swapABweeks; if (updateChannel != null && updateChannel != _updateChannel) _updateChannel = updateChannel; if (config != null && config != _config) _config = config; + if (xFilcId != null && xFilcId != _xFilcId) _xFilcId = xFilcId; if (database == null) database = Provider.of(context, listen: false); await database.store.storeSettings(this); diff --git a/filcnaplo_kreta_api b/filcnaplo_kreta_api index 8b3ec15..08d8022 160000 --- a/filcnaplo_kreta_api +++ b/filcnaplo_kreta_api @@ -1 +1 @@ -Subproject commit 8b3ec15500b0fb686c75383704c393c524d00878 +Subproject commit 08d802282c044be666f7b7ae55ae88b334ba515b