102 lines
3.0 KiB
Dart
102 lines
3.0 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../../../models/home_config.dart';
|
|
import '../../auth/providers/auth_providers.dart';
|
|
import '../../shared/providers/core_providers.dart';
|
|
|
|
/// Текущий выбранный дом (null если ни одного нет)
|
|
final currentHomeProvider = NotifierProvider<CurrentHomeNotifier, HomeConfig?>(
|
|
() => CurrentHomeNotifier(),
|
|
);
|
|
|
|
class CurrentHomeNotifier extends Notifier<HomeConfig?> {
|
|
@override
|
|
HomeConfig? build() => null;
|
|
|
|
/// Загрузить текущий дом из SharedPreferences
|
|
Future<void> load() async {
|
|
final svc = ref.read(settingsServiceProvider);
|
|
state = await svc.getCurrentHome();
|
|
if (state != null) {
|
|
await _initApi(state!);
|
|
}
|
|
}
|
|
|
|
/// Переключиться на другой дом
|
|
Future<void> switchTo(HomeConfig home) async {
|
|
final svc = ref.read(settingsServiceProvider);
|
|
await svc.setCurrentHomeId(home.id);
|
|
state = home;
|
|
await _initApi(home);
|
|
}
|
|
|
|
/// Выбрать дом как активный и сразу проверить auth-state.
|
|
/// Если `auth/me` падает, откатываемся к предыдущему дому и auth-state.
|
|
Future<void> select(HomeConfig home) async {
|
|
final previousHome = state;
|
|
final previousAuthState = ref.read(authInfoProvider);
|
|
|
|
try {
|
|
await switchTo(home);
|
|
await ref.read(authInfoProvider.notifier).load(failOnError: true);
|
|
} catch (error) {
|
|
await _restoreSelection(previousHome);
|
|
ref.read(authInfoProvider.notifier).restore(previousAuthState);
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<void> clear() async {
|
|
await ref.read(settingsServiceProvider).setCurrentHomeId(null);
|
|
state = null;
|
|
}
|
|
|
|
/// Инициализировать API-клиент текущим домом
|
|
Future<void> _initApi(HomeConfig home) async {
|
|
final apiKey = await ref
|
|
.read(settingsServiceProvider)
|
|
.requireHomeApiKey(home.id);
|
|
ref.read(apiProvider).init(home.url, apiKey);
|
|
}
|
|
|
|
Future<void> _restoreSelection(HomeConfig? home) async {
|
|
if (home == null) {
|
|
await clear();
|
|
return;
|
|
}
|
|
|
|
final svc = ref.read(settingsServiceProvider);
|
|
await svc.setCurrentHomeId(home.id);
|
|
state = home;
|
|
await _initApi(home);
|
|
}
|
|
}
|
|
|
|
final homesProvider = NotifierProvider<HomesNotifier, List<HomeConfig>>(
|
|
() => HomesNotifier(),
|
|
);
|
|
|
|
class HomesNotifier extends Notifier<List<HomeConfig>> {
|
|
@override
|
|
List<HomeConfig> build() => [];
|
|
|
|
Future<void> load() async {
|
|
state = await ref.read(settingsServiceProvider).getHomes();
|
|
}
|
|
|
|
Future<void> add(HomeConfig home, {required String apiKey}) async {
|
|
await ref.read(settingsServiceProvider).upsertHome(home, apiKey: apiKey);
|
|
await load();
|
|
}
|
|
|
|
Future<void> remove(String id) async {
|
|
await ref.read(settingsServiceProvider).deleteHome(id);
|
|
await load();
|
|
}
|
|
|
|
Future<void> update(HomeConfig home, {String? apiKey}) async {
|
|
await ref.read(settingsServiceProvider).upsertHome(home, apiKey: apiKey);
|
|
await load();
|
|
}
|
|
}
|