Make endpoint search cancellable

This commit is contained in:
Vishnu Mohandas 2020-05-17 23:18:49 +05:30
parent 6c4f99ab3c
commit 51b97dd698
2 changed files with 34 additions and 10 deletions

View file

@ -12,15 +12,15 @@ class SetupPage extends StatefulWidget {
}
class _SetupPageState extends State<SetupPage> {
bool _errorFindingEndpoint = false;
bool _shouldSearchForEndpoint = true;
String _enteredEndpoint = "";
@override
Widget build(BuildContext context) {
if (Configuration.instance.getEndpoint() == null &&
!_errorFindingEndpoint) {
_shouldSearchForEndpoint) {
EndpointFinder.instance.findEndpoint().then((endpoint) {
if (mounted) {
if (mounted && endpoint != null) {
setState(() {
Configuration.instance.setEndpoint(endpoint);
});
@ -28,7 +28,7 @@ class _SetupPageState extends State<SetupPage> {
}).catchError((e) {
if (mounted) {
setState(() {
_errorFindingEndpoint = true;
_shouldSearchForEndpoint = false;
});
}
});
@ -44,10 +44,10 @@ class _SetupPageState extends State<SetupPage> {
Widget _getBody() {
if (Configuration.instance.getEndpoint() == null &&
!_errorFindingEndpoint) {
_shouldSearchForEndpoint) {
return _getSearchScreen();
} else if (Configuration.instance.getEndpoint() == null &&
_errorFindingEndpoint) {
!_shouldSearchForEndpoint) {
return _getManualEndpointEntryScreen();
} else {
return SignInWidget(() {
@ -83,7 +83,7 @@ class _SetupPageState extends State<SetupPage> {
await EndpointFinder.instance.ping(_enteredEndpoint);
if (success) {
setState(() {
_errorFindingEndpoint = false;
_shouldSearchForEndpoint = false;
Configuration.instance.setEndpoint(_enteredEndpoint);
});
} else {
@ -102,11 +102,23 @@ class _SetupPageState extends State<SetupPage> {
Center _getSearchScreen() {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
AnimatedSearchIconWidget(),
Text("Searching for ente server..."),
Align(
alignment: Alignment.bottomCenter,
child: CupertinoButton(
child: Text("Enter manually instead"),
onPressed: () async {
EndpointFinder.instance.cancelSearch();
setState(() {
_shouldSearchForEndpoint = false;
});
},
),
),
],
),
);

View file

@ -14,7 +14,10 @@ class EndpointFinder {
static final EndpointFinder instance = EndpointFinder._privateConstructor();
bool _shouldContinueSearch;
Future<String> findEndpoint() {
_shouldContinueSearch = true;
return (Connectivity().getWifiIP()).then((ip) async {
logger.info(ip);
final ipSplit = ip.split(".");
@ -26,7 +29,7 @@ class EndpointFinder {
}
logger.info(prefix);
for (int i = 1; i <= 255; i++) {
for (int i = 1; i <= 255 && _shouldContinueSearch; i++) {
var endpoint = prefix + i.toString();
try {
final success = await ping(endpoint);
@ -37,10 +40,19 @@ class EndpointFinder {
// Do nothing
}
}
throw TimeoutException("Could not find a valid endpoint");
if (_shouldContinueSearch) {
throw TimeoutException("Could not find a valid endpoint");
} else {
// Exit gracefully
return Future.value(null);
}
});
}
void cancelSearch() {
_shouldContinueSearch = false;
}
Future<bool> ping(String endpoint) async {
return _dio.get("http://" + endpoint + ":8080/ping").then((response) {
if (response.data["message"] == "pong") {