finished calendar sync settings part

This commit is contained in:
Kima
2024-03-01 22:38:46 +01:00
parent 7ebc470a7b
commit 32c5e8ae91
7 changed files with 507 additions and 66 deletions

View File

@@ -45,6 +45,10 @@ const settingsDB = DatabaseStruct("settings", {
"show_breaks": int,
"font_family": String,
"plus_session_id": String,
"cal_sync_room_location": String,
"cal_sync_show_exams": int,
"cal_sync_show_teacher": int,
"cal_sync_renamed": int,
});
// DON'T FORGET TO UPDATE DEFAULT VALUES IN `initDB` MIGRATION OR ELSE PARENTS WILL COMPLAIN ABOUT THEIR CHILDREN MISSING
// YOU'VE BEEN WARNED!!!

View File

@@ -1,6 +1,7 @@
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/services.dart';
import 'package:refilc/api/providers/database_provider.dart';
import 'package:refilc/models/config.dart';
import 'package:refilc/models/icon_pack.dart';
@@ -92,6 +93,10 @@ class SettingsProvider extends ChangeNotifier {
bool _showBreaks;
String _fontFamily;
String _plusSessionId;
String _calSyncRoomLocation;
bool _calSyncShowExams;
bool _calSyncShowTeacher;
bool _calSyncRenamed;
SettingsProvider({
DatabaseProvider? database,
@@ -150,6 +155,10 @@ class SettingsProvider extends ChangeNotifier {
required String pinSetExtras,
required String fontFamily,
required String plusSessionId,
required String calSyncRoomLocation,
required bool calSyncShowExams,
required bool calSyncShowTeacher,
required bool calSyncRenamed,
}) : _database = database,
_language = language,
_startPage = startPage,
@@ -205,7 +214,11 @@ class SettingsProvider extends ChangeNotifier {
_pinSetNotify = pinSetNotify,
_pinSetExtras = pinSetExtras,
_fontFamily = fontFamily,
_plusSessionId = plusSessionId;
_plusSessionId = plusSessionId,
_calSyncRoomLocation = calSyncRoomLocation,
_calSyncShowExams = calSyncShowExams,
_calSyncShowTeacher = calSyncShowTeacher,
_calSyncRenamed = calSyncRenamed;
factory SettingsProvider.fromMap(Map map,
{required DatabaseProvider database}) {
@@ -281,6 +294,10 @@ class SettingsProvider extends ChangeNotifier {
pinSetExtras: map['extras_s_pin'],
fontFamily: map['font_family'],
plusSessionId: map['plus_session_id'],
calSyncRoomLocation: map['cal_sync_room_location'],
calSyncShowExams: map['cal_sync_show_exams'] == 1,
calSyncShowTeacher: map['cal_sync_show_teacher'] == 1,
calSyncRenamed: map['cal_sync_renamed'] == 1,
);
}
@@ -344,6 +361,10 @@ class SettingsProvider extends ChangeNotifier {
"extras_s_pin": _pinSetExtras,
"font_family": _fontFamily,
"plus_session_id": _plusSessionId,
"cal_sync_room_location": _calSyncRoomLocation,
"cal_sync_show_exams": _calSyncShowExams ? 1 : 0,
"cal_sync_show_teacher": _calSyncShowTeacher ? 1 : 0,
"cal_sync_renamed": _calSyncRenamed ? 1 : 0,
};
}
@@ -411,6 +432,10 @@ class SettingsProvider extends ChangeNotifier {
pinSetExtras: '',
fontFamily: '',
plusSessionId: '',
calSyncRoomLocation: 'location',
calSyncShowExams: true,
calSyncShowTeacher: true,
calSyncRenamed: false,
);
}
@@ -469,6 +494,10 @@ class SettingsProvider extends ChangeNotifier {
bool get showBreaks => _showBreaks;
String get fontFamily => _fontFamily;
String get plusSessionId => _plusSessionId;
String get calSyncRoomLocation => _calSyncRoomLocation;
bool get calSyncShowExams => _calSyncShowExams;
bool get calSyncShowTeacher => _calSyncShowTeacher;
bool get calSyncRenamed => _calSyncRenamed;
Future<void> update({
bool store = true,
@@ -523,6 +552,10 @@ class SettingsProvider extends ChangeNotifier {
bool? showBreaks,
String? fontFamily,
String? plusSessionId,
String? calSyncRoomLocation,
bool? calSyncShowExams,
bool? calSyncShowTeacher,
bool? calSyncRenamed,
}) async {
if (language != null && language != _language) _language = language;
if (startPage != null && startPage != _startPage) _startPage = startPage;
@@ -673,8 +706,27 @@ class SettingsProvider extends ChangeNotifier {
if (plusSessionId != null && plusSessionId != _plusSessionId) {
_plusSessionId = plusSessionId;
}
if (calSyncRoomLocation != null &&
calSyncRoomLocation != _calSyncRoomLocation) {
_calSyncRoomLocation = calSyncRoomLocation;
}
if (calSyncShowExams != null && calSyncShowExams != _calSyncShowExams) {
_calSyncShowExams = calSyncShowExams;
}
if (calSyncShowTeacher != null &&
calSyncShowTeacher != _calSyncShowTeacher) {
_calSyncShowTeacher = calSyncShowTeacher;
}
if (calSyncRenamed != null && calSyncRenamed != _calSyncRenamed) {
_calSyncRenamed = calSyncRenamed;
}
// store or not
if (store) await _database?.store.storeSettings(this);
notifyListeners();
}
void exportJson() {
String sets = json.encode(toMap());
Clipboard.setData(ClipboardData(text: sets));
}
}

View File

@@ -3,6 +3,7 @@ import 'package:provider/provider.dart';
import 'package:refilc/api/providers/database_provider.dart';
import 'package:refilc/api/providers/user_provider.dart';
import 'package:refilc/models/linked_account.dart';
import 'package:refilc/models/user.dart';
import 'package:refilc_kreta_api/controllers/timetable_controller.dart';
import 'package:refilc_kreta_api/models/lesson.dart';
import 'package:flutter/foundation.dart';
@@ -14,7 +15,7 @@ class ThirdPartyProvider with ChangeNotifier {
late List<LinkedAccount> _linkedAccounts;
// google specific
late List<Event>? _googleEvents;
late List<Calendar>? _googleCalendars;
late List<CalendarListEntry> _googleCalendars;
late BuildContext _context;
@@ -27,16 +28,18 @@ class ThirdPartyProvider with ChangeNotifier {
List<LinkedAccount> get linkedAccounts => _linkedAccounts;
List<Event> get googleEvents => _googleEvents ?? [];
List<Calendar> get googleCalendars => _googleCalendars ?? [];
List<CalendarListEntry> get googleCalendars => _googleCalendars;
ThirdPartyProvider({
required BuildContext context,
List<LinkedAccount>? initialLinkedAccounts,
}) {
_context = context;
_linkedAccounts = initialLinkedAccounts ?? [];
_linkedAccounts = List.castFrom(initialLinkedAccounts ?? []);
_googleCalendars = [];
if (_linkedAccounts.isEmpty) restore();
if (_googleCalendars.isEmpty) fetchGoogle();
}
Future<void> restore() async {
@@ -49,23 +52,47 @@ class ThirdPartyProvider with ChangeNotifier {
.userQuery
.getLinkedAccounts(userId: userId);
_linkedAccounts = dbLinkedAccounts;
// if (res == null) {
// throw 'Google sign in failed, some data will be unavailable.';
// }
notifyListeners();
}
}
Future<void> store(List<LinkedAccount> accounts) async {
User? user = Provider.of<UserProvider>(_context, listen: false).user;
if (user == null) throw "Cannot store Linked Accounts for User null";
String userId = user.id;
await Provider.of<DatabaseProvider>(_context, listen: false)
.userStore
.storeLinkedAccounts(accounts, userId: userId);
_linkedAccounts = accounts;
notifyListeners();
}
void fetch() async {}
Future<GoogleSignInAccount?> googleSignIn() async {
// signOutAll();
if (!await _googleSignIn.isSignedIn()) {
GoogleSignInAccount? account = await _googleSignIn.signIn();
if (account == null) return null;
LinkedAccount linked = LinkedAccount.fromJson({
'type': 'google',
'username': account?.email ?? '',
'display_name': account?.displayName ?? '',
'id': account?.id ?? ''
'username': account.email,
'display_name': account.displayName ?? '',
'id': account.id,
});
_linkedAccounts.add(linked);
store(_linkedAccounts);
return account;
}
return null;
@@ -99,6 +126,24 @@ class ThirdPartyProvider with ChangeNotifier {
// }
// }
Future<void> fetchGoogle() async {
await _googleSignIn.signInSilently();
try {
var httpClient = (await _googleSignIn.authenticatedClient())!;
var calendarApi = CalendarApi(httpClient);
_googleCalendars = (await calendarApi.calendarList.list()).items ?? [];
print(_googleCalendars);
notifyListeners();
} catch (e) {
print(e);
// await _googleSignIn.signOut();
}
}
Future<Event?> pushEvent({
required String title,
required String calendarId,