Phase 3.1 WIP: Riverpod migration (client_app Auth + ChatOpening)

- Add phase3.1 requirement and implementation plan docs
- Add Riverpod dependencies to both client_app and mitra_app
- Wrap both app roots with ProviderScope
- Migrate client_app AuthBloc → AuthNotifier (@riverpod annotation)
- Migrate client_app ChatOpeningBloc → chatPricingProvider (FutureProvider)
- Update router to use Riverpod-based auth state for redirects
- Update all auth screens (display name, register, OTP, force register)
- Update home screen and pricing bottom sheet
- Add android:usesCleartextTraffic for dev HTTP access on both apps
- mitra_app prepared with ProviderScope + ApiClient provider (blocs next)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-09 13:51:17 +08:00
parent b0502ac92b
commit d15b2f05fc
25 changed files with 2513 additions and 461 deletions

View File

@@ -2,9 +2,9 @@ import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'core/api/api_client.dart';
import 'core/auth/auth_bloc.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'core/api/api_client_provider.dart';
import 'core/auth/auth_notifier.dart';
import 'core/chat/chat_bloc.dart';
import 'core/chat/session_closure_bloc.dart';
import 'core/pairing/pairing_bloc.dart';
@@ -16,68 +16,64 @@ void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
// Request notification permission
final messaging = FirebaseMessaging.instance;
await messaging.requestPermission();
runApp(const App());
runApp(const ProviderScope(child: App()));
}
class App extends StatefulWidget {
class App extends ConsumerStatefulWidget {
const App({super.key});
@override
State<App> createState() => _AppState();
ConsumerState<App> createState() => _AppState();
}
class _AppState extends State<App> {
final _apiClient = ApiClient();
late final AuthBloc _authBloc;
late final GoRouter _router;
class _AppState extends ConsumerState<App> {
bool _fcmRegistered = false;
@override
void initState() {
super.initState();
_authBloc = AuthBloc(apiClient: _apiClient)..add(AppStarted());
_router = buildRouter(_authBloc);
NotificationService.initialize(_router);
_registerFcmToken();
}
Future<void> _registerFcmToken() async {
// Listen for auth state, then register token
_authBloc.stream.listen((state) async {
if (state is AuthAuthenticated || state is AuthAnonymous) {
try {
final token = await FirebaseMessaging.instance.getToken();
if (token != null) {
await _apiClient.post('/api/shared/device-token', data: {'token': token});
}
} catch (_) {}
void _registerFcmToken() {
if (_fcmRegistered) return;
_fcmRegistered = true;
Future(() async {
try {
final token = await FirebaseMessaging.instance.getToken();
if (token != null) {
await ref.read(apiClientProvider).post('/api/shared/device-token', data: {'token': token});
}
} catch (_) {
_fcmRegistered = false;
}
});
}
@override
void dispose() {
_authBloc.close();
_router.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
// Listen for auth changes to register FCM token
ref.listen(authProvider, (prev, next) {
final data = next.valueOrNull;
if (data is AuthAuthenticatedData || data is AuthAnonymousData) {
_registerFcmToken();
}
});
final router = ref.watch(routerProvider);
final apiClient = ref.watch(apiClientProvider);
// Initialize notifications once router is available
NotificationService.initialize(router);
// Keep BlocProviders for non-migrated blocs (will be removed as they're migrated)
return MultiBlocProvider(
providers: [
BlocProvider.value(value: _authBloc),
BlocProvider(create: (_) => PairingBloc(apiClient: _apiClient)),
BlocProvider(create: (_) => ChatBloc(apiClient: _apiClient)),
BlocProvider(create: (_) => SessionClosureBloc(apiClient: _apiClient)),
RepositoryProvider.value(value: _apiClient),
BlocProvider(create: (_) => PairingBloc(apiClient: apiClient)),
BlocProvider(create: (_) => ChatBloc(apiClient: apiClient)),
BlocProvider(create: (_) => SessionClosureBloc(apiClient: apiClient)),
RepositoryProvider.value(value: apiClient),
],
child: MaterialApp.router(
title: 'Halo Bestie',
routerConfig: _router,
routerConfig: router,
),
);
}