veilid/veilid-flutter/example/lib/app.dart

248 lines
8.0 KiB
Dart
Raw Normal View History

2022-12-10 12:11:46 -05:00
import 'dart:async';
2023-05-29 15:24:57 -04:00
import 'dart:convert';
2022-12-10 12:11:46 -05:00
2024-03-02 17:45:26 -05:00
import 'package:flutter/foundation.dart';
2022-12-10 12:11:46 -05:00
import 'package:flutter/material.dart';
import 'package:loggy/loggy.dart';
2024-05-02 14:15:42 -04:00
import 'package:veilid/veilid.dart';
2022-12-10 12:11:46 -05:00
2022-12-14 16:50:33 -05:00
import 'history_wrapper.dart';
2024-05-02 14:15:42 -04:00
import 'log.dart';
import 'log_terminal.dart';
import 'veilid_theme.dart';
2022-12-10 12:11:46 -05:00
// Main App
class MyApp extends StatefulWidget {
2024-03-24 22:38:27 -04:00
const MyApp({super.key});
2022-12-10 12:11:46 -05:00
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with UiLoggy {
String _veilidVersion = 'Unknown';
2022-12-10 17:07:52 -05:00
bool _startedUp = false;
2022-12-10 12:11:46 -05:00
Stream<VeilidUpdate>? _updateStream;
Future<void>? _updateProcessor;
2022-12-14 16:50:33 -05:00
final _debugHistoryWrapper = HistoryWrapper();
String? _errorText;
2022-12-10 12:11:46 -05:00
@override
void initState() {
super.initState();
2024-05-02 14:15:42 -04:00
unawaited(initPlatformState());
2022-12-10 12:11:46 -05:00
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String veilidVersion;
// Platform messages may fail, so we use a try/catch PlatformException.
// We also handle the message potentially returning null.
try {
veilidVersion = Veilid.instance.veilidVersionString();
} on Exception {
veilidVersion = 'Failed to get veilid version.';
}
// In case of hot restart shut down first
try {
await Veilid.instance.shutdownVeilidCore();
} on Exception {
//
}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
2024-05-02 14:15:42 -04:00
if (!mounted) {
return;
}
2022-12-10 12:11:46 -05:00
setState(() {
_veilidVersion = veilidVersion;
});
}
Future<void> processLog(VeilidLog log) async {
StackTrace? stackTrace;
Object? error;
final backtrace = log.backtrace;
if (backtrace != null) {
2024-05-02 14:15:42 -04:00
stackTrace = StackTrace.fromString('$backtrace\n${StackTrace.current}');
2022-12-10 12:11:46 -05:00
error = 'embedded stack trace for ${log.logLevel} ${log.message}';
}
switch (log.logLevel) {
case VeilidLogLevel.error:
loggy.error(log.message, error, stackTrace);
break;
case VeilidLogLevel.warn:
loggy.warning(log.message, error, stackTrace);
break;
case VeilidLogLevel.info:
loggy.info(log.message, error, stackTrace);
break;
case VeilidLogLevel.debug:
loggy.debug(log.message, error, stackTrace);
break;
case VeilidLogLevel.trace:
loggy.trace(log.message, error, stackTrace);
break;
}
}
Future<void> processUpdates() async {
2024-05-02 14:15:42 -04:00
final stream = _updateStream;
2022-12-10 12:11:46 -05:00
if (stream != null) {
await for (final update in stream) {
if (update is VeilidLog) {
await processLog(update);
} else if (update is VeilidAppMessage) {
2024-05-02 14:15:42 -04:00
loggy.info('AppMessage: ${jsonEncode(update)}');
2022-12-10 12:11:46 -05:00
} else if (update is VeilidAppCall) {
2024-05-02 14:15:42 -04:00
loggy.info('AppCall: ${jsonEncode(update)}');
2022-12-10 12:11:46 -05:00
} else {
2024-05-02 14:15:42 -04:00
loggy.trace('Update: ${jsonEncode(update)}');
2022-12-10 12:11:46 -05:00
}
}
}
}
2022-12-10 17:07:52 -05:00
Future<void> toggleStartup(bool startup) async {
if (startup && !_startedUp) {
2024-03-02 17:45:26 -05:00
var config = await getDefaultVeilidConfig(
isWeb: kIsWeb,
programName: 'Veilid Plugin Example',
// ignore: avoid_redundant_argument_values, do_not_use_environment
bootstrap: const String.fromEnvironment('BOOTSTRAP'),
// ignore: avoid_redundant_argument_values, do_not_use_environment
networkKeyPassword: const String.fromEnvironment('NETWORK_KEY'));
2024-05-02 14:15:42 -04:00
// ignore: do_not_use_environment
if (const String.fromEnvironment('DELETE_TABLE_STORE') == '1') {
2023-07-05 18:48:06 -04:00
config = config.copyWith(
tableStore: config.tableStore.copyWith(delete: true));
2023-05-29 20:59:52 -04:00
}
2024-05-02 14:15:42 -04:00
// ignore: do_not_use_environment
if (const String.fromEnvironment('DELETE_PROTECTED_STORE') == '1') {
2023-07-05 18:48:06 -04:00
config = config.copyWith(
protectedStore: config.protectedStore.copyWith(delete: true));
2023-05-29 20:59:52 -04:00
}
2024-05-02 14:15:42 -04:00
// ignore: do_not_use_environment
if (const String.fromEnvironment('DELETE_BLOCK_STORE') == '1') {
2023-07-05 18:48:06 -04:00
config = config.copyWith(
blockStore: config.blockStore.copyWith(delete: true));
2023-05-29 20:59:52 -04:00
}
2024-05-02 14:15:42 -04:00
final updateStream = await Veilid.instance.startupVeilidCore(config);
2022-12-10 17:07:52 -05:00
setState(() {
_updateStream = updateStream;
_updateProcessor = processUpdates();
_startedUp = true;
});
await Veilid.instance.attach();
} else if (!startup && _startedUp) {
2023-03-12 12:24:21 -04:00
try {
await Veilid.instance.shutdownVeilidCore();
if (_updateProcessor != null) {
await _updateProcessor;
}
} finally {
setState(() {
_updateProcessor = null;
_updateStream = null;
_startedUp = false;
});
2022-12-10 17:07:52 -05:00
}
}
}
2022-12-10 12:11:46 -05:00
@override
2024-05-02 14:15:42 -04:00
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text('Veilid Plugin Version $_veilidVersion'),
),
body: Column(children: [
const Expanded(child: LogTerminal()),
Container(
decoration: BoxDecoration(
color: materialBackgroundColor.shade100,
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.15),
spreadRadius: 4,
blurRadius: 4,
)
]),
padding: const EdgeInsets.all(5),
child: Row(children: [
Expanded(
child: pad(_debugHistoryWrapper.wrap(
setState,
TextField(
controller: _debugHistoryWrapper.controller,
decoration: newInputDecoration(
'Debug Command', _errorText, _startedUp),
textInputAction: TextInputAction.unspecified,
enabled: _startedUp,
onChanged: (v) {
setState(() {
_errorText = null;
});
},
onSubmitted: (v) async {
try {
if (v.isEmpty) {
return;
}
final res = await Veilid.instance.debug(v);
loggy.info(res);
2022-12-14 16:50:33 -05:00
setState(() {
2024-05-02 14:15:42 -04:00
_debugHistoryWrapper.submit(v);
2022-12-14 16:50:33 -05:00
});
2024-05-02 14:15:42 -04:00
} on VeilidAPIException catch (e) {
2022-12-14 16:50:33 -05:00
setState(() {
2024-05-02 14:15:42 -04:00
_errorText = e.toDisplayError();
2022-12-14 16:50:33 -05:00
});
2024-05-02 14:15:42 -04:00
}
}),
))),
pad(
Column(children: [
const Text('Startup'),
Switch(
value: _startedUp,
onChanged: (value) async {
await toggleStartup(value);
}),
]),
),
pad(Column(children: [
const Text('Log Level'),
DropdownButton<LogLevel>(
value: loggy.level.logLevel,
onChanged: (newLevel) {
setState(() {
setRootLogLevel(newLevel);
});
},
items: const [
DropdownMenuItem<LogLevel>(
value: LogLevel.error, child: Text('Error')),
DropdownMenuItem<LogLevel>(
value: LogLevel.warning, child: Text('Warning')),
DropdownMenuItem<LogLevel>(
value: LogLevel.info, child: Text('Info')),
DropdownMenuItem<LogLevel>(
value: LogLevel.debug, child: Text('Debug')),
DropdownMenuItem<LogLevel>(
value: traceLevel, child: Text('Trace')),
DropdownMenuItem<LogLevel>(
value: LogLevel.all, child: Text('All')),
]),
])),
]),
),
]));
2022-12-10 12:11:46 -05:00
}