ConanServerManager/client/lib/autorestart.dart
2024-05-22 16:09:36 -07:00

174 lines
4.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:libac_flutter/nbt/NbtUtils.dart';
import 'package:libac_flutter/nbt/impl/CompoundTag.dart';
import 'package:libac_flutter/nbt/impl/IntTag.dart';
import 'package:servermanager/settings.dart';
class AutomaticRestartInfo {
final int hours;
final int minutes;
final int seconds;
final bool enabled;
const AutomaticRestartInfo(
{this.hours = 0,
this.minutes = 0,
this.seconds = 0,
this.enabled = false});
static AutomaticRestartInfo deserialize(CompoundTag tag) {
return AutomaticRestartInfo(
hours: tag.get(TAG_HOURS)?.asInt() ?? 12,
minutes: tag.get(TAG_MINUTES)?.asInt() ?? 0,
seconds: tag.get(TAG_SECONDS)?.asInt() ?? 0,
enabled: NbtUtils.readBoolean(tag, TAG_ENABLED));
}
CompoundTag serialize() {
CompoundTag tag = CompoundTag();
tag.put(TAG_HOURS, IntTag.valueOf(hours));
tag.put(TAG_MINUTES, IntTag.valueOf(minutes));
tag.put(TAG_SECONDS, IntTag.valueOf(seconds));
NbtUtils.writeBoolean(tag, TAG_ENABLED, enabled);
return tag;
}
static const TAG_NAME = "ari";
static const TAG_HOURS = "hours";
static const TAG_MINUTES = "minutes";
static const TAG_SECONDS = "seconds";
static const TAG_ENABLED = "enabled";
}
class AutoRestartPage extends StatefulWidget {
AutoRestartPage({super.key});
@override
AutoRestartState createState() => AutoRestartState();
}
class AutoRestartState extends State<AutoRestartPage> {
Settings settings = Settings();
bool firstDisplay = true;
bool enabled = false;
int seconds = 0;
int minutes = 0;
int hours = 0;
@override
Widget build(BuildContext context) {
if (firstDisplay) {
var args =
ModalRoute.of(context)!.settings.arguments as AutomaticRestartInfo;
enabled = args.enabled;
seconds = args.seconds;
minutes = args.minutes;
hours = args.hours;
firstDisplay = false;
}
return Scaffold(
appBar: AppBar(
title: Text("Automatic Restart"),
backgroundColor: Color.fromARGB(255, 100, 0, 0),
),
body: PopScope(
onPopInvoked: (v) async {
Navigator.pop(
context,
AutomaticRestartInfo(
enabled: enabled,
hours: hours,
minutes: minutes,
seconds: seconds));
},
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(children: [
SwitchListTile(
value: enabled,
onChanged: (val) {
setState(() {
enabled = !enabled;
});
},
title: Text("Enabled"),
),
Row(
children: [
SizedBox(
width: 256,
child: ListTile(
title: Text("Hours"),
subtitle: Text("${hours}"),
),
),
Expanded(
child: Slider(
max: 24,
min: 0,
value: hours.toDouble(),
onChanged: (value) {
setState(() {
hours = value.toInt();
});
},
),
)
],
),
Row(
children: [
SizedBox(
width: 256,
child: ListTile(
title: Text("Minutes"),
subtitle: Text("${minutes}"),
),
),
Expanded(
child: Slider(
max: 60,
min: 0,
value: minutes.toDouble(),
onChanged: (value) {
setState(() {
minutes = value.toInt();
});
},
),
)
],
),
Row(
children: [
SizedBox(
width: 256,
child: ListTile(
title: Text("Seconds"),
subtitle: Text("${seconds}"),
),
),
Expanded(
child: Slider(
max: 60,
min: 0,
value: seconds.toDouble(),
onChanged: (value) {
setState(() {
seconds = value.toInt();
});
},
),
)
],
)
]),
)),
),
);
}
}