2022-04-15 19:57:15 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
2022-08-25 17:48:09 +00:00
|
|
|
import 'preview_tile.dart';
|
2022-04-15 19:57:15 +00:00
|
|
|
|
|
|
|
import 'api/api.dart';
|
|
|
|
import 'platform.dart';
|
|
|
|
|
|
|
|
class ShuffleScreen extends StatefulWidget {
|
|
|
|
const ShuffleScreen({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ShuffleScreen> createState() => _ShuffleScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ShuffleScreenState extends State<ShuffleScreen> {
|
|
|
|
late Future<List<VideoT>> _data;
|
|
|
|
|
2022-08-25 17:48:09 +00:00
|
|
|
Future<List<VideoT>> loadData(int nr) async {
|
|
|
|
final data = await API.query("video", "getRandomMovies", {'Number': nr, 'Seed': Random().nextInt(0x7fffffff)});
|
2022-04-15 19:57:15 +00:00
|
|
|
|
|
|
|
final d = jsonDecode(data);
|
|
|
|
|
|
|
|
List<VideoT> dta = (d['Videos'] as List).map((e) => VideoT.fromJson(e)).toList();
|
|
|
|
|
|
|
|
return dta;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2022-08-25 17:48:09 +00:00
|
|
|
_data = Future.delayed(Duration.zero).then((_) {
|
|
|
|
double width = MediaQuery.of(context).size.width;
|
|
|
|
return loadData((isTV() ? width ~/ 200 : width ~/ 330) * 2);
|
|
|
|
});
|
|
|
|
|
2022-04-15 19:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
double width = MediaQuery.of(context).size.width;
|
|
|
|
return FutureBuilder<List<VideoT>>(
|
|
|
|
future: _data, // a previously-obtained Future<String> or null
|
|
|
|
builder: (BuildContext context, AsyncSnapshot<List<VideoT>> snapshot) {
|
|
|
|
if (snapshot.hasError) {
|
|
|
|
return Text("Error");
|
|
|
|
} else if (snapshot.hasData) {
|
|
|
|
return Column(children: [
|
|
|
|
MasonryGridView.count(
|
|
|
|
shrinkWrap: true,
|
|
|
|
// every tile should be at max 330 pixels long...
|
|
|
|
crossAxisCount: isTV() ? width ~/ 200 : width ~/ 330,
|
|
|
|
mainAxisSpacing: 4,
|
|
|
|
crossAxisSpacing: 4,
|
2022-08-21 20:44:12 +00:00
|
|
|
padding: EdgeInsets.all(5),
|
2022-08-25 17:48:09 +00:00
|
|
|
itemCount: snapshot.data!.length,
|
2022-04-15 19:57:15 +00:00
|
|
|
itemBuilder: (context, index) {
|
|
|
|
return PreviewTile(dta: snapshot.data![index]);
|
|
|
|
},
|
|
|
|
),
|
2022-08-21 20:44:12 +00:00
|
|
|
const SizedBox(
|
|
|
|
height: 25,
|
|
|
|
),
|
|
|
|
TextButton.icon(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
2022-08-25 17:48:09 +00:00
|
|
|
_data = loadData((isTV() ? width ~/ 200 : width ~/ 330) * 2);
|
2022-08-21 20:44:12 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.update),
|
|
|
|
label: const Text("Shuffle"))
|
2022-04-15 19:57:15 +00:00
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return Column(children: const <Widget>[
|
|
|
|
SizedBox(
|
|
|
|
width: 60,
|
|
|
|
height: 60,
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.only(top: 16),
|
|
|
|
child: Text('Awaiting result...'),
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|