improvements and fix on change nodes

This commit is contained in:
Matthew Fosse 2024-03-01 12:21:06 -08:00
parent 835c7d941b
commit 0cc1f0806e
2 changed files with 24 additions and 17 deletions

View file

@ -725,7 +725,7 @@ Future<void> setup({
getIt.registerFactory(() => TrocadorProvidersViewModel(getIt.get<SettingsStore>())); getIt.registerFactory(() => TrocadorProvidersViewModel(getIt.get<SettingsStore>()));
getIt.registerSingleton(TorViewModel(getIt.get<SettingsStore>())); getIt.registerSingleton(TorViewModel(getIt.get<SettingsStore>(), getIt.get<SettingsStore>().nodes));
getIt.registerSingleton(ProxyWrapper( getIt.registerSingleton(ProxyWrapper(
settingsStore: getIt.get<SettingsStore>(), settingsStore: getIt.get<SettingsStore>(),
torViewModel: getIt.get<TorViewModel>(), torViewModel: getIt.get<TorViewModel>(),

View file

@ -19,7 +19,7 @@ class TorViewModel = TorViewModelBase with _$TorViewModel;
enum TorConnectionStatus { connecting, connected, disconnected } enum TorConnectionStatus { connecting, connected, disconnected }
abstract class TorViewModelBase with Store { abstract class TorViewModelBase with Store {
TorViewModelBase(this._settingsStore) { TorViewModelBase(this._settingsStore, this.nodes) {
reaction((_) => torConnectionMode, (TorConnectionMode mode) async { reaction((_) => torConnectionMode, (TorConnectionMode mode) async {
if (mode == TorConnectionMode.enabled || mode == TorConnectionMode.torOnly) { if (mode == TorConnectionMode.enabled || mode == TorConnectionMode.torOnly) {
startTor(); startTor();
@ -35,10 +35,16 @@ abstract class TorViewModelBase with Store {
await connectOrDisconnectNodeToProxy(connect: true); await connectOrDisconnectNodeToProxy(connect: true);
} }
}); });
this.nodes.observe((change) async {
if (change.newValue != null && change.key != null) {
await connectOrDisconnectNodeToProxy(connect: true);
}
});
} }
bool torStarted = false; bool torStarted = false;
final SettingsStore _settingsStore; final SettingsStore _settingsStore;
final ObservableMap<WalletType, Node> nodes;
Tor torInstance = Tor.instance; Tor torInstance = Tor.instance;
@computed @computed
@ -52,22 +58,23 @@ abstract class TorViewModelBase with Store {
Future<void> connectOrDisconnectNodeToProxy({required bool connect}) async { Future<void> connectOrDisconnectNodeToProxy({required bool connect}) async {
final appStore = getIt.get<AppStore>(); final appStore = getIt.get<AppStore>();
if (appStore.wallet != null) { if (appStore.wallet == null) {
final node = _settingsStore.getCurrentNode(appStore.wallet!.type); return;
if (connect && (node.socksProxyAddress?.isEmpty ?? true)) {
node.socksProxyAddress = "${InternetAddress.loopbackIPv4.address}:${torInstance.port}";
} else if (!connect) {
node.socksProxyAddress = null;
}
bool torOnly = _settingsStore.torConnectionMode == TorConnectionMode.torOnly;
if ([WalletType.bitcoin, WalletType.litecoin, WalletType.bitcoinCash]
.contains(appStore.wallet!.type)) {
bitcoin!.setTorOnly(appStore.wallet!, torOnly);
}
await appStore.wallet!.connectToNode(node: node);
} }
final node = _settingsStore.getCurrentNode(appStore.wallet!.type);
if (connect && (node.socksProxyAddress?.isEmpty ?? true)) {
node.socksProxyAddress = "${InternetAddress.loopbackIPv4.address}:${torInstance.port}";
} else if (!connect) {
node.socksProxyAddress = null;
}
bool torOnly = _settingsStore.torConnectionMode == TorConnectionMode.torOnly;
if ([WalletType.bitcoin, WalletType.litecoin, WalletType.bitcoinCash]
.contains(appStore.wallet!.type)) {
bitcoin!.setTorOnly(appStore.wallet!, torOnly);
}
await appStore.wallet!.connectToNode(node: node);
} }
Future<void> disconnectFromNode() async { Future<void> disconnectFromNode() async {