2023-01-11 16:18:40 +00:00
|
|
|
import 'package:date_field/date_field.dart';
|
2023-01-08 21:34:24 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-01-11 16:18:40 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2023-01-08 21:34:24 +00:00
|
|
|
import 'package:nextcloud_reminder/repeating_task.dart';
|
2023-01-10 22:55:38 +00:00
|
|
|
import 'package:nextcloud_reminder/types/repeat.dart';
|
2023-01-09 16:27:04 +00:00
|
|
|
import 'package:nextcloud_reminder/types/tasks.dart';
|
2023-01-11 16:18:40 +00:00
|
|
|
import 'package:tuple/tuple.dart';
|
2023-01-08 21:34:24 +00:00
|
|
|
|
|
|
|
class AddTaskWidget extends StatefulWidget {
|
|
|
|
const AddTaskWidget({super.key, this.restorationId, required this.onSave});
|
|
|
|
|
|
|
|
final ValueChanged<RepeatingTask> onSave;
|
|
|
|
final String? restorationId;
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => _AddTaskWidgetState();
|
|
|
|
}
|
|
|
|
|
2023-01-08 21:39:44 +00:00
|
|
|
//TODO: make _repeat changeable.
|
|
|
|
|
2023-01-11 16:18:40 +00:00
|
|
|
class _AddTaskWidgetState extends State<AddTaskWidget> {
|
2023-01-08 21:34:24 +00:00
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
final _titleController = TextEditingController();
|
2023-01-11 16:18:40 +00:00
|
|
|
static _emptyRepetition() {
|
|
|
|
return Tuple2(TextEditingController(text: "1"), ValueNotifier(DateInterval.daily));
|
|
|
|
}
|
|
|
|
final List<Tuple2<TextEditingController,ValueNotifier<DateInterval>>> _repeatEveryController = [_emptyRepetition()];
|
|
|
|
DateTime _beginDate = DateTime.now();
|
2023-01-08 21:34:24 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_titleController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2023-01-11 16:18:40 +00:00
|
|
|
String _prettyInterval(DateInterval d) {
|
|
|
|
switch (d) {
|
|
|
|
default:
|
|
|
|
return d.toString();
|
|
|
|
}
|
2023-01-08 21:34:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 16:18:40 +00:00
|
|
|
Widget _repeatBuilder(BuildContext context, Tuple2<TextEditingController,ValueNotifier<DateInterval>> data) {
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
Text("Repeat every " ),
|
|
|
|
Expanded(
|
|
|
|
flex: 1,
|
|
|
|
child: TextFormField(
|
|
|
|
controller: data.item1,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
hintText: "1",
|
|
|
|
),
|
|
|
|
keyboardType: const TextInputType.numberWithOptions(signed: false, decimal: false),
|
|
|
|
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
flex: 3,
|
|
|
|
child: DropdownButton<DateInterval>(
|
|
|
|
items: DateInterval.values.map((v) => DropdownMenuItem(value: v, child: Text(_prettyInterval(v)))).toList(),
|
|
|
|
onChanged: (v) => setState(() {
|
|
|
|
data.item2.value = v ?? data.item2.value;
|
|
|
|
}),
|
|
|
|
value: data.item2.value,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
IconButton(onPressed: () => setState(() {
|
|
|
|
_repeatEveryController.remove(data);
|
|
|
|
}), icon: Icon(Icons.remove, color: Theme.of(context).errorColor,)),
|
|
|
|
],
|
|
|
|
);
|
2023-01-08 21:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Add new repeating Task')
|
|
|
|
),
|
|
|
|
body: Form(
|
|
|
|
key: _formKey,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
TextFormField(
|
|
|
|
// The validator receives the text that the user has entered.
|
|
|
|
validator: (value) {
|
|
|
|
if (value == null || value.isEmpty) {
|
|
|
|
return 'Please enter a name';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
controller: _titleController,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
labelText: "Taskname"
|
|
|
|
),
|
|
|
|
),
|
2023-01-11 16:18:40 +00:00
|
|
|
|
|
|
|
DateTimeField(
|
|
|
|
onDateSelected: (v) => setState(() { _beginDate = v; }),
|
|
|
|
selectedDate: _beginDate,
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
suffixIcon: Icon(Icons.event_note),
|
|
|
|
labelText: "Begin"
|
|
|
|
),
|
|
|
|
mode: DateTimeFieldPickerMode.date,
|
2023-01-08 21:34:24 +00:00
|
|
|
),
|
2023-01-11 16:18:40 +00:00
|
|
|
] + _repeatEveryController.map((c) => _repeatBuilder(context,c)).toList()
|
|
|
|
+ [
|
|
|
|
ElevatedButton(onPressed: () => setState(() {
|
|
|
|
_repeatEveryController.add(_emptyRepetition());
|
|
|
|
}), child: const Text("add repetition")),
|
2023-01-08 21:34:24 +00:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
|
|
|
child: ElevatedButton(
|
|
|
|
onPressed: () {
|
|
|
|
// Validate returns true if the form is valid, or false otherwise.
|
|
|
|
if (_formKey.currentState!.validate()) {
|
|
|
|
// If the form is valid, display a snackbar. In the real world,
|
|
|
|
// you'd often call a server or save the information in a database.
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
const SnackBar(content: Text('Task added.')),
|
|
|
|
);
|
2023-01-11 16:18:40 +00:00
|
|
|
var repeats = _repeatEveryController.map((e) => RepeatInterval(interval: e.item2.value, every: int.parse(e.item1.text))).toList();
|
|
|
|
var meta = repeats.map((e) => e.toString()).join("/");
|
2023-01-08 21:34:24 +00:00
|
|
|
widget.onSave(RepeatingTask(
|
2023-01-11 16:18:40 +00:00
|
|
|
task: TaskExtra(
|
|
|
|
title: _titleController.text,
|
|
|
|
begin: _beginDate,
|
|
|
|
meta: {"repeat": meta},
|
|
|
|
repeat: repeats,
|
|
|
|
)
|
|
|
|
));
|
2023-01-08 21:34:24 +00:00
|
|
|
Navigator.pop(context);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: const Text('Submit'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|