2019-07-11 06:32:17 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2019-06-29 22:56:11 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2019-07-04 21:45:05 +00:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
|
|
|
import '../types/abstracts.dart';
|
|
|
|
import '../types/credential.dart';
|
2019-07-07 04:14:09 +00:00
|
|
|
import '../types/screen_arguments.dart';
|
2019-06-29 22:56:11 +00:00
|
|
|
|
|
|
|
import '../widgets/tappable_text_list.dart';
|
|
|
|
|
2019-07-04 21:45:05 +00:00
|
|
|
class Home extends StatefulWidget {
|
2019-07-11 06:32:17 +00:00
|
|
|
const Home({Key key}) : super(key: key);
|
|
|
|
|
2019-07-04 21:45:05 +00:00
|
|
|
@override
|
|
|
|
State createState() => _HomeState();
|
|
|
|
}
|
|
|
|
|
2019-07-11 06:32:17 +00:00
|
|
|
class _HomeState extends State<Home> with WidgetsBindingObserver {
|
2019-07-18 02:20:04 +00:00
|
|
|
CredentialsRepo client;
|
|
|
|
ConfigRepo config;
|
|
|
|
Future<List<Metadata>> metadatas;
|
|
|
|
bool stateIsPaused = false;
|
|
|
|
Timer pausedStateTimer;
|
2019-07-11 06:32:17 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
WidgetsBinding.instance.addObserver(this);
|
|
|
|
}
|
2019-07-04 21:45:05 +00:00
|
|
|
|
|
|
|
@override
|
2019-07-11 06:32:17 +00:00
|
|
|
void didChangeDependencies() {
|
2019-07-04 21:45:05 +00:00
|
|
|
super.didChangeDependencies();
|
2019-07-05 09:15:57 +00:00
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
config = Provider.of<ConfigRepo>(context);
|
|
|
|
client = Provider.of<CredentialsRepo>(context);
|
2019-07-11 06:32:17 +00:00
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
metadatas = client.getAllMetadata('').toList();
|
2019-07-04 21:45:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 06:32:17 +00:00
|
|
|
@override
|
|
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
2019-07-18 02:20:04 +00:00
|
|
|
stateIsPaused = state == AppLifecycleState.paused;
|
2019-07-11 06:32:17 +00:00
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
if (stateIsPaused) {
|
|
|
|
pausedStateTimer = newPausedStateTimer();
|
2019-07-11 06:32:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
if (pausedStateTimer != null) pausedStateTimer.cancel();
|
2019-07-11 06:32:17 +00:00
|
|
|
}
|
|
|
|
|
2019-06-29 22:56:11 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return CupertinoPageScaffold(
|
2019-07-04 21:45:05 +00:00
|
|
|
child: FutureBuilder<List<Metadata>>(
|
2019-07-18 02:20:04 +00:00
|
|
|
future: metadatas,
|
2019-07-11 06:32:17 +00:00
|
|
|
builder: (
|
|
|
|
BuildContext context,
|
|
|
|
AsyncSnapshot<List<Metadata>> snapshot,
|
|
|
|
) =>
|
2019-07-04 21:45:05 +00:00
|
|
|
(snapshot.connectionState == ConnectionState.done)
|
|
|
|
? TappableTextList(
|
2019-07-18 02:20:04 +00:00
|
|
|
tappableText: buildTappableText(context, snapshot.data))
|
2019-07-05 09:15:57 +00:00
|
|
|
: Center(child: CupertinoActivityIndicator()),
|
|
|
|
),
|
|
|
|
navigationBar: CupertinoNavigationBar(
|
2019-07-18 02:20:04 +00:00
|
|
|
leading: CupertinoButton(
|
|
|
|
child: Text(
|
|
|
|
'Lock',
|
|
|
|
style: TextStyle(color: CupertinoColors.destructiveRed),
|
|
|
|
),
|
|
|
|
onPressed: makeLockOnTapHandler(context),
|
|
|
|
padding: EdgeInsets.zero,
|
2019-07-05 09:15:57 +00:00
|
|
|
),
|
2019-07-18 02:20:04 +00:00
|
|
|
trailing: CupertinoButton(
|
2019-07-05 09:15:57 +00:00
|
|
|
child: Icon(CupertinoIcons.gear),
|
2019-07-18 02:20:04 +00:00
|
|
|
onPressed: makeConfigOnTapHandler(context),
|
|
|
|
padding: EdgeInsets.zero,
|
2019-07-05 09:15:57 +00:00
|
|
|
),
|
2019-07-04 21:45:05 +00:00
|
|
|
),
|
2019-06-29 22:56:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-11 06:32:17 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
WidgetsBinding.instance.removeObserver(this);
|
2019-07-18 02:20:04 +00:00
|
|
|
if (pausedStateTimer != null) pausedStateTimer.cancel();
|
2019-07-11 06:32:17 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
Timer newPausedStateTimer() {
|
2019-07-11 06:32:17 +00:00
|
|
|
const checkPeriod = 30;
|
|
|
|
|
|
|
|
return Timer(Duration(seconds: checkPeriod), () {
|
2019-07-18 02:20:04 +00:00
|
|
|
config.reset();
|
2019-07-11 06:32:17 +00:00
|
|
|
Navigator.of(context)
|
|
|
|
.pushNamedAndRemoveUntil('/', ModalRoute.withName('/home'));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
Map<String, GestureTapCallback> buildTappableText(
|
2019-07-04 21:45:05 +00:00
|
|
|
BuildContext context,
|
|
|
|
List<Metadata> metadatas,
|
|
|
|
) {
|
|
|
|
final Map<String, List<Metadata>> metaMap = {};
|
|
|
|
|
|
|
|
for (var metadata in metadatas) {
|
|
|
|
final source = metadata.sourceHost;
|
|
|
|
|
|
|
|
if (metaMap[source] == null) {
|
|
|
|
metaMap[source] = [metadata];
|
|
|
|
} else {
|
|
|
|
metaMap[source].add(metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 04:14:09 +00:00
|
|
|
final handleOnTap = (List<Metadata> metadatas) => () async =>
|
2019-07-04 21:45:05 +00:00
|
|
|
Navigator.of(context).pushNamed('/credentials', arguments: metadatas);
|
|
|
|
|
|
|
|
final Map<String, GestureTapCallback> tappableText = {};
|
|
|
|
|
|
|
|
metaMap.forEach((String key, List<Metadata> value) =>
|
|
|
|
tappableText[key] = handleOnTap(value));
|
2019-06-29 22:56:11 +00:00
|
|
|
|
|
|
|
return tappableText;
|
|
|
|
}
|
2019-07-05 09:15:57 +00:00
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
GestureTapCallback makeLockOnTapHandler(BuildContext context) {
|
2019-07-16 06:14:54 +00:00
|
|
|
return () {
|
2019-07-18 02:20:04 +00:00
|
|
|
config.reset();
|
2019-07-16 06:14:54 +00:00
|
|
|
Navigator.of(context)
|
|
|
|
.pushNamedAndRemoveUntil('/', ModalRoute.withName('/home'));
|
|
|
|
};
|
2019-07-05 09:15:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 02:20:04 +00:00
|
|
|
GestureTapCallback makeConfigOnTapHandler(BuildContext context) {
|
2019-07-07 04:14:09 +00:00
|
|
|
return () async => Navigator.of(context).pushNamed('/config',
|
|
|
|
arguments: ConfigScreenArguments(
|
2019-07-18 02:20:04 +00:00
|
|
|
connectionConfig: await config.connectionConfig,
|
|
|
|
privateKey: await config.privateKey));
|
2019-07-05 09:15:57 +00:00
|
|
|
}
|
2019-06-29 22:56:11 +00:00
|
|
|
}
|