refactor connect and disconnect and apply it to onion and row

This commit is contained in:
sneurlax 2023-09-12 15:15:08 -05:00
parent f53bd53af4
commit 4773f1ab9f

View file

@ -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,7 +216,8 @@ class _TorSettingsViewState extends ConsumerState<TorSettingsView> {
const SizedBox( const SizedBox(
height: 30, height: 30,
), ),
RoundedWhiteContainer( GestureDetector(
child: RoundedWhiteContainer(
child: Padding( child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0), padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Row( child: Row(
@ -262,6 +232,51 @@ class _TorSettingsViewState extends ConsumerState<TorSettingsView> {
), ),
), ),
), ),
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;
}
} }