mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 23:34:24 +01:00
new: wear settings (#358) & opt.: android widget edit
This commit is contained in:
@@ -1,21 +1,22 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:fl_lib/fl_lib.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:toolbox/core/extension/context/locale.dart';
|
||||
import 'package:toolbox/core/route.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 AndroidSettingsPage extends StatefulWidget {
|
||||
const AndroidSettingsPage({super.key});
|
||||
|
||||
@override
|
||||
_AndroidSettingsPageState createState() => _AndroidSettingsPageState();
|
||||
State<AndroidSettingsPage> createState() => _AndroidSettingsPageState();
|
||||
}
|
||||
|
||||
class _AndroidSettingsPageState extends State<AndroidSettingsPage> {
|
||||
late SharedPreferences _sp;
|
||||
final wc = WatchConnectivity();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -34,6 +35,7 @@ class _AndroidSettingsPageState extends State<AndroidSettingsPage> {
|
||||
children: [
|
||||
_buildBgRun(),
|
||||
_buildAndroidWidgetSharedPreference(),
|
||||
_buildWatch(),
|
||||
if (BioAuth.isPlatformSupported)
|
||||
PlatformPublicSettings.buildBioAuth(),
|
||||
].map((e) => CardX(child: e)).toList(),
|
||||
@@ -49,10 +51,9 @@ class _AndroidSettingsPageState extends State<AndroidSettingsPage> {
|
||||
);
|
||||
}
|
||||
|
||||
void _saveWidgetSP(String data, Map<String, String> old) {
|
||||
void _saveWidgetSP(Map<String, String> map, Map<String, String> old) {
|
||||
context.pop();
|
||||
try {
|
||||
final map = Map<String, String>.from(json.decode(data));
|
||||
final keysDel = old.keys.toSet().difference(map.keys.toSet());
|
||||
for (final key in keysDel) {
|
||||
_sp.remove(key);
|
||||
@@ -70,7 +71,7 @@ class _AndroidSettingsPageState extends State<AndroidSettingsPage> {
|
||||
return ListTile(
|
||||
title: Text(l10n.homeWidgetUrlConfig),
|
||||
trailing: const Icon(Icons.keyboard_arrow_right),
|
||||
onTap: () {
|
||||
onTap: () async {
|
||||
final data = <String, String>{};
|
||||
_sp.getKeys().forEach((key) {
|
||||
final val = _sp.getString(key);
|
||||
@@ -78,25 +79,57 @@ class _AndroidSettingsPageState extends State<AndroidSettingsPage> {
|
||||
data[key] = val;
|
||||
}
|
||||
});
|
||||
final ctrl = TextEditingController(text: json.encode(data));
|
||||
context.showRoundDialog(
|
||||
title: l10n.homeWidgetUrlConfig,
|
||||
child: Input(
|
||||
autoFocus: true,
|
||||
controller: ctrl,
|
||||
label: 'JSON',
|
||||
type: TextInputType.visiblePassword,
|
||||
maxLines: 7,
|
||||
onSubmitted: (p0) => _saveWidgetSP(p0, data),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
_saveWidgetSP(ctrl.text, data);
|
||||
},
|
||||
child: Text(l10n.ok),
|
||||
),
|
||||
],
|
||||
final result = await AppRoutes.kvEditor(data: data).go(context);
|
||||
if (result != null) {
|
||||
if (result is Map<String, String>) {
|
||||
_saveWidgetSP(result, data);
|
||||
} else {
|
||||
final err = 'Save Android widget SharedPreference failed: '
|
||||
'unexpected type: ${result.runtimeType}';
|
||||
Loggers.app.warning(err);
|
||||
context.showRoundDialog(
|
||||
title: l10n.error,
|
||||
child: SingleChildScrollView(
|
||||
child: SimpleMarkdown(data: '$err\n\n```$result```'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildWatch() {
|
||||
return FutureWidget(
|
||||
future: wc.isReachable,
|
||||
error: (e, s) {
|
||||
Loggers.app.warning('WatchOS error', e, s);
|
||||
return ListTile(
|
||||
title: const Text('Watch app'),
|
||||
subtitle: Text(l10n.viewErr, style: UIs.textGrey),
|
||||
trailing: const Icon(Icons.keyboard_arrow_right),
|
||||
onTap: () {
|
||||
context.showRoundDialog(
|
||||
title: l10n.error,
|
||||
child: SingleChildScrollView(
|
||||
child: SimpleMarkdown(data: '${e.toString()}\n```$s```'),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
success: (val) {
|
||||
if (val == null) {
|
||||
return ListTile(
|
||||
title: const Text('Watch app'),
|
||||
subtitle: Text(l10n.watchNotPaired, style: UIs.textGrey),
|
||||
);
|
||||
}
|
||||
return ListTile(
|
||||
title: const Text('Watch app'),
|
||||
subtitle: Text(l10n.sync, style: UIs.textGrey),
|
||||
trailing: const Icon(Icons.keyboard_arrow_right),
|
||||
onTap: () async {},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user