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