analytics client

This commit is contained in:
unknown 2021-09-19 17:56:38 +02:00
parent 903cbba69a
commit 2a7265256d
No known key found for this signature in database
GPG Key ID: 1D070E0B09CFB257
5 changed files with 26 additions and 9 deletions

View File

@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:filcnaplo/models/config.dart'; import 'package:filcnaplo/models/config.dart';
import 'package:filcnaplo/models/news.dart'; import 'package:filcnaplo/models/news.dart';
import 'package:filcnaplo/models/release.dart'; import 'package:filcnaplo/models/release.dart';
import 'package:filcnaplo/models/settings.dart';
import 'package:filcnaplo/models/supporter.dart'; import 'package:filcnaplo/models/supporter.dart';
import 'package:filcnaplo_kreta_api/models/school.dart'; import 'package:filcnaplo_kreta_api/models/school.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
@ -41,10 +42,10 @@ class FilcAPI {
} }
} }
// TODO: implement `x-filc-id` header static Future<Config?> getConfig(SettingsProvider settings) async {
static Future<Config?> getConfig() async {
Map<String, String> headers = { Map<String, String> headers = {
"x-filc-id": "", "x-filc-id": settings.xFilcId,
"user-agent": settings.config.userAgent,
}; };
try { try {
@ -52,9 +53,11 @@ class FilcAPI {
if (res.statusCode == 200) { if (res.statusCode == 200) {
return Config.fromJson(jsonDecode(res.body)); return Config.fromJson(jsonDecode(res.body));
} else { } else if (res.statusCode == 429) {
throw "HTTP ${res.statusCode}: ${res.body}"; 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) { } catch (error) {
print("ERROR: FilcAPI.getConfig: $error"); print("ERROR: FilcAPI.getConfig: $error");
} }
@ -115,4 +118,6 @@ class FilcAPI {
return Future.value(null); return Future.value(null);
} }
// TODO: static Future<void> sendReport(ErrorReport report) async {}
} }

View File

@ -46,7 +46,7 @@ class App extends StatelessWidget {
setSystemChrome(context); setSystemChrome(context);
WidgetsBinding.instance?.addPostFrameCallback((_) { WidgetsBinding.instance?.addPostFrameCallback((_) {
FilcAPI.getConfig().then((Config? config) { FilcAPI.getConfig(settings).then((Config? config) {
settings.update(context, database: database, config: config ?? Config.fromJson({})); settings.update(context, database: database, config: config ?? Config.fromJson({}));
}); });
}); });

View File

@ -9,7 +9,8 @@ Future<Database> initDB() async {
var settingsDB = await createSettingsTable(db); var settingsDB = await createSettingsTable(db);
// Create table Users // 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 (" 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)"); "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<DatabaseStruct> createSettingsTable(Database db) async {
"grade_color1": int, "grade_color2": int, "grade_color3": int, "grade_color4": int, "grade_color5": int, // grade colors "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, "vibration_strength": int, "ab_weeks": int, "swap_ab_weeks": int,
"notifications": int, "notifications_bitfield": int, "notification_poll_interval": int, // notifications "notifications": int, "notifications_bitfield": int, "notification_poll_interval": int, // notifications
"x_filc_id": String,
}); });
// Create table Settings // Create table Settings

View File

