This repository has been archived on 2020-05-24. You can view files and clone it, but cannot push or open issues or pull requests.
gopher/lib/page/text.dart

85 lines
2.5 KiB
Dart

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:share/share.dart';
import 'dart:io';
import 'dart:convert';
import '../constants.dart';
import '../gopher.dart';
class TextPage extends StatefulWidget {
final Uri uri;
const TextPage({Key key, this.uri}) : super(key: key);
createState() => new TextPageState();
static void openURI(BuildContext context, Uri uri) {
if (!uri.hasScheme || uri.scheme == "gopher") {
Navigator.push(context, MaterialPageRoute<void>(
builder: (BuildContext context) {
return TextPage(uri: uri);
},
));
} else {
Scaffold.of(context).showSnackBar(const SnackBar(content: Text("URL does not have the gopher scheme")));
}
}
}
class TextPageState extends State<TextPage> {
String content = "";
TextStyle tstyle = const TextStyle(fontFamily: 'monospace', fontSize: DEFAULT_FONT_SIZE);
TextPageState() {
SharedPreferences.getInstance().then((prefs) {
setState(() { this.tstyle = TextStyle(fontFamily: 'monospace', fontSize: prefs.getDouble('font_size')); });
});
}
void initState() {
super.initState();
_fetchContent(widget.uri);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(generateGopherTitle(widget.uri)),
actions: <Widget>[
IconButton(
icon: Icon(Icons.home),
tooltip: "go back",
onPressed: () => Navigator.popUntil(context, ModalRoute.withName('/'))
),
IconButton(
icon: Icon(Icons.share),
tooltip: "Share URL",
onPressed: () => Share.share(widget.uri.toString()),
),
],
),
body: SingleChildScrollView( // TODO Extend to width of context
child: Text(content, style: tstyle, softWrap: true),
padding: EdgeInsets.all(8.0),
scrollDirection: Axis.vertical,
),
);
}
void _fetchContent(Uri uri) {
var path = GopherPath(uri.path);
if (path.type != GopherType.Text)
throw "Invalid gopher type, expected 0";
Socket.connect(uri.host, uri.port != 0 ? uri.port : 70).then((socket) {
socket.write(Uri.decodeComponent(path.selector) + "\r\n");
socket.cast<List<int>>().transform(utf8.decoder).join().then((cont) {
if (mounted) {
setState(() {
content = cont;
});
}
});
}).catchError((e) { Navigator.pop(context); });
}
}