83 lines
2.3 KiB
Dart
83 lines
2.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../models/app_user.dart';
|
|
import '../models/sync_result.dart';
|
|
import '../models/user_profile.dart';
|
|
import '../repositories/user_profile_repository.dart';
|
|
import '../services/questions_hub_service.dart';
|
|
import '../theme/app_theme.dart';
|
|
|
|
/// Starts a profile sync session for [user] and exposes profile + sync state.
|
|
class ProfileSession extends StatefulWidget {
|
|
const ProfileSession({
|
|
super.key,
|
|
required this.user,
|
|
required this.builder,
|
|
});
|
|
|
|
final AppUser user;
|
|
final Widget Function(
|
|
BuildContext context,
|
|
UserProfile? profile,
|
|
ProfileSyncStatus syncStatus,
|
|
) builder;
|
|
|
|
@override
|
|
State<ProfileSession> createState() => _ProfileSessionState();
|
|
}
|
|
|
|
class _ProfileSessionState extends State<ProfileSession> {
|
|
late final Future<void> _sessionReady;
|
|
ProfileSyncStatus _syncStatus = ProfileSyncStatus.idle;
|
|
StreamSubscription<ProfileSyncStatus>? _syncStatusSubscription;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_sessionReady = UserProfileRepository.instance.startSession(widget.user)
|
|
..then((_) => QuestionsHubService.instance.onLogin());
|
|
_syncStatusSubscription =
|
|
UserProfileRepository.instance.syncStatusStream.listen((
|
|
ProfileSyncStatus status,
|
|
) {
|
|
if (mounted) {
|
|
setState(() => _syncStatus = status);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_syncStatusSubscription?.cancel();
|
|
unawaited(QuestionsHubService.instance.disconnect());
|
|
UserProfileRepository.instance.endSession();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return FutureBuilder<void>(
|
|
future: _sessionReady,
|
|
builder: (BuildContext context, AsyncSnapshot<void> sessionSnap) {
|
|
if (sessionSnap.connectionState != ConnectionState.done) {
|
|
return const Scaffold(
|
|
body: Center(
|
|
child: CircularProgressIndicator(color: AppColors.accent),
|
|
),
|
|
);
|
|
}
|
|
|
|
return StreamBuilder<UserProfile?>(
|
|
stream: UserProfileRepository.instance.profileStream,
|
|
initialData: UserProfileRepository.instance.currentProfile,
|
|
builder: (BuildContext context, AsyncSnapshot<UserProfile?> snap) {
|
|
return widget.builder(context, snap.data, _syncStatus);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|