@ -6,6 +6,7 @@ import 'package:filcnaplo/theme.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart'; import 'package:package_info_plus/package_info_plus.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart';
enum Pages { home, grades, timetable, messages, absences } enum Pages { home, grades, timetable, messages, absences }
enum UpdateChannel { stable, beta, dev } enum UpdateChannel { stable, beta, dev }
@ -47,6 +48,7 @@ class SettingsProvider extends ChangeNotifier {
bool _swapABweeks; bool _swapABweeks;
UpdateChannel _updateChannel; UpdateChannel _updateChannel;
Config _config; Config _config;
String _xFilcId;
SettingsProvider({ SettingsProvider({
required String language, required String language,
@ -66,6 +68,7 @@ class SettingsProvider extends ChangeNotifier {
required bool swapABweeks, required bool swapABweeks,
required UpdateChannel updateChannel, required UpdateChannel updateChannel,
required Config config, required Config config,
required String xFilcId,
}) : _language = language, }) : _language = language,
_startPage = startPage, _startPage = startPage,
_rounding = rounding, _rounding = rounding,
@ -82,7 +85,8 @@ class SettingsProvider extends ChangeNotifier {
_ABweeks = ABweeks, _ABweeks = ABweeks,
_swapABweeks = swapABweeks, _swapABweeks = swapABweeks,
_updateChannel = updateChannel, _updateChannel = updateChannel,
_config = config { _config = config,
_xFilcId = xFilcId {
PackageInfo.fromPlatform().then((PackageInfo packageInfo) { PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
_packageInfo = packageInfo; _packageInfo = packageInfo;
}); });
@ -113,6 +117,7 @@ class SettingsProvider extends ChangeNotifier {
swapABweeks: map["swap_ab_weeks"] == 1 ? true : false, swapABweeks: map["swap_ab_weeks"] == 1 ? true : false,
updateChannel: UpdateChannel.values[map["update_channel"]], updateChannel: UpdateChannel.values[map["update_channel"]],
config: Config.fromJson(jsonDecode(map["config"] ?? "{}")), 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, "swap_ab_weeks": _swapABweeks ? 1 : 0,
"notification_poll_interval": _notificationPollInterval, "notification_poll_interval": _notificationPollInterval,
"config": jsonEncode(config.json), "config": jsonEncode(config.json),
"x_filc_id": _xFilcId,
}; };
} }
@ -167,6 +173,7 @@ class SettingsProvider extends ChangeNotifier {
swapABweeks: false, swapABweeks: false,
updateChannel: UpdateChannel.stable, updateChannel: UpdateChannel.stable,
config: Config.fromJson({}), config: Config.fromJson({}),
xFilcId: Uuid().v4(),
); );
} }
@ -189,6 +196,7 @@ class SettingsProvider extends ChangeNotifier {
UpdateChannel get updateChannel => _updateChannel; UpdateChannel get updateChannel => _updateChannel;
PackageInfo? get packageInfo => _packageInfo; PackageInfo? get packageInfo => _packageInfo;
Config get config => _config; Config get config => _config;
String get xFilcId => _xFilcId;
Future<void> update( Future<void> update(
BuildContext context, { BuildContext context, {
@ -210,6 +218,7 @@ class SettingsProvider extends ChangeNotifier {
bool? swapABweeks, bool? swapABweeks,
UpdateChannel? updateChannel, UpdateChannel? updateChannel,
Config? config, Config? config,
String? xFilcId,
}) async { }) async {
if (language != null && language != _language) _language = language; if (language != null && language != _language) _language = language;
if (startPage != null && startPage != _startPage) _startPage = startPage; if (startPage != null && startPage != _startPage) _startPage = startPage;
@ -229,6 +238,7 @@ class SettingsProvider extends ChangeNotifier {
if (swapABweeks != null && swapABweeks != _swapABweeks) _swapABweeks = swapABweeks; if (swapABweeks != null && swapABweeks != _swapABweeks) _swapABweeks = swapABweeks;
if (updateChannel != null && updateChannel != _updateChannel) _updateChannel = updateChannel; if (updateChannel != null && updateChannel != _updateChannel) _updateChannel = updateChannel;
if (config != null && config != _config) _config = config; if (config != null && config != _config) _config = config;
if (xFilcId != null && xFilcId != _xFilcId) _xFilcId = xFilcId;
if (database == null) database = Provider.of<DatabaseProvider>(context, listen: false); if (database == null) database = Provider.of<DatabaseProvider>(context, listen: false);
await database.store.storeSettings(this); await database.store.storeSettings(this);

@ -1 +1 @@
Subproject commit 8b3ec15500b0fb686c75383704c393c524d00878 Subproject commit 08d802282c044be666f7b7ae55ae88b334ba515b