43 lines
1.0 KiB
Dart
43 lines
1.0 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;
|
|
|
|
DioRemote setEnvironment(ApiEnvironment env) {
|
|
if (_currentEnv != env) {
|
|
_currentClient = DioRemote(env.baseUrl);
|
|
_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 = manager.setEnvironment(env);
|
|
|
|
if (diAuth.isRegistered<AuthRepositoryImpl>()) {
|
|
await diAuth.unregister<AuthRepositoryImpl>();
|
|
}
|
|
|
|
diAuth.registerLazySingleton<AuthRepositoryImpl>(
|
|
() => AuthRepositoryImpl(dioRemote),
|
|
);
|
|
}
|