1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-29 08:16:28 +00:00
comunicmobile/lib/helpers/forez_presence_helper.dart

79 lines
2.0 KiB
Dart
Raw Normal View History

2021-04-22 13:41:35 +00:00
import 'package:comunic/helpers/websocket_helper.dart';
import 'package:comunic/lists/forez_presences_set.dart';
import 'package:comunic/models/forez_presence.dart';
/// Presence helper
///
/// @author Pierre Hubert
int? _cachedGroup;
PresenceSet? _cache;
2021-04-22 13:41:35 +00:00
class ForezPresenceHelper {
/// Refresh presence cache
///
/// Throws in case of failure
static Future<void> refreshCache(int groupID) async {
final response = await ws("forez_presence/list", {"group": groupID});
final list = response
.cast<String>()
.map((element) {
final cut = element.split(",").map((e) => int.parse(e)).toList();
assert(cut.length == 4);
return Presence(
userID: cut[0], year: cut[1], month: cut[2], day: cut[3]);
})
.toList()
.cast<Presence>();
_cachedGroup = groupID;
_cache = PresenceSet()..addAll(list);
}
/// Initialize cache if required
static Future<void> _checkCache(int groupID) async {
if (_cache == null || _cachedGroup != groupID) await refreshCache(groupID);
}
/// Get the presences of a given user
///
/// Throws in case of failure
static Future<PresenceSet> getForUser(int groupID, int? userID) async {
2021-04-22 13:41:35 +00:00
await _checkCache(groupID);
return _cache!.getForUser(userID);
2021-04-22 13:41:35 +00:00
}
/// Get all the available presences
///
/// Throws in case of failure
static Future<PresenceSet?> getAll(int groupID) async {
2021-04-22 13:41:35 +00:00
await _checkCache(groupID);
return _cache;
}
/// Add a new day of presence
///
/// Throws in case of failure
2021-04-22 13:54:30 +00:00
static Future<void> addDay(int groupID, DateTime dt) async =>
2021-04-22 13:41:35 +00:00
await ws("forez_presence/add_day", {
2021-04-22 13:54:30 +00:00
"group": groupID,
2021-04-22 13:41:35 +00:00
"year": dt.year,
"month": dt.month,
"day": dt.day,
});
/// Remove a new day of presence
///
/// Throws in case of failure
2021-04-22 13:54:30 +00:00
static Future<void> delDay(int groupID, DateTime dt) async =>
2021-04-22 13:41:35 +00:00
await ws("forez_presence/del_day", {
2021-04-22 13:54:30 +00:00
"group": groupID,
2021-04-22 13:41:35 +00:00
"year": dt.year,
"month": dt.month,
"day": dt.day,
});
}