2021-04-22 13:54:30 +00:00
|
|
|
import 'package:comunic/helpers/forez_presence_helper.dart';
|
|
|
|
import 'package:comunic/lists/forez_presences_set.dart';
|
|
|
|
import 'package:comunic/ui/widgets/async_screen_widget.dart';
|
|
|
|
import 'package:comunic/ui/widgets/forez_presence_calendar_widget.dart';
|
|
|
|
import 'package:comunic/utils/account_utils.dart';
|
|
|
|
import 'package:comunic/utils/intl_utils.dart';
|
|
|
|
import 'package:comunic/utils/log_utils.dart';
|
|
|
|
import 'package:comunic/utils/ui_utils.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
/// Forez presence settings route
|
|
|
|
///
|
|
|
|
/// On this route, users can change there presence settings
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
/// Show presence settings route
|
|
|
|
Future<void> showPresenceSettingsRoute(
|
2022-03-10 18:39:57 +00:00
|
|
|
BuildContext context, int? groupID) async =>
|
2021-04-22 13:54:30 +00:00
|
|
|
await Navigator.push(context,
|
2022-03-10 18:39:57 +00:00
|
|
|
MaterialPageRoute(builder: (c) => PresenceSettings(groupID: groupID!)));
|
2021-04-22 13:54:30 +00:00
|
|
|
|
|
|
|
class PresenceSettings extends StatefulWidget {
|
|
|
|
final int groupID;
|
|
|
|
|
|
|
|
const PresenceSettings({
|
2022-03-10 18:39:57 +00:00
|
|
|
Key? key,
|
|
|
|
required this.groupID,
|
2022-03-11 16:02:06 +00:00
|
|
|
}) : super(key: key);
|
2021-04-22 13:54:30 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_PresenceSettingsState createState() => _PresenceSettingsState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PresenceSettingsState extends State<PresenceSettings> {
|
2022-03-10 18:39:57 +00:00
|
|
|
late PresenceSet _currPresences;
|
2021-04-22 13:54:30 +00:00
|
|
|
|
|
|
|
Future<void> _load() async {
|
|
|
|
await ForezPresenceHelper.refreshCache(widget.groupID);
|
|
|
|
_currPresences =
|
|
|
|
await ForezPresenceHelper.getForUser(widget.groupID, userID());
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2022-03-10 18:39:57 +00:00
|
|
|
title: Text(tr("Presence settings")!),
|
2021-04-22 13:54:30 +00:00
|
|
|
),
|
|
|
|
body: AsyncScreenWidget(
|
|
|
|
onReload: _load,
|
|
|
|
onBuild: _buildScreen,
|
2022-03-10 18:39:57 +00:00
|
|
|
errorMessage: tr("Failed to load data!")!,
|
2021-04-22 13:54:30 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildScreen() => ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(8.0),
|
|
|
|
child: Text(
|
2022-03-10 18:39:57 +00:00
|
|
|
tr("Please click on the day you will be in the plain, so that everyone gets informed ! ;)")!,
|
2021-04-22 13:54:30 +00:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
PresenceCalendarWidget(
|
|
|
|
presenceSet: _currPresences,
|
|
|
|
onDayClicked: _toggleDay,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
void _toggleDay(DateTime dt) async {
|
|
|
|
try {
|
|
|
|
// Update current list
|
|
|
|
_currPresences.toggleDate(dt, userID());
|
|
|
|
|
|
|
|
// Update on server
|
|
|
|
if (_currPresences.containsDate(dt))
|
|
|
|
await ForezPresenceHelper.addDay(widget.groupID, dt);
|
|
|
|
else
|
|
|
|
await ForezPresenceHelper.delDay(widget.groupID, dt);
|
|
|
|
} catch (e, s) {
|
|
|
|
logError(e, s);
|
|
|
|
_currPresences.toggleDate(dt, userID());
|
2022-03-10 18:39:57 +00:00
|
|
|
snack(context, tr("Failed to update information!")!);
|
2021-04-22 13:54:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|