Skip to content

Commit

Permalink
ui: new_group_page: improve policy ui
Browse files Browse the repository at this point in the history
  • Loading branch information
dufkan committed May 3, 2024
1 parent bd13158 commit 1eb1e93
Showing 1 changed file with 133 additions and 6 deletions.
139 changes: 133 additions & 6 deletions lib/ui/new_group_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:math';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -56,9 +57,14 @@ class _NewGroupPageState extends State<NewGroupPage> {
final List<Device> _members = [];
final _nameController = TextEditingController();
final _policyController = TextEditingController();
String? _nameErr, _memberErr;
String? _nameErr, _memberErr, _policyErr;
KeyType _keyType = KeyType.signPdf;
Protocol _protocol = KeyType.signPdf.supportedProtocols.first;
bool _policyAfter = false;
TimeOfDay _policyAfterTime = const TimeOfDay(hour: 8, minute: 0);
bool _policyBefore = false;
TimeOfDay _policyBeforeTime = const TimeOfDay(hour: 20, minute: 0);
bool _policyDecline = false;

@override
void initState() {
Expand All @@ -70,6 +76,13 @@ class _NewGroupPageState extends State<NewGroupPage> {
});
}
});
_policyController.addListener(() {
if (_policyErr != null) {
setState(() {
_policyErr = null;
});
}
});
}

@override
Expand Down Expand Up @@ -125,12 +138,36 @@ class _NewGroupPageState extends State<NewGroupPage> {
_memberErr = "Add member";
});
}
if (_nameErr != null || _memberErr != null) return;

var policy = <String, dynamic>{};

String pad(num n) => n.toString().padLeft(2, '0');
if (_policyAfter) {
policy['after'] =
'${pad(_policyAfterTime.hour)}:${pad(_policyAfterTime.minute)}';
}
if (_policyBefore) {
policy['before'] =
'${pad(_policyBeforeTime.hour)}:${pad(_policyBeforeTime.minute)}';
}
policy['decline'] = _policyDecline;

if (_policyController.text.trim().isNotEmpty) {
try {
final customPolicy = jsonDecode(_policyController.text);
policy = {...policy, ...customPolicy};
} catch (e) {
setState(() {
_policyErr = 'Invalid JSON';
});
}
}
if (_nameErr != null || _memberErr != null || _policyErr != null) return;

Navigator.pop(
context,
Group(const [], _nameController.text, _members, _threshold, _protocol,
_keyType, _hasBot() ? _policyController.text : null));
_keyType, _hasBot() ? jsonEncode(policy) : null));
}

@override
Expand Down Expand Up @@ -165,9 +202,8 @@ class _NewGroupPageState extends State<NewGroupPage> {
children: [
TextField(
controller: _policyController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
decoration: InputDecoration(
border: OutlineInputBorder(), errorText: _policyErr),
maxLines: null,
),
],
Expand Down Expand Up @@ -281,6 +317,97 @@ class _NewGroupPageState extends State<NewGroupPage> {
],
));

if (_hasBot()) {
options.add(OptionTile(
title: 'Policy',
children: [
Padding(
padding: const EdgeInsets.only(top: 4.0, bottom: 4.0),
child: Row(
children: [
Checkbox(
value: _policyAfter,
onChanged: (bool? value) {
setState(() {
_policyAfter = value!;
});
},
),
Text('Approve after ${_policyAfterTime.format(context)}'),
const Spacer(),
FilledButton.tonalIcon(
icon: const Icon(Icons.access_time),
label: const Text('Select time'),
onPressed: _policyAfter
? (() async => await showTimePicker(
context: context,
initialTime: _policyAfterTime,
).then((value) {
if (value != null) {
setState(() {
_policyAfterTime = value;
});
}
}))
: null,
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Row(
children: [
Checkbox(
value: _policyBefore,
onChanged: (bool? value) {
setState(() {
_policyBefore = value!;
});
},
),
Text('Approve before ${_policyBeforeTime.format(context)}'),
const Spacer(),
FilledButton.tonalIcon(
icon: const Icon(Icons.access_time),
label: const Text('Select time'),
onPressed: _policyBefore
? (() async => await showTimePicker(
context: context,
initialTime: _policyBeforeTime,
).then((value) {
if (value != null) {
setState(() {
_policyBeforeTime = value;
});
}
}))
: null,
),
],
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 4.0),
child: Row(
children: [
Checkbox(
value: _policyDecline,
onChanged: (bool? value) {
setState(() {
_policyDecline = value!;
});
},
),
const Text(
'Decline if the policy is not satisfied immediately'),
],
),
),
],
));
}

options.add(ExpansionTile(
title: const Text('Advanced options'),
collapsedTextColor:
Expand Down

0 comments on commit 1eb1e93

Please sign in to comment.