75 lines
2.2 KiB
Dart
75 lines
2.2 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:share/share.dart';
|
|
import 'dart:io';
|
|
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
import '../gopher.dart';
|
|
|
|
class ImagePage extends StatefulWidget {
|
|
final Uri uri;
|
|
|
|
const ImagePage({Key key, this.uri}) : super(key: key);
|
|
|
|
createState() => new ImagePageState();
|
|
|
|
static void openURI(BuildContext context, Uri uri) {
|
|
if (!uri.hasScheme || uri.scheme == "gopher") {
|
|
Navigator.push(context, MaterialPageRoute<void>(
|
|
builder: (BuildContext context) {
|
|
return ImagePage(uri: uri);
|
|
},
|
|
));
|
|
} else {
|
|
Scaffold.of(context).showSnackBar(const SnackBar(content: Text("URL does not have the gopher scheme")));
|
|
}
|
|
}
|
|
}
|
|
class ImagePageState extends State<ImagePage> {
|
|
Widget imageWidget = Placeholder();
|
|
Matrix4 _matrix = Matrix4.identity();
|
|
|
|
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: imageWidget
|
|
);
|
|
}
|
|
|
|
void _fetchContent(Uri uri) {
|
|
var path = GopherPath(uri.path);
|
|
if (path.type != GopherType.Image)
|
|
throw "Invalid gopher type, expected an image type!";
|
|
Socket.connect(uri.host, uri.port != 0 ? uri.port : 70).then((socket) {
|
|
socket.write(Uri.decodeComponent(path.selector) + "\r\n");
|
|
socket.toList().then((rawImage) {
|
|
if (mounted) {
|
|
setState(() {
|
|
// "This is bullshit." "Yes!" -- Somebody change this to something that does streaming
|
|
imageWidget = Image.memory(Uint8List.fromList(rawImage.fold(List<int>(), (acc, list) { acc.addAll(list.toList()); return acc; })));
|
|
});
|
|
}
|
|
});
|
|
}).catchError((e) { Navigator.pop(context); });
|
|
}
|
|
}
|