mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-11-17 09:47:37 +00:00
refactor connect and disconnect and apply it to onion and row
This commit is contained in:
parent
f53bd53af4
commit
4773f1ab9f
1 changed files with 107 additions and 47 deletions
|
@ -62,25 +62,7 @@ class _TorSettingsViewState extends ConsumerState<TorSettingsView> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
// Init the Tor service if it hasn't already been.
|
await connect();
|
||||||
ref.read(pTorService).init();
|
|
||||||
|
|
||||||
// Start the Tor service.
|
|
||||||
try {
|
|
||||||
await ref.read(pTorService).start();
|
|
||||||
|
|
||||||
// Toggle the useTor preference on success.
|
|
||||||
ref.read(prefsChangeNotifierProvider).useTor = true;
|
|
||||||
} catch (e, s) {
|
|
||||||
Logging.instance.log(
|
|
||||||
"Error starting tor: $e\n$s",
|
|
||||||
level: LogLevel.Error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
setState(() {
|
|
||||||
_networkStatus = TorConnectionStatus.connecting;
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
case TorConnectionStatus.connected:
|
case TorConnectionStatus.connected:
|
||||||
return GestureDetector(
|
return GestureDetector(
|
||||||
|
@ -104,21 +86,8 @@ class _TorSettingsViewState extends ConsumerState<TorSettingsView> {
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
// Init the Tor service if it hasn't already been.
|
// TODO we could make this sync.
|
||||||
ref.read(pTorService).init();
|
await disconnect(); // TODO we could do away with the Future here.
|
||||||
|
|
||||||
// Start the Tor service.
|
|
||||||
try {
|
|
||||||
await ref.read(pTorService).start();
|
|
||||||
|
|
||||||
// Toggle the useTor preference on success.
|
|
||||||
ref.read(prefsChangeNotifierProvider).useTor = true;
|
|
||||||
} catch (e, s) {
|
|
||||||
Logging.instance.log(
|
|
||||||
"Error starting tor: $e\n$s",
|
|
||||||
level: LogLevel.Error,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
case TorConnectionStatus.connecting:
|
case TorConnectionStatus.connecting:
|
||||||
return Stack(
|
return Stack(
|
||||||
|
@ -247,21 +216,67 @@ class _TorSettingsViewState extends ConsumerState<TorSettingsView> {
|
||||||
const SizedBox(
|
const SizedBox(
|
||||||
height: 30,
|
height: 30,
|
||||||
),
|
),
|
||||||
RoundedWhiteContainer(
|
GestureDetector(
|
||||||
child: Padding(
|
child: RoundedWhiteContainer(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
child: Padding(
|
||||||
child: Row(
|
padding: const EdgeInsets.symmetric(vertical: 8.0),
|
||||||
children: [
|
child: Row(
|
||||||
Text(
|
children: [
|
||||||
"Tor status",
|
Text(
|
||||||
style: STextStyles.titleBold12(context),
|
"Tor status",
|
||||||
|
style: STextStyles.titleBold12(context),
|
||||||
|
),
|
||||||
|
const Spacer(),
|
||||||
|
_buildTorStatus(_networkStatus),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
const Spacer(),
|
),
|
||||||
_buildTorStatus(_networkStatus),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
onTap: () async {
|
||||||
),
|
// Connect or disconnect when the user taps the status.
|
||||||
|
switch (_networkStatus) {
|
||||||
|
case TorConnectionStatus.disconnected:
|
||||||
|
// Update the UI.
|
||||||
|
setState(() {
|
||||||
|
_networkStatus = TorConnectionStatus.connecting;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await connect();
|
||||||
|
} catch (e, s) {
|
||||||
|
Logging.instance.log(
|
||||||
|
"Error starting tor: $e\n$s",
|
||||||
|
level: LogLevel.Error,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the UI.
|
||||||
|
setState(() {
|
||||||
|
_networkStatus = TorConnectionStatus.connected;
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case TorConnectionStatus.connected:
|
||||||
|
try {
|
||||||
|
await disconnect();
|
||||||
|
} catch (e, s) {
|
||||||
|
Logging.instance.log(
|
||||||
|
"Error stopping tor: $e\n$s",
|
||||||
|
level: LogLevel.Error,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the UI.
|
||||||
|
setState(() {
|
||||||
|
_networkStatus = TorConnectionStatus.disconnected;
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case TorConnectionStatus.connecting:
|
||||||
|
// Do nothing.
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}),
|
||||||
const SizedBox(
|
const SizedBox(
|
||||||
height: 8,
|
height: 8,
|
||||||
),
|
),
|
||||||
|
@ -348,4 +363,49 @@ class _TorSettingsViewState extends ConsumerState<TorSettingsView> {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> connect() async {
|
||||||
|
// Init the Tor service if it hasn't already been.
|
||||||
|
ref.read(pTorService).init();
|
||||||
|
|
||||||
|
// Start the Tor service.
|
||||||
|
try {
|
||||||
|
await ref.read(pTorService).start();
|
||||||
|
|
||||||
|
// Toggle the useTor preference on success.
|
||||||
|
ref.read(prefsChangeNotifierProvider).useTor = true;
|
||||||
|
} catch (e, s) {
|
||||||
|
Logging.instance.log(
|
||||||
|
"Error starting tor: $e\n$s",
|
||||||
|
level: LogLevel.Error,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_networkStatus = TorConnectionStatus.connecting;
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> disconnect() async {
|
||||||
|
// Stop the Tor service.
|
||||||
|
try {
|
||||||
|
await ref.read(pTorService).stop();
|
||||||
|
|
||||||
|
// Toggle the useTor preference on success.
|
||||||
|
ref.read(prefsChangeNotifierProvider).useTor = false;
|
||||||
|
} catch (e, s) {
|
||||||
|
Logging.instance.log(
|
||||||
|
"Error stopping tor: $e\n$s",
|
||||||
|
level: LogLevel.Error,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_networkStatus = TorConnectionStatus.disconnected;
|
||||||
|
});
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue