Files
flutter_server_box/lib/view/page/setting/platform/ios.dart
2024-05-14 22:29:37 +08:00

140 lines
4.0 KiB
Dart

import 'dart:convert';
import 'package:fl_lib/fl_lib.dart';
import 'package:flutter/material.dart';
import 'package:toolbox/core/extension/context/locale.dart';
import 'package:toolbox/core/route.dart';
import 'package:toolbox/core/utils/misc.dart';
import 'package:toolbox/data/res/misc.dart';
import 'package:toolbox/data/res/store.dart';
import 'package:toolbox/view/page/setting/platform/platform_pub.dart';
import 'package:watch_connectivity/watch_connectivity.dart';
class IOSSettingsPage extends StatefulWidget {
const IOSSettingsPage({super.key});
@override
_IOSSettingsPageState createState() => _IOSSettingsPageState();
}
class _IOSSettingsPageState extends State<IOSSettingsPage> {
final _pushToken = ValueNotifier<String?>(null);
final wc = WatchConnectivity();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const CustomAppBar(
title: Text('iOS'),
),
body: ListView(
padding: const EdgeInsets.symmetric(horizontal: 17),
children: [
_buildPushToken(),
_buildAutoUpdateHomeWidget(),
_buildWatchApp(),
if (BioAuth.isPlatformSupported)
PlatformPublicSettings.buildBioAuth(),
].map((e) => CardX(child: e)).toList(),
),
);
}
Widget _buildPushToken() {
return ListTile(
title: Text(l10n.pushToken),
trailing: IconButton(
icon: const Icon(Icons.copy),
alignment: Alignment.centerRight,
padding: EdgeInsets.zero,
onPressed: () {
if (_pushToken.value != null) {
Pfs.copy(_pushToken.value!);
context.showSnackBar(l10n.success);
} else {
context.showSnackBar(l10n.getPushTokenFailed);
}
},
),
subtitle: FutureWidget<String?>(
future: getToken(),
loading: Text(l10n.gettingToken),
error: (error, trace) => Text('${l10n.error}: $error'),
success: (text) {
_pushToken.value = text;
return Text(
text ?? l10n.nullToken,
style: UIs.textGrey,
overflow: TextOverflow.ellipsis,
maxLines: 1,
);
},
),
);
}
Widget _buildAutoUpdateHomeWidget() {
return ListTile(
title: Text(l10n.autoUpdateHomeWidget),
subtitle: Text(l10n.whenOpenApp, style: UIs.textGrey),
trailing: StoreSwitch(prop: Stores.setting.autoUpdateHomeWidget),
);
}
Widget _buildWatchApp() {
return FutureWidget<Map<String, dynamic>?>(
future: () async {
if (!await wc.isPaired) {
return null;
}
return await wc.applicationContext;
}(),
loading: UIs.centerLoading,
error: (e, trace) {
Loggers.app.warning('WatchOS error', e, trace);
return ListTile(
title: const Text('Watch app'),
subtitle: Text('${l10n.error}: $e', style: UIs.textGrey),
);
},
success: (ctx) {
if (ctx == null) {
return ListTile(
title: const Text('Watch app'),
subtitle: Text(l10n.watchNotPaired, style: UIs.textGrey),
);
}
return ListTile(
title: const Text('Watch app'),
trailing: const Icon(Icons.keyboard_arrow_right),
onTap: () async => _onTapWatchApp(ctx),
);
},
);
}
void _onTapWatchApp(Map<String, dynamic> map) async {
/// Encode [map] to String with indent `\t`
final text = Miscs.jsonEncoder.convert(map);
final result = await AppRoutes.editor(
text: text,
langCode: 'json',
title: 'Watch app',
).go<String>(context);
if (result == null) {
return;
}
try {
final newCtx = json.decode(result) as Map<String, dynamic>;
await wc.updateApplicationContext(newCtx);
} catch (e, trace) {
context.showRoundDialog(
title: l10n.error,
child: Text('${l10n.save}:\n$e'),
);
Loggers.app.warning('Update watch config failed', e, trace);
}
}
}