Skip to content

Commit

Permalink
Add discover page and button
Browse files Browse the repository at this point in the history
  • Loading branch information
Piero512 committed Mar 12, 2021
1 parent ff86e39 commit b2ddfdd
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
83 changes: 83 additions & 0 deletions lib/pages/discover_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'dart:async';

import 'package:bonsoir/bonsoir.dart';
import 'package:flutter/material.dart';
import 'package:rutorrentflutter/api/api_conf.dart';

class DiscoverPage extends StatefulWidget {
@override
_DiscoverPageState createState() => _DiscoverPageState();
}

class _DiscoverPageState extends State<DiscoverPage> {
final BonsoirDiscovery zeroconfService =
BonsoirDiscovery(type: "_rutorrent_mobile._tcp");
final List<ResolvedBonsoirService> urls = [];
StreamSubscription zeroconfSub;

@override
void dispose() {
zeroconfSub?.cancel();
if (zeroconfService.isReady && !zeroconfService.isStopped) {
zeroconfService.stop();
}
super.dispose();
}

static buildURL(ResolvedBonsoirService resolved) {
// TODO: Enable password authentication
var protocol = resolved.attributes["protocol"];
var port = resolved.port;
if (port == 80) {
return "http://${resolved.ip}/";
} else if (port == 443) {
return "https://${resolved.ip}/";
} else {
if (protocol != null) {
return "$protocol://${resolved.ip}:${resolved.port}/";
} else {
return "http://${resolved.ip}:${resolved.port}";
}
}
}

void setupListener() {
zeroconfSub = zeroconfService.eventStream.listen((event) {
if (event.isServiceResolved &&
event.type == BonsoirDiscoveryEventType.DISCOVERY_SERVICE_RESOLVED) {
var resolved = event.service as ResolvedBonsoirService;
setState(() {
urls.add(resolved);
});
}
});
zeroconfService.start();
}

@override
void initState() {
zeroconfService.ready.then((_) => setupListener());
super.initState();
}

@override
Widget build(BuildContext context) {
return Dialog(
child: Container(
child: ListView.builder(
itemBuilder: (context, index) => ListTile(
title: Text(urls[index].name),
subtitle: Text("IP address: ${urls[index].ip}"),
onTap: () {
var api = Api();
api.setUrl(buildURL(urls[index]));
api.setUsername("");
api.setPassword("");
Navigator.of(context).pop(api);
},
),
itemCount: urls.length,
)),
);
}
}
42 changes: 42 additions & 0 deletions lib/screens/configurations_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:provider/provider.dart';
import 'package:rutorrentflutter/components/data_input.dart';
import 'package:rutorrentflutter/components/password_input.dart';
import 'package:rutorrentflutter/models/mode.dart';
import 'package:rutorrentflutter/pages/discover_page.dart';
import 'package:rutorrentflutter/screens/main_screen.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:rutorrentflutter/utilities/preferences.dart';
Expand Down Expand Up @@ -215,6 +216,47 @@ class _ConfigurationsScreenState extends State<ConfigurationsScreen> {
passwordFocus: passwordFocus,
),
),
Padding(
padding:
const EdgeInsets.symmetric(vertical: 16, horizontal: 32),
child: Container(
width: double.infinity,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
side:
BorderSide(color: Theme.of(context).primaryColor),
),
primary: Provider.of<Mode>(context).isLightMode
? Colors.white
: Colors.black,
),
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 28, vertical: 16),
child: Text(
'Discover ruTorrent servers in LAN',
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 18),
),
),
onPressed: () async {
var received = await showDialog<Api>(
context: context, builder: (ctx) => DiscoverPage());
if (received != null) {
Api api = Provider.of<Api>(context,
listen: false); // One call to provider
api.setUrl(api.url);
api.setUsername(api.username);
api.setPassword(api.password);
_validateConfigurationDetails(api);
}
},
),
),
),
Padding(
padding:
const EdgeInsets.symmetric(vertical: 16, horizontal: 32),
Expand Down

0 comments on commit b2ddfdd

Please sign in to comment.