init
This commit is contained in:
41
filcnaplo/lib/models/config.dart
Normal file
41
filcnaplo/lib/models/config.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
|
||||
class Config {
|
||||
String _userAgent;
|
||||
String? _version;
|
||||
Map? json;
|
||||
|
||||
Config({required String userAgent, this.json}) : _userAgent = userAgent {
|
||||
PackageInfo.fromPlatform().then((value) => _version = value.version);
|
||||
}
|
||||
|
||||
factory Config.fromJson(Map json) {
|
||||
return Config(
|
||||
userAgent: json["user_agent"] ?? "hu.filc.naplo/\$0/\$1/\$2",
|
||||
json: json,
|
||||
);
|
||||
}
|
||||
|
||||
String get userAgent => _userAgent.replaceAll("\$0", _version ?? "0").replaceAll("\$1", platform).replaceAll("\$2", "0");
|
||||
|
||||
static String get platform {
|
||||
if (Platform.isAndroid) {
|
||||
return "Android";
|
||||
} else if (Platform.isIOS) {
|
||||
return "iOS";
|
||||
} else if (Platform.isLinux) {
|
||||
return "Linux";
|
||||
} else if (Platform.isWindows) {
|
||||
return "Windows";
|
||||
} else if (Platform.isMacOS) {
|
||||
return "MacOS";
|
||||
} else {
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() => json.toString();
|
||||
}
|
||||
31
filcnaplo/lib/models/news.dart
Normal file
31
filcnaplo/lib/models/news.dart
Normal file
@@ -0,0 +1,31 @@
|
||||
class News {
|
||||
String title;
|
||||
String content;
|
||||
String link;
|
||||
String openLabel;
|
||||
String platform;
|
||||
bool emergency;
|
||||
Map? json;
|
||||
|
||||
News({
|
||||
required this.title,
|
||||
required this.content,
|
||||
required this.link,
|
||||
required this.openLabel,
|
||||
required this.platform,
|
||||
required this.emergency,
|
||||
this.json,
|
||||
});
|
||||
|
||||
factory News.fromJson(Map json) {
|
||||
return News(
|
||||
title: json["title"] ?? "",
|
||||
content: json["content"] ?? "",
|
||||
link: json["link"] ?? "",
|
||||
openLabel: json["open_label"] ?? "",
|
||||
platform: json["platform"] ?? "",
|
||||
emergency: json["emergency"] ?? false,
|
||||
json: json,
|
||||
);
|
||||
}
|
||||
}
|
||||
129
filcnaplo/lib/models/release.dart
Normal file
129
filcnaplo/lib/models/release.dart
Normal file
@@ -0,0 +1,129 @@
|
||||
class Release {
|
||||
String tag;
|
||||
Version version;
|
||||
String author;
|
||||
String body;
|
||||
List<String> downloads;
|
||||
bool prerelease;
|
||||
|
||||
Release({
|
||||
required this.tag,
|
||||
required this.author,
|
||||
required this.body,
|
||||
required this.downloads,
|
||||
required this.prerelease,
|
||||
required this.version,
|
||||
});
|
||||
|
||||
factory Release.fromJson(Map json) {
|
||||
return Release(
|
||||
tag: json["tag_name"] ?? Version.zero.toString(),
|
||||
author: json["author"] != null ? json["author"]["login"] ?? "" : "",
|
||||
body: json["body"] ?? "",
|
||||
downloads: json["assets"] != null ? json["assets"].map((a) => a["browser_download_url"] ?? "").toList().cast<String>() : [],
|
||||
prerelease: json["prerelease"] ?? false,
|
||||
version: Version.fromString(json["tag_name"] ?? ""),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Version {
|
||||
final int major;
|
||||
final int minor;
|
||||
final int patch;
|
||||
final String prerelease;
|
||||
final int prever;
|
||||
|
||||
const Version(this.major, this.minor, this.patch, {this.prerelease = "", this.prever = 0});
|
||||
|
||||
factory Version.fromString(String o) {
|
||||
String string = o;
|
||||
int x = 0, y = 0, z = 0; // major, minor, patch (1.1.1)
|
||||
String pre = ""; // prerelease string (-beta)
|
||||
int prev = 0; // prerelease version (.1)
|
||||
|
||||
try {
|
||||
// cut build
|
||||
string = string.split("+")[0];
|
||||
|
||||
// cut prerelease
|
||||
var p = string.split("-");
|
||||
string = p[0];
|
||||
if (p.length > 1) pre = p[1];
|
||||
|
||||
// prerelease
|
||||
p = pre.split(".");
|
||||
|
||||
if (p.length > 1) prev = int.tryParse(p[1]) ?? 0;
|
||||
|
||||
// check for valid prerelease name
|
||||
if (p[0] != "") {
|
||||
if (prereleases.contains(p[0].toLowerCase().trim()))
|
||||
pre = p[0];
|
||||
else
|
||||
throw "invalid prerelease name: ${p[0]}";
|
||||
}
|
||||
|
||||
// core
|
||||
p = string.split(".");
|
||||
if (p.length != 3) throw "invalid core length: ${p.length}";
|
||||
|
||||
x = int.tryParse(p[0]) ?? 0;
|
||||
y = int.tryParse(p[1]) ?? 0;
|
||||
z = int.tryParse(p[2]) ?? 0;
|
||||
|
||||
return Version(x, y, z, prerelease: pre, prever: prev);
|
||||
} catch (error) {
|
||||
print("WARNING: Failed to parse version ($o): $error");
|
||||
return Version.zero;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(other) {
|
||||
if (other is! Version) return false;
|
||||
return other.major == major && other.minor == minor && other.patch == patch && other.prei == prei && other.prever == prever;
|
||||
}
|
||||
|
||||
int compareTo(Version other) {
|
||||
if (other == this) return 0;
|
||||
|
||||
if (major > other.major) {
|
||||
return 1;
|
||||
} else if (major == other.major) {
|
||||
if (minor > other.minor) {
|
||||
return 1;
|
||||
} else if (minor == other.minor) {
|
||||
if (patch > other.patch) {
|
||||
return 1;
|
||||
} else if (patch == other.patch) {
|
||||
if (prei > other.prei) {
|
||||
return 1;
|
||||
} else if (other.prei == prei) {
|
||||
if (prever > other.prever) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
String str = "$major.$minor.$patch";
|
||||
if (prerelease != "") str += "-$prerelease";
|
||||
if (prever != 0) str += ".$prever";
|
||||
return str;
|
||||
}
|
||||
|
||||
int get prei {
|
||||
if (prerelease != "") return prereleases.indexOf(prerelease);
|
||||
return prereleases.length;
|
||||
}
|
||||
|
||||
static const zero = Version(0, 0, 0);
|
||||
static const List<String> prereleases = ["dev", "pre", "alpha", "beta", "rc"];
|
||||
}
|
||||
246
filcnaplo/lib/models/settings.dart
Normal file
246
filcnaplo/lib/models/settings.dart
Normal file
@@ -0,0 +1,246 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:filcnaplo/api/providers/database_provider.dart';
|
||||
import 'package:filcnaplo/models/config.dart';
|
||||
import 'package:filcnaplo/theme.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
enum Pages { home, grades, timetable, messages, absences }
|
||||
enum UpdateChannel { stable, beta, dev }
|
||||
enum VibrationStrength { light, medium, strong }
|
||||
|
||||
class SettingsProvider extends ChangeNotifier {
|
||||
PackageInfo? _packageInfo;
|
||||
|
||||
// en_en, hu_hu, de_de
|
||||
String _language;
|
||||
Pages _startPage;
|
||||
// divide by 10
|
||||
int _rounding;
|
||||
ThemeMode _theme;
|
||||
AccentColor _accentColor;
|
||||
// zero is one, ...
|
||||
List<Color> _gradeColors;
|
||||
bool _newsEnabled;
|
||||
int _newsState;
|
||||
bool _notificationsEnabled;
|
||||
/*
|
||||
notificationsBitfield values:
|
||||
|
||||
1 << 0 current lesson
|
||||
1 << 1 newsletter
|
||||
1 << 2 grades
|
||||
1 << 3 notes and events
|
||||
1 << 4 inbox messages
|
||||
1 << 5 substituted lessons and cancelled lessons
|
||||
1 << 6 absences and misses
|
||||
1 << 7 exams and homework
|
||||
*/
|
||||
int _notificationsBitfield;
|
||||
// minutes: times 15
|
||||
int _notificationPollInterval;
|
||||
bool _developerMode;
|
||||
bool _vibrate;
|
||||
VibrationStrength _vibrationStrength;
|
||||
bool _ABweeks;
|
||||
bool _swapABweeks;
|
||||
UpdateChannel _updateChannel;
|
||||
Config _config;
|
||||
|
||||
SettingsProvider({
|
||||
required String language,
|
||||
required Pages startPage,
|
||||
required int rounding,
|
||||
required ThemeMode theme,
|
||||
required AccentColor accentColor,
|
||||
required List<Color> gradeColors,
|
||||
required bool newsEnabled,
|
||||
required int newsState,
|
||||
required bool notificationsEnabled,
|
||||
required int notificationsBitfield,
|
||||
required bool developerMode,
|
||||
required int notificationPollInterval,
|
||||
required bool vibrate,
|
||||
required VibrationStrength vibrationStrength,
|
||||
required bool ABweeks,
|
||||
required bool swapABweeks,
|
||||
required UpdateChannel updateChannel,
|
||||
required Config config,
|
||||
}) : _language = language,
|
||||
_startPage = startPage,
|
||||
_rounding = rounding,
|
||||
_theme = theme,
|
||||
_accentColor = accentColor,
|
||||
_gradeColors = gradeColors,
|
||||
_newsEnabled = newsEnabled,
|
||||
_newsState = newsState,
|
||||
_notificationsEnabled = notificationsEnabled,
|
||||
_notificationsBitfield = notificationsBitfield,
|
||||
_developerMode = developerMode,
|
||||
_notificationPollInterval = notificationPollInterval,
|
||||
_vibrate = vibrate,
|
||||
_vibrationStrength = vibrationStrength,
|
||||
_ABweeks = ABweeks,
|
||||
_swapABweeks = swapABweeks,
|
||||
_updateChannel = updateChannel,
|
||||
_config = config {
|
||||
PackageInfo.fromPlatform().then((PackageInfo packageInfo) {
|
||||
_packageInfo = packageInfo;
|
||||
});
|
||||
}
|
||||
|
||||
factory SettingsProvider.fromMap(Map map) {
|
||||
return SettingsProvider(
|
||||
language: map["language"],
|
||||
startPage: Pages.values[map["start_page"]],
|
||||
rounding: map["rounding"],
|
||||
theme: ThemeMode.values[map["theme"]],
|
||||
accentColor: AccentColor.values[map["accent_color"]],
|
||||
gradeColors: [
|
||||
Color(map["grade_color1"]),
|
||||
Color(map["grade_color2"]),
|
||||
Color(map["grade_color3"]),
|
||||
Color(map["grade_color4"]),
|
||||
Color(map["grade_color5"]),
|
||||
],
|
||||
newsEnabled: map["news"] == 1 ? true : false,
|
||||
newsState: map["news_state"],
|
||||
notificationsEnabled: map["notifications"] == 1 ? true : false,
|
||||
notificationsBitfield: map["notifications_bitfield"],
|
||||
notificationPollInterval: map["notification_poll_interval"],
|
||||
developerMode: map["developer_mode"] == 1 ? true : false,
|
||||
vibrate: map["vibrate"] == 1 ? true : false,
|
||||
vibrationStrength: VibrationStrength.values[map["vibration_strength"]],
|
||||
ABweeks: map["ab_weeks"] == 1 ? true : false,
|
||||
swapABweeks: map["swap_ab_weeks"] == 1 ? true : false,
|
||||
updateChannel: UpdateChannel.values[map["update_channel"]],
|
||||
config: Config.fromJson(jsonDecode(map["config"] ?? "{}")),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, Object?> toMap() {
|
||||
return {
|
||||
"language": _language,
|
||||
"start_page": _startPage.index,
|
||||
"rounding": _rounding,
|
||||
"theme": _theme.index,
|
||||
"accent_color": _accentColor.index,
|
||||
"news": _newsEnabled ? 1 : 0,
|
||||
"news_state": _newsState,
|
||||
"notifications": _notificationsEnabled ? 1 : 0,
|
||||
"notifications_bitfield": _notificationsBitfield,
|
||||
"developer_mode": _developerMode ? 1 : 0,
|
||||
"grade_color1": _gradeColors[0].value,
|
||||
"grade_color2": _gradeColors[1].value,
|
||||
"grade_color3": _gradeColors[2].value,
|
||||
"grade_color4": _gradeColors[3].value,
|
||||
"grade_color5": _gradeColors[4].value,
|
||||
"update_channel": _updateChannel.index,
|
||||
"vibrate": _vibrate ? 1 : 0,
|
||||
"vibration_strength": _vibrationStrength.index,
|
||||
"ab_weeks": _ABweeks ? 1 : 0,
|
||||
"swap_ab_weeks": _swapABweeks ? 1 : 0,
|
||||
"notification_poll_interval": _notificationPollInterval,
|
||||
"config": jsonEncode(config.json),
|
||||
};
|
||||
}
|
||||
|
||||
factory SettingsProvider.defaultSettings() {
|
||||
return SettingsProvider(
|
||||
language: "hu",
|
||||
startPage: Pages.home,
|
||||
rounding: 5,
|
||||
theme: ThemeMode.system,
|
||||
accentColor: AccentColor.filc,
|
||||
gradeColors: [
|
||||
DarkAppColors().red,
|
||||
DarkAppColors().orange,
|
||||
DarkAppColors().yellow,
|
||||
DarkAppColors().green,
|
||||
DarkAppColors().filc,
|
||||
],
|
||||
newsEnabled: true,
|
||||
newsState: -1,
|
||||
notificationsEnabled: true,
|
||||
notificationsBitfield: 255,
|
||||
developerMode: false,
|
||||
notificationPollInterval: 1,
|
||||
vibrate: true,
|
||||
vibrationStrength: VibrationStrength.medium,
|
||||
ABweeks: false,
|
||||
swapABweeks: false,
|
||||
updateChannel: UpdateChannel.stable,
|
||||
config: Config.fromJson({}),
|
||||
);
|
||||
}
|
||||
|
||||
// Getters
|
||||
String get language => _language;
|
||||
Pages get startPage => _startPage;
|
||||
int get rounding => _rounding;
|
||||
ThemeMode get theme => _theme;
|
||||
AccentColor get accentColor => _accentColor;
|
||||
List<Color> get gradeColors => _gradeColors;
|
||||
bool get newsEnabled => _newsEnabled;
|
||||
int get newsState => _newsState;
|
||||
bool get notificationsEnabled => _notificationsEnabled;
|
||||
int get notificationsBitfield => _notificationsBitfield;
|
||||
bool get developerMode => _developerMode;
|
||||
int get notificationPollInterval => _notificationPollInterval;
|
||||
bool get vibrate => _vibrate;
|
||||
VibrationStrength get vibrationStrength => _vibrationStrength;
|
||||
bool get ABweeks => _ABweeks;
|
||||
bool get swapABweeks => _swapABweeks;
|
||||
UpdateChannel get updateChannel => _updateChannel;
|
||||
PackageInfo? get packageInfo => _packageInfo;
|
||||
Config get config => _config;
|
||||
|
||||
Future<void> update(
|
||||
BuildContext context, {
|
||||
DatabaseProvider? database,
|
||||
String? language,
|
||||
Pages? startPage,
|
||||
int? rounding,
|
||||
ThemeMode? theme,
|
||||
AccentColor? accentColor,
|
||||
List<Color>? gradeColors,
|
||||
bool? newsEnabled,
|
||||
int? newsState,
|
||||
bool? notificationsEnabled,
|
||||
int? notificationsBitfield,
|
||||
bool? developerMode,
|
||||
int? notificationPollInterval,
|
||||
bool? vibrate,
|
||||
VibrationStrength? vibrationStrength,
|
||||
bool? ABweeks,
|
||||
bool? swapABweeks,
|
||||
UpdateChannel? updateChannel,
|
||||
Config? config,
|
||||
}) async {
|
||||
if (language != null && language != _language) _language = language;
|
||||
if (startPage != null && startPage != _startPage) _startPage = startPage;
|
||||
if (rounding != null && rounding != _rounding) _rounding = rounding;
|
||||
if (theme != null && theme != _theme) _theme = theme;
|
||||
if (accentColor != null && accentColor != _accentColor) _accentColor = accentColor;
|
||||
if (gradeColors != null && gradeColors != _gradeColors) _gradeColors = gradeColors;
|
||||
if (newsEnabled != null && newsEnabled != _newsEnabled) _newsEnabled = newsEnabled;
|
||||
if (newsState != null && newsState != _newsState) _newsState = newsState;
|
||||
if (notificationsEnabled != null && notificationsEnabled != _notificationsEnabled) _notificationsEnabled = notificationsEnabled;
|
||||
if (notificationsBitfield != null && notificationsBitfield != _notificationsBitfield) _notificationsBitfield = notificationsBitfield;
|
||||
if (developerMode != null && developerMode != _developerMode) _developerMode = developerMode;
|
||||
if (notificationPollInterval != null && notificationPollInterval != _notificationPollInterval)
|
||||
_notificationPollInterval = notificationPollInterval;
|
||||
if (vibrate != null && vibrate != _vibrate) _vibrate = vibrate;
|
||||
if (vibrationStrength != null && vibrationStrength != _vibrationStrength) _vibrationStrength = vibrationStrength;
|
||||
if (ABweeks != null && ABweeks != _ABweeks) _ABweeks = ABweeks;
|
||||
if (swapABweeks != null && swapABweeks != _swapABweeks) _swapABweeks = swapABweeks;
|
||||
if (updateChannel != null && updateChannel != _updateChannel) _updateChannel = updateChannel;
|
||||
if (config != null && config != _config) _config = config;
|
||||
|
||||
if (database == null) database = Provider.of<DatabaseProvider>(context, listen: false);
|
||||
await database.store.storeSettings(this);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
64
filcnaplo/lib/models/user.dart
Normal file
64
filcnaplo/lib/models/user.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'dart:convert';
|
||||
import 'package:filcnaplo_kreta_api/client/api.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/student.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
class User {
|
||||
late String id;
|
||||
String username;
|
||||
String password;
|
||||
String instituteCode;
|
||||
String name;
|
||||
Student student;
|
||||
|
||||
User({
|
||||
String? id,
|
||||
required this.name,
|
||||
required this.username,
|
||||
required this.password,
|
||||
required this.instituteCode,
|
||||
required this.student,
|
||||
}) {
|
||||
if (id != null) {
|
||||
this.id = id;
|
||||
} else {
|
||||
this.id = Uuid().v4();
|
||||
}
|
||||
}
|
||||
|
||||
factory User.fromMap(Map map) {
|
||||
return User(
|
||||
id: map["id"],
|
||||
instituteCode: map["institute_code"],
|
||||
username: map["username"],
|
||||
password: map["password"],
|
||||
name: map["name"],
|
||||
student: Student.fromJson(jsonDecode(map["student"])),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, Object?> toMap() {
|
||||
return {
|
||||
"id": id,
|
||||
"username": username,
|
||||
"password": password,
|
||||
"institute_code": instituteCode,
|
||||
"name": name,
|
||||
"student": jsonEncode(student.json),
|
||||
};
|
||||
}
|
||||
|
||||
static Map<String, Object?> loginBody({
|
||||
required String username,
|
||||
required String password,
|
||||
required String instituteCode,
|
||||
}) {
|
||||
return {
|
||||
"userName": username,
|
||||
"password": password,
|
||||
"institute_code": instituteCode,
|
||||
"grant_type": "password",
|
||||
"client_id": KretaAPI.CLIENT_ID,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user