#165 new: bio auth

This commit is contained in:
lollipopkit
2023-09-16 17:26:40 +08:00
parent 2e8761f533
commit 8152829c89
25 changed files with 260 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ import 'package:get_it/get_it.dart';
import 'package:toolbox/core/channel/bg_run.dart';
import 'package:toolbox/core/channel/home_widget.dart';
import 'package:toolbox/core/extension/context/dialog.dart';
import 'package:toolbox/core/utils/platform/auth.dart';
import 'package:toolbox/core/utils/platform/base.dart';
import 'package:toolbox/data/res/github_id.dart';
import 'package:toolbox/data/res/logger.dart';
@@ -46,6 +47,7 @@ class _HomePageState extends State<HomePage>
late S _s;
bool _switchingPage = false;
bool _isAuthing = false;
@override
void initState() {
@@ -81,6 +83,7 @@ class _HomePageState extends State<HomePage>
switch (state) {
case AppLifecycleState.resumed:
_auth();
if (!Providers.server.isAutoRefreshOn) {
Providers.server.startAutoRefresh();
}
@@ -338,6 +341,8 @@ class _HomePageState extends State<HomePage>
@override
Future<void> afterFirstLayout(BuildContext context) async {
// Auth required for first launch
_auth();
if (Stores.setting.autoCheckAppUpdate.fetch()) {
doUpdate(context);
}
@@ -385,4 +390,32 @@ class _HomePageState extends State<HomePage>
Loggers.app.warning('Update json settings failed', e, trace);
}
}
void _auth() {
if (Stores.setting.useBioAuth.fetch()) {
if (!_isAuthing) {
_isAuthing = true;
BioAuth.auth(_s.authRequired).then(
(val) {
switch (val) {
case AuthResult.success:
// wait for animation
Future.delayed(
const Duration(seconds: 1), () => _isAuthing = false);
break;
case AuthResult.fail:
case AuthResult.cancel:
_isAuthing = false;
_auth();
break;
case AuthResult.notAvail:
_isAuthing = false;
Stores.setting.useBioAuth.put(false);
break;
}
},
);
}
}
}
}

View File

@@ -12,6 +12,7 @@ import 'package:toolbox/core/extension/context/snackbar.dart';
import 'package:toolbox/core/extension/locale.dart';
import 'package:toolbox/core/extension/context/dialog.dart';
import 'package:toolbox/core/extension/stringx.dart';
import 'package:toolbox/core/utils/platform/auth.dart';
import 'package:toolbox/core/utils/platform/base.dart';
import 'package:toolbox/data/res/provider.dart';
import 'package:toolbox/data/res/store.dart';
@@ -183,13 +184,16 @@ class _SettingPageState extends State<SettingPage> {
//_buildLaunchPage(),
_buildCheckUpdate(),
];
if (isAndroid) {
children.add(_buildBgRun());
children.add(_buildAndroidWidgetSharedPreference());
}
if (isIOS) {
children.add(_buildPushToken());
children.add(_buildAutoUpdateHomeWidget());
}
if (isAndroid) {
children.add(_buildBgRun());
children.add(_buildAndroidWidgetSharedPreference());
if (BioAuth.isPlatformSupported) {
children.add(_buildBioAuth());
}
return Column(
children: children.map((e) => RoundRectCard(e)).toList(),
@@ -1096,4 +1100,41 @@ class _SettingPageState extends State<SettingPage> {
// trailing: StoreSwitch(prop: _setting.doubleColumnServersPage),
// );
// }
Widget _buildBioAuth() {
return FutureWidget<bool>(
future: BioAuth.isAvail,
loading: ListTile(
title: Text(_s.bioAuth),
subtitle: Text(_s.serverTabLoading, style: UIs.textGrey),
),
error: (e, __) => ListTile(
title: Text(_s.bioAuth),
subtitle: Text('${_s.failed}: $e', style: UIs.textGrey),
),
success: (can) {
return ListTile(
title: Text(_s.bioAuth),
trailing: can
? StoreSwitch(
prop: Stores.setting.useBioAuth,
func: (val) async {
if (val) {
Stores.setting.useBioAuth.put(false);
return;
}
// Only auth when turn off (val == false)
final result = await BioAuth.auth(_s.authRequired);
// If failed, turn on again
if (result != AuthResult.success) {
Stores.setting.useBioAuth.put(true);
}
},
)
: Text(_s.error, style: UIs.textGrey),
);
},
noData: UIs.placeholder,
);
}
}

View File

@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
class FutureWidget<T> extends StatelessWidget {
final Future future;
final Future<T> future;
final Widget loading;
final Widget Function(Object? error, StackTrace? trace) error;
final Widget Function(T data) success;