yokutango-mobile-reader/lib/screens/home.dart

82 lines
2.4 KiB
Dart
Raw Normal View History

2022-01-26 00:25:07 +01:00
import 'dart:convert';
import 'dart:io';
import 'package:tangocard_reader/models/yokutango_entry.dart';
import 'package:tangocard_reader/screens/error.dart';
import 'package:tangocard_reader/screens/loading.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("よく単語"),
centerTitle: true,
),
body: const TangocardList());
}
}
class TangocardList extends StatelessWidget {
const TangocardList({Key? key}) : super(key: key);
Future<Map<File, List<YokutangoEntry>>> get tangocardFilePaths => rootBundle
.loadString('AssetManifest.json')
.then(
(json) => jsonDecode(json)
.keys
.where((String key) =>
key.contains('yokutango/json/') && key.contains('.json'))
.toList(),
)
.then(
(l) async => Map.fromIterables(
l.map<File>((f) => File(f)),
await Future.wait<List<YokutangoEntry>>(
l
.map<Future<List<YokutangoEntry>>>(
(String t) => rootBundle
.loadString(t)
.then<List<YokutangoEntry>>((s) => jsonDecode(s)
.map<YokutangoEntry>(
(e) => YokutangoEntry.fromJson(e))
.toList()),
)
),
),
);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: tangocardFilePaths,
builder: (context, snapshot) {
if (snapshot.hasError) {
debugPrint(snapshot.error.toString());
return const ErrorScreen();
} else if (!snapshot.hasData) {
return const LoadingScreen();
}
return ListView(
children: (snapshot.data as Map<File, List<YokutangoEntry>>)
.entries
.map(
(e) => ListTile(
title: Text(
"${e.key.uri.pathSegments.last} - ${e.value.length} cards"),
onTap: () => Navigator.pushNamed(context, '/tangolist',
arguments: e.key),
),
)
.toList(),
);
},
);
}
}