LiveActivities design fix

- Fixed if the user opens the app 1 hour before their first class, the Room section is empty in LiveActivity lock screen
- DynamicIsland reworked and improved
- The room section moved to Flutter side
This commit is contained in:
Horváth Gergely 2024-05-06 06:03:06 +02:00
parent ae66a462e7
commit 2ca8f4b8fe

View File

@ -1,7 +1,6 @@
// ignore_for_file: no_leading_underscores_for_local_identifiers
import 'dart:async';
import 'dart:io';
import 'package:refilc/api/providers/liveactivity/platform_channel.dart';
import 'package:refilc/helpers/subject.dart';
@ -18,9 +17,10 @@ enum LiveCardState {
duringLesson,
duringBreak,
morning,
weekendMorning,
afternoon,
night,
summary
summary,
}
class LiveCardProvider extends ChangeNotifier {
@ -34,18 +34,19 @@ class LiveCardProvider extends ChangeNotifier {
static bool hasDayEnd = false;
static DateTime? storeFirstRunDate;
static bool hasActivitySettingsChanged = false;
// ignore: non_constant_identifier_names
static Map<String, String> LAData = {};
static DateTime? now;
//
LiveCardState currentState = LiveCardState.empty;
// ignore: unused_field
late Timer _timer;
late final TimetableProvider _timetable;
late final SettingsProvider _settings;
late Duration _delay;
bool _hasCheckedTimetable = false;
LiveCardProvider({
@ -87,19 +88,24 @@ class LiveCardProvider extends ChangeNotifier {
case LiveCardState.morning:
return {
"color":
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
"icon": nextLesson != null
? SubjectIcon.resolveName(subject: nextLesson?.subject)
: "book",
"title": "Jó reggelt! Az első órádig:",
"subtitle": "",
"description": "",
"startDate": storeFirstRunDate != null ? ((storeFirstRunDate?.millisecondsSinceEpoch ?? 0) - (_delay.inMilliseconds)).toString(): "",
"startDate": storeFirstRunDate != null
? ((storeFirstRunDate?.millisecondsSinceEpoch ?? 0) -
(_delay.inMilliseconds))
.toString()
: "",
"endDate": ((nextLesson?.start.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"nextSubject": nextLesson != null
? nextLesson?.subject.renamedTo ?? ShortSubject.resolve(subject: nextLesson?.subject).capital()
? nextLesson?.subject.renamedTo ??
ShortSubject.resolve(subject: nextLesson?.subject).capital()
: "",
"nextRoom": nextLesson?.room.replaceAll("_", " ") ?? "",
};
@ -107,19 +113,24 @@ class LiveCardProvider extends ChangeNotifier {
case LiveCardState.afternoon:
return {
"color":
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
"icon": nextLesson != null
? SubjectIcon.resolveName(subject: nextLesson?.subject)
: "book",
"title": "Jó napot! Az első órádig:",
"subtitle": "",
"description": "",
"startDate": storeFirstRunDate != null ? ((storeFirstRunDate?.millisecondsSinceEpoch ?? 0) - (_delay.inMilliseconds)).toString(): "",
"startDate": storeFirstRunDate != null
? ((storeFirstRunDate?.millisecondsSinceEpoch ?? 0) -
(_delay.inMilliseconds))
.toString()
: "",
"endDate": ((nextLesson?.start.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"nextSubject": nextLesson != null
? nextLesson?.subject.renamedTo ?? ShortSubject.resolve(subject: nextLesson?.subject).capital()
? nextLesson?.subject.renamedTo ??
ShortSubject.resolve(subject: nextLesson?.subject).capital()
: "",
"nextRoom": nextLesson?.room.replaceAll("_", " ") ?? "",
};
@ -127,19 +138,24 @@ class LiveCardProvider extends ChangeNotifier {
case LiveCardState.night:
return {
"color":
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
"icon": nextLesson != null
? SubjectIcon.resolveName(subject: nextLesson?.subject)
: "book",
"title": "Jó estét! Az első órádig:",
"subtitle": "",
"description": "",
"startDate": storeFirstRunDate != null ? ((storeFirstRunDate?.millisecondsSinceEpoch ?? 0) - (_delay.inMilliseconds)).toString(): "",
"startDate": storeFirstRunDate != null
? ((storeFirstRunDate?.millisecondsSinceEpoch ?? 0) -
(_delay.inMilliseconds))
.toString()
: "",
"endDate": ((nextLesson?.start.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"nextSubject": nextLesson != null
? nextLesson?.subject.renamedTo ?? ShortSubject.resolve(subject: nextLesson?.subject).capital()
? nextLesson?.subject.renamedTo ??
ShortSubject.resolve(subject: nextLesson?.subject).capital()
: "",
"nextRoom": nextLesson?.room.replaceAll("_", " ") ?? "",
};
@ -147,25 +163,28 @@ class LiveCardProvider extends ChangeNotifier {
case LiveCardState.duringLesson:
return {
"color":
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
"icon": currentLesson != null
? SubjectIcon.resolveName(subject: currentLesson?.subject)
: "book",
"index":
currentLesson != null ? '${currentLesson!.lessonIndex}. ' : "",
currentLesson != null ? '${currentLesson!.lessonIndex}. ' : "",
"title": currentLesson != null
? currentLesson?.subject.renamedTo ?? ShortSubject.resolve(subject: currentLesson?.subject).capital()
? currentLesson?.subject.renamedTo ??
ShortSubject.resolve(subject: currentLesson?.subject)
.capital()
: "",
"subtitle": currentLesson?.room.replaceAll("_", " ") ?? "",
"subtitle": "Terem: ${currentLesson?.room.replaceAll("_", " ") ?? ""}",
"description": currentLesson?.description ?? "",
"startDate": ((currentLesson?.start.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"endDate": ((currentLesson?.end.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"nextSubject": nextLesson != null
? nextLesson?.subject.renamedTo ?? ShortSubject.resolve(subject: nextLesson?.subject).capital()
? nextLesson?.subject.renamedTo ??
ShortSubject.resolve(subject: nextLesson?.subject).capital()
: "",
"nextRoom": nextLesson?.room.replaceAll("_", " ") ?? "",
};
@ -181,21 +200,23 @@ class LiveCardProvider extends ChangeNotifier {
return {
"color":
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
'#${_settings.liveActivityColor.toString().substring(10, 16)}',
"icon": iconFloorMap[diff] ?? "cup.and.saucer",
"title": "Szünet",
"description": "go $diff".i18n.fill([
diff != "to room" ? (nextLesson!.getFloor() ?? 0) : nextLesson!.room
]),
"startDate": ((prevLesson?.end.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"endDate": ((nextLesson?.start.millisecondsSinceEpoch ?? 0) -
_delay.inMilliseconds)
_delay.inMilliseconds)
.toString(),
"nextSubject": (nextLesson != null
? nextLesson?.subject.renamedTo ?? ShortSubject.resolve(subject: nextLesson?.subject).capital()
: "")
? nextLesson?.subject.renamedTo ??
ShortSubject.resolve(subject: nextLesson?.subject)
.capital()
: "")
.capital(),
"nextRoom": nextLesson?.room.replaceAll("_", " ") ?? "",
"index": "",
@ -219,12 +240,12 @@ class LiveCardProvider extends ChangeNotifier {
? Duration(seconds: _settings.bellDelay)
: Duration.zero;
DateTime now = _now().add(_delay);
if ((currentState == LiveCardState.morning ||
currentState == LiveCardState.afternoon ||
currentState == LiveCardState.night) && storeFirstRunDate == null) {
currentState == LiveCardState.afternoon ||
currentState == LiveCardState.night) &&
storeFirstRunDate == null) {
storeFirstRunDate = now;
}
@ -232,9 +253,9 @@ class LiveCardProvider extends ChangeNotifier {
// Filter label lessons #128
today = today
.where((lesson) =>
lesson.status?.name != "Elmaradt" &&
lesson.subject.id != '' &&
!lesson.isEmpty)
lesson.status?.name != "Elmaradt" &&
lesson.subject.id != '' &&
!lesson.isEmpty)
.toList();
if (today.isNotEmpty) {
@ -242,7 +263,7 @@ class LiveCardProvider extends ChangeNotifier {
today.sort((a, b) => a.start.compareTo(b.start));
final _lesson = today.firstWhere(
(l) => l.start.isBefore(now) && l.end.isAfter(now),
(l) => l.start.isBefore(now) && l.end.isAfter(now),
orElse: () => Lesson.fromJson({}));
if (_lesson.start.year != 0) {
@ -283,7 +304,11 @@ class LiveCardProvider extends ChangeNotifier {
} else if (now.hour >= 20) {
currentState = LiveCardState.night;
} else if (now.hour >= 5 && now.hour <= 10) {
currentState = LiveCardState.morning;
if (nextLesson == null || now.weekday == 6 || now.weekday == 7) {
currentState = LiveCardState.empty;
} else {
currentState = LiveCardState.morning;
}
} else {
currentState = LiveCardState.empty;
}
@ -291,22 +316,21 @@ class LiveCardProvider extends ChangeNotifier {
//LIVE ACTIVITIES
//CREATE
if (!hasActivityStarted && nextLesson != null && nextLesson!
.start
.difference(now)
.inMinutes <= 60 && (currentState == LiveCardState.morning ||
currentState == LiveCardState.afternoon ||
currentState == LiveCardState.night)) {
if (!hasActivityStarted &&
nextLesson != null &&
nextLesson!.start.difference(now).inMinutes <= 60 &&
(currentState == LiveCardState.morning ||
currentState == LiveCardState.afternoon ||
currentState == LiveCardState.night)) {
debugPrint(
"Az első óra előtt állunk, kevesebb mint egy órával. Létrehozás...");
PlatformChannel.createLiveActivity(toMap());
hasActivityStarted = true;
}
else if (!hasActivityStarted && ((currentState == LiveCardState.duringLesson &&
currentLesson != null) ||
currentState == LiveCardState.duringBreak)) {
debugPrint(
"Óra van, vagy szünet, de nincs LiveActivity. létrehozás...");
} else if (!hasActivityStarted &&
((currentState == LiveCardState.duringLesson &&
currentLesson != null) ||
currentState == LiveCardState.duringBreak)) {
debugPrint("Óra van, vagy szünet, de nincs LiveActivity. létrehozás...");
PlatformChannel.createLiveActivity(toMap());
hasActivityStarted = true;
}
@ -317,15 +341,18 @@ class LiveCardProvider extends ChangeNotifier {
debugPrint("Valamelyik beállítás megváltozott. Frissítés...");
PlatformChannel.updateLiveActivity(toMap());
hasActivitySettingsChanged = false;
}
else if (nextLesson != null || currentLesson != null) {
} else if (nextLesson != null || currentLesson != null) {
bool afterPrevLessonEnd = prevLesson != null &&
now.subtract(const Duration(seconds: 1)).isBefore(
prevLesson!.end) && now.isAfter(prevLesson!.end);
now
.subtract(const Duration(seconds: 1))
.isBefore(prevLesson!.end) &&
now.isAfter(prevLesson!.end);
bool afterCurrentLessonStart = currentLesson != null &&
now.subtract(const Duration(seconds: 1)).isBefore(
currentLesson!.start) && now.isAfter(currentLesson!.start);
now
.subtract(const Duration(seconds: 1))
.isBefore(currentLesson!.start) &&
now.isAfter(currentLesson!.start);
if (afterPrevLessonEnd || afterCurrentLessonStart) {
debugPrint(
"Óra kezdete/vége után 1 másodperccel vagyunk. Frissítés...");
@ -335,7 +362,9 @@ class LiveCardProvider extends ChangeNotifier {
}
//END
if (hasActivityStarted && !hasDayEnd && nextLesson == null &&
if (hasActivityStarted &&
!hasDayEnd &&
nextLesson == null &&
now.isAfter(prevLesson!.end)) {
debugPrint("Az utolsó óra véget ért. Befejezés...");
PlatformChannel.endLiveActivity();