forked from firka/firka
ref: reauth_cubit -> toast_cubit
This commit is contained in:
@@ -24,7 +24,7 @@ import 'package:isar_community/isar.dart';
|
||||
import 'package:kreta_api/kreta_api.dart';
|
||||
|
||||
import 'package:firka/app/app_state.dart';
|
||||
import 'package:firka/core/bloc/reauth_cubit.dart';
|
||||
import 'package:firka/core/bloc/toast_cubit.dart';
|
||||
import 'package:firka_common/data/models/token_model.dart';
|
||||
import 'package:firka/core/debug_helper.dart';
|
||||
import 'package:firka_common/data/util.dart';
|
||||
@@ -44,12 +44,11 @@ const backoffStep = 500;
|
||||
class KretaClient {
|
||||
Future<TokenModel>? _tokenMutexCompleter;
|
||||
final CacheManager cache;
|
||||
final ReauthCubit _reauthCubit;
|
||||
final ToastCubit _toastCubit = initData.toastCubit;
|
||||
|
||||
KretaClient(TokenModel model, this._reauthCubit)
|
||||
: cache = CacheManager(model);
|
||||
KretaClient(TokenModel model) : cache = CacheManager(model);
|
||||
|
||||
bool get needsReauth => _reauthCubit.state.needsReauth;
|
||||
bool get needsReauth => _toastCubit.state.type == ActiveToastType.reauth;
|
||||
|
||||
static int lessonStartToMins(LessonCacheModel lesson) {
|
||||
return Duration(
|
||||
@@ -135,30 +134,32 @@ class KretaClient {
|
||||
}
|
||||
|
||||
Future<void> renewCache({bool reInit = false}) async {
|
||||
if (reInit) {
|
||||
await init();
|
||||
_toastCubit.setActiveToast(.fetching);
|
||||
try {
|
||||
if (reInit) {
|
||||
await init();
|
||||
}
|
||||
await Future.wait([
|
||||
getTests(),
|
||||
getHomework(),
|
||||
renewMessages(),
|
||||
getGrades(),
|
||||
]);
|
||||
await renewTimetable();
|
||||
|
||||
// manual link
|
||||
await getOmissions();
|
||||
|
||||
cache.resolveTeachers();
|
||||
_toastCubit.setActiveToast(.none);
|
||||
} catch (e) {
|
||||
if (!isTokenExpired(e)) {
|
||||
_toastCubit.setActiveToast(.error, e);
|
||||
}
|
||||
}
|
||||
await Future.wait([
|
||||
getTests(),
|
||||
getHomework(),
|
||||
renewMessages(),
|
||||
getGrades(),
|
||||
]);
|
||||
await renewTimetable();
|
||||
|
||||
// manual link
|
||||
await getOmissions();
|
||||
|
||||
cache.resolveTeachers();
|
||||
}
|
||||
|
||||
void clearReauthFlag() {
|
||||
_reauthCubit.clear();
|
||||
debugPrint('[KretaClient] Reauth flag cleared');
|
||||
}
|
||||
|
||||
Future<void> _setReauthFlag() async {
|
||||
if (needsReauth) return;
|
||||
if (Platform.isIOS) {
|
||||
try {
|
||||
_watchChannel.invokeMethod('notifyReauthRequired');
|
||||
@@ -166,7 +167,7 @@ class KretaClient {
|
||||
debugPrint('[KretaClient] Watch reauth notification skipped: $e');
|
||||
}
|
||||
}
|
||||
_reauthCubit.setNeedsReauth(true);
|
||||
_toastCubit.setActiveToast(ActiveToastType.reauth);
|
||||
debugPrint('[KretaClient] Reauth flag set');
|
||||
}
|
||||
|
||||
@@ -237,18 +238,14 @@ class KretaClient {
|
||||
}
|
||||
|
||||
Future<bool> recoverToken() async {
|
||||
logger.info("[Recovery] Starting central token recovery...");
|
||||
final now = DateTime.now();
|
||||
final localExpiry = cache.token.expiryDate;
|
||||
if (localExpiry != null &&
|
||||
localExpiry.isAfter(now.add(const Duration(seconds: 60)))) {
|
||||
logger.info(
|
||||
"[Recovery] Existing token is still valid, skipping recovery steps",
|
||||
);
|
||||
clearReauthFlag();
|
||||
return true;
|
||||
}
|
||||
|
||||
logger.info("[Recovery] Starting central token recovery...");
|
||||
logger.info("[Recovery] Step 1: Trying local token refresh...");
|
||||
try {
|
||||
var tokenModel = await _refreshModelWithCrossDeviceLease(cache.token);
|
||||
@@ -259,7 +256,6 @@ class KretaClient {
|
||||
|
||||
cache.token = tokenModel;
|
||||
await _syncTokenToAppleTargets(cache.token);
|
||||
clearReauthFlag();
|
||||
logger.info("[Recovery] Step 1 SUCCESS: Local refresh succeeded");
|
||||
return true;
|
||||
} catch (e) {
|
||||
@@ -313,7 +309,6 @@ class KretaClient {
|
||||
logger.info(
|
||||
"[Recovery] Step 2 SUCCESS on attempt ${attempt + 1}: usable iCloud token applied without immediate refresh",
|
||||
);
|
||||
clearReauthFlag();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -329,7 +324,6 @@ class KretaClient {
|
||||
|
||||
cache.token = tokenModel;
|
||||
await _syncTokenToAppleTargets(cache.token);
|
||||
clearReauthFlag();
|
||||
logger.info("[Recovery] Step 2 SUCCESS on attempt ${attempt + 1}");
|
||||
return true;
|
||||
} catch (e) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import 'package:go_router/go_router.dart';
|
||||
import 'package:firka/api/client/kreta_client.dart';
|
||||
import 'package:firka/core/bloc/home_refresh_cubit.dart';
|
||||
import 'package:firka/core/bloc/profile_picture_cubit.dart';
|
||||
import 'package:firka/core/bloc/reauth_cubit.dart';
|
||||
import 'package:firka/core/bloc/toast_cubit.dart';
|
||||
import 'package:firka/core/bloc/settings_cubit.dart';
|
||||
import 'package:firka/core/bloc/theme_cubit.dart';
|
||||
import 'package:firka/core/settings.dart';
|
||||
@@ -59,7 +59,7 @@ class AppInitialization {
|
||||
final ThemeCubit themeCubit = ThemeCubit();
|
||||
final SettingsCubit settingsCubit = SettingsCubit();
|
||||
final ProfilePictureCubit profilePictureCubit = ProfilePictureCubit();
|
||||
final ReauthCubit reauthCubit = ReauthCubit();
|
||||
final ToastCubit toastCubit = ToastCubit();
|
||||
final HomeRefreshCubit homeRefreshCubit = HomeRefreshCubit();
|
||||
AppLocalizations l10n;
|
||||
String userAgent;
|
||||
|
||||
@@ -162,13 +162,13 @@ Future<void> _initData(AppInitialization init) async {
|
||||
return;
|
||||
}
|
||||
logger.fine("Initializing kréta client as: ${token.username}");
|
||||
init.client = KretaClient(token, init.reauthCubit!);
|
||||
init.client = KretaClient(token);
|
||||
await init.client!.init();
|
||||
|
||||
if (Platform.isIOS) {
|
||||
final expiryDate = token.expiryDate;
|
||||
if (expiryDate != null && expiryDate.isAfter(DateTime.now())) {
|
||||
init.reauthCubit?.clear();
|
||||
init.toastCubit?.clear();
|
||||
}
|
||||
|
||||
unawaited(() async {
|
||||
|
||||
@@ -13,7 +13,7 @@ import 'package:firka/app/initialization.dart';
|
||||
import 'package:firka/core/bloc/home_refresh_cubit.dart';
|
||||
import 'package:firka/core/settings.dart';
|
||||
import 'package:firka/core/bloc/profile_picture_cubit.dart';
|
||||
import 'package:firka/core/bloc/reauth_cubit.dart';
|
||||
import 'package:firka/core/bloc/toast_cubit.dart';
|
||||
import 'package:firka/core/bloc/settings_cubit.dart';
|
||||
import 'package:firka/core/bloc/theme_cubit.dart';
|
||||
import 'package:firka/core/firka_bundle.dart';
|
||||
@@ -181,7 +181,7 @@ class _InitializationScreenState extends State<InitializationScreen> {
|
||||
final themeCubit = initData.themeCubit!;
|
||||
final settingsCubit = initData.settingsCubit!;
|
||||
final profilePictureCubit = initData.profilePictureCubit!;
|
||||
final reauthCubit = initData.reauthCubit!;
|
||||
final reauthCubit = initData.toastCubit!;
|
||||
final homeRefreshCubit = initData.homeRefreshCubit!;
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
@@ -190,7 +190,7 @@ class _InitializationScreenState extends State<InitializationScreen> {
|
||||
BlocProvider<ProfilePictureCubit>.value(
|
||||
value: profilePictureCubit,
|
||||
),
|
||||
BlocProvider<ReauthCubit>.value(value: reauthCubit),
|
||||
BlocProvider<ToastCubit>.value(value: reauthCubit),
|
||||
BlocProvider<HomeRefreshCubit>.value(value: homeRefreshCubit),
|
||||
],
|
||||
child: BlocBuilder<ThemeCubit, ThemeState>(
|
||||
@@ -207,8 +207,6 @@ class _InitializationScreenState extends State<InitializationScreen> {
|
||||
systemStatusBarContrastEnforced: false,
|
||||
);
|
||||
|
||||
logger.info("asked");
|
||||
|
||||
final themeMode = isLight ? ThemeMode.light : ThemeMode.dark;
|
||||
|
||||
final fallbackBg = isLight
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
class ReauthState {
|
||||
final bool needsReauth;
|
||||
|
||||
const ReauthState({this.needsReauth = false});
|
||||
}
|
||||
|
||||
class ReauthCubit extends Cubit<ReauthState> {
|
||||
ReauthCubit() : super(const ReauthState());
|
||||
|
||||
void setNeedsReauth(bool value) {
|
||||
emit(ReauthState(needsReauth: value));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
emit(const ReauthState(needsReauth: false));
|
||||
}
|
||||
}
|
||||
37
firka/lib/core/bloc/toast_cubit.dart
Normal file
37
firka/lib/core/bloc/toast_cubit.dart
Normal file
@@ -0,0 +1,37 @@
|
||||
import 'package:firka/ui/phone/pages/extras/firka_toast.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
|
||||
enum ActiveToastType { fetching, error, reauth, none }
|
||||
|
||||
class ToastState {
|
||||
final ActiveToastType type;
|
||||
final Object? extra;
|
||||
|
||||
const ToastState(this.type, [this.extra]);
|
||||
|
||||
Widget? buildWidget(BuildContext context) {
|
||||
switch (type) {
|
||||
case .fetching:
|
||||
return FirkaToastWidget.fetching(context);
|
||||
case .error:
|
||||
return FirkaToastWidget.error(context, extra!);
|
||||
case .reauth:
|
||||
return FirkaToastWidget.reauth(context);
|
||||
case .none:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ToastCubit extends Cubit<ToastState> {
|
||||
ToastCubit() : super(const ToastState(ActiveToastType.none));
|
||||
|
||||
void setActiveToast(ActiveToastType type, [Object? extra]) {
|
||||
emit(ToastState(type, extra));
|
||||
}
|
||||
|
||||
void clear() {
|
||||
setActiveToast(ActiveToastType.none);
|
||||
}
|
||||
}
|
||||
@@ -412,7 +412,7 @@ class WatchSyncHelper {
|
||||
await isar.tokenModels.clear();
|
||||
});
|
||||
|
||||
if (initDone) initData.reauthCubit?.clear();
|
||||
if (initDone) initData.toastCubit?.clear();
|
||||
|
||||
await prefs.setBool(_iosFreshInstallHandledKey, true);
|
||||
return true;
|
||||
@@ -495,7 +495,7 @@ class WatchSyncHelper {
|
||||
final token = initData.client!.cache.token;
|
||||
final expiryDate = token?.expiryDate;
|
||||
if (expiryDate != null && expiryDate.isAfter(DateTime.now())) {
|
||||
if (initDone) initData.reauthCubit?.clear();
|
||||
if (initDone) initData.toastCubit?.clear();
|
||||
debugPrint(
|
||||
'[WatchSync] Cleared reauth flag after iCloud notification (token is valid)',
|
||||
);
|
||||
@@ -742,7 +742,7 @@ class WatchSyncHelper {
|
||||
|
||||
if (isForActiveAccount) {
|
||||
initData.client!.cache.token = newToken;
|
||||
if (initDone) initData.reauthCubit?.clear();
|
||||
if (initDone) initData.toastCubit?.clear();
|
||||
} else {
|
||||
debugPrint(
|
||||
'[WatchSync] Stored token for inactive account ($watchStudentIdNorm), active is $expectedStudentIdNorm',
|
||||
@@ -948,7 +948,7 @@ class WatchSyncHelper {
|
||||
(expectedStudentIdNorm == null ||
|
||||
newToken.key == expectedStudentIdNorm);
|
||||
if (shouldClearReauth) {
|
||||
if (initDone) initData.reauthCubit?.clear();
|
||||
if (initDone) initData.toastCubit?.clear();
|
||||
}
|
||||
|
||||
debugPrint(
|
||||
@@ -1023,7 +1023,7 @@ class WatchSyncHelper {
|
||||
currentToken.accessToken != null &&
|
||||
currentToken.refreshToken != null &&
|
||||
currentToken.expiryDate != null &&
|
||||
!(initData.reauthCubit?.state.needsReauth ?? false)) {
|
||||
!initData.client!.needsReauth) {
|
||||
debugPrint('[WatchSync] Sending iPhone token to Watch (no response)');
|
||||
await _sendTokenToWatchInternal(
|
||||
currentToken,
|
||||
@@ -1040,7 +1040,7 @@ class WatchSyncHelper {
|
||||
currentToken.accessToken != null &&
|
||||
currentToken.refreshToken != null &&
|
||||
currentToken.expiryDate != null &&
|
||||
!(initData.reauthCubit?.state.needsReauth ?? false)) {
|
||||
!initData.client!.needsReauth) {
|
||||
debugPrint(
|
||||
'[WatchSync] Sending iPhone token to Watch (Watch has no token)',
|
||||
);
|
||||
@@ -1073,7 +1073,7 @@ class WatchSyncHelper {
|
||||
currentToken.accessToken != null &&
|
||||
currentToken.refreshToken != null &&
|
||||
currentToken.expiryDate != null &&
|
||||
!(initData.reauthCubit?.state.needsReauth ?? false)) {
|
||||
!(initData.client!.needsReauth)) {
|
||||
await _sendTokenToWatchInternal(
|
||||
currentToken,
|
||||
allowExpiredAccessToken: true,
|
||||
@@ -1095,7 +1095,7 @@ class WatchSyncHelper {
|
||||
currentToken.expiryDate,
|
||||
skew: const Duration(),
|
||||
) &&
|
||||
!(initData.reauthCubit?.state.needsReauth ?? false)) {
|
||||
!initData.client!.needsReauth) {
|
||||
await _sendTokenToWatchInternal(
|
||||
currentToken,
|
||||
allowExpiredAccessToken: true,
|
||||
@@ -1140,7 +1140,7 @@ class WatchSyncHelper {
|
||||
|
||||
if (expectedStudentIdNorm == null ||
|
||||
newToken.key == expectedStudentIdNorm) {
|
||||
if (initDone) initData.reauthCubit?.clear();
|
||||
if (initDone) initData.toastCubit?.clear();
|
||||
}
|
||||
|
||||
debugPrint(
|
||||
|
||||
@@ -8,7 +8,7 @@ import 'package:isar_community/isar.dart';
|
||||
import 'package:watch_connectivity/watch_connectivity.dart';
|
||||
|
||||
import 'package:firka/api/client/kreta_client.dart';
|
||||
import 'package:firka/core/bloc/reauth_cubit.dart';
|
||||
import 'package:firka/core/bloc/toast_cubit.dart';
|
||||
import 'package:firka_common/data/models/app_settings_model.dart';
|
||||
import 'package:firka_common/data/models/generic_cache_model.dart';
|
||||
import 'package:firka_common/data/models/homework_cache_model.dart';
|
||||
@@ -33,8 +33,7 @@ void wearSyncBackgroundEntrypoint() {
|
||||
final tokens = await isarInit.tokenModels.where().findAll();
|
||||
if (tokens.isEmpty) return null;
|
||||
final token = tokens.first;
|
||||
final reauthCubit = ReauthCubit();
|
||||
final client = KretaClient(token, reauthCubit);
|
||||
final client = KretaClient(token);
|
||||
final payload = await buildWearSyncPayload(client);
|
||||
if (payload == null) return null;
|
||||
await writeWearSyncCache(cachePath, payload);
|
||||
|
||||
149
firka/lib/ui/phone/pages/extras/firka_toast.dart
Normal file
149
firka/lib/ui/phone/pages/extras/firka_toast.dart
Normal file
@@ -0,0 +1,149 @@
|
||||
import 'package:firka/app/app_state.dart';
|
||||
import 'package:firka/ui/phone/pages/extras/main_error.dart';
|
||||
import 'package:firka/ui/theme/style.dart';
|
||||
import 'package:firka/ui/phone/pages/extras/main_reauth.dart';
|
||||
import 'package:firka/ui/shared/firka_icon.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:majesticons_flutter/majesticons_flutter.dart';
|
||||
|
||||
class FirkaToastWidget extends StatefulWidget {
|
||||
final Function()? onTap;
|
||||
final Function()? onDismiss;
|
||||
final List<Widget> children;
|
||||
final Color backgroundColor;
|
||||
|
||||
const FirkaToastWidget({
|
||||
super.key,
|
||||
required this.backgroundColor,
|
||||
this.onTap,
|
||||
this.onDismiss,
|
||||
required this.children,
|
||||
});
|
||||
|
||||
factory FirkaToastWidget.fetching(BuildContext context) {
|
||||
return FirkaToastWidget(
|
||||
backgroundColor: appStyle.colors.card,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(color: appStyle.colors.accent),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
Text(
|
||||
initData.l10n.refreshing,
|
||||
style: appStyle.fonts.B_16SB.copyWith(
|
||||
color: appStyle.colors.textPrimary,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
factory FirkaToastWidget.error(BuildContext context, Object e) {
|
||||
return FirkaToastWidget(
|
||||
backgroundColor: appStyle.colors.errorCard,
|
||||
children: [
|
||||
Text(
|
||||
initData.l10n.api_error,
|
||||
style: appStyle.fonts.B_16SB.copyWith(
|
||||
color: appStyle.colors.errorText,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
GestureDetector(
|
||||
child: FirkaIconWidget(
|
||||
FirkaIconType.majesticons,
|
||||
Majesticon.questionCircleSolid,
|
||||
color: appStyle.colors.errorAccent,
|
||||
size: 24,
|
||||
),
|
||||
onTap: () {
|
||||
showErrorBottomSheet(
|
||||
context,
|
||||
"$e\n${e is Error ? e.stackTrace ?? '' : ''}",
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
factory FirkaToastWidget.reauth(BuildContext context) {
|
||||
return FirkaToastWidget(
|
||||
backgroundColor: appStyle.colors.errorCard,
|
||||
onTap: () {
|
||||
showReauthBottomSheet(context, initData, initData.l10n.reauth);
|
||||
},
|
||||
onDismiss: () {
|
||||
initData.toastCubit.clear();
|
||||
},
|
||||
children: [
|
||||
Text(
|
||||
initData.l10n.reauth,
|
||||
style: appStyle.fonts.B_16SB.copyWith(
|
||||
color: appStyle.colors.errorText,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
FirkaIconWidget(
|
||||
FirkaIconType.majesticons,
|
||||
Majesticon.loginSolid,
|
||||
color: appStyle.colors.errorAccent,
|
||||
size: 24,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
State<FirkaToastWidget> createState() => _FirkaToastWidgetState();
|
||||
}
|
||||
|
||||
class _FirkaToastWidgetState extends State<FirkaToastWidget> {
|
||||
double _dragOffset = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Positioned(
|
||||
top: MediaQuery.of(context).size.height / 1.6 + _dragOffset,
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
bottom: 0,
|
||||
child: Center(
|
||||
child: GestureDetector(
|
||||
onTap: widget.onTap,
|
||||
onVerticalDragUpdate: (details) {
|
||||
setState(() {
|
||||
if (widget.onDismiss != null) _dragOffset += details.delta.dy;
|
||||
if (_dragOffset < 0) _dragOffset = 0;
|
||||
});
|
||||
},
|
||||
onVerticalDragEnd: (details) {
|
||||
if (_dragOffset > 50) {
|
||||
widget.onDismiss!();
|
||||
} else {
|
||||
setState(() {
|
||||
_dragOffset = 0;
|
||||
});
|
||||
}
|
||||
},
|
||||
child: Card(
|
||||
color: appStyle.colors.errorCard,
|
||||
shadowColor: Colors.transparent,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(20)),
|
||||
),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(10),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: widget.children,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
import 'package:firka/app/app_state.dart';
|
||||
import 'package:firka/ui/theme/style.dart';
|
||||
import 'package:firka/ui/phone/pages/extras/main_reauth.dart';
|
||||
import 'package:firka/ui/shared/firka_icon.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:majesticons_flutter/majesticons_flutter.dart';
|
||||
|
||||
class ReauthToastWidget extends StatefulWidget {
|
||||
final AppInitialization data;
|
||||
final Function() onDismiss;
|
||||
|
||||
const ReauthToastWidget({
|
||||
super.key,
|
||||
required this.data,
|
||||
required this.onDismiss,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ReauthToastWidget> createState() => _ReauthToastWidgetState();
|
||||
}
|
||||
|
||||
class _ReauthToastWidgetState extends State<ReauthToastWidget> {
|
||||
double _dragOffset = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Positioned(
|
||||
top: MediaQuery.of(context).size.height / 1.6 + _dragOffset,
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
bottom: 0,
|
||||
child: Center(
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
showReauthBottomSheet(
|
||||
context,
|
||||
widget.data,
|
||||
widget.data.l10n.reauth,
|
||||
);
|
||||
},
|
||||
onVerticalDragUpdate: (details) {
|
||||
setState(() {
|
||||
_dragOffset += details.delta.dy;
|
||||
if (_dragOffset < 0) _dragOffset = 0;
|
||||
});
|
||||
},
|
||||
onVerticalDragEnd: (details) {
|
||||
if (_dragOffset > 50) {
|
||||
widget.onDismiss();
|
||||
} else {
|
||||
setState(() {
|
||||
_dragOffset = 0;
|
||||
});
|
||||
}
|
||||
},
|
||||
child: Card(
|
||||
color: appStyle.colors.errorCard,
|
||||
shadowColor: Colors.transparent,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(200)),
|
||||
),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
widget.data.l10n.reauth,
|
||||
style: appStyle.fonts.B_16SB.copyWith(
|
||||
color: appStyle.colors.errorText,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
FirkaIconWidget(
|
||||
FirkaIconType.majesticons,
|
||||
Majesticon.loginSolid,
|
||||
color: appStyle.colors.errorAccent,
|
||||
size: 24,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Widget buildReauthToast(
|
||||
BuildContext context,
|
||||
AppInitialization data,
|
||||
Function() onDismiss,
|
||||
) {
|
||||
return ReauthToastWidget(data: data, onDismiss: onDismiss);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:firka/core/bloc/toast_cubit.dart';
|
||||
import 'package:kreta_api/kreta_api.dart';
|
||||
import 'package:firka/core/extensions.dart';
|
||||
import 'package:firka/core/firka_bundle.dart';
|
||||
@@ -8,7 +9,7 @@ import 'package:firka/core/settings.dart';
|
||||
import 'package:firka/services/watch_sync_helper.dart';
|
||||
import 'package:firka/app/app_state.dart';
|
||||
import 'package:firka/ui/theme/style.dart';
|
||||
import 'package:firka/ui/phone/pages/extras/reauth_toast.dart';
|
||||
import 'package:firka/ui/phone/pages/extras/firka_toast.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
@@ -18,7 +19,6 @@ import 'package:majesticons_flutter/majesticons_flutter.dart';
|
||||
|
||||
import 'package:firka/core/debug_helper.dart';
|
||||
import 'package:firka/core/bloc/profile_picture_cubit.dart';
|
||||
import 'package:firka/core/bloc/reauth_cubit.dart';
|
||||
import 'package:firka/core/bloc/settings_cubit.dart';
|
||||
import 'package:firka/core/state/firka_state.dart';
|
||||
import 'package:firka/core/image_preloader.dart';
|
||||
@@ -26,8 +26,6 @@ import 'package:firka/ui/shared/delayed_spinner.dart';
|
||||
import 'package:firka/ui/shared/firka_icon.dart';
|
||||
import '../../pages/extras/main_error.dart';
|
||||
|
||||
enum ActiveToastType { fetching, error, reauth, none }
|
||||
|
||||
bool _fetching = false;
|
||||
bool _prefetched = false;
|
||||
|
||||
@@ -42,15 +40,12 @@ class HomeScreen extends StatefulWidget {
|
||||
|
||||
class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
with WidgetsBindingObserver {
|
||||
Widget? toast;
|
||||
bool _disposed = false;
|
||||
bool _preloadDone = false;
|
||||
bool _didRunSecondaryICloudRecovery = false;
|
||||
bool _didRunLiveActivityLogin = false;
|
||||
bool _hasCompletedFirstPrefetch = false;
|
||||
|
||||
ActiveToastType activeToast = ActiveToastType.none;
|
||||
|
||||
void _setupNotificationListener() {
|
||||
final notificationChannel = MethodChannel('firka.app/notifications');
|
||||
|
||||
@@ -105,44 +100,6 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _runSecondaryICloudRecoveryIfNeeded() async {
|
||||
if (!Platform.isIOS || _didRunSecondaryICloudRecovery) return;
|
||||
_didRunSecondaryICloudRecovery = true;
|
||||
|
||||
final activeToken = initData.client!.cache.token;
|
||||
|
||||
final now = DateTime.now();
|
||||
final shouldRunRecovery =
|
||||
initData.client!.needsReauth ||
|
||||
activeToken == null ||
|
||||
activeToken.expiryDate == null ||
|
||||
activeToken.expiryDate!.isBefore(now.add(const Duration(seconds: 60)));
|
||||
|
||||
if (!shouldRunRecovery) return;
|
||||
|
||||
logger.info(
|
||||
'[Home] Secondary iCloud recovery scheduled (5s delay, startup safety pass)',
|
||||
);
|
||||
await Future.delayed(const Duration(seconds: 5));
|
||||
if (_disposed) return;
|
||||
|
||||
try {
|
||||
final recovered = await WatchSyncHelper.checkAndRecoverFromiCloud(
|
||||
isar: initData.isar,
|
||||
client: initData.client,
|
||||
);
|
||||
if (!recovered) {
|
||||
logger.info('[Home] Secondary iCloud recovery found no fresher token');
|
||||
return;
|
||||
}
|
||||
|
||||
initData.reauthCubit?.clear();
|
||||
logger.info('[Home] Secondary iCloud recovery applied a fresher token');
|
||||
} catch (e) {
|
||||
logger.warning('[Home] Secondary iCloud recovery failed: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void prefetch() async {
|
||||
if (_prefetched || _fetching) return;
|
||||
|
||||
@@ -158,24 +115,7 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
_fetching = true;
|
||||
});
|
||||
try {
|
||||
_prefetched = true;
|
||||
|
||||
await _runSecondaryICloudRecoveryIfNeeded();
|
||||
|
||||
try {
|
||||
await initData.client!.refreshTokenProactively().timeout(
|
||||
const Duration(seconds: 60),
|
||||
onTimeout: () {
|
||||
logger.warning('[Home] Token refresh/recovery timed out after 60s');
|
||||
return false;
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
logger.warning('[Home] Token refresh/recovery failed: $e');
|
||||
}
|
||||
|
||||
await initData.client!.renewCache(reInit: true);
|
||||
initData.homeRefreshCubit.requestRefresh();
|
||||
|
||||
if (Platform.isAndroid) {
|
||||
await HomeWidget.updateWidget(
|
||||
@@ -198,105 +138,15 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!_disposed &&
|
||||
(LiveActivityService.isTokenExpired ||
|
||||
initData.client!.needsReauth)) {
|
||||
activeToast = ActiveToastType.reauth;
|
||||
setState(() {
|
||||
toast = buildReauthToast(context, initData, () {
|
||||
if (!_disposed) {
|
||||
setState(() {
|
||||
activeToast = ActiveToastType.none;
|
||||
toast = null;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
if (e is TokenExpiredException || e is InvalidGrantException) {
|
||||
activeToast = ActiveToastType.reauth;
|
||||
if (_disposed) return;
|
||||
setState(() {
|
||||
toast = buildReauthToast(context, initData, () {
|
||||
if (!_disposed) {
|
||||
setState(() {
|
||||
activeToast = ActiveToastType.none;
|
||||
toast = null;
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
activeToast = ActiveToastType.error;
|
||||
var dismissDelay = 120;
|
||||
if (kDebugMode) dismissDelay = 2;
|
||||
Timer(Duration(seconds: dismissDelay), () {
|
||||
if (_disposed) return;
|
||||
setState(() {
|
||||
activeToast = ActiveToastType.none;
|
||||
toast = null;
|
||||
});
|
||||
});
|
||||
|
||||
if (_disposed) return;
|
||||
setState(() {
|
||||
toast = Positioned(
|
||||
top: MediaQuery.of(context).size.height / 1.6,
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
bottom: 0,
|
||||
child: Center(
|
||||
child: Card(
|
||||
color: appStyle.colors.errorCard,
|
||||
shadowColor: Colors.transparent,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.all(Radius.circular(200)),
|
||||
),
|
||||
child: Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 15, vertical: 8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
initData.l10n.api_error,
|
||||
style: appStyle.fonts.B_16SB.copyWith(
|
||||
color: appStyle.colors.errorText,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 8),
|
||||
GestureDetector(
|
||||
child: FirkaIconWidget(
|
||||
FirkaIconType.majesticons,
|
||||
Majesticon.questionCircleSolid,
|
||||
color: appStyle.colors.errorAccent,
|
||||
size: 24,
|
||||
),
|
||||
onTap: () {
|
||||
var stackTrace = "";
|
||||
if (e is Error && e.stackTrace != null) {
|
||||
stackTrace = e.stackTrace.toString();
|
||||
}
|
||||
showErrorBottomSheet(context, "$e\n$stackTrace");
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
(context);
|
||||
} finally {
|
||||
_hasCompletedFirstPrefetch = true;
|
||||
if (!_disposed) {
|
||||
setState(() {
|
||||
_prefetched = true;
|
||||
_fetching = false;
|
||||
if (activeToast == ActiveToastType.fetching) toast = null;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -363,45 +213,6 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
);
|
||||
}
|
||||
|
||||
if (_fetching) {
|
||||
if (_disposed) return const SizedBox.shrink();
|
||||
activeToast = ActiveToastType.fetching;
|
||||
toast = Positioned(
|
||||
top: MediaQuery.of(context).size.height / 1.6,
|
||||
left: 0.0,
|
||||
right: 0.0,
|
||||
bottom: 0,
|
||||
child: Center(
|
||||
child: Card(
|
||||
color: appStyle.colors.card,
|
||||
shadowColor: Colors.transparent,
|
||||
child: Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(
|
||||
color: appStyle.colors.accent,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 16),
|
||||
Text(
|
||||
initData.l10n.refreshing,
|
||||
style: appStyle.fonts.B_16SB.copyWith(
|
||||
color: appStyle.colors.textPrimary,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
||||
return BlocListener<SettingsCubit, SettingsState>(
|
||||
listener: (context, state) {
|
||||
@@ -411,14 +222,11 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
listener: (context, state) {
|
||||
if (mounted) setState(() {});
|
||||
},
|
||||
child: BlocListener<ReauthCubit, ReauthState>(
|
||||
child: BlocListener<ToastCubit, ToastState>(
|
||||
listener: (context, state) {
|
||||
if (!mounted || _disposed) return;
|
||||
if (!state.needsReauth && activeToast == ActiveToastType.reauth) {
|
||||
setState(() {
|
||||
activeToast = ActiveToastType.none;
|
||||
toast = null;
|
||||
});
|
||||
if (mounted) setState(() {});
|
||||
if (state.type == .none) {
|
||||
initData.homeRefreshCubit.requestRefresh();
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
@@ -427,7 +235,10 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
child: SizedBox(
|
||||
height: MediaQuery.of(context).size.height,
|
||||
child: Stack(
|
||||
children: [widget.child, toast ?? SizedBox.shrink()],
|
||||
children: [
|
||||
widget.child,
|
||||
?(context.watch<ToastCubit>().state.buildWidget(context)),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -496,7 +307,6 @@ class _HomeScreenState extends FirkaState<HomeScreen>
|
||||
_disposed = true;
|
||||
_fetching = false;
|
||||
_prefetched = false;
|
||||
activeToast = ActiveToastType.none;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1014,7 +1014,6 @@ class _SettingsScreenState extends FirkaState<SettingsScreen> {
|
||||
'[Settings] Failed to clear iCloud token: $e',
|
||||
);
|
||||
}
|
||||
initData.client!.clearReauthFlag();
|
||||
}
|
||||
} else {
|
||||
if (Platform.isIOS) {
|
||||
|
||||
Reference in New Issue
Block a user