46 lines
1.1 KiB
Dart
46 lines
1.1 KiB
Dart
import 'package:rasadyar_auth/data/repositories/auth_repository_imp.dart';
|
|
import 'package:rasadyar_core/core.dart';
|
|
|
|
import '../di/auth_di.dart';
|
|
import 'constant.dart';
|
|
|
|
class DioRemoteManager {
|
|
DioRemote? _currentClient;
|
|
ApiEnvironment? _currentEnv;
|
|
|
|
Future<DioRemote> setEnvironment([
|
|
ApiEnvironment env = ApiEnvironment.dam,
|
|
]) async {
|
|
if (_currentEnv != env) {
|
|
_currentClient = DioRemote(baseUrl: env.baseUrl);
|
|
await _currentClient?.init();
|
|
_currentEnv = env;
|
|
}
|
|
return _currentClient!;
|
|
}
|
|
|
|
DioRemote get currentClient {
|
|
if (_currentClient == null) {
|
|
throw Exception('Call setEnvironment() before accessing DioRemote.');
|
|
}
|
|
|
|
return _currentClient!;
|
|
}
|
|
|
|
ApiEnvironment? get currentEnv => _currentEnv;
|
|
}
|
|
|
|
Future<void> switchAuthEnvironment(ApiEnvironment env) async {
|
|
final manager = diAuth.get<DioRemoteManager>();
|
|
|
|
final dioRemote = await manager.setEnvironment(env);
|
|
|
|
if (diAuth.isRegistered<AuthRepositoryImpl>()) {
|
|
await diAuth.unregister<AuthRepositoryImpl>();
|
|
}
|
|
|
|
diAuth.registerLazySingleton<AuthRepositoryImpl>(
|
|
() => AuthRepositoryImpl(dioRemote),
|
|
);
|
|
}
